Question

public class Auto { private String make; private String model; private int year; } a) write...

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

Homework Answers

Answer #1

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:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
For the following class Book: 1- Write a default constructor to give default values (title= Intro...
For the following class Book: 1- Write a default constructor to give default values (title= Intro to Programming and price = 20) to the class’s variables. 2 - Write a parameterized constructor to allow the user to initialize the class variables. 3- Write getter & setter for each variable of this class. 4- Then write a main code to create an instance of this class using parameterized constructor, ask user for inputs to initialize the variables of this instance and...
Part 1 Given the following code: public class MyClass { private double score; private String studentID;...
Part 1 Given the following code: public class MyClass { private double score; private String studentID; //code of the class ..... } //end of MyClass Code a default constructor and an overloaded constructor of MyClass that will assign values to its two instance fields: Please write in JAVA Part 2 Continue from question about, code an mutator of instance field called studentID:
Please show fully functioning java code and outputs. Design a Java Animal class (assuming in Animal.java...
Please show fully functioning java code and outputs. Design a Java Animal class (assuming in Animal.java file) and a sub class of Animal named Cat (assuming in Cat.java file).   The Animal class has the following protected instance variables: boolean vegetarian, String eatings, int numOfLegs and the following public instance methods: constructor without parameters: initialize all of the instance variables to some default values constructor with parameters: initialize all of the instance variables to the arguments SetAnimal: assign arguments to all...
Given a class: public class Money { int wholeNumber; int decimalPart; boolean positive; char currencySymbol; }...
Given a class: public class Money { int wholeNumber; int decimalPart; boolean positive; char currencySymbol; } Write a constructor for setting the 4 instance variables (in the order specified above). The constructor must validate the input throwing an IllegalArgumentException (you can specify any error message you see fit) if any of the following conditions are not met: either the int values are negative decimalPart is greater than 99 the currencySymbol is not one of '$', '€', or '£' Write an...
Exercise 2 Write a class named Vehicle Holds make and model (strings) and year (int) Contains...
Exercise 2 Write a class named Vehicle Holds make and model (strings) and year (int) Contains this method: Vehicle(String make, String model, int year) { this.make = make; this.model = model; this.year = year; } Add getters and setters for each variable Write a class called FordMustang that extends the Vehicle class               Create instance of object               Pass variables               Print
public class Date {    private int month;    private int day;    private int year;...
public class Date {    private int month;    private int day;    private int year;    public Date( int monthValue, int dayValue, int yearValue )    {       month = monthValue;       day = dayValue;       year = yearValue;    }    public void setMonth( int monthValue )    {       month = monthValue;    }    public int getMonth()    {       return month;    }    public void setDay( int dayValue )    {       day =...
Design a class with the following requirements: 1- give the class a name from your choice....
Design a class with the following requirements: 1- give the class a name from your choice. 2- write some code that creates three instance variables, choose a name and type for each one of them. (must have two private and one public variables) 3- Initialize the variables using two different constructors. 4- Use mutator and accessor methods (set and get) for the private data members. 5- Display the value of each member variable using a method. 6- In the main,...
Use the class definition below to answer the following questions. public class TrafficLight { String stopLight...
Use the class definition below to answer the following questions. public class TrafficLight { String stopLight = "red"; String waitLight; String goLight; public void setStopLight(String colour) { stopLight = colour; } public String getGreenLight() { return goLight; } How many field attributes are there in the TrafficLight class? Name a field attribute for this class. What is the name of the method that is an accessor? What is the name of the method that is a mutator? What is the...
write in C++ program and write an accessor that returns the value of the Car class’s...
write in C++ program and write an accessor that returns the value of the Car class’s make private member variable. Given the following class definition: class Car { public: Car() : make {}, model {}, year {} {} Car(std::string make, std::string model, int year) : make {make}, model {model} year {year} { } private: std::string make; std::string model; int year; };
Write a Python class, Flower, that has 3 instance variables name, num_petals and price which have...
Write a Python class, Flower, that has 3 instance variables name, num_petals and price which have types str, int and float. Your class must have a constructor method to initialize the variables to an appropriate value, and methods for setting the value of each instance variable (set methods) and for retrieving the instance variable values (get methods). You can use the credit card code we looked at for assistance.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT