COSC 211
Programming Assignment #2
What to do:
Problem number 6 on P. 479
You will need the following files:
Person.java
Vehicle.java
Truck.java
UseTruck.java (described below)
Make sure Person, Vehicle, and Truck all override the equals method, as in
public boolean equals(Object obj)
and make use of the super
construct whenever possible. Also use super
in constructors.
The class Truck should include a method called compareTo:
public int compareTo(Truck
t)
where
a.compareTo(b) >0 if Truck a's
load capacity is greater than Truck b's
a.compareTo(b) ==0 if Truck a and Truck
b have the same load capacity
a.compareTo(b) <0 if Truck a's load
capacity is less than Truck b's
/* This is the file UseTruck.java that you should use to run your
program */
public class UseTruck
{
public static void main(String[] args)
{ Truck truckArray=new
Truck[10];
truckArray[0]=new Truck("Ford", 8 , "John Smith", 10000.0 ,
20000); // Manufacturer - cylinders - owner - load capacity - tow
capacity
truckArray[1]=new Truck("GM" , 6 , "Mary Jones" , 5000.0 , 10000);
truckArray[2]=new Truck("Chrysler" , 8 , "Harry Smith", 12000.0 ,
25000);
truckArray[3]=new Truck("Ford", 6 , "Ellen Harris" , 8000.0 , 12000);
int count =
4; //the number of entries in the array
// output
array before sort
for(i=0;i<count; i++)
System.out.println(truckArray[i]);
// sort the
array
sortit(truckArray, count);
// output
array after sort
for(i=0;i<count;i++)
System.out.println(truckArray[i]);
}
public static void sortit(Truck[] ar,
int c)
{
// You write the code to sort the
array using the compareTo method from Truck
// HINT: go back to class web site and
look at "Things You Should Remember from CS1 and then look at the code
for an array of objects!
}
}
What to hand in: A well documented source listing of all code. All code
MUST begin with a heading (comment) that includes your first name, last
name, student ID, and assignment number) as in
/* First Name : Bill
Last Name: Jones
ID: 76111
Take Home Assignment #2
File :
Floop.java
//This is new!! PLEASE remember to include
this line
*/
Failure to make this the first thing I see when grading your
homework will result in a 10% penalty. Programs should employ
descriptive variable names, all methods should be commented!
You will demo the actual running of your program to me in class.