Write a program to create objects that represent automobiles Program Requirement
ID: 3710538 • Letter: W
Question
Write a program to create objects that represent automobiles Program Requirements The program will be written in NetBeans, and will follow the CS210 Coding Standards Create a new project named with a main class named: Topic11partl AutoManager NetBeans will create your main class file, AutoManager.java, containing the AutoManager class But this program will also need second class, used to define objects. Use File | New to create a new class named: Auto NetBeans will create your Auto class file, Auto.java, containing the Auto class Within the Auto class (which will be used to create Auto type objects), define Four private data fields (members) to hold o int modelYear the year the auto was built o String makeModel the auto's make and model, as one String o double mileage the auto's mileage in miles per gallon o double tankCapacity the auto's tank capacity in gallons A default constructor o Constructor will initializes the integer fields to 0, double fields to 0.0 and the String to "unknown" Auto Standard public mutator & accessor methods for each member (must use these exact names) o A setter for each data field/member "setModelYear () ' setMakeModel () "setMileage () "setTankCapacity() o A getter for each data field/member "getModelYear () "getMakeModel () "getMileage () getTankCapacity ()Explanation / Answer
Auto.java
public class Auto {
// Declaring instance variables
private int modelYear;
private String makeModel;
private double mileage;
private double tankCapacity;
// Zero argumented constructor
public Auto() {
this.modelYear = 0;
this.makeModel = "unknown";
this.mileage = 0.0;
this.tankCapacity = 0.0;
}
// getters and setters
public int getModelYear() {
return modelYear;
}
public void setModelYear(int modelYear) {
this.modelYear = modelYear;
}
public String getMakeModel() {
return makeModel;
}
public void setMakeModel(String makeModel) {
this.makeModel = makeModel;
}
public double getMileage() {
return mileage;
}
public void setMileage(double mileage) {
this.mileage = mileage;
}
public double getTankCapacity() {
return tankCapacity;
}
public void setTankCapacity(double tankCapacity) {
this.tankCapacity = tankCapacity;
}
}
_________________
AutoManager.java
import java.util.Scanner;
public class AutoManager {
public static void main(String[] args) {
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Creating an instance of Auto class
Auto auto=new Auto();
//calling the method
readAutoData(sc,auto);
System.out.println();
//Displaying the output
System.out.printf("%d %s has a %.1f gallon tank and gets %.1f miles per gallon ",auto.getModelYear(),auto.getMakeModel(),auto.getTankCapacity(),auto.getMileage());
}
//This method will read the input entered by the user
private static void readAutoData(Scanner sc, Auto auto) {
System.out.print("Enter auto model year :");
int year=sc.nextInt();
sc.nextLine();
System.out.print("Enter auto make and model :");
String make=sc.nextLine();
System.out.print("Enter auto mileage (in miles per gallon) :");
double mileage=sc.nextDouble();
System.out.print("Enter auto tank capacity (in gallons) :");
double gallons=sc.nextDouble();
//calling the setters
auto.setModelYear(year);
auto.setMakeModel(make);
auto.setMileage(mileage);
auto.setTankCapacity(gallons);
}
}
___________________
Output:
Enter auto model year :2010
Enter auto make and model :Honda Accord
Enter auto mileage (in miles per gallon) :27.5
Enter auto tank capacity (in gallons) :13.9
2010 Honda Accord has a 13.9 gallon tank and gets 27.5 miles per gallon
_______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.