Write a class that represents a car. Each car has a year and a current mileage r
ID: 3579586 • Letter: W
Question
Write a class that represents a car. Each car has a year and a current mileage reading. Include an appropriate constructor, a to string () method, and an update Mileage () method that increases the mileage. You only need to provide one constructor, and that constructor must have two parameters that are used to initialize both data fields. The to String () method must return a string that contains the year and mileage in a readable fashion. The update Mileage () method should have one parameter which is the account to increase mileage by. You do not need to write a main () method to test the class, just the class itself. public class car {Explanation / Answer
Car.java
public class Car {
private int year;
private double currentMileage;
public Car(int year, double milegae){
this.year = year;
this.currentMileage = milegae;
}
public String toString(){
return "Car Year: "+year+" Mileage: "+currentMileage;
}
public void updateMileage(double mileage){
currentMileage = currentMileage + mileage;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public double getCurrentMileage() {
return currentMileage;
}
public void setCurrentMileage(double currentMileage) {
this.currentMileage = currentMileage;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.