Part A: Short Answer Questions. This section will be worth 70 marks in the exam. You must answer all questions in this section in the exam.
- What is polymorphism? (2 marks)
- What is the difference between accessor method and mutator method? (2 marks)
- What annotation is used when implementing a method from an abstract class or interface?
(2 marks) - What is an overloaded constructor? (2 marks)
- What is the difference between Iterator and ListIterator ? (2 marks)
- Provide 2 differences between linkedList and ArrayList. (2marks)
- Explain what the Super keyword is used for in Java (2 marks)
- Write 2 example method signatures for an overloaded method called getName (2 marks)
- Write the code for an interface called Car that has a method called numberOfWheels that returns an integer (4 marks)
- Write a constructor for the Date class given the following sample object creation code (2 marks)
new Date(“March”, 30, 1932); - Write a default constructor for the Date class above (2 marks)
- Given the Animal class below, create a class Dog that inherits animal. The Dog class must have at least 2 instance variables that are initialized in a constructor (6 marks)
abstract class Animal
{
private int numberOfLegs;
private String colour;
protected Animal(int numberOfLegs, String colour)
{
this.numberOfLegs = numberOfLegs;
this.colour = colour;
}
public abstract String colour();
}
- Write the code to use a ListIterator to loop through the following LinkedList (2 marks)
LinkedList colours = new LinkedList();
colours.add(new Colour(“Blue”));
colours.add(new Colour(“Green”));
colours.add(new Colour(“Yellow”));
colours.add(new Colour(“White”));
colours.add(new Colour(“Black”));
- Write a declaration for a new HashSet of Person objects, called students. Initialise the new HashSet. Note: you may leave it empty (2 marks)
- Write the code to add a Person object to the HashSet declared for question 14 above. Assume the Person class has a default constructor (2 marks)
- Write a class called Cat that implements the Animal interface shown below (6 marks)
public interface Animal {
String colour(); // return colour
void newColour(String s); // update colour
}
Part B: Java Programming Problems. There will be 2 questions in this section in the exam however you will have to complete both of them. This section will be worth 30 marks. Marks will be deducted for incorrect brackets, syntax etc, so be careful as possible.
- Write a program in Java to store a collection of movie names. There should be no duplicate movies in the collection. The program must be able to (20 mark):
• add a movie
• remove a movie
• check if a movie exists
• delete all movies
• print out the number of movies
• print a list of all movie names - Write a program in java that implements a HashMap to store students (student IDs as the key, and student name as the value). Program must be able to (10 mark):
• add some students, then print all students’ name
• remove all of the students at once