Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Term XXXXXXXXXXExam Question Paper Data Structures and Algorithms – COIT20256 TOTAL 50 MARKS Attempt all 10 questions in the Microsoft Word template provided. All answers must be...

1 answer below »
Term XXXXXXXXXXExam
Question Pape
Data Structures and Algorithms – COIT20256
TOTAL                                         50 MARKS
Attempt all 10 questions in the Microsoft Word template provided.
All answers must be written in the answer template provided, not in this question paper.
Question 1                                        5 Marks
This question relates to the following class
public class QuestionX {
private static QuestionX quest = new QuestionX();
private QuestionX(){
}
public static QuestionX getQuestion() {
XXXXXXXXXXreturn quest;
}
public void doSomething(){
XXXXXXXXXXSystem.out.println("In doSomething");
}


other methods for this class to do things
}
a) How is this class used by a client? Provide code showing how you would use this class in the main method of a program.                    (2 marks)    
) What constraints are placed on class usage by the above implementation? Explain how this has been achieved.                      (3 marks)
Question 2                                         4 Marks
With the use of examples:
a) Discuss the two major benefits of using an interface.          (2 marks)
) When would you use an abstract class in preference to an interface? (2 marks)
Question 3                                         2 Marks
Often when a method encounters an e
or it returns a value that indicates that it has been unsuccessful. This allows the invoking code to test that return value and deal with the problem in some way before progressing.
However, that is not an option when you use a constructor. Suppose you want to notify clients that create an instance of a class about any e
ors that occur in the construction process. Discuss two ways in which this can be achieved.          
                                    
Question 4                                         6 Marks
a) What is the best case performance for a hashtable lookup? You must explain your answer very clearly. In your answer explain how hashtables work and how that relates to performance.     (3 marks)
) What is the worst case performance for a hashtable lookup. You must explain your answer and again relate it to how hashtables work.     (3 marks)
Question 5                                        5 Marks
The following Stack class uses the generic List class developed in chapter 21 of the text book and in the week 12 slides (Fig 21.3 ).
public class Stack {
private final List< E > stackList;
see Fig 21.3 for List

public Stack(){
stackList = new List
("stack");
}

public void push(E object) {
stackList.insertAtFront(object);
}

public E pop(){
return stackList.removeFromFront();
}

public boolean isEmpty(){
return stackList.isEmpty();
}
}
public class GenericStack {
public static void main(String[] args) {
XXXXXXXXXXStack stack = new Stack
();
XXXXXXXXXXstack.push(10);
XXXXXXXXXXstack.push(20);
XXXXXXXXXXstack.push(15);

XXXXXXXXXXwhile (!stack.isEmpty()){
XXXXXXXXXXint item = stack.pop();
XXXXXXXXXXSystem.out.printf("%d%n", item);
}
}
}
Question 5 continued over page
Question 5 continued
a) What is output by this code? Explain your answer.     (1 mark)
) The data pushed onto the stack is of type int, but the stack is of type Integer. What conversion happens when the int data is pushed onto the stack?     (1 mark)
c) Where else in the code do you see such mixing of types and what happens in that case?     (1 mark)
d) Suppose you want a stack of Result records (see the class diagram for Result below). Rewrite the main method to test your stack with 3 Result records. Highlight the changes you have made (in bold and yellow highlight).    (2 marks)
Question 6                                         3 Marks
a) What are the advantages of a using a linked list rather than a conventional a
ay in Java?      (2 marks)
) When would it be more efficient to use an a
ay rather than a linked list? Explain your answer.    (1 mark)
Question 7                                         6 Marks
a) Given the following code, use lambdas and streams to calculate and display the number of marks greater than or equal to 50 (i.e. the total number that have passed).                                    (1 mark)
public static void main(String[] args) {
int[] marks = {10, 53, 65, 49, 46, 95, 81, 45, 72, 85};

use lambdas and streams to work out how many

marks are >= 50 and display the result.
}
Output should be similar to the following:
The number of marks greater than or equal to 50 = 6
) The following is the class diagram for the Result class used in this question.
Given the starting point for the code below, use lambdas and streams to complete the main method as follows:
i. Write a Predicate called supplementary that returns true if the mark is in the supplementary range (>=45 and <50) and false if the mark is not in this range.                             XXXXXXXXXXmark)
ii. Make use of the predicate from part (i) to display all the result records (student id and mark) with marks in the supplementary range XXXXXXXXXXmarks)
iii. Use lambdas and streams to calculate and display the average mark.
XXXXXXXXXXmarks)
Question 7 continued over page
Question 7 continued
In the answer template complete the code for parts (i) to (iii) where you see in the code below:
public class StreamQuestion {
public static void main(String[] args) {
XXXXXXXXXXResult[] results = {new Result("S123345", 10),
XXXXXXXXXXnew Result("S678901", 53),
XXXXXXXXXXnew Result("S778901", 65),
XXXXXXXXXXnew Result("S878901", 49),
XXXXXXXXXXnew Result("S078901", 81),
XXXXXXXXXXnew Result("S688901", 45),
XXXXXXXXXXnew Result("S698901", 72),
XXXXXXXXXXnew Result("S679901", 46),
XXXXXXXXXXnew Result("S678911", 72),
XXXXXXXXXXnew Result("S678912", 85)};

XXXXXXXXXXList resultList = A
ays.asList(results);


(i) Write the code for the supplementary predicate


(ii) using the predicate (and lambdas and streams),

display the student id and mark for all results in

the supplementary range


(iii) using lambdas and streams calculate and display

the average mark

}
}

The output generated should be similar to the following:
Supplementary List:
Average Mark: 57.80
Question 8                                         7 Marks
question 8 was about JDBC – this has been removed from the unit and replaced with Graphs. See the week 12 tutorial questions for the types of questions you could be asked about graphs
Question 9                                        10 Marks
The following code was meant to print out the elements in an a
ay in reverse order. However, it does not behave co
ectly.
public static void reverse(int[] a, int index) {
if (index == (a.length - 1))
XXXXXXXXXXSystem.out.printf("%d%n", a[index]);
else {
XXXXXXXXXXreverse(a, index);
a) What does it do? Explain why it behaves in this way.      XXXXXXXXXXmarks)
) There is more than one e
or in the code. Co
ect the code so that it will recursively print out the elements of the a
ay in reverse order. Highlight your changes clearly (in bold with yellow highlight).    
        (2 marks)
c) Complete the following main method to show how you would invoke your co
ected reverse method to test that it now works co
ectly.      (1
Answered 9 days After Oct 08, 2022

Solution

Sairama answered on Oct 17 2022
51 Votes
1.
a.
     public static void main(String[] args) {
QuestionX q = QuestionX.getQuestion(); q.doSomething(); }
    b. It is a Singleton pattern. The constructor has been made private. Hence the object instantiation can only be accessed from only inside the class and not from outside the class. Also the constructor has been made a static and private one.This way only a single instance of the class exists in the program. Also there exists a private object instance of the class. This single instance can be accessed by the static public method of the class. In our example, it's the getQuestion function, through which we can access the instance.
2.
a.
i. It is used to achieve total abstraction.    
ii. Since java does not support multiple inheritances in the case of class, by using an interface it can achieve multiple inheritances
interface Polygon{
void getArea(int length, int
eadth);
}
implement the Polygon interface
class Rectangle implements Polygon {

implementation of abstract method
public void getArea(int length, int
eadth) {
System.out.println("The area of the rectangle is " + (length *
eadth));
}
}
Multiple Inheritance Example
interface A {

members of A
}
interface B {

members of B
}
class C implements A, B {

abstract members of A

abstract members of B
}
B. An abstract class is a good choice if we are using the inheritance concept since it provides a common base class implementation to derived classes.
· An abstract class is also good if we want to declare non-public members. In an interface, all methods must be public.
· If we want to add new methods in the future, then an abstract class is a better choice. Because if we add new methods to an interface, then all of the classes that already implemented that interface will have to be changed to implement the new methods.
· If we want to create multiple versions of our component, create an abstract class. Abstract classes provide a simple and easy way to version our components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, we must create a whole new...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here