using java write a code with notepad++
Create the following classes using inheritance and polymorphism
Ship named Sailboat Sally with max speed of 35.0 mph was built in 1980
Cruise Ship named Triumph with max speed of 52.0 mph was built in 2007 carries 5500 passengers
Cargo Ship named Valdez with max speed of 45.8 mph was built in 1968 with maximum cargo of 350.3 tons
Aircraft Carrier named Alabama with max speed of 55.5 mph was built in 1939 carries 2300 crew members with 42 planes
Following is the answer:
Ship.java
public class Ship { private String name; private float maxSpeed; private int year; Ship(){} Ship(String name, float maxSpeed, int year){ this.name = name; this.maxSpeed = maxSpeed; this.year = year; } public String getName() { return name; } public void setName(String name) { this.name = name; } public float getMaxSpeed() { return maxSpeed; } public void setMaxSpeed(float maxSpeed) { this.maxSpeed = maxSpeed; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } @Override public String toString() { return "Ship named " + this.name + " with max speed of " + this.maxSpeed + "mph was built in " + this.year; } }
CruiseShip.java
public class CruiseShip extends Ship { private int maxPassengers; CruiseShip(){} CruiseShip(String name, float maxSpeed, int year, int maxPassengers){ super(name, maxSpeed, year); this.maxPassengers = maxPassengers; } public int getMaxPassengers() { return maxPassengers; } public void setMaxPassengers(int maxPassengers) { this.maxPassengers = maxPassengers; } @Override public String toString() { return super.toString() + " carries " + this.maxPassengers + "passengers" ; } }
CargoShip.java
public class CargoShip extends Ship { private float maxTons; CargoShip(){} CargoShip(String name, float maxSpeed, int year, float maxTons){ super(name, maxSpeed, year); this.maxTons = maxTons; } public float getMaxTons() { return maxTons; } public void setMaxTons(float maxTons) { this.maxTons = maxTons; } @Override public String toString() { return super.toString() + " with maximum cargo of " + this.maxTons + " tons"; } }
AircraftCarrier.java
public class AircraftCarrier extends CruiseShip { private int maxPlane; AircraftCarrier(){} AircraftCarrier(String name, float maxSpeed, int year, int maxPassengers, int maxPlane){ super(name, maxSpeed, year, maxPassengers); this.maxPlane = maxPlane; } public int getMaxPlane() { return maxPlane; } public void setMaxPlane(int maxPlane) { this.maxPlane = maxPlane; } @Override public String toString() { return super.toString() + " and " + this.maxPlane + " planes"; } }
main.java
public class main { public static void main(String[] args) { Ship ship = new Ship(); ship.setName("Ship1"); ship.setYear(2016); ship.setMaxSpeed(300); System.out.println("Ship1"); System.out.println(ship.toString()); Ship ship1 = new Ship("Ship2", 302.5f,2017); System.out.println("Ship2"); System.out.println(ship1.toString()); System.out.println(""); CruiseShip cruiseShip1 = new CruiseShip(); cruiseShip1.setName("Cruise1"); cruiseShip1.setMaxPassengers(300); cruiseShip1.setMaxSpeed(300); cruiseShip1.setYear(2013); System.out.println("CruiseShip1"); System.out.println(cruiseShip1.toString()); CruiseShip cruiseShip2 = new CruiseShip("Cruise2",300,2015,200); System.out.println("CruiseShip2"); System.out.println(cruiseShip2.toString()); System.out.println(""); CargoShip cargoShip1 = new CargoShip(); cargoShip1.setName("Cargo1"); cargoShip1.setMaxSpeed(300); cargoShip1.setMaxTons(1000); cargoShip1.setYear(2012); System.out.println("CargoShip1"); System.out.println(cargoShip1.toString()); CargoShip cargoShip2 = new CargoShip("Cargo2",250,2016,2000); System.out.println("CargoShip2"); System.out.println(cargoShip2.toString()); System.out.println(""); AircraftCarrier aircraftCarrier1 = new AircraftCarrier(); aircraftCarrier1.setMaxPlane(100); aircraftCarrier1.setMaxSpeed(1000); aircraftCarrier1.setName("AirCraft1"); aircraftCarrier1.setYear(2015); aircraftCarrier1.setMaxPassengers(500); System.out.println("Aircraft1"); System.out.println(aircraftCarrier1.toString()); AircraftCarrier aircraftCarrier2 = new AircraftCarrier("AirCraft2", 1000, 2016,300, 200); System.out.println("AirCraft2"); System.out.println(aircraftCarrier2.toString()); } }
Output:
using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship...
QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members: • A private String data field named name for the name of the ship. • A private String data field named yearBuilt for the year that the ship was built. • A constructor that creates a ship with the specified name and the specified year that the ship was built. • Appropriate getter and setter methods. A toString method that overrides the toString method...
[JAVA] Program: Design a Ship class that the following members: A field for the name of the ship (a string) o A field for the year the the ship was built (a string) o A constructor and appropriate accessors and mutators A toString method that displays the ship's name and the year it was built Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members: A field for the maximum number of passengers...
Program Challenge 10 Design a Ship class that the following members: · A field for the name of the ship (a string). · A field for the year that the ship was built (a string). · A constructor and appropriate accessors and mutators. · A toString method that displays the ship’s name and the year it was built. Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members: · A field for the...
C++ code For each of the following classes create default constructor and parameterized constructors(calling parent(s) parameterized constructors where it applies Add accessor and mutator function for the attribute inherent to the class Create a Base class called Shape2d with the protected floating point attribute area operator overload the + & - and operations to return the float respective to the area Derive from the Base class from called Shape2d called Rectangle with the additional floating-point attributes length & width Derive...
java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the below classes: public class Date { private String month; private String day; private String year; public Date(String month, String day, String year) { this.month = month; this.day = day; this.year = year; } public String getMonth() { return month; } public String getDay() { return day; } public String...
Ship, CruiseShip, and CargoShip Classes (in C++ language i use visual studios to code with) design a Ship class that has the following members: - A member variable for the name of the ship (a string) - A member variable for the year that the ship was built (a string) - A contsructor and appropriate accessors and mutators - A virtual print function that displays the ship's name and the year it was built (nobody seems to get this part...
1. Please write the following program in Python 3. Also, please create a UML and write the test program. Please show all outputs. (The Fan class) Design a class named Fan to represent a fan. The class contains: Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed. A private int data field named speed that specifies the speed of the fan. A private bool data field named on that specifies...
Language is JAVA. Clarification for the Shape class (highlighted): The following requirements specify what fields you are expected to implement in your Shape class; - A private String color that specifies the color of the shape - A private boolean filled that specifies whether the shape is filled - A private date (java.util.date) field dateCreated that specifies the date the shape was created Your Shape class will provide the following constructors; - A two-arg constructor that creates a shape with...
Create the Python code for a program adhering to the following specifications. Write an Employee class that keeps data attributes for the following pieces of information: - Employee Name (a string) - Employee Number (a string) Make sure to create all the accessor, mutator, and __str__ methods for the object. Next, write a class named ProductionWorker that is a subclass of the Employee class. The ProductionWorker class should keep data attributes for the following information: - Shift number (an integer,...
Part I: Problem: Code the following in Java: Candy Class -price -number of pounds sold Create the following methods: -Two constructors – default and second - accessor and mutator methods for each of the fields. - check for invalid price. Negative price is invalid (use if then else and display error message) - a method named userInput to get user input for all the fields. - check for invalid price. Negative price is invalid (use a loop to prompt the...