Design and code a class called Date that includes two integer instance variables
ID: 3565906 • Letter: D
Question
Design and code a class called Date that includes two integer instance variables, day and year, and a String variable called month. Include a parameterized constructor, toString, and equals method.
Design and code a class called Car that includes the following instance variables: double dealerCost, integer idNumber, Date dateArrived, integer modelYear, and String makeModel. You should have a parameterized constructor, toString, and equals method (have the same modelYear, dateArrived, and makeModel).
Design and code a class called SoldCar that is derived from Car. Add the instance variables of double price, String customer, and Date dateSold. Include two constructor methods one that allows the user to have eight arguments (dealerCost, idNumber, dateArrived, modelYear, make, price, customer, dateSold) and one that allows the user to have four arguments (someCar, price, customer, and dateSold). SoldCar should also have a toString method, an equals method (have the same modelYear, dateArrived, makeModel, and dateSold) and calculateProfit method.
Your code should run with a data file of Car and SoldCar objects. Define the text file according to Chapter 11 do not use Scanner class. Read all the data and put the Car and SoldCar objects into an array. Test to see if any Car or SoldCar objects added to the array are equal (similar) (same modelYear, dateArrived, and make for Car objects same modelYear, dateArrived, make and dateSold for SoldCar objects) to ones already in the array. Print the idNumbers of similar cars. This can be done once with a double loop at the end of main.
Use a trailer read while loop to solve this problem. Use an ArrayList or your own design of a array list for the array that holds the Car and SoldCar objects. Remember that when you retrieve an object from an ArrayList (get method), you must cast it before assigning it to a storage location.
Read the code first. While the code is not X, do the following:
If code is C - 7 variables follow to create a Car object. If it is not an error record (same idNumber as another car in the array), add it to the array, and check for similar makes and models (print those idNumbers).
S1 indicates an idNumber followed by 5 variables to make that Car (idNumber) a SoldCar. Look up that car in the array and make it a SoldCar and store back in array
S2 - 12 variables follow to create a SoldCar object. If it is not an error (same idNumber), add it to the array and check for similar makes and models (print those idNumbers).
X end of file
Each time you are about to add an object to the array, check to see if there is an object with the same idNumber in the array. Print an error message if there is a duplicate idNumber. You will also be checking to see if there are similar (equals method) Car or SoldCar objects as you add new objects to the array. Just print the IdNumbers of similar objects. This can be done once with a double loop at the end of main.
When the data file has all been read, print out the ArrayList or the array holding your data. if printing a SoldCar, also print the profit made on the car. The SoldCar toString() method can call the calcProfit() method. Profit does not have to be another instance variable.
Include a UML chart in the folder for this program no algorithms this time.
Data File:
C 5000.00 1234 January 1 2004 2000 Honda_Accord
S1 1234 6000.00 Roth February 15 2004
S2 8000.00 2222 January 15 2004 1999 Ford_F150 9500.00 Jones January 25 2004
C 14000.00 3333 June 6 2003 1999 GMC_Suburban
C 5000.00 1222 January 1 2004 2000 Honda_Civic
C 10000.00 4444 July 10 2002 2001 Toyota_Tundra
S2 12000.00 5555 February 6 2003 1965 Ford_Mustang 15000 Williams September 4 2003
S1 3333 15650 Brown December 25 2003
C 10500.00 4000 July 10 2002 2001 Toyota_Tundra
S2 9500.00 6666 January 21 2004 1999 Ford_F150 9000.00 Smith January 31 2004
C 5000.00 1222 January 1 2004 2000 Honda_Civic
S2 8000.00 2222 January 15 2004 1999 Ford_F150 9500.00 Jones January 25 2004
S2 9900.00 6667 January 21 2004 1999 Ford_F150 9007.00 Byrd January 31 2004
X
Explanation / Answer
Program Code:
public class Date
{
int day, year;
String month;
public Date(String m, int d, int y)
{
month=m;
day=d;
year=y;
}
public String toString()
{
String s="";
s+=month+"-"+day+"-"+year;
return s;
}
public boolean equals(Date d)
{
if(day==d.day && month.equals(d.month) && year==d.year)
{
return true;
}
return false;
}
}
public class Car
{
double dealerCost;
int idNumber;
Date dateArrived;
int modelYear;
String makeModel;
public Car(double dCost, int id, Date dA, int mYear, String mModel)
{
dealerCost=dCost;
idNumber=id;
dateArrived=dA;
modelYear=mYear;
makeModel=mModel;
}
public String toString()
{
String s="";
s+=dealerCost+" "+idNumber+" "+dateArrived.toString()+" "+ modelYear+" "+makeModel;
return s;
}
public boolean equals(Car c)
{
if(modelYear==c.modelYear && dateArrived.equals(c.dateArrived) && makeModel.equals(c.makeModel) )
{
return true;
}
return false;
}
}
public class SoldCar extends Car
{
double price;
String customer;
Date dateSold;
public SoldCar(double dealerCost,int idNumber,Date dateArrived,int modelYear,String make,double p,String cust,Date dSold)
{
super(dealerCost, idNumber, dateArrived, modelYear, make);
price=p;
customer=cust;
dateSold=dSold;
}
public SoldCar(Car c,double p,String cust, Date dSold)
{
super(c.dealerCost, c.idNumber, c.dateArrived, c.modelYear,c.makeModel);
price=p;
customer=cust;
dateSold=dSold;
}
public String toString()
{
String s="";
s+=super.toString()+" "+price+" "+customer+" "+dateSold.toString();
return s;
}
}
import java.io.*;
import java.util.*;
public class CarProjectImplementation
{
static ArrayList<Object> o=new ArrayList<Object>();
static ArrayList<Car> car=new ArrayList<Car>();
static ArrayList<SoldCar> sCar=new ArrayList<SoldCar>();
public static void main(String args[])throws Exception
{
ArrayList<String> al=new ArrayList<String>();
String line="";
//ArrayList<String> strtoken=new ArrayList<String>();
BufferedReader br=new BufferedReader(new FileReader("CarData"));
while((line=br.readLine())!=null)
{
al.add(line);
}
for(int i=0;i<al.size(); i++)
System.out.println(al.get(i));
System.out.println("*****************************************************");
for(int i=0;i<al.size();i++)
{
if(al.get(i).substring(0,1).equals("C"))
{
StringTokenizer st = new StringTokenizer(al.get(i)," ");
String[] pieces=new String[8];
int j=0;
while(st.hasMoreTokens())
{
pieces[j]=st.nextToken();
j++;
}
Date d=new Date(pieces[3], Integer.parseInt(pieces[4]), Integer.parseInt(pieces[5]));
Car c=new Car(Double.parseDouble(pieces[1]),Integer.parseInt(pieces[2]), d, Integer.parseInt(pieces[6]), pieces[7]);
car.add(c);
o.add(c);
}
else if(al.get(i).substring(0,2).equals("S1"))
{
StringTokenizer st = new StringTokenizer(al.get(i)," ");
String[] pieces=new String[7];
int j=0;
while(st.hasMoreTokens())
{
pieces[j]=st.nextToken();
j++;
}
Date d=new Date(pieces[4], Integer.parseInt(pieces[5]), Integer.parseInt(pieces[6]));
int k=0;
while(k!=car.size())
{
if(car.get(k).idNumber==Integer.parseInt(pieces[1]))
{
SoldCar sc=new SoldCar(car.get(k),Double.parseDouble(pieces[2]),pieces[3], d);
sCar.add(sc);
o.add(sc);
}
k++;
}
}
else if(al.get(i).substring(0,2).equals("S2"))
{
StringTokenizer st = new StringTokenizer(al.get(i)," ");
String[] pieces=new String[13];
int j=0;
while(st.hasMoreTokens())
{
pieces[j]=st.nextToken();
j++;
}
Date d=new Date(pieces[3], Integer.parseInt(pieces[4]), Integer.parseInt(pieces[5]));
Date d1=new Date(pieces[10], Integer.parseInt(pieces[11]), Integer.parseInt(pieces[12]));
SoldCar sc=new SoldCar(Double.parseDouble(pieces[1]),Integer.parseInt(pieces[2]), d, Integer.parseInt(pieces[6]), pieces[7],Double.parseDouble(pieces[8]), pieces[9],d1);
sCar.add(sc);
o.add(sc);
}
}//end of for loop
System.out.println("==============================================================");
for(int i=0;i<o.size();i++)
{
System.out.println(o.get(i).toString());
}
}
}
Sample Input:
C 5000.00 1234 January 1 2004 2000 Honda_Accord
S1 1234 6000.00 Roth February 15 2004
S2 8000.00 2222 January 15 2004 1999 Ford_F150 9500.00 Jones January 25 2004
C 14000.00 3333 June 6 2003 1999 GMC_Suburban
C 5000.00 1222 January 1 2004 2000 Honda_Civic
C 10000.00 4444 July 10 2002 2001 Toyota_Tundra
S2 12000.00 5555 February 6 2003 1965 Ford_Mustang 15000 Williams September 4 2003
S1 3333 15650 Brown December 25 2003
C 10500.00 4000 July 10 2002 2001 Toyota_Tundra
S2 9500.00 6666 January 21 2004 1999 Ford_F150 9000.00 Smith January 31 2004
C 5000.00 1222 January 1 2004 2000 Honda_Civic
S2 8000.00 2222 January 15 2004 1999 Ford_F150 9500.00 Jones January 25 2004
S2 9900.00 6667 January 21 2004 1999 Ford_F150 9007.00 Byrd January 31 2004
Sample Output:
C 5000.00 1234 January 1 2004 2000 Honda_Accord
S1 1234 6000.00 Roth February 15 2004
S2 8000.00 2222 January 15 2004 1999 Ford_F150 9500.00 Jones January 25 2004
C 14000.00 3333 June 6 2003 1999 GMC_Suburban
C 5000.00 1222 January 1 2004 2000 Honda_Civic
C 10000.00 4444 July 10 2002 2001 Toyota_Tundra
S2 12000.00 5555 February 6 2003 1965 Ford_Mustang 15000 Williams September 4 2003
S1 3333 15650 Brown December 25 2003
C 10500.00 4000 July 10 2002 2001 Toyota_Tundra
S2 9500.00 6666 January 21 2004 1999 Ford_F150 9000.00 Smith January 31 2004
C 5000.00 1222 January 1 2004 2000 Honda_Civic
S2 8000.00 2222 January 15 2004 1999 Ford_F150 9500.00 Jones January 25 2004
S2 9900.00 6667 January 21 2004 1999 Ford_F150 9007.00 Byrd January 31 2004
*****************************************************
==============================================================
5000.0 1234 January-1-2004 2000 Honda_Accord
5000.0 1234 January-1-2004 2000 Honda_Accord 6000.0 Roth February-15-2004
8000.0 2222 January-15-2004 1999 Ford_F150 9500.0 Jones January-25-2004
14000.0 3333 June-6-2003 1999 GMC_Suburban
5000.0 1222 January-1-2004 2000 Honda_Civic
10000.0 4444 July-10-2002 2001 Toyota_Tundra
12000.0 5555 February-6-2003 1965 Ford_Mustang 15000.0 Williams September-4-2003
14000.0 3333 June-6-2003 1999 GMC_Suburban 15650.0 Brown December-25-2003
10500.0 4000 July-10-2002 2001 Toyota_Tundra
9500.0 6666 January-21-2004 1999 Ford_F150 9000.0 Smith January-31-2004
5000.0 1222 January-1-2004 2000 Honda_Civic
8000.0 2222 January-15-2004 1999 Ford_F150 9500.0 Jones January-25-2004
9900.0 6667 January-21-2004 1999 Ford_F150 9007.0 Byrd January-31-2004
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.