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

Write a class named car that contains the following instance variables: int data

ID: 3808473 • Letter: W

Question

Write a class named car that contains the following instance variables: int data field named plateNumber for the car plate number. int data field named yearModel for the car year model A string data field named color for the car color. It contains the following methods: A constructor that initializes the plateNumber, yearModel and color. A set and get method for each instance variable. write a class CarService that contains the following instance variables: Array named carsArray of type Car. int data field named maxSize for the array size. It contains the following methods: A constructor that initializes the maxSize and creates the array carsArray. void Add(int, int, String) that creates and adds the Car to the array. The method prints an error message if the array is full. boolean search (int) that checks whether the Car object with car plate number passed as parameter exists in the array or not. Car serviced () that removes the first car from the array and shift the array to the left. void print () that prints all cars in the array in a tabular format as shown in the sample output. write a test application named CarTest. In the main method, do the following: Creates a Carservice object with max size = 5. Display a menu of choices as shown in the sample output. The program stops when the user enters choice 5.

Explanation / Answer

Car.java

package simple;

public class Car {

   private int plateNumber;

   private int yearModel;

   private String color;

  

   Car(int plateNumber, int yearModel, String color)

   {

       this.plateNumber = plateNumber;

       this.yearModel = yearModel;

       this.color = color;

   }

   public int getPlateNumber() {

       return plateNumber;

   }

   public void setPlateNumber(int plateNumber) {

       this.plateNumber = plateNumber;

   }

   public int getYearModel() {

       return yearModel;

   }

   public void setYearModel(int yearModel) {

       this.yearModel = yearModel;

   }

   public String getColor() {

       return color;

   }

   public void setColor(String color) {

       this.color = color;

   }

  

  

}

CarService.java

package simple;

public class CarService {

  

   private Car carsArray[];

   private int maxSize;

   private int size;

   public CarService(int maxSize) {

       size = 0;

       this.maxSize = maxSize;

       carsArray = new Car[maxSize];

   }

  

   public void Add(int plateNumber, int yearModel, String color)

   {

       Car car = new Car(plateNumber, yearModel, color);

       if(size < maxSize)

       {

           carsArray[size++] = car;

       }

       else

           System.out.println("Cars Array is full");

   }

  

   public boolean search(int plateNumber)

   {

       for(int i=0; i<carsArray.length; i++)

       {

           if(carsArray[i].getPlateNumber() == plateNumber)

           {

               return true;

           }

       }

       return false;

   }

  

   public Car serviced()

   {

       Car car = carsArray[0];

       for(int i=0; i<size-1; i++)

       {

           carsArray[i] = carsArray[i+1];

       }

       carsArray[size-1] = null;

       size--;

       return car;

   }

  

   public void print()

   {

       System.out.println("Plate Number Model Year Color");

       for(int i=0; i<size; i++)

       {

           System.out.println(carsArray[i].getPlateNumber() + " " + carsArray[i].getYearModel() + " " + carsArray[i].getColor());

       }

   }

}

CarTest.java

package simple;

import java.util.Scanner;

public class CarTest {

   public static void main(String[] args) {

       int choice;

       Scanner keyboard = new Scanner(System.in);

       CarService servicer = new CarService(5);

       String color;

       int platenumber, year;

       while(true)

       {

           System.out.println("MENU 1. Add a car 2. Search for a Car 3. Service a car 4. Print all cars 5. Exit");

           System.out.println("Enter your choice: ");

           choice = Integer.parseInt(keyboard.nextLine());

          

           switch(choice)

           {

           case 1: System.out.print("Enter the plate number: ");

                   platenumber = Integer.parseInt(keyboard.nextLine());;

                   System.out.print("Enter the model year: ");

                   year = Integer.parseInt(keyboard.nextLine());

                   System.out.print("Enter the color:");

                   color = keyboard.nextLine();

                   servicer.Add(platenumber, year, color);

                   break;

           case 2: System.out.print("Enter the plate number: ");

                   platenumber = Integer.parseInt(keyboard.nextLine());;

                   if(servicer.search(platenumber))

                       System.out.println("The car with plate number " + platenumber + " is found");

                   else System.out.println("The car with plate number " + platenumber + " is not found");

                   break;

           case 3: servicer.serviced();break;

           case 4: servicer.print();break;

           case 5: System.exit(0);

           }

       }

   }

}

OUTPUT:

MENU

1. Add a car

2. Search for a Car

3. Service a car

4. Print all cars

5. Exit

Enter your choice:

1

Enter the plate number: 1234

Enter the model year: 1998

Enter the color:Red

MENU

1. Add a car

2. Search for a Car

3. Service a car

4. Print all cars

5. Exit

Enter your choice:

1

Enter the plate number: 2345

Enter the model year: 2009

Enter the color:Blue

MENU

1. Add a car

2. Search for a Car

3. Service a car

4. Print all cars

5. Exit

Enter your choice:

4

Plate Number   Model Year   Color

1234       1998       Red

2345       2009       Blue

MENU

1. Add a car

2. Search for a Car

3. Service a car

4. Print all cars

5. Exit

Enter your choice:

3

MENU

1. Add a car

2. Search for a Car

3. Service a car

4. Print all cars

5. Exit

Enter your choice:

4

Plate Number   Model Year   Color

2345       2009       Blue

MENU

1. Add a car

2. Search for a Car

3. Service a car

4. Print all cars

5. Exit

Enter your choice:

2

Enter the plate number: 2345

The car with plate number 2345 is found

MENU

1. Add a car

2. Search for a Car

3. Service a car

4. Print all cars

5. Exit

Enter your choice:

5

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote