Question

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

*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

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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 } }

Add a comment
Know the answer?
Add Answer to:
*Update, I was just in the wrong file of the code. I'm not sure where to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT