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

JAVA In the first part of this assignment, you will design and write a class tha

ID: 650047 • Letter: J

Question

JAVA

In the first part of this assignment, you will design and write a class that represents a real-world object of your choice. In the second part, you will write a program that demonstrates the use of the class.

Part I:

Select a "real-world" object that has not been used in class lecture and/or the textbook.

The object you choose must be defined by at least:

Have at least two characteristics (attributes).

Have at least two behaviors (operations).

The class that you write to represent the object must minimally meet the requirements of:

The "data" part must declare the attributes correctly.

The "operations" part must include at least the following methods:

A no-arg constructor method that creates an object with reasonable defaults for the initial state.

The set and get methods for each attribute.

Two methods that appropriately carry out the behaviors identified in the design.

Bonus (10 points) : A constructor that takes the initial values for the object's attributes from the user rather than setting defaults.

Part II: Write a "client" class that demonstrates the use of the object. Examples from class: RectangleDemo class & CircleDemo.

Explanation / Answer

Comment if you have any doubts

//Car class

p

public class Car
{
   private String name;
   private int noOfPassengers;
  
   public Car()
   {
       this.name="My Car";
       this.noOfPassengers=0;
   }


   public int getNoOfPassengers() {
       return noOfPassengers;
   }
  
   public void setNoOfPassengers(int noOfPassengers) {
       this.noOfPassengers = noOfPassengers;
   }
  
   public void addPassenger()
   {
       this.noOfPassengers++;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public void resetMyName()
   {
       this.setName("My Car");
   }
   public String toString()
   {
       return "My car's name is "+this.name+" and it has "+this.noOfPassengers+" passegers in it currently.";
   }
}

//CarDemo class

import java.util.*;

public class CarDemo
{
   public static void main(String[] args)
   {
       Scanner input=new Scanner(System.in);
       Car car=new Car();
       System.out.println(car.toString());
      
       System.out.print("Enter a name to your car: ");
       String name=input.nextLine();
       car.setName(name);
      
       System.out.println("Enter number of passengers in your car: ");
       int number=input.nextInt();
       input.nextLine();
      
       car.setNoOfPassengers(number);
      
       System.out.println(car.toString());
      
       System.out.println("Resetting car's name...");
       car.resetMyName();
       System.out.println(car.toString());
   }
}