You are on page 1of 8

Family Name: ______________________________

Given Name: ______________________________

Student Id: _________________________________

PROGRAMMING FUNDAMENTALS SAMPLE PRACTICAL TEST 2


ALL ANSWERS ARE TO BE WRITTEN ON THIS PAPER

PART A Class Student 3 marks

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:

Q1. In the class Student, add a class attribute for numberOfSubjects.


Select the most appropriate data type. (0.5 marks)

Q2. Add a parameter to the Student constructor that can be used to


initialise the numberOfSubjects attribute. Initialise numberOfSubjects
using this parameter. (0.5 marks)

Q3. In the class Student, add the accessor getNumberOfSubjects() that


will return the numberOfSubjects attribute. (0.5 marks)

Q4. In the class Student, add the mutator setNumberOfSubjects () that


will assign a new value for the attribute numberOfSubjects. (0.5
marks)

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)

WRITE THE CODE FOR CLASS STUDENT ON THE BACK PAGES


INSEARCH
CRICOS Provider Code: 00859D
PROGRAMMING FUNDAMENTALS PRACTICAL TEST 2 - SAMPLE SEMESTER 2 2017

PART B Class Tutorial 6 marks

Q1. In the class Tutorial, declare an array of type Student called


students. Initialise the size of the students array to five. (0.5 marks)

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:

WRITE THE CODE FOR CLASS TUTORIAL ON THE BACK PAGES

2
PROGRAMMING FUNDAMENTALS PRACTICAL TEST 2 - SAMPLE SEMESTER 2 2017

public class Student


{

private String name;


private int id;
private String address;

public Student(String theName, int theId, String


theAddress, )
{
name = theName;
id = theId;
address = theAddress;

}// End of class Student


3
PROGRAMMING FUNDAMENTALS PRACTICAL TEST 2 - SAMPLE SEMESTER 2 2017

public class Tutorial


{

public Tutorial()
{

}// End of class Tutorial


4
PROGRAMMING FUNDAMENTALS PRACTICAL TEST 2 - SAMPLE SEMESTER 2 2017

THIS PAGE HAS BEEN LEFT BLANK INTENTIONALLY IF EXTRA


SPACE IS REQUIRED

5
PROGRAMMING FUNDAMENTALS PRACTICAL TEST 2 - SAMPLE SEMESTER 2 2017

PART C Code Analysis 6 marks

Study the code below and answer the following questions.

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)

Q5. Assume the program is currently executing, and is paused on line 8.


Write down the line numbers in execution order from this point until the
MyVehicleProgram constructor finishes executing. You do not have to
include the repetitive line numbers in any loops you encounter (LIST THE
LINE NUMBERS FOR THE LOOP BODY ONLY ONCE). (3 marks)

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

58 public void moveBackward()


59 {
60 frontWheel.moveBackward();
61 backWheel.moveBackward();
62 }
63
64 public void driveForward(int distance)
65 {
66 for(int i=0;i<distance;i++)
67 {
68 moveForward();
69 }
70 }
71}
72
73 public class Wheel
74 {
75 private double position;
76
77 public Wheel()
78 {
79 position = 1;
80 }
81
82 void moveForward()
83 {
84 position = position + 1;
85 }
86
87 void moveBackward()
88 {
89 position = position - 1;
90 }
91}

You might also like