public class Auto
{
private String make;
private String model;
private int year;
}
a) write a default constructor for the Auto Class
n) Write a constructor to initialize all instance variables
c) write accessor and mutator for make variable
d) create an onject name it myAuto with values of Toyota, Camry, and 2016
Program:
public class Auto
{
private String make;
private String model;
private int year;
//default constructor
Auto()
{
make="";
model="";
year=0;
}
//constructor to initialize all instance
variables
Auto(String strMake,String strModel, int y)
{
make=strMake;
model=strModel;
year=y;
}
//accessor and mutators for make variable
//method that returns make
public String getMake()
{
return make;
}
//method that sets make
public void setMake(String strMake)
{
make=strMake;
}
//method that returns model
public String getModel()
{
return model;
}
//method that returns year
public int getYear()
{
return year;
}
public static void main(String[] args)
{
//create an object name it myAuto
with values of Toyota, Camry, and 2016
Auto myAuto=new
Auto("Toyota","Camry",2016);
//print the object state using get
methods
System.out.println("Make:
"+myAuto.getMake());
System.out.println("Model:
"+myAuto.getModel());
System.out.print("Year:
"+myAuto.getYear());
}
}
Output:
Get Answers For Free
Most questions answered within 1 hours.