*Update, I was just in the wrong file of the code.
I'm not sure where to start regarding this lab? Does the exercise want me to write methods for the four "myCar..." statements?
9.20 LAB: Car value (classes)
Given main(), complete the Car class (in file Car.java) with methods to set and get the purchase price of a car (setPurchasePrice(), getPurchasePrice()), and to output the car's information (printInfo()).
Ex: If the input is:
2011 18000 2018
where 2011 is the car's model year, 18000 is the purchase price, and 2018 is the current year, the output is:
Car's information: Model year: 2011 Purchase price: 18000 Current value: 5770
import java.util.Scanner;
import java.lang.Math;
public class CarValue {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
Car myCar = new Car();
int userYear = scnr.nextInt();
int userPrice = scnr.nextInt();
int userCurrentYear = scnr.nextInt();
myCar.setModelYear(userYear);
myCar.setPurchasePrice(userPrice);
myCar.calcCurrentValue(userCurrentYear);
myCar.printInfo();
}
}
public class Car {
private int modelYear;
// TODO: Declare purchasePrice field (int)
private int currentValue;
public void setModelYear(int userYear){
modelYear = userYear;
}
public int getModelYear() {
return modelYear;
}
// TODO: Define setPurchasePrice() method
// TODO: Define getPurchasePrice() method
public void calcCurrentValue(int currentYear) {
double depreciationRate = 0.15;
int carAge = currentYear - modelYear;
// Car depreciation formula
currentValue = (int)
Math.round(purchasePrice * Math.pow((1 - depreciationRate),
carAge));
}
// TODO: Define printInfo() method to output modelYear,
purchasePrice, and currentValue
}
Answer: Hey dear student find the solution of your query, if you have any doubt feel free to ask. Thanks!!
Java Code:
import java.io.*;
import java.util.Scanner;
import java.lang.Math;
class Car //class Car
{
int modelYear; //data members of this class
double purchasePrice;
int currentYear;
int currentValue;
public int setModelYear(int model) //set and get
methods to all data members
{
modelYear = model;
return modelYear;
}
public int getModelYear() //return model year
{
return modelYear;
}
public double setPurchasePrice(int p) //set
purchasePrice
{
purchasePrice = p;
return purchasePrice;
}
public double getPurchasePrice() //get
purchasePrice
{
return purchasePrice;
}
public void setCurrentYear(int CY) //set
currentYear
{
currentYear = CY;
}
public void calcCurrentValue(int currentYear) //get
currentValue of Car
{
double depreciationRate = 0.15;
//defined depreciationRate
int carAge = currentYear
-modelYear; //calculate car age
//formula to calculate car
Value
currentValue =
(int)Math.round(purchasePrice
*Math.pow((1-depreciationRate),carAge));
}
public void printInfo() //printInfo method to print
all values
{
System.out.println("Car's
Information: Model Year: "+modelYear +" Purchase Price:
"+(int)purchasePrice+" Current Value: "+currentValue);
}
}
//driver class
class Main
{
public static void main(String[] args) //main method
{
Scanner scnr = new Scanner(System.in); //scanner call
Car myCar = new Car(); //create objectof the Car class
int userYear = scnr.nextInt(); //prompt for userYear
int userPrice = scnr.nextInt(); //prompt for purchasePrice
int userCurrentYear = scnr.nextInt(); //prompt for
currentYear
myCar.setModelYear(userYear); //call setModelYear
myCar.setPurchasePrice(userPrice); //call setPurchasePrice
method
myCar.calcCurrentValue(userCurrentYear); //call calcCurrentValue
method
myCar.printInfo(); //call printInfo method
}
}
import java.io.*; import java.util.Scanner; import java.lang.Math; class Car //class Car { int modelYear; //data members of this class double purchasePrice; int currentYear; int currentValue; public int setModelYear(int model) //set and get methods to all data members { modelYear = model; return modelYear; } public int getModelYear() //return model year { return modelYear; } public double setPurchasePrice(int p) //set purchasePrice { purchasePrice = p; return purchasePrice; } public double getPurchasePrice() //get purchasePrice { return purchasePrice; } public void setCurrentYear(int CY) //set currentYear { currentYear = CY; } public void calcCurrentValue(int currentYear) //get currentValue of Car { double depreciationRate = 0.15; //defined depreciationRate int carAge = currentYear -modelYear; //calculate car age //formula to calculate car Value currentValue = (int)Math.round(purchasePrice *Math.pow((1-depreciationRate),carAge)); } public void printInfo() //printInfo method to print all values { System.out.println("Car's Information: Model Year: "+modelYear +" Purchase Price: "+(int)purchasePrice+" Current Value: "+currentValue); } } //driver class class Main { public static void main(String[] args) //main method { Scanner scnr = new Scanner(System.in); //scanner call Car myCar = new Car(); //create objectof the Car class int userYear = scnr.nextInt(); //prompt for userYear int userPrice = scnr.nextInt(); //prompt for purchasePrice int userCurrentYear = scnr.nextInt(); //prompt for currentYear myCar.setModelYear(userYear); //call setModelYear myCar.setPurchasePrice(userPrice); //call setPurchasePrice method myCar.calcCurrentValue(userCurrentYear); //call calcCurrentValue method myCar.printInfo(); //call printInfo method } }

*Update, I was just in the wrong file of the code. I'm not sure where to...