Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class that represents a car. Each car has a year, and a current mileage

ID: 3575063 • Letter: W

Question

Write a class that represents a car. Each car has a year, and a current mileage reading. Include an appropriate constructor, getYear() and getMileage() methods, a toString() method, and an updateMileage() 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 toString() method must return a string that contains the year and mileage in a readable fashion. The updateMileage() method should have one parameter, which is the amount to increase the mileage. You should not write a main method to use/test the class, just the class itself. public class

Car { }

Explanation / Answer

import java.util.Scanner;
public class Car{
public static int year;
public static int mileage;
Car(int year,int mileage){
this.year=year;
this.mileage=mileage;
}
public static void getYear(){
  
Scanner input=new Scanner(System.in);
System.out.println("Enter year of the car : ");
year=input.nextInt();
}
public static void updateMileage(int amount){
mileage=mileage+amount;
}
public static String tostring(){
String readable="Year = "+Integer.toString(year)+" Mileage = "+Integer.toString(mileage);
return readable;
}
public static void main(String []args){
//System.out.println("Hello World");
Car c=new Car(2016,10);
getYear();
updateMileage(2);
System.out.println(tostring());
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote