Professional Documents
Culture Documents
The back pages of this test paper contain code for the classes Student
and Tutorial. You are required to add your own code to these classes
and answer the following questions:
Q5. In the class Student, add the function toString() that returns a String
value that contains the value of ALL attributes in class Student. (1 mark)
Q2. In the class Tutorial, add a method called setupStudents() that will
create five student objects and save them to the students array. (1
mark)
Q3. Add a procedure called displayAll() that will display all attribute
details for all students. You must use a for loop and you must call
toString() for each student. (1 mark)
Q4. Add a procedure called listFullTimers(int num) that will display the
details of any student that is studying a number of subjects greater than
or equal to the parameter num. You must use a while loop and you must
call toString() for each full time student. (2 marks)
Q5. Add code to the Tutorial constructor that calls all three methods
detailed above. You must call them in the order necessary for the code to
compile and run successfully. Use the value of 4 for the parameter num
when calling the method listFullTimers(int num). (1.5 marks)
Sample output:
2
PROGRAMMING FUNDAMENTALS PRACTICAL TEST 2 - SAMPLE SEMESTER 2 2017
public Tutorial()
{
5
PROGRAMMING FUNDAMENTALS PRACTICAL TEST 2 - SAMPLE SEMESTER 2 2017
Q1. How many methods are being called between lines 1 and 38
inclusive? NOTE: Count method calls inside a loop only once. (1 mark)
Q2. How many times will the body of the while loop on line 29 execute?
(0.5 marks)
Q3. If we required more bike objects to be stored in the array, which line
number in the code would need changing? (0.5 marks)
Q4. If the user of the program enters the value 6 after line 17 executes,
what will be the position of the front wheel and back wheel for all bikes?
(1 mark)
6
PROGRAMMING FUNDAMENTALS PRACTICAL TEST 2 - SAMPLE SEMESTER 2 2017
1 import java.util.Scanner;
2
3 public class MyVehicleProgram
4 {
5 private final int NUM_BIKES = 20;
6 private Bike bikes[] = new Bike[NUM_BIKES];
7
8 public MyVehicleProgram()
9 {
10 paintBikes();
11 moveAll();
12 }
13
14 private void moveAll()
15 {
16 Scanner in = new Scanner(System.in);
17 System.out.println("Please enter the length: ");
18 int length = in.nextInt();
19 for(int i = 0; i<bikes.length; i++)
20 {
21 bikes[i].driveForward(length);
22 }
23 }
24
25 private void paintBikes()
26 {
27 Scanner in = new Scanner(System.in);
28 int i = 0;
29 while(i<bikes.length)
30 {
31 System.out.println("Please enter the colour: ");
32 String colour = in.next();
33 bikes[i] = new Bike(colour);
34 i++;
35 }
36 }
37}
38
39 public class Bike
40 {
41 private String colour;
42 private Wheel frontWheel;
43 private Wheel backWheel;
44
45 public Bike(String col)
46 {
47 colour = col;
48 frontWheel = new Wheel();
49 backWheel = new Wheel();
50 }
51
52 public void moveForward()
53 {
54 frontWheel.moveForward();
55 backWheel.moveForward();
56 }
57
7
PROGRAMMING FUNDAMENTALS PRACTICAL TEST 2 - SAMPLE SEMESTER 2 2017