Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
CSSE2002/7023 Tutorial Week 13
Page 1 of 20
Tutorial 13
Practice Exam
Part A: Practical Questions
Question 1 [30 marks]
a) Write a class which implements the MergeSort algorithm on ArrayLists.
In the exam, the algorithm for MergeSort would be provided for you on the next
page. During your practice, you can refer to the Week 12 lecture, where it was
discussed.
In the exam, extracts from the API for java.util.ArrayList would be provided.
During your practice, please refer to the online API.
b) Create a main method which does the following:
1. Creates an ArrayList holding the following Strings:
i. “turn”
ii. “butter”
iii. “for”
2. Sorts this ArrayList using your mergesort method.
3. Prints the sorted ArrayList.
Hints:
• Marks will be awarded for good coding style practices.
CSSE2002/7023 Tutorial Week 13
Page 2 of 20CSSE2002/7023 Tutorial Week 13
Page 3 of 20 CSSE2002/7023 Tutorial Week 13
Page 4 of 20 CSSE2002/7023 Tutorial Week 13
Page 5 of 20 CSSE2002/7023 Tutorial Week 13
Page 6 of 20
Question 2 [20 marks]
In the following code snippet, identify elements of poor commenting, style, design or
programming practice, and:
a) Explain why they are poor practice.
b) Explain how they could be rewritten to demonstrate better commenting, style,
design and/or programming practice.
In the exam, extracts from our course’s Java Programming Style Guide and the
Google Java Style Guide will be provided for you. During your practice, please refer
to them directly.
In the exam, extracts from the java.util.ArrayList and java.lang.Math APIs would
be provided for you. During your practice, please refer to them directly.
Hints:
• This code snippet belongs to the class Traveller, which has members of type
Location.
• Location is a class with 3 public members:
o String townName
o double latitude
o double longitude
• The code snippet spans 2 pages.
• If you are not sure how to explain the good practices, we will also accept
examples of rewritten code which demonstrate good practices for part marks.
• You may annotate the code snippet directly, although if you do that, please
write clearly and not too small – we cannot give you marks if we cannot read
your answer.
• Line numbers have been included to make it easier for you to refer to specific
lines / sections. Blank lines have not been numbered.
CSSE2002/7023 Tutorial Week 13
Page 7 of 20
1 //imports
2 import java.lang.*;
3 //Variables
4 public List towns;
5 public Location currentLocation;
6 public double maximumTravelDistance;
7 public String name;
8 //Constructors
9 //Default constructor
10 public Traveller(String n, List t)
11 {
12 name = n;
13 towns = t;
14 currentLocation = towns[0];
15 maximumTravelDistance = 1000.0;
16 }
17 //Constructor with starting town assigned
18 public Traveller(String n, List t, int s)
19 {
20 name = n;
21 towns = t;
22 currentLocation = towns[s];
23 maximumTravelDistance = 1000.0;
24 }
CSSE2002/7023 Tutorial Week 13
Page 8 of 20
25 //Methods
26 public double calculateDistance(Location l)
27 {
28 return Math.abs(Math.sqrt(Math.pow(l.longitude-
currentLocation.longitude,2)+Math.pow(l.latitude-
currentLocation.latitude,2)));
29 }
30 public List townsWithinTravelRange() {
31 List townList = new ArrayList<>();
32 for (Location t : towns) {
33 if (!t.equals(currentLocation) &&
34 t.calculateDistance<=maximumTravelDistance)
35 {
36 townList.add(t);
37 }
38 }
39 }
CSSE2002/7023 Tutorial Week 13
Page 9 of 20
CSSE2002/7023 Tutorial Week 13
Page 10 of 20 CSSE2002/7023 Tutorial Week 13
Page 11 of 20
CSSE2002/7023 Tutorial Week 13
Page 12 of 20
Question 3 [30 marks]
Design a multi-class Java program based on the following description.
Your answer should include:
• The classes you have identified
• What variables those classes should have, including:
o Variable type
• What methods those closes should have
• Identification of places where you would need more information in the next
iteration of the design process
• Justifications for your decisions
Hint:
• There are multiple valid answers.
At the airport, planes are assigned runways and crew. Crew members include a pilot,
a co-pilot, and head flight attendant, and a number of other flight attendants,
depending on the size of the plane. Passengers book tickets to fly on particular
planes going to particular destinations. Their ticket has an assigned seat number.
Passengers are allowed to check in one suitcase each, and have one item of hand
luggage. Passengers must check in their luggage one hour before their flight, and
board the plane at a gate corresponding to the plane’s runway. Each flight attendant
looks after a set of seats on the plane. The pilot and co-pilot of a plane fly the plane
to a destination.
CSSE2002/7023 Tutorial Week 13
Page 13 of 20 CSSE2002/7023 Tutorial Week 13
Page 14 of 20 CSSE2002/7023 Tutorial Week 13
Page 15 of 20 CSSE2002/7023 Tutorial Week 13
Page 16 of 20 CSSE2002/7023 Tutorial Week 13
Page 17 of 20 Use this page for diagrams if required.
CSSE2002/7023 Tutorial Week 13
Page 18 of 20
Part B: Short Answer Questions
Question 4 [3 marks]
Given two classes:
• Class Parent has a method f which takes a positive int as a parameter; and
• Subclass Child overrides f to accept a positive or negative int as the
parameter
a) Is the child class’ requirement weaker or stronger than the parent class’
requirement? Why?
b) Does this violate the substitution principle? Why/why not?
c) Define the substitution principle.
CSSE2002/7023 Tutorial Week 13
Page 19 of 20
Question 5 [4 marks]
When creating GUIs using Java Swing, which class must you instantiate to create
each of the following?
a) The application window
b) A text label
c) A button
d) Interactions when a button is clicked
An example of a Java GUI with a window, label and button is provided below for your
reference:
Question 6 [1 mark]
Java is a compiled language. What does this mean?
CSSE2002/7023 Tutorial Week 13
Page 20 of 20
Question 7 [2 marks]
Identify two advantages of separating a program into model and view.