Java assignment, i have done till part D, I just want part E solution
assignmentTwo/Car.java
assignmentTwo/Car.java
package
assignmentTwo
;
// incomplete starter code for assignment two programming 1, sem 2 2014
// author: Kathleen Keogh version 1, incomplete. September 2014
public
class
Car
extends
Vehicle
{
// instance variables
private
int
numSeats
,
numDoors
;
private
boolean
hatch
,
tintedWindows
;
public
Car
()
{
}
public
Car
(
int
_numSeats
,
int
_numDoors
,
boolean
_hatch
,
boolean
_tintedWindows
)
{
// insert appropriate initialisation code here based on the parameters provided
}
// get and set methods for each car instance variable
public
int
getNumSeats
()
{
return
numSeats
;
}
public
boolean
setNumSeats
(
int
_numSeats
)
{
return
true
;
// change this based on validation
}
public
int
getNumDoors
()
{
return
1
;
// change this to return appropriate value
}
public
void
setNumDoors
()
{
}
public
boolean
getHatch
()
{
return
true
;
// change this to return the appropriate value
}
public
void
setHatch
(
boolean
_hatch
)
{
}
public
boolean
getTintedWindows
()
{
return
true
;
// update this to return the appropriate value
}
public
void
setTintedWindows
(
boolean
_tintedWindows
)
{
}
// toString method
public
String
toString
()
{
// insert code here to appropriately return a string of data for this car, use superclass methods where appropriate
return
(
"Car. Make "
+
getMake
())
;
// change this to return more than just the make
}
}
assignmentTwo/Garage.java
assignmentTwo/Garage.java
package
assignmentTwo
;
// Author, Version 1. Starter Code: Kathleen Keogh September 2014
// Author, Version 2. :
// Garage class is for a garage that contains a number of vehicles
public
class
Garage
{
//instance variables
Vehicle
vehicleList
[]
;
int
maxVehicles
=
10
;
// default maximum number of vehicles is 10
int
currsize
;
public
Garage
()
{
// constructor with no parameters
vehicleList
=
new
Vehicle
[
maxVehicles
];
currsize
=
0
;
}
public
Garage
(
int
_numMaxVehicles
)
{
// constructor with one parameter - maximum number of vehicles for garage
maxVehicles
=
_numMaxVehicles
;
vehicleList
=
new
Vehicle
[
maxVehicles
];
currsize
=
0
;
}
// TODO: add get and set methods
// TODO: add toString() method
// TODO: add other methods including addVehicle, sortVehicles
}
assignmentTwo/Rectangle.java
assignmentTwo/Rectangle.java
package
assignmentTwo
;
public
class
Rectangle
extends
Shapes
{
double
length
,
width
;
// extra instance variables associated with Rectangle particularly
public
Rectangle
()
{
// constructor to create an 'empty' triangle
super
(
""
,
""
,
0
,
0
);
length
=
0.0
;
//initialise length and width to zero for new rectangle
width
=
0.0
;
}
public
Rectangle
(
double
initLength
,
double
initWidth
,
String
initName
,
String
initColour
,
double
xCoord
,
double
yCoord
)
{
super
(
initName
,
initColour
,
xCoord
,
yCoord
);
// use constructor from super class: Shapes
setLength
(
initLength
);
// initialise length to provided value
setWidth
(
initWidth
);
}
public
boolean
setLength
(
double
_length
)
{
// mutator method to set length to new value
if
(
_length
>
0.0
)
{
// validate length is positive value greater than zero
length
=
_length
;
return
true
;
}
else
return
false
;
}
public
boolean
setWidth
(
double
_width
)
{
// mutator method to set width to new value
if
(
_width
>
0.0
)
{
// validate width is positive value greater than zero
width
=
_width
;
return
true
;
}
else
return
false
;
}
public
double
getWidth
()
{
return
width
;
}
public
double
getLength
()
{
return
length
;
}
public
double
calcArea
()
{
return
length
*
width
;
}
public
double
getArea
()
{
return
calcArea
();
}
public
String
toString
()
{
return
(
"Rectangle "
+
getName
()
+
" Colour "
+
getColour
()
+
" Area "
+
getArea
()
+
" located at "
+
getXcoord
()
+
" "
+
getYcoord
()
);
}
}
assignmentTwo/Shapes.java
assignmentTwo/Shapes.java
package
assignmentTwo
;
public
class
Shapes
{
private
String
name
;
private
String
colour
;
private
double
Xcoord
;
private
double
Ycoord
;
public
Shapes
()
{
name
=
new
String
(
""
);
colour
=
new
String
(
""
);
Xcoord
=
0.0
;
Ycoord
=
0.0
;
}
public
Shapes
(
String
initName
,
String
initColour
,
double
initXcoord
,
double
initYcoord
)
{
name
=
new
String
(
initName
);
colour
=
new
String
(
initColour
);
setXcoord
(
initXcoord
);
setYcoord
(
initYcoord
);
}
public
void
setName
(
String
newName
)
{
name
=
new
String
(
newName
);
}
public
boolean
setXcoord
(
double
newXcoord
)
{
if
(
newXcoord
>
0.0
)
{
Xcoord
=
newXcoord
;
return
true
;
}
else
return
false
;
}
public
boolean
setYcoord
(
double
newYcoord
)
{
if
(
newYcoord
>
0.0
)
{
Ycoord
=
newYcoord
;
return
true
;
}
else
return
false
;
}
public
void
setColour
(
String
newColour
)
{
colour
=
new
String
(
newColour
);
}
public
double
getXcoord
()
{
return
Xcoord
;
}
public
double
getYcoord
()
{
return
Ycoord
;
}
public
String
getName
()
{
return
name
;
}
public
String
getColour
()
{
return
colour
;
}
public
double
getArea
()
{
return
0.0
;
//dummy method will be overridden in extended classes
}
}
assignmentTwo/ShapesList.java
assignmentTwo/ShapesList.java
package
assignmentTwo
;
import
java
.
io
.
BufferedWriter
;
import
java
.
io
.
File
;
import
java
.
io
.
FileWriter
;
import
java
.
io
.
IOException
;
import
java
.
util
.
Scanner
;
public
class
ShapesList
{
static
final
int
MAXSIZE
=
10
;
Shapes
myList
[];
// ShapesList contains multiple Shapes (up to MAXSIZE)
int
currsize
=
0
;
public
ShapesList
()
{
// default constructor create a shapes list of MAXSIZE size
myList
=
new
Shapes
[
MAXSIZE
];
currsize
=
0
;
}
public
ShapesList
(
int
_maxsize
)
{
// constructor to create a shapes list of max size given
myList
=
new
Shapes
[
_maxsize
];
currsize
=
0
;
}
public
void
addShape
(
Shapes
_newShape
)
{
if
(
currsize
<
MAXSIZE
)
{
myList
[
currsize
++
]
=
_newShape
;
}
else
System
.
out
.
println
(
"shape list is full"
);
}
public
String
toString
()
{
String
result
=
""
;
for
(
int
i
=
0
;
i
<
currsize
;
i
++
)
result
=
result
+
myList
[
i
].
toString
()
+
"; \r\n"
;
return
(
result
);
}
public
void
bubbleSort
()
{
// sort shapes based on Area of shape
Shapes
tempShape
;
/*
* pseudo code for bubble sort:
* for index1 in range(len(sList) -1):
* for index2 in range (len(sList) - (index1 + 1)):
* if (sList[index2] > sList[index2 + 1]):
* swapListPositions (sList, index2, index2 + 1)
*
*/
for
(
int
i
=
0
;
i
<
(
currsize
-
1
);
i
++
)
{
for
(
int
j
=
0
;
j
<
(
currsize
-
(
i
+
1
));
j
++
)
{
if
(
((
myList
[
j
]).
getArea
())
>
((
myList
[
j
+
1
]).
getArea
())
)
{
tempShape
=
myList
[
j
+
1
];
myList
[
j
+
1
]
=
myList
[
j
];
myList
[
j
]
=
tempShape
;
}
}
}
}
public
void
exportToFile
(
String
outputString
)
{
System
.
out
.
println
(
"about to open export file"
);
try
{
BufferedWriter
output
;
File
file
=
new
File
(
"export.txt"
);
System
.
out
.
println
(
file
.
getAbsolutePath
());
output
=
new
BufferedWriter
(
new
FileWriter
(
file
));
output
.
write
(
outputString
);
output
.
newLine
();
System
.
out
.
println
(
"just wrote to file"
);
output
.
close
();
}
catch
(
IOException
e
)
{
System
.
out
.
println
(
"IO Error with file export"
);
e
.
printStackTrace
();
}
}
public
void
readFromFile
()
{
int
numAdded
=
0
;
File
inputfile
;
inputfile
=
new
File
(
"ShapesFile.txt"
);
try
{
Scanner
inputScanner
=
new
Scanner
(
inputfile
);
while
(
inputScanner
.
hasNextLine
()){
//Read the shapes details from the file
String
type
=
inputScanner
.
next
();
switch
(
type
)
{
case
"square"
:
{
System
.
out
.
println
(
"processing a square..."
);
Square
newSq
=
new
Square
();
newSq
.
setName
(
inputScanner
.
next
());
newSq
.
setColour
(
inputScanner
.
next
());
newSq
.
setSide
(
inputScanner
.
nextDouble
());
newSq
.
setXcoord
(
inputScanner
.
nextDouble
());
newSq
.
setYcoord
(
inputScanner
.
nextDouble
());
addShape
(
newSq
);
break
;
}
case
"rectangle"
:
{
System
.
out
.
println
(
"processing a rectangle..."
);
Rectangle
newRect
=
new
Rectangle
();
newRect
.
setName
(
inputScanner
.
next
());
newRect
.
setColour
(
inputScanner
.
next
());
newRect
.
setLength
(
inputScanner
.
nextDouble
());
newRect
.
setWidth
(
inputScanner
.
nextDouble
());
newRect
.
setXcoord
(
inputScanner
.
nextDouble
());
newRect
.
setYcoord
(
inputScanner
.
nextDouble
());
addShape
(
newRect
);
}
}
}
inputScanner
.
close
();
}
catch
(
IOException
e
)
{
System
.
out
.
println
(
"IO Exception reading shapes from file"
);
e
.
printStackTrace
()
;
}
}
}
assignmentTwo/Square.java
assignmentTwo/Square.java
package
assignmentTwo
;
public
class
Square
extends
Shapes
{
double
side
;
public
Square
()
{
//create 'empty' square object with zero coord values and "" for Strings name and colour
super
(
""
,
""
,
0
,
0
);
side
=
0.0
;
//initialise side to zero for new square
}
public
Square
(
double
initside
,
String
initName
,
String
initColour
,
double
xCoord
,
double
yCoord
)
{
super
(
initName
,
initColour
,
xCoord
,
yCoord
);
// use constructor from super class: Shapes
setSide
(
initside
);
// initialise length to provided value
}
public
void
setSide
(
double
newSide
)
{
side
=
newSide
;
}
public
double
calcArea
()
{
return
side
*
side
;
// area of Square is side * side
}
public
double
getArea
()
{
return
calcArea
();
}
public
String
toString
()
{
return
(
"Square "
+
getName
()
+
" Colour "
+
getColour
()
+
" Area "
+
getArea
()
+
" located at "
+
getXcoord
()
+
" "
+
getYcoord
()
);
}
}
assignmentTwo/TestClass.java
assignmentTwo/TestClass.java
package
assignmentTwo
;
// author, version 1: Kathleen Keogh September 2014. Version 1 is incomplete, provides stubs for some methods
// This test class is used to test the Garage system for assignment 2, Programming 1, Sem 2 2014.
// author, version 2:
public
class
TestClass
{
public
TestClass
()
{
// constructor for TestClass
}
public
void
runtest1
()
{
// first test case
// create a car object and test that it is working
Car
myTestCar
=
new
Car
();
System
.
out
.
println
(
myTestCar
.
toString
());
}
public
void
runtest2
()
{
// second test case
// create another vehicle object and test that it is working
}
public
void
runtest3
()
{
// third test case
// create a garage object with a number of vehicles in the garage, then print it out to test it worked
}
public
static
void
main
(
String
[]
args
)
{
// TODO write code here to invoke test methods to test the Garage system
TestClass
mytest
=
new
TestClass
();
System
.
out
.
println
(
"Running test1... "
);
mytest
.
runtest1
();
System
.
out
.
println
(
"Running test2... "
);
mytest
.
runtest2
();
System
.
out
.
println
(
"Running test3... "
);
mytest
.
runtest3
();
}
}
assignmentTwo/TestingShapesListClass.java
assignmentTwo/TestingShapesListClass.java
package
assignmentTwo
;
// this testing class is used to test classes : ShapesList, Rectangle, Square, Shapes.
public
class
TestingShapesListClass
{
//instance reference variable to point to a shapes list object used in testing
ShapesList
shList
;
public
TestingShapesListClass
(){
shList
=
new
ShapesList
();
// create a shapes list object to use in testing
}
public
Square
testrun1a
()
{
Square
s
=
new
Square
(
4.0
,
"mySquare"
,
"Blue"
,
3.0
,
10.0
);
System
.
out
.
println
(
s
.
toString
()
);
return
s
;
}
public
Square
testrun1b
()
{
Square
s1
=
new
Square
();
// test empty constructor
s1
.
setName
(
"Anothersquare"
);
// use set methods to set values for instance variables
s1
.
setSide
(
5.0
);
s1
.
setColour
(
"Red"
);
// test set methods to change values of colour and coords
s1
.
setXcoord
(
4.0
);
s1
.
setYcoord
(
20.0
);
System
.
out
.
println
(
s1
.
toString
());
return
s1
;
}
public
Rectangle
testrun2
()
{
// create a rectangle shape and print it out
Rectangle
r
=
new
Rectangle
(
2.0
,
6.0
,
"rectangle"
,
"Orange"
,
5.0
,
10.0
);
System
.
out
.
println
(
r
.
toString
()
);
return
r
;
}
public
void
testrun3
(
Square
_s
,
Square
_s1
,
Rectangle
_r
)
{
// test adding 3 shapes to the shapes list
System
.
out
.
println
(
"My list of Shapes"
);
shList
.
addShape
(
_s
);
shList
.
addShape
(
_s1
);
shList
.
addShape
(
_r
);
System
.
out
.
println
(
shList
.
toString
()
);
}
public
void
testrun4
()
{
// test reading shapes from file and adding to shapes list
shList
.
readFromFile
();
}
public
void
testrun5
()
{
// test sorting list of shapes
shList
.
bubbleSort
();
System
.
out
.
println
(
"My list of sorted Shapes"
);
System
.
out
.
println
(
shList
.
toString
()
);
}
public
void
testrun6
()
{
// test exporting shapes list to a file
shList
.
exportToFile
(
shList
.
toString
());
}
public
static
void
main
(
String
[]
args
)
{
TestingShapesListClass
t
=
new
TestingShapesListClass
();
Square
testSquare1
=
t
.
testrun1a
();
Square
testSquare2
=
t
.
testrun1b
();
Rectangle
testRectangle
=
t
.
testrun2
();
t
.
testrun3
(
testSquare1
,
testSquare2
,
testRectangle
);
t
.
testrun4
();
t
.
testrun5
();
t
.
testrun6
();
}
}
assignmentTwo/Vehicle.java
assignmentTwo/Vehicle.java
package
assignmentTwo
;
public
class
Vehicle
{
//instance variables
private
String
make
,
model
,
registration
,
vinNum
;
private
int
yearOfManufacture
;
public
Vehicle
()
{
// empty constructor
// code to initialise values to "" and 0 goes here
}
// constructor with initialisation values
public
Vehicle
(
String
_make
,
String
_model
,
String
_registration
,
String
_vinNum
,
int
_yearOfManufacture
)
{
}
// get and set methods for instance variables
public
void
setMake
(
String
_make
)
{
}
public
String
getMake
()
{
return
""
;
// change this to return appropriate value
}
public
void
setModel
(
String
_model
)
{
}
public
String
getModel
()
{
return
""
;
// change this to return appropriate value
}
public
void
setRegistration
(
String
_registration
)
{
}
public
String
getRegistration
()
{
return
""
;
// change this to return appropriate value
}
//vin num
public
void
setVinNum
(
String
_vinNum
)
{
}
public
String
getVinNum
()
{
return
""
;
// change this to return appropriate value
}
// year of manufacture
public
void
setYearOfManufacture
(
int
_year
)
{
}
public
int
getYearOfManufacture
()
{
return
1
;
// change this to return appropriate value
}
}
ITECH1000 5000 Programming 1_Assignment2Specification 2014-20.pdf
CRICOS Provider No. 00103D Programming 1 Assignment 2, 2014 Sem 2 Page 1 of 9
ITECH1000 Programming 1, Semester 2 2014
Assignment 2 – Development of a Simple Program Involving Multiple Classes
Due Date: see Course Description for full details
Please see the Course Description for further information related to extensions for assignments and Special
Consideration.
Project Specification Please read through the entire specification PRIOR to beginning work. The assignment should be completed in the stages mentioned below. The objectives of this assignment are for you to:
Write your own classes from a specification
Instantiate objects of these classes
Perform required tasks by creating and manipulating objects in the code of one class through the public interfaces (methods) of the classes of the objects being manipulated.
Resources Required The following files/links are available on Moodle:
An electronic copy of this assignment specification sheet
A sample program with similar features to the assignment requirements involving multiple classes
Starting (incomplete) code for four of the classes to be developed Note: If you use any resources apart from the course material to complete your assignment you MUST provide an in‐
text citation within your documentation and/or code, as well as providing a list of references in APA formatting. This
includes the use of any websites, online forums, books, or text books. If you are unsure of how to do this please ask for
help.
Design Constraints Your program should conform to the following constraints.
Use a while‐loop when the number of iterations is not known before loop execution.
Use a for‐loop when the number of iterations is known before loop execution.
Indent your code correctly to aid readability and use a consistent curly bracket placement scheme.
Use white space to make your code as readable as possible.
Validation of input should be limited to ensuring that the user has entered values that are within correct bounds. You do not have to check that they have entered the correct data type.
Comment your code appropriately.
CRICOS Provider No. 00103D ITECH 1000 Programming 1 Assignment 2 Specification Semester 2, 2014 Page 2 of 9
Part A – Code Comprehension A Sample program is provided that creates a list of shapes stored in an array. This program uses classes: Shapes, Square, Rectangle and ShapesList. The main method is in the class: TestingShapesListClass. Conduct a careful examination of this code. Make sure you understand this code as it will help you with your own programming for this assignment. Using the uncommented sample code for classes: Shapes, Square, Rectangle and ShapesList provided, answer the following questions:
1. Draw a UML diagram of each of the Shapes, Rectangle and Square classes using the code that has been
provided. Complete this using the examples that have been provide in the lecture slides.
2. Draw a UML diagram of the ShapesList class using the code that has been provided. Complete this using the
examples that have been provide in the lecture slides.
3. Add appropriate comments into the file: ShapesList.java to explain all the methods. At a mimimum, explain the
purpose of each method. For the more complex methods reading input from a file, sorting the data and
exporting data to the file, insert comments to explain the code.
4. Explain in what the purpose of the call to hasNextLine() in readFromFile() in ShapesList.java.
5. Briefly explain the code in bubbleSort() in the ShapesList class. What attribute are the shapes being sorted by?
Name the sort algorithm that is being used. Explain in words how it works, using an example. Why do we need
to use two for loops?
6. Briefly name and describe one other sorting algorithm that could have been used.
7. Explain the use of the return value in the getArea() method in the Rectangle class. What is it returning? Where
does the value come from?
8. Examine the lines to open the output file and write the outputString in the ShapesList class:
File file = new File(“export.txt”); System.out.println(file.getAbsolutePath()); output = new BufferedWriter(new FileWriter(file)); output.write(outputString);
Where is the output file located? Find BufferedWriter in the Java API documentation and describe the use of
this object.
9. Examine the code in the exportToFile method. What is the purpose of the ‘\r\n’ in the toString() method that is
used to produce the output string?
10. Briefly explain why a try and catch clause has been included in the code to read from the file. Remove this from
the code and remove the input file and take note of the error. Re‐add the line and investigate what happens if
the input file is not present.
Part B – Development of a basic Class Your first coding task is to implement and test a class that represents a Vehicle. A Vehicle object has a make a model, a registration number, vinnumber and year of manufacture.
To complete this task, you are required to:
1. Create a new package in eclipse named assignTwoStudentNNNNN where NNNNN is your student number. You will author a number of classes for this assignment and they will all be in this package.
CRICOS Provider No. 00103D ITECH 1000 Programming 1 Assignment 2 Specification Semester 2, 2014 Page 3 of 9
2. Use the UML diagram provided to implement the class Vehicle in a file called Vehicle.java. Starter code has been provided with this assignment, copy this starter code into your Vehicle.java file. Complete the constructors that will create a Vehicle object.
3. Author all mutator and accessor (set and get) methods and the toString() as indicated in the UML diagram.
4. Ensure that your Vehicle class includes the following validation within the mutator (set) methods:
a. The make of the Vehicle cannot be null nor an empty String (ie “”). If an attempt is made to set the
name to an invalid value the method should return false.
b. The year of manufacture of the Vehicle must be greater than or equal to 1950. If an attempt is made to
set the value of the year of manufacture to an invalid value the method should return false and not
change the value of the yearOfManufacture.
Vehicle
- String make;
- String model;
- String registration;
- String vinNumber;
- int yearOfManufacture;
~ Vehicle(make:String, model:String, registration:String, vinNumber:String, yearOfManufacture:int) <<create>>
~ Vehicle() <<create>>
+ getMake():String
+ setMake(_make:String):boolean
+ getModel():String
+ setModel(_model:String)
+ getRegistration():String
+ setRegistration(_registration:String)
+ getVinNumber():String
+ setVinNumber(_vinNumber:String)
+ getYearOfManufacture():int
+ setYearOfManufacture(_year:int):boolean
+ toString():String
Figure 1: UML Diagram for the class Vehicle
Part C – Development of extended Classes using inheritance Your second coding task is to implement and test a class that represents a Car. For this assignment a Car is a vehicle with additional attributes such as number of seats, number of doors, whether it is a hatchback or not, whether or not it has tinted windows and whether it has manual transmission or automatic transition. A Car isA Vehicle. The Car class will extend a Vehicle class, so the make, model, registration, vin number and
CRICOS Provider No. 00103D ITECH 1000 Programming 1 Assignment 2 Specification Semester 2, 2014 Page 4 of 9
year of manufacture will be inherited from the Vehicle class. After you have created the Car class, you are asked to create a similar class for another type of vehicle that you choose (e.g. Truck, Caravan, Boat, Tractor….).
To complete this task you are required to:
1. Use the UML diagram provided to implement the class Car in a file called Car.java. Ensure that you adhere to
the naming used below as this class will be used by other classes that you will develop. Starter code is provided
for you to copy.
2. Make sure that your Car class extends the Vehicle class.
3. Ensure that your class includes the following validation within the mutator (set) methods:
a. numSeats cannot be less than two. If an attempt is made to set the numSeats to an invalid amount,
then the method should set the numSeats to two and return false, otherwise set the numSeats as given
and return true.
b. The numDoors cannot be less than two. If an attempt is made to set the value of the numDoors to an
invalid value the method should return false.
4. Write additional methods for toString() and a method to check equality with another car (equality should be
based on vinNumber being the same for both vehicles).
5. Select two Cars that you will create. Choose values for each of the data attributes for each car. Write a class
called TestClass (starter code is provided to copy from) and within the main method write code that will :
a. Instantiate an instance of the Car class for one of the Cars you selected using the Car() constructor
b. Sets all of the instance variables to the appropriate values using the mutator methods (set)
c. Change the yearOfManufacture to increase the yearOfManufacture of the first Car by 5 years.
d. Instantiates an instance of the Class Car for a second different Car contained in the table below using
the Car(make:String, model:String, registration: String, vinNumber: String, yearOfManufacture: int,
numSeats:int, numDoors: int, hatch: boolean, tintedWindows: boolean,manual: boolean) constructor
e. Display both of the Cars that you have created using the toString() method
f. Display both of the Cars that you have created using individual accessor (get) methods in a print
statement
CRICOS Provider No. 00103D ITECH 1000 Programming 1 Assignment 2 Specification Semester 2, 2014 Page 5 of 9
Car
- numSeats: int
- numDoors: int
- hatch: boolean
- tintedWindows: boolean
- manual: boolean
~ Car(make:String, model:String, registration: String, vinNumber: String, yearOfManufacture: int, numSeats:int, numDoors: int, hatch: boolean, tintedWindows: boolean,manual: boolean) <<create>>
~ Car() <<create>>
+ setNumSeats(_numSeats: int): boolean
+ getNumSeats(): int
+ setNumDoors(_numDoors: int): boolean
+ getNumDoors(): int
+ setHatch(_hatch: boolean)
+ getHatch(): boolean
+ setTintedWindows(_tinted: boolean)
+ getTintedWindows() : boolean
+ getManual(): boolean
+ setManual(_manual: boolean)
+ equalsIgnoreCase(anotherCar: Car): boolean
+ toString():String
Figure 2: UML Diagram for the class Car
6. Now create a second class representing another vehicle (e.g. bike, trailer, caravan, truck, or tractor etc.) in a similar fashion to the way you created the car class. (The car class is similar to the square class in the sample code provided. The square class extends the Shapes class. The Rectangle class also extends the Shapes class. Your second vehicle type is similar to the Rectangle class. Your vehicle e.g. truck will extend the Vehicle class.)
a. Decide on your vehicle type and then choose attributes particular to that vehicle that will be instance variables
b. Draw a UML class diagram for your new class c. Create your new class in a new file named appropriately e.g. Tractor.java d. Make sure your new class extends the Vehicle class e. Write get and set methods for your new class f. Write a toString() method for your new class g. Test your new class by writing code to create an object based on this class in your TestClass class h. Test your object by modifying one of the instance variables using a set method then display the object
CRICOS Provider No. 00103D ITECH 1000 Programming 1 Assignment 2 Specification Semester 2, 2014 Page 6 of 9
Part D – Development of the Garage class Using the UML diagrams provided and the information below you are required to implement and test classes that represent a Garage object. For this assignment a Garage contains multiple vehicles up to a specified capacity. (This may be modeled on the ShapesList class in the sample code. The ShapesList class contains multiple Shapes in an array. Your Garage will in a similar way contain multiple Vehicles in an array.)
Garage.java
Garage
- name: String
- Vehicles: Vehicle[]
- numVehicles: int
- capacity: int
~ Garage (initName: String, capacity: int) <<create>>
+ getName():String
+ getNumVehicles():int
+ setName(newName: String): boolean
+ addVehicle(newVehicle: Vehicle): Boolean
+ sortVehicles()
+ exportToFile() //itech5000 students only
+ readFromFile() //itech5000 students only
+ toString():String
Figure 3: UML Diagram for the class Garage
1. You have been provided with starting point in Garage.java Write get and set methods as well as an addVehicle
method.
2. Create a method for sorting the Vehicles in the Garage (model this on the ShapesList bubblesort method)
3. Update the class TestClass adding in code to:
a. Create an instance of the Garage class
b. Add at least two Cars created to the Garage class using the addVehicle() method
c. Add at least two other vehicles (e.g. Tractor or Bike) to the Garage class
d. Display the details of the garage that you have created using the toString() method in the Garage class –
these should be accessed via the instance of the garage class that you have created
e. Display all of the vehicles in the garage by looping through and accessing each vehicle in turn. Each
vehicle should be accessed via the instance of the Garage class you have created, then printed using the
toString() method on that vehicle.
(Hint: Look at the TestingShapesListClass.java class for examples of similar code to the code you will need in your
TestClass. In TestingShapesListClass, Shapes are added to a ShapesList object. In your code Vehicles are added to the
Garage object).
CRICOS Provider No. 00103D ITECH 1000 Programming 1 Assignment 2 Specification Semester 2, 2014 Page 7 of 9
Part D – Using the Classes Create a new class called GarageSystem.java . This class is to be used to implement a basic menu system. You may find some of the code you have already written in the TestClass.java can be copied and used with some modification here. You may also find the menu code from assignment one helpful as an example of using a switch statement to process the menu options. Each menu item should be enacted by choosing the appropriate method on the Garage object. The menu should include the following options:
1. Populate the Garage
2. Display Vehicles in Garage
3. Sort Vehicles data
4. Import data (ITECH5000 students only)
5. Export data (ITECH5000 students only
6. Exit
Populate the Garage – this option will allow the user to enter data for each vehicle OR you may write a populate method that will insert data hardcoded in your method directly into each vehicle. You will use the set methods on your Car object and the set methods on your additional vehicle (Hint: copy this code from your TestClass.)
Display Data – display the data of all vehicles in the garage, using your toString() method on the Garage object.
Sort data should utilise some form of sorting algorithm to sort the vehicles into alphabetical order based on name. (Hint: look at the sort method in the ShapesList class. It sorts on area of the shape, you need to sort based on name of the vehicle.)
For students enrolled in ITECH5000 ONLY:
Import data – this menu should read in data from the filed called SampleData.txt. You are required to add the additional data described above to this file.
Export data – save the data to a new file ExportData.txt
Assignment Submission The following criteria will be used when marking your assignment:
successful completion of the required tasks
quality of code that adheres to the programming standards for the course; including:
comments and documentation
code layout
meaningful variable names You are required to provide documentation, contained in an appropriate file, which includes:
a front page ‐ indicating your name, a statement of what has been completed and acknowledgement of the names of all people (including other students and people outside of the university) who have assisted you and details on what parts of the assignment that they have assisted you with
a table of contents and page numbers
answers to the questions from Part A
list of references used (APA style); please specify if none have been used
An appendix containing copies of your code and evidence of your testing
CRICOS Provider No. 00103D ITECH 1000 Programming 1 Assignment 2 Specification Semester 2, 2014 Page 8 of 9
Using the link provided in Moodle, please upload the following:
1. All of the classes created in a single zip file – SurnameStudentIdAssign2.zip
2. A copy of your report – surnameStudentIDAssign2.docx or surnameStudentIDAssign2.pdf
Marking Guide PART A – Code Comprehension /20 PART B – Vehicle class + Testing /20 PART C – Car and another vehicle Classes + Testing /40 PART D - Garage Class + Testing /40 PART E – Implementation of Menu /20 Quality of code created /15 Presentation of documentation / report /5 Total /20 /160
CRICOS
Provider No. 00103D
Appendix
gar
vehi
car
D ITECH 100
x Figure 4. U
rage
icle
00 Programming 1 As
co
is
UML Class Di
ssignment 2 Specific
ontains
A
iagram for th
cation Semester 2, 20
he Garage sy
014
ystem
Page 99 of 9
ShapesFile.txt
square mybestsquare purple 6.0 1.0 5.0 square mylastsquare green 7.0 3.0 4.0 rectangle myfirstrectangle aqua 8.0 3.0 2.5 6.5