In java write a class Car that contains fields “make”, “model” and “year.” The f
ID: 3668183 • Letter: I
Question
In java write a class Car that contains fields “make”, “model” and “year.” The first two fields are Strings and the third one is an int. Define a constructor to accept and initialize instance data. Then define more methods. A toString method is included to print the description of the car. Then create a drive class called CarTest which has a main method.. You’ll complete the program. When you run CarTest the result should look like this:
> run CarTest
2007 Ford Taurus
2011 Honda Odyssey
1999 Toyota Corolla
1966 Ford Mustang
Update data!
2007 Ford Explorer
2010 Honda Odyssey
1999 Nissan Maxima
1966 Ford Mustang
Explanation / Answer
public class Car {
private String Make;
private String Model;
private int year;
public Car(String Make, String Model, String year){
//Constructor,
//stores the make, model and registration number of the new car
//sets its status as available for hire.
Make = "";
Model = "";
year = "";
}
public String getMake(){
return Make;
}
public String getModel(){
return Model;
}
public boolean hire(String newHirer){
{
//Hire this car to the named hirer and return true.
return true;
}
//Returns false if the car is already out on hire.
}
public boolean returnFromHire(){
{
//Receive this car back from a hire and returns true.
return true;
}
//Returns false if the car was not out on hire
}
public int getRyear(){
//Accessor method to return the car’s registration number
RegistrationNum++;
return RegistrationNum;
}
public boolean hireable(){
//Accessor method to return the car’s hire status.
{
//returns true if car available for hire
return true;
}
}
public String toString(){
//return car details as formatted string
//output should be single line containing the make model and reg number
//followed by either "Available for hire" or "On hire to: <name>"
return "Vehicle ID number: "+ getyear()+"-"+"Make is: "+ getMake()+"-"+"Model is: "+getModel();
}
}
Following is my Driver class:
import java.util.*;
public class CarDriver {
static Car car1;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
{
System.out.println("Make?");
String Make=scan.nextLine();
System.out.println("Model");
String Model=scan.nextLine();
System.out.println("year?");
String year=scan.nextLine();
car1 = new Car(Make,Model,year);
System.out.println("What you input :");
System.out.println(car1.toString());
}}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.