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

. Create a class named after your favorite object in the whole world. For exampl

ID: 3916607 • Letter: #

Question

. Create a class named after your favorite object in the whole world. For example, if you love pizza, then create a class called Pizza.java. This class should not have a main method . Create Instance Variables (attributes) (1 point Create at least 3 private instance variables (attributes) for your class e You must use at least 3 different data types for your instance variables o Create getter (accessor) and setter (mutator) methods (1 point) . Create a getter (accessor) method for each of your instance variables Create a setter (mutator) method for each of your instance variables Create a Method to display your data (1 point) . Create a method called display, that simply prints out the values of all instance variables of your object The method should have no parameters and no return type Create another method that simply increments any one of your instance variables by 1 (2 points The method should have no parameters and no return type Create 2 Constructors Create a default constructor (no parameters) that assigns all your instance variables to default values (1 point) e Create a parameterized constructor that takes all instance variables as parameters, and sets the instance variables to the values provided by the parameters (1 point Testing your program Create a class called Demojava. This class will contain your main method Create an instance of your class by using the default constructor. (1 point) Call all your objects set methods to assign values to your object . Call the objects display method, to print out it's values Create another instance of your class by using the parameterized constructor (1 point) . Call the objects display method, to print out it's values Call the method you created that increments one instance variable, and then call the display method again (1 point)

Explanation / Answer

ScreenShot

-----------------------------------------------------------------

Program

/* This program create a class Car
* 3 instance variables
* 2 constructors
* Setter and getter methods
* Increment one instance variable member function
* Display Car details
*/
//Create a car class
class Car{
   //Attributes
   String name;
   int seatingCapacity;
   double mileage;
   //Default constructor
   Car(){
       name="";
       seatingCapacity=0;
       mileage=0.0;
   }
   //Parameterized constructor
   Car(String name,int seatingCapacity,double mileage){
       this.name=name;
       this.seatingCapacity=seatingCapacity;
       this.mileage=mileage;
   }
   //Mutators
   public void setName(String name) {
       this.name=name;
   }
   public void setSeatingCapacity(int seatingCapacity) {
       this.seatingCapacity=seatingCapacity;
   }
   public void setMileage(double mileage) {
       this.mileage=mileage;
   }
   //Accessors
   public String getName() {
       return name;
   }
   public int getSeatingCapacity() {
       return seatingCapacity;
   }
   public double getMileage() {
       return mileage;
   }
   //Instance variable increments 1
   public void incrementMileage() {
       mileage+=1;
   }
   //Display method
   public void display() {
       System.out.println("NameOfTheVehicle="+name+" SeatingCapacity="+seatingCapacity+" Mileage="+mileage+"km");
   }
}
public class Demo {

   public static void main(String[] args) {
       //Default object creation
       Car c=new Car();
       //Set method calling
       c.setName("Hundai Grand i10");
       c.setSeatingCapacity(5);
       c.setMileage(17);
       System.out.println("Display Car Details using Default Constructor: ---------------------------------------");
       c.display();
       //Parameterized constructor
       Car cs=new Car("Ford Eco Sport",7,20);
       System.out.println("Display Car Details using Parameterized Constructor: --------------------------------------");
       cs.display();
       //Increment mileage
       cs.incrementMileage();
       System.out.println("Display Car Details After increment mileage call: -------------------------------------------");
       cs.display();

   }

}

---------------------------------------------------------

Output

Display Car Details using Default Constructor:
---------------------------------------
NameOfTheVehicle=Hundai Grand i10
SeatingCapacity=5
Mileage=17.0km
Display Car Details using Parameterized Constructor:
--------------------------------------
NameOfTheVehicle=Ford Eco Sport
SeatingCapacity=7
Mileage=20.0km
Display Car Details After increment mileage call:
-------------------------------------------
NameOfTheVehicle=Ford Eco Sport
SeatingCapacity=7
Mileage=21.0km