First Name: Last Name: Student Number: University of Cape Town Department of Computer Science Computer Science CSC1017F Class Test 4 - Solutions Wednesday, 17 May 2006 Marks: 40 Time: 40 Minutes Approximate marks per question are shown in brackets The use of calculators is permitted COURSE CODE: CSC117F This paper consists of 3 questions and 12 pages (including this cover page). Mark Allocation Question Marks Internal External Quest Marks Internal External 1 [20] 2 [15] 3 [5] Total Internal Examiner: Total Grand Total Final Mark External Examiner: 1
Question 1: Inheritance [20 marks] The classes listed below are used in writing an application for a Pet Shop. Examine the classes closely and answer the questions that follow. class Cat extends Mammal String Breed; boolean neutered; public Cat() Breed="mongrel"; neutered=false; public Cat(String ID, int sex, int agemonths, String Breed, boolean neutered) super(id,sex,agemonths); this.breed=breed; this.neutered = neutered; public abstract class Mammal extends Pet public Mammal() public Mammal(String ID, int sex, int agemonths) super(id,sex,agemonths); abstract class Pet protected String ID; protected int sex; protected int agemonths; public Pet() ID=""; sex=0; agemonths=0; public Pet(String ID, int sex, int agemonths) this.id=id; this.sex=sex; this.agemonths=agemonths; public abstract void MakeNoise(int NoOfNoises); 2
public abstract class Rodent extends Mammal public Rodent() public class TestPet public static void main(string[] args) Pet mypet = new Pet("Tibbles", 1, 14); a) Draw inheritance diagram for the classes listed above. [2] Pet Mammal Cat Rodent 3
b) What is an abstract class? [1] Class containing one or more abstract methods (i.e. no method implementation) c) What is wrong with the following code? [1] public class TestPet public static void main(string[] args) Pet mypet = new Pet("Tibbles", 1, 14); Pet is abstract cannot be instantiated. d) What is the keyword this used for? [1] to refer to the current object from inside the class. e) Give and example of a has a (aggregation) relationship from the classes above. [1] Pet String f) The class Rodent as written is incomplete. Write the code for a parameterized constructor for the class Rodent. [3] public Rodent(String ID, int sex, int agemonths) super(id,sex,agemonths); g) Class Cat is missing a MakeNoise method. Why does it have to have this method? [1] Cat extends Pet through Mammal which has an abstract method MakeNoise. Since Cat is not abstract it must implement MakeNoise 4
h) Show how you would create a one year old, female, neutered, Persian cat called Fluffball in the TestPet class. [2] //assume 0=female, 1=male Cat cat = new Cat("Fluffball", 0, 12, "Persian", true); i) Give an example of a method that the Cat class inherits from the Object class.[1] tostring j) You now want to include a Rat class. Write down the code for this class. Note that Rats have a breed variable and boolean variable indicating if they have long hair or not. Include a default and a parameterized constructor for your class and any other methods (see (g)) you have to define. [7] public class Rat extends Rodent String breed; boolean longhair; public Rat() 5
breed = "Brown"; longhair = false; public Rat(String ID, int sex, int agemonths, String breed, boolean longhair) super(id,sex,agemonths); this.breed = breed; this.longhair = longhair; public void MakeNoise(int NoOfNoises) System.out.println(NoOfNoises); 6
Question 2 : GUI s. [15 marks] Examine the program below and answer the questions that follow. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class FirstGUI2 extends JFrame JButton all, reset; JCheckBox [] chboxes = new JCheckBox("Cheese"), new JCheckBox("Tomato"), new JCheckBox("Guacamole"), new JCheckBox("Chicken"); JPanel panel,panel2; butlistener bs; public FirstGUI2() all = new JButton("All"); reset = new JButton("Reset"); all.addactionlistener(new alllistener()); reset.addactionlistener(new butlistener()); panel= new JPanel(); panel.setlayout(new GridLayout(4,1)); for(int i=0;i<4;i++) panel.add(chboxes[i]); panel2= new JPanel(); panel2.add(all); panel2.add(reset); getcontentpane ().setlayout(new GridLayout(2,1)); getcontentpane ().add(panel); getcontentpane ().add(panel2); pack(); class butlistener implements ActionListener public void actionperformed(actionevent e) class alllistener implements ActionListener public void actionperformed(actionevent e) for(int i=0;i<4;i++) chboxes[i].setselected(true); 7
a) What is a top-level component? [1] A component that cannot be added to other containers b) Given an example of a top-level component from the program above [1] JFrame c) What is an event? [1] It is an action that can be taken (by a user) such as typing in a text box, resizing a window, pressing a button etc d) Give an example of an event from the program above. [1] Pressing a button e) What does the implements keyword mean? [1] Used to indicate that a particular class conforms to the specified interface 8
f) Draw a diagram of the component hierarchy for the program above. [3] JFrame (First GUI) JPanel (panel) JPanel (panel2) JCheckBox (checkbox[])* JButton (all) JButton (reset) g) The program above produces a window like this: 9
How would you alter it so that it looks like the picture below? Just rewrite the altered line(s) of code. [2] panel.setlayout(new GridLayout(2,0)); h) What happens when the user clicks on the All button? [2] All the checkboxes are selected. i) We want button Reset to reset the GUI to its initial state. Explain how you would alter the program so that it would do that, writing down code where necessary. [3] Add the following lines of code to the actionperformed method in the butlistener class for(int i=0;i<4;i++) 10
chboxes[i].setselected(false); Question 3: Input and Output. [5 marks] Examine the program below: public class CommandLine3 public static void main(string args[]) if (args.length!=3) System.out.println("Incorrect number of command line parameters!"); else System.out.print(args[0] + " + " + args[1] + " - " +args[2] + " = "); int i = Integer.parseInt(args[0]); int j = Integer.parseInt(args[1]); int k = Integer.parseInt(args[2]); int sum = i+j-k; System.out.println(sum); a) What are command-line arguments? [1] Input passed into a program by a user when running the program via a console (command line) can be accessed via the args array of the main method b) Write down the exact output if the user types the following line into the DOS window: [2] java CL 4 6 7 4 + 6-7 = 3 11
c) Write down the exact output if the user types the following line into the DOS window: [2] java CL 4 6 3 6 7 Incorrect number of command line parameters! 12