question archive Choose one of the thing classes(class Ferrari or class McLaren) and write a main class to store an array of objects

Choose one of the thing classes(class Ferrari or class McLaren) and write a main class to store an array of objects

Subject:Computer SciencePrice: Bought3

Choose one of the thing classes(class Ferrari or class McLaren) and write a main class to store an array of
objects. Write a menu() in the main class, to allows the user to view the contents of each object in the array,
change the attribute value of one speci?c object, change the attribute value of all objects in the array.

HERE IS THE CODE:

 

import java.util.Scanner;

public class Main { 
   
   private static Scanner sc = new Scanner(System.in);
   private static Ferrari fr = new Ferrari();
   private static McLaren mc = new McLaren();

   public static void main(String[] args) {

       System.out.println(fr.toString());
       System.out.println();
       System.out.println(mc.toString());

       System.out.println("Enter Ferrari 812 engine Specs: ");
       String engine = sc.nextLine();
       fr.setEngine(engine);

       System.out.println("Enter McLaren 720S top speed: ");
       int speed = sc.nextInt();
       mc.setSpeed(speed);

       System.out.println(" Available Options: ");
       displayCar(fr, mc);

       int choice = getUserChoice();

       if(choice ==1) {
          System.out.println("Fantastic!! You chose Ferrari 812 Superfast.");
          System.out.println("with 789-819 hp");
       } else if(choice == 2) {
          System.out.println("Nice choice, the McLaren720S has 710hp.");
          System.out.println("0-60mph takes just 2.8sec and 124mph arrives in an astonishing 7.8 seconds.");
       }
   }

   public static void displayCar(Ferrari f, McLaren m) {
       System.out.println("n. FERRARI 812:");
       System.out.println(f.toString());
       System.out.println();
   }

   public static int getUserChoice() {
       System.out.println("Enter your choice from the available options:");
       int choice = sc.nextInt();
       return choice;
   }
}

class McLaren {
   
   private int speed;
   private String engine;
   private boolean sunroof;
   private final double PRICE = 225000.00;

   public McLaren() {
       this(200,"McLaren 4.0-liter twin-turbo V8", false);
   }
   
   public McLaren(int speed, String engine, boolean sunroof) {
       this.speed = speed;
       this.engine = engine;
       this.sunroof = sunroof;
   }
   
   public void setSpeed(int s) {
       this.speed = s;
   }

   public void setEngine(String e) {
       this.engine = e;
   }

   public void setSunroof(boolean s) {
       this.sunroof = s;
   }

   public int getSpeed() {
       return speed;
   }

   public String getEngine() {
       return engine;
   }

   public double getPrice() {
       return PRICE;
   }

   public boolean getSunroof() {
       return sunroof;
   }

   public String toString() {
       return "Speed: " + getSpeed() + "mphnEngine:" + getEngine() + "nSunroof:" + getSunroof() + "nPrice: $" + getPrice();
   }
}

class Ferrari {
   
   private int speed;
   private String engine;
   private boolean sunroof;
   private final double PRICE = 300000.00;

   public Ferrari() {
       this(190, "Ferrari 296 3.0- Liter V6", true);
   }

   public Ferrari(int speed, String engine, boolean sunroof) {
       this.speed = speed;
       this.engine = engine;
       this.sunroof = sunroof;
   }

   public void setSpeed(int s) {
       this.speed = s;
   }

   public void setEngine(String e) {
       this.engine = e;
   }

   public void setSunroof(boolean s) {
       this.sunroof = s;
   }

   public int getSpeed() {
       return speed;
   }

   public String getEngine() {
       return engine;
   }

   public boolean getSunroof() {
       return sunroof;
   }

   public double getPrice() {
       return PRICE;
   }

   public String toString() {
       return "Speed: " + getSpeed() + "mphnEngine: " + getEngine() + "nSunroof: " + getSunroof() + "nPrice: $" + getPrice();
   }
}

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE