Question

using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship...

using java write a code with notepad++

Create the following classes using inheritance and polymorphism

  1. Ship (all attributes private)
    • String attribute for the name of ship
    • float attribute for the maximum speed the of ship
    • int attribute for the year the ship was built
    • Add the following behaviors constructor(default and parameterized) , finalizer, and appropriate accessor and mutator methods
    • Override the toString() method to output in the following format if the attributes were name="Sailboat Sally", speed=35.0f and year built of 1980 be
      Ship named Sailboat Sally with max speed of 35.0 mph was built in 1980
  2. CruiseShip extending Ship(all attributes private)
    • Additional int attribute for the maximum number of passengers
    • Add the following behaviors constructor(default and parameterized) , finalizer, and appropriate accessor and mutator methods
    • Override the toString() method to output in the following format if the attributes were name="Triumph", speed=52.0f and year built of 2007 and a maximum of 5500 passengers be
      Cruise Ship named Triumph with max speed of 52.0 mph was built in 2007 carries 5500 passengers
  3. Cargo Ship extending Ship(all attributes private)
    • Additional float attribute for the maximum tons of cargo
    • Add the following behaviors constructor(default and parameterized) , finalizer, and appropriate accessor and mutator methods
    • Override the toString() method to output in the following format if the attributes were name="Valdez", speed=45.8f and year built of 1968 and a maximum of 350.3 tons be
      Cargo Ship named Valdez with max speed of 45.8 mph was built in 1968 with maximum cargo of 350.3 tons
  4. AircraftCarrier extending CruiseShip(all attributes private)
    • Additional int attribute for the maximum number of planes
    • Add the following behaviors constructor(default and parameterized) , finalizer, and appropriate accessor and mutator methods
    • Override the toString() method to output in the following format if the attributes were name="Alabama", speed=55.5f and year built of 1939 and a maximum of 2300 crewmen and 42 planes
      Aircraft Carrier named Alabama with max speed of 55.5 mph was built in 1939 carries 2300 crew members with 42 planes
  5. All derived class constructors should call the appropriate parent constructors
  6. Create two instances of each class using both the parameterized and non parameterized constructors demonstrate all the methods accessible with each instance and send each instance to the System.out text or GUI messagebox using the toString()
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:

Add a comment
Know the answer?
Add Answer to:
using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members:...

    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...

    [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...

    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...

    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...

    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...

    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...

    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...

    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...

    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...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT