In: Computer Science
Please note that problem solving often requires you to consider additional requirements that are not specifically listed.
You are to write a Java program to represent cars.
2020 Ford Mustang 28999 27500
The program has a Driver class (Driver.java) with the main method. The main method:
package Array;
// Defines class Car
public class Car
{
// Instance variable to store data
private int carModelYear;
private String make;
private String model;
private double retailPrice;
private double currentValue;
// Parameterized constructor to assign all parameter
except current value
// to instance variables
Car(int carModelYear, String make, String model,
double retailPrice)
{
this.carModelYear =
carModelYear;
this.make = make;
this.model = model;
this.retailPrice =
retailPrice;
this.currentValue = 0.0;
}// End of parameterized constructor
// Overrides parameterized constructor to assign all
parameter value
// to instance variables
Car(int carModelYear, String make, String model,
double
retailPrice, double currentValue)
{
this.carModelYear =
carModelYear;
this.make = make;
this.model = model;
this.retailPrice =
retailPrice;
this.currentValue =
currentValue;
}// End of parameterized constructor
// Method to return model year
int getCarModelYear()
{
return carModelYear;
}// End of method
// Method to return make
String getMake()
{
return make;
}// End of method
// Method to return model
String getModel()
{
return model;
}// End of method
// Method to return retail price
double getRetailPrice()
{
return retailPrice;
}// End of method
// Method to return current value
double getCurrentValue()
{
return currentValue;
}// End of method
// Method to set model year
void setCarModelYear(int carModelYear)
{
this.carModelYear =
carModelYear;
}// End of method
// Method to set make
void setMake(String make)
{
this.make = make;
}// End of method
// Method to set model
void setModel(String model)
{
this.model = model;
}// End of method
// Method to set retail price
void setRetailPrice(double retailPrice)
{
this.retailPrice =
retailPrice;
}// End of method
// Method to set current value
void setCurrentValue(double currentValue)
{
this.currentValue =
currentValue;
}// End of method
// Method to display car information
public void print()
{
System.out.println(carModelYear + "
" + make + " " + model + " "
+ retailPrice + " " + currentValue);
}// End of method
}// End of class Car
-------------------------------------------------------------------------------------------------------------
package Array;
import java.util.ArrayList;
// Driver class definition
public class CarDriver
{
// main method definition
public static void main(String ss[])
{
// Creates an array list to store
Car class objects
ArrayList <Car> cars = new
ArrayList<Car>();
// Creates 3 object of Car class
using parameterized constructor
Car car1 = new Car(2020, "Ford",
"Mustang", 28999, 27500);
Car car2 = new Car(2020, "Santro",
"Fero", 89456, 32809);
Car car3 = new Car(2020, "Maruti",
"M800", 45988, 25357);
// Adds the car objects to array
list
cars.add(car1);
cars.add(car2);
cars.add(car3);
System.out.println("\n
************* Car Information ************* ");
// Loops till end of the array
list
for(int c = 0; c < cars.size();
c++)
// Calls the
method to display each car object information
cars.get(c).print();
// Calculates 10% less value of
third car object current value
double valueLess =
cars.get(2).getCurrentValue() -
(cars.get(2).getCurrentValue()* 0.10);
// Sets the third car object's
current value
cars.get(2).setCurrentValue(valueLess);
// Sets the first car object's
retail price
cars.get(0).setRetailPrice(30000);
System.out.println("\n
************* Car Information after modification *************
");
// Loops till end of the array
list
for(int c = 0; c < cars.size();
c++)
// Calls the
method to display each car object information
cars.get(c).print();
}// End of main method
}// End of driver class
Sample Output:
************* Car Information *************
2020 Ford Mustang 28999.0 27500.0
2020 Santro Fero 89456.0 32809.0
2020 Maruti M800 45988.0 25357.0
************* Car Information after modification
*************
2020 Ford Mustang 30000.0 27500.0
2020 Santro Fero 89456.0 32809.0
2020 Maruti M800 45988.0 22821.3