Uploaded by Kung chung

CSE1IOX 2021 Assessment 2

advertisement
Assessment 2
Real-Time Programming Assignment
CSE1IOX Intermediate Object-Oriented Programming
2021
Before you begin
Objectives
This is an individual assignment. Students are not permitted to work in a group when writing this
assignment.
Copying and Plagiarism
This is an individual assignment. Students are not permitted to work in a group when writing this
assignment. Plagiarism is the submission of another person’s work in a manner that gives the
impression that the work is their own. La Trobe University treats plagiarism seriously. When detected,
penalties are strictly imposed.
Further information can be found on http://www.latrobe.edu.au/students/academicintegrity/explanation/plagiarism
Submission Guidelines
Your assignment submission should be typed, not written/drawn by hand.
Submit the electronic copy of your assignment through the subject LMS.
Submission after the deadline will incur a penalty of 5% of the available assignment mark per day
capped at 5 days. No assignment will be accepted after 5 days. If you have encountered difficulties
that lead to late submission or no submission, you should apply for special consideration.
2
© Didasko 2021. All rights reserved.
2
Background
This real-time programming assignment contains three (3) parts. Each part contains multiple
questions. The questions are on the topics mentioned in the objectives and repeated here:
1. Access the Java collections framework in the Java standard library (API) and use the
provided generic ArrayList class to solve problems.
2. Use input and output streams provided in the Java standard library (API) to code programs
that manipulate text and binary files
3. Explain the object-oriented concepts of inheritance and polymorphism and apply them to
solve problems in Java
The real-time programming assignment runs according to this schedule on the nominated day:
Time
6:15pm
6:15pm to 6:30pm
6:30pm to 9:30pm
9:30pm to 9:45pm
9:45pm
Description
Real-time programming assignment automatically opens on the
LMS.
Reading time (15 minutes).
Writing time (3 hours).
Submission to LMS (15 minutes).
Real-time programming assignment automatically closes on the
LMS.
If you finish any activity above in the schedule early, you are permitted to move onto the next step
immediately.
Note that it is not possible to pause the real-time programming assignment and resume later.
Importantly the real-time programming assignment is strictly limited to one attempt only.
Before starting the real-time programming assignment, you should familiarise yourself with the
submission instructions under the “Submission” heading below.
If you have any questions about the real-time programming assignment, make sure you submit a
support request before you start.
Further information is provided in the assessment 2 overview webinar and the date is also in your
calendar.
© Didasko 2021. All rights reserved.
3
PART 1 – Questions about ArrayList
Question 1 (18 marks)
Create a new NetBeans 8.2 Java project called Part1 and write a complete Java console application
that performs the following sequential tasks in the main() method:
a) Define an ArrayList<String> with the following names: “Alice”, “Bob”, “Chelsie”, “Dave”,
“Edward”.
b) Add “Francene” to the end of the list.
c) Add “George” to the start of the list.
d) Print the index of “Bob”.
e) Print the name of the fifth person.
f) Remove “Alice”.
g) Remove the third person.
h) Print the names of all people using the enhanced for-loop.
[18 marks]
PART 2 – Questions about Text and Binary Files
The questions for this part are based on generating and displaying some scores for games of
Australian Rules Football. Games of football are scored based on “goals” (worth 6 points) and
“behinds” (worth 1 point). The points are summed to determine the winner. Example:
Goals
Home team
Behinds
(6 points each)
(1 point each)
6
3
Score
39
Goals
Away team
Behinds
(6 points each)
(1 point each)
5
2
Score
32
Create a new NetBeans 8.2 Java project called Part2 and add the following starter code in a class
called FootballScores:
import
import
import
import
java.io.FileNotFoundException;
java.io.FileOutputStream;
java.io.PrintWriter;
java.util.Random;
public class FootballScores {
private final static int NUM_ROUNDS = 5;
private final static int MAX_SHOTS = 10;
private final static String TEXT_FILE = "text-scores.txt";
public static void main(String[] args) throws FileNotFoundException {
writeTextScores();
printTextScores();
}
public static void writeTextScores() throws FileNotFoundException {
// Implementation omitted
}
public static void printTextScores() throws FileNotFoundException {
// Implementation omitted
}
}
4
© Didasko 2021. All rights reserved.
4
The following questions for this part either ask you to define a helper class, read or write football
scores in a text file, or consider how the solution could be extended for binary files.
Question 2 (14 marks)
Add a class called FootballGame to your Part2 project and implement it based on the following
class diagram:
FootballGame
- roundNumber : int
- homeGoals : int
- homeBehinds : int
- awayGoals : int
- awayBehinds : int
+ FootballGame ( roundNumber : int,
homeGoals : int, homeBehinds : int,
awayGoals : int, awayBehinds : int )
+ summarize () : String
+ toString() : String
The key methods are described as follows:
•
•
summarize(): Calculates the winner of the game. Prints a message reporting whether the
home team won, lost, or tied the game. Example output formats:
o Round 1: Home team won 26 points to 20.
o Round 2: Home team lost 8 points to 33.
o Round 3: Match was tied 42 points each.
toString(): Gives a comma separated values (CSV) representation of the football game
details:
o roundNumber,homeGoals,homeBehinds,awayGoals,awayBehinds
[14 marks]
Question 3 (9 marks)
Complete the body of the writeTextScores() method from the FootballScores class provided in the
starter code. This method will randomly generate the scores of NUM_ROUNDS football games.
Each goal and behind score is a randomly generated number that must be at least 0 and less than
MAX_SHOTS. The scores are written to the file represented by the TEXT_FILE constant. Each line
of the file will be in the format from the toString() method in the FootballGame class.
[9 marks]
Question 4 (10 marks)
Complete the body of the printTextScores() method from the FootballScores class provided in the
starter code. This method reads the scores from the TEXT_FILE file for one game at a time. For
each game, the data is converted back to a FootballGame object and the result of the game is
printed to the console using the summarise() method. (Hint: You can use the split() method of the
String class to convert CSV data to tokens.)
[10 marks]
© Didasko 2021. All rights reserved.
5
Question 5 (8 marks)
Consider hypothetically the work required to convert your solutions in questions 3-4 to use a binary
file rather than a text file. Add a multiline java comment at the end of your FootballScores class with
written answers to the following questions:
a)
b)
c)
d)
What change would you need to make to the FootballGame class?
Which Java 8 API class would you use to write football scores to a binary file?
Which Java 8 API class would you use to read football scores from a binary file?
Briefly describe a technique to make sure you do not exceed the end of the file when
reading a binary file?
[2 + 2 + 2 + 2 = 8 marks]
6
© Didasko 2021. All rights reserved.
6
PART 3 – Questions about Inheritance and Polymorphism
The questions for this part are based on an organisation that manages vehicle registrations. Vehicle
registrations are made up of the owner name (“name”), vehicle registration number (“regNum”),
location (one of inner metropolitan, outer metropolitan, or regional) and the engine capacity of
motorcycles if relevant (“engCapacity”, measured in “cc”, which is cubic centimeters).
Here are the registration fees that are charged for an annual registration:
Location
Car
Motorcycle
(100cc or more)
Inner metropolitan
$800
$500
Outer metropolitan
$750
$465
Regional
$690
$420
The questions are also based on the following class diagram:
Motorcycle
(less than 100cc)
$250
$230
$205
RegistrationTester
Vehicle
- owner : String
- regNum : String
- location : Location
+ main ( args[] : String ) : void
+ Vehicle ( owner : String, regNum :
String, location : Location )
+ getOwner() : String
+ getRegNum() : String
+ getLocation() : Location
+ calcRegFee() : double
+ toString() : String
Car
<<Enumeration>>
Location
INNER_METRO
OUTER_METRO
REGIONAL
Motorcycle
+ Car ( owner : String, regNum : String,
location : Location)
+ calcRegFee() : double
- engCapacity
+ Motorcycle ( owner : String, regNum :
String, location : Location, engCapacity :
int )
+ getEngCapacity() : int
+ calcRegFee() : double
+ toString() : String
Create a new NetBeans 8.2 Java project called Part3 and complete the following questions.
Question 6 (21 marks)
Implement the Vehicle class in your Part3 project as per the problem description and UML class
diagram above. Some points to highlight are:
•
•
•
•
•
•
The Vehicle class is an abstract class.
The Location enumeration is defined inside the Vehicle class, as indicated by .
The “location” member is additional to the “Location” enumeration.
The calcRegFee() method (for calculating registration fees) is an abstract method.
The toString() method provides a short summary of the member fields.
You need to interpret the remainder yourself from the problem statement and UML.
[21 marks]
© Didasko 2021. All rights reserved.
7
Question 7 (10 marks)
Implement the Car class in your Part3 project as per the problem description and UML class diagram
above. Some points to highlight are:
•
•
•
•
The Car class extends the Vehicle class.
The calcRegFee() method overrides the version in the parent class.
Members inherited (but not overridden) are not repeated in the UML diagram.
You need to interpret the remainder yourself from the problem statement and UML.
[10 marks]
Question 8 (17 marks)
Implement the Motorcycle class in your Part3 project as per the problem description and UML class
diagram above. Some points to highlight are:
•
•
•
•
•
The Motorcycle class also extends the Vehicle class.
The calcRegFee() method also overrides the version in the parent class.
The toString() method is overridden to additionally report the engine capacity.
Members inherited (but not overridden) are not repeated in the UML diagram.
You need to interpret the remainder yourself from the problem statement and UML.
[17 marks]
Question 9 (13 marks)
Implement the RegistrationTester class in your Part3 project to test the other classes. It will perform
the following 4 tasks in the main() method:
•
•
Instantiate an array of type Vehicle and length 4.
Initialise the elements of the array with the following vehicles:
Type
•
•
Name
Registration
Location
Engine
number
capacity
Car
John Smith
ABC222
Inner metro
n/a
Motorcycle
Sandy Phillips DEF333
Outer metro
80
Car
Bob Jones
GHI444
Regional
n/a
Motorcycle
Jane Doe
JKL555
Inner metro
250
Write a loop that prints the details of each Car in the array and the registration fee.
Write a loop that prints the details of each Motorcycle in the array and the registration fee.
[13 marks]
8
© Didasko 2021. All rights reserved.
8
Submission
To summarise, the answers to the questions are separated into the following projects:
Project name
Part1
Part2
Part3
Question answers
Question 1
Question 2
Question 3
Question 4
Question 5
Question 6
Question 7
Question 8
Question 9
When you are finished your real-time programming assignment, close NetBeans, and copy the
project folders into a new empty folder called XXX_cse1iox_programming_test, where XXX is your
Student ID. Zip the folder as XXX_cse1iox_programming_test.zip, and submit to the LMS.
[TOTAL: 120 marks]
(End of exam.)
© Didasko 2021. All rights reserved.
9
Download