University of Cape Town Department of Computer Science Computer Science CSC117F Solutions Class Test 4 Wednesday 14 May 2003 Marks: 40 Approximate marks per question are shown in brackets Time: 40 minutes The use of calculators is permitted NAME: Surname Initials STUDENT NO: COURSE CODE: CSC This paper consists of 7 questions and 11 pages (including this cover page). Mark Allocation Question Marks Internal External Quest Marks Internal External 1 [11] 5 [7] 2 [5] 6 [4] 3 [3] 7 [6] 4 [4] Total Internal Examiner: Total Grand Total Final Mark External Examiner: 1
Question 1. [11 marks] Consider the Person class below public class Person private String name; public void setname (String n) name = n; public void print () System.out.println (name); a) Write a Java class Student using the following information: 1. The Student class must inherit the data member and the methods of the Person class 2. The Student class must have the following: student number (a String) as data member setstudentno method that sets the student number print method that calls the print method of the Person class to print the name of the student and prints the student number as well public class Student extends Person private String studentno; public void setstudentno (String s) studentno = s; super.print (); System.out.println (studentno); [7] 2
b) Write a main method that tests the above classes. It must do the following: Create a new student Set the name of the student to Jill Hope Set the student number to HPXJIL003 Print the student s name and student number Use the structure below as a template for your answer. public class UseStudent public static void main (String [] args) Student s = new Student (); s.setname ("Jill Hope"); s.setstudentno ("HPXJIL003"); s.print (); 3
a) Consider the following statement Rat r = new Rat(); What output will this statement produce? Why? Making a mammal Making a rodent Making a rat by the compiler [4] b) Insert a statement into the Rat class that calls the superclass constructor explicitly // The "Rat" class. class Rat extends Rodent public Rat () 4
Question 3. [3 marks] a) Complete the FarmyardAnimal class by filling in the missing statement so that all classes that inherit from this class will be forced to define a makenoise method as in the Duck and Pig classes below. abstract class FarmyardAnimal protected int positionx, positiony; public abstract void makenoise(); public void move (int newx, int newy) positionx = newx; positiony = newy; class Duck extends FarmyardAnimal public void makenoise () // code to go quack quack... class Pig extends FarmyardAnimal public void makenoise () // code to go oink oink... b) What do we call a method such as the makenoise method? Abstract Method c) What do we call a class that contains at least one method such as the makenoise method? Abstract Class 5
Question 4. [4 marks] Consider the classes below. Study the comments for each class, and complete the statements that define the classes class FarmyardAnimal protected int positionx, positiony; public void move (int newx, int newy) positionx = newx; positiony = newy; interface CanMakeNoise public abstract void makenoise (); /* The Duck class inherits from the FarmyardAnimal class and conforms to the CanMakeNoise interface */ class Duck extends FarmyardAnimal implements CanMakeNoise public void makenoise () /* code to go quack quack */... /* The Rabbit class inherits from the FarmyardAnimal class (it makes no noise )*/ class Rabbit extends FarmyardAnimal public void wriggleears ()...... /* The Windmill class conforms to the CanMakeNoise interface */ class Windmill implements CanMakeNoise public void makenoise () /* code to go squeak squeak */ [4] 6
Question 5. [7 marks] a) What is inheritance? Inheritance is a relationship between classes where one class is a more specialized version of another class b) What is aggregation? Aggregation is a relationship between classes where one class is part of another class c) What is an abstract class? An abstract class is a class which contains at least one abstract method (a method without a body). Abstract classes establish a basic form to which all subclasses must conform. A interface is a pure abstract class i.e. it contains no data variables or method bodies only constants and method headers [2] e) Complete the following sentence: Every class in Java is a subclass of class Object 7
Question 6. [4 marks] Examine the program below: import java.awt.event.*; import javax.swing.*; public class testbuttoneg extends JFrame private JPanel background = new JPanel (); private JButton button1 = new JButton ("One"); private JButton button2 = new JButton ("Two"); private JLabel thetext = new JLabel ("Nothing"); public testbuttoneg () button1.addactionlistener (new ButtonListener ("Pressed one")); button2.addactionlistener (new ButtonListener ("Pressed two")); background.add (button1); background.add (button2); background.add (thetext); getcontentpane ().add (background); setdefaultcloseoperation (JFrame.EXIT_ON_CLOSE); pack (); class ButtonListener implements ActionListener public void actionperformed (ActionEvent e) thetext.settext (buttonstring); pack (); buttonstring = buttonstring + " again"; public ButtonListener (String newstring) buttonstring = newstring; private String buttonstring = ""; public static void main (String [] args) testbuttoneg frame = new testbuttoneg (); frame.setvisible (true); 8
a) This program creates a window containing three atomic components (two JButtons and a JLabel). Draw a diagram to show what this window looks like when the program starts, including all text that is displayed. b) What happens when the user clicks the right mouse button on button1 for the first time? [2] The display text (the JLabel object thetext ) changes from Nothing to Pressed one c) What happens when the user clicks the right mouse button on button1 for the second time? The display text (the JLabel object thetext ) changes from Pressed one to Pressed one again 9
Question 7. [6 marks] a) What are layout managers used for in java? Layout managers are used to control the size and position of components in containers 10
PackedFrame frame = new PackedFrame (); frame.setvisible (true); c) Write down the program lines that you would have to add to create the following layout of the buttons in the main window (just add the missing code): //added code panelone.setlayout(new BorderLayout()); panelone.add(button1,borderlayout.north); panelone.add(button2,borderlayout.west); panelone.add(button3,borderlayout.center); panelone.add(button4,borderlayout.east); panelone.add(button5,borderlayout.south); paneltwo.add(button6); paneltwo.add(button7); paneltwo.add(button8); //end added code (it is not necessary to specify the layout for paneltwo, as FlowLayout is the default. However, it is not incorrect to do so) 11