'F' → Fullscreen
Implement the Engine class. It should track horsepower and whether it is running.
public class Engine{ private int horsepower; private boolean running;
public Engine(int horsepower) { this.horsepower = horsepower; running = false; }
public int getHorsepower() { return horsepower; }
public boolean isRunning() { return running; }
public void start() { running = true; }
public void stop() { running = false; }}
Implement the Car class that has-an Engine.
The engine may be null if it has not been installed yet.
Add the AP-style method getEngineHorsepower() that returns
-1 when there is no engine.
public class Car{ private String make; private String model; private Engine engine; // Composition: Car has-an Engine (can be null)
public Car(String make, String model) { this.make = make; this.model = model; engine = null; // no engine installed yet }
public String getMake() { return make; }
public String getModel() { return model; }
public void installEngine(Engine engine) { this.engine = engine; }
public void removeEngine() { engine = null; }
public boolean hasEngine() { return engine != null; }
// AP-style sentinel value method public int getEngineHorsepower() { if (engine == null) { return -1; // sentinel value: no engine installed }
return engine.getHorsepower(); }
public void start() { if (engine == null) { System.out.println("Cannot start: no engine installed."); return; }
engine.start(); System.out.println("Car started."); }
public void stop() { if (engine == null) { System.out.println("Cannot stop: no engine installed."); return; }
engine.stop(); System.out.println("Car stopped."); }
public String getStatus() { int hp = getEngineHorsepower();
if (hp == -1) { return make + " " + model + " | Engine: none"; }
String state;
if (engine.isRunning()) { state = "running"; } else { state = "stopped"; }
return make + " " + model + " | Engine: " + hp + " HP (" + state + ")"; }}Write a small menu-driven program that allows a user to install/remove an engine, start/stop the car, and print horsepower using the sentinel-value method.
import java.util.Scanner;
public class Program{ public static void main(String[] args) { Scanner input = new Scanner(System.in);
Car car = new Car("Honda", "Civic"); Engine smallEngine = new Engine(140); Engine sportEngine = new Engine(220);
System.out.println("-- Composition Demo: Car has-an Engine --"); System.out.println(car.getStatus());
boolean running = true;
while (running) { System.out.println("\n--- Menu ---"); System.out.println("1) Show status"); System.out.println("2) Install 140 HP engine"); System.out.println("3) Install 220 HP engine"); System.out.println("4) Remove engine"); System.out.println("5) Start car"); System.out.println("6) Stop car"); System.out.println("7) Print engine horsepower (AP-style)"); System.out.println("8) Quit"); System.out.print("> ");
String choice = input.nextLine();
if (choice.equals("1")) { System.out.println(car.getStatus()); } else if (choice.equals("2")) { car.installEngine(smallEngine); System.out.println("Installed 140 HP engine."); System.out.println(car.getStatus()); } else if (choice.equals("3")) { car.installEngine(sportEngine); System.out.println("Installed 220 HP engine."); System.out.println(car.getStatus()); } else if (choice.equals("4")) { car.removeEngine(); System.out.println("Engine removed."); System.out.println(car.getStatus()); } else if (choice.equals("5")) { car.start(); System.out.println(car.getStatus()); } else if (choice.equals("6")) { car.stop(); System.out.println(car.getStatus()); } else if (choice.equals("7")) { int hp = car.getEngineHorsepower();
if (hp == -1) { System.out.println("Horsepower: (no engine installed)"); } else { System.out.println("Horsepower: " + hp); } } else if (choice.equals("8")) { running = false; } else { System.out.println("Invalid choice."); } }
System.out.println("\nGoodbye!"); }}Your output should look similar to the sample below (values may vary slightly).
-- Composition Demo: Car has-an Engine --Honda Civic | Engine: none
--- Menu ---1) Show status2) Install 140 HP engine3) Install 220 HP engine4) Remove engine5) Start car6) Stop car7) Print engine horsepower (AP-style)8) Quit> 7Horsepower: (no engine installed)
--- Menu ---> 5Cannot start: no engine installed.Honda Civic | Engine: none
--- Menu ---> 2Installed 140 HP engine.Honda Civic | Engine: 140 HP (stopped)
--- Menu ---> 7Horsepower: 140
--- Menu ---> 5Car started.Honda Civic | Engine: 140 HP (running)
--- Menu ---> 4Engine removed.Honda Civic | Engine: none
--- Menu ---> 7Horsepower: (no engine installed)
--- Menu ---> 8
Goodbye!null in a program?
Give a real-world reason.
engine.start() without checking for null?
getEngineHorsepower() return
-1 instead of returning
0? What does -1 represent?
Submit your project files (Engine.java, Car.java, Program.java) and your reflection answers to the appropriate dropbox.