Question

In: Computer Science

Design a ship class that has the following data fields: A data field for the name...

  1. Design a ship class that has the following data fields:
    • A data field for the name of the ship (a string).
    • A data 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.
  2. Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following member:
    • A field for the maximum number of passengers (an int).
    • A constructor and appropriate accessors and mutators.
    • A toString method that overrides the toString method in the base class. The CruiseShip class's toString method should display only the ships name and the maximum number of passengers.
  3. Design a CargoShip class that extends the Ship class. The CargoShip class should have the following members:
    • A field for the cargo capacity in tonnage (an int).
    • A constructor and appropriate accessors and mutators.
    • A toString method that overrides the toString method in the base class. The CargoShip class's toString method should display only the ships name and ship’s cargo capacity.
  4. Demonstrate the classes in a program that has a Ship array.  Assign various Ship, CruiseShip, and CargoShip objects to the array elements. The program should then step through the array, calling each objects toString method.

Solutions

Expert Solution

//Java code

//ship class
public class Ship {
    /**
     * A data field for the name of the ship (a string).*/
    private String shipName;
     /*
     A data field for the year that the ship was built (a String).
     */
     private String year;
    /**
     * A constructor
     */
    public Ship(String shipName, String year) {
        this.shipName = shipName;
        this.year = year;
    }

    /**
     * accessors and mutators.
     */
    public String getShipName() {
        return shipName;
    }

    public void setShipName(String shipName) {
        this.shipName = shipName;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }
    /**
     * A toString method that displays
     * the ship’s name and the year it was built.
     */
    @Override
    public String toString() {
        return "Ship's Name: "+shipName+" and the year "+ shipName+" was built: "+year;
    }
}

//==============================

/**
 * Design a CruiseShip class that extends the Ship class.
 */
public class CruiseShip extends Ship{
    /**
     * A field for the maximum number of passengers (an int).
     */
    private int maximumPassenger;

    //Constructor

    public CruiseShip(String shipName, String year, int maximumPassenger) {
        super(shipName, year);
        this.maximumPassenger = maximumPassenger;
    }

    //Accessor and mutator

    public int getMaximumPassenger() {
        return maximumPassenger;
    }

    public void setMaximumPassenger(int maximumPassenger) {
        this.maximumPassenger = maximumPassenger;
    }
    /**
     * A toString method that overrides the toString method in the base class. The CruiseShip class's toString method should
     * display only the ships name and the maximum number of passengers.
     */
    @Override
    public String toString() {
        return "Ship's Name: "+getShipName()+"\nMaximum number of passenger: "+maximumPassenger;
    }
}

//===================================================

/**
 * Design a CargoShip class that extends the Ship class
 */
public class CargoShip extends Ship {
    /**
     * A field for the cargo capacity in tonnage (an int).
     */
    private int cargoCapacity;

    //Constructor

    public CargoShip(String shipName, String year, int cargoCapacity) {
        super(shipName, year);
        this.cargoCapacity = cargoCapacity;
    }

    //Accessor and mutator


    public int getCargoCapacity() {
        return cargoCapacity;
    }

    public void setCargoCapacity(int cargoCapacity) {
        this.cargoCapacity = cargoCapacity;
    }
    /**
     * A toString method that overrides the toString method in the base class. The CargoShip class's toString method
     * should display only the ships name and ship’s cargo capacity.
     */
    @Override
    public String toString() {
        return "Ship's Name: "+getShipName()+"\nCapacity: "+cargoCapacity+" tonnes.";
    }
}

//==================================

public class ShipDriver {
    public static void main(String[] args)
    {
        //Create array of object
        Ship[] ships = new Ship[3];
        ships[0] = new Ship("Common ship","2000");
        ships[1] = new CruiseShip("Cruise Ship","2010",150);
        ships[2] = new CargoShip("Cargo ship","2015",20000);

        //Now print info
        for (Ship s:ships
             ) {
            System.out.println(s);
        }
    }
}

//Output

//If you need any help regarding this solution ......... please leave a comment ....... thanks


Related Solutions

Design a Ship class that the following members: A field for the name of the ship...
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 maximum number of passengers (an int). A constructor...
Design a class named Pet, which should have the following fields: Name – The name field...
Design a class named Pet, which should have the following fields: Name – The name field holds the name of a pet. Type – The type field holds the type of animal that is the pet. Example values are “Dog”, “Cat”, and “Bird”. Age – The age field holds the pet’s age. The Pet class should also have the following methods: setName – The setName method stores a value in the name field. setType – The setType method stores a...
Design a Ship class that has the following members: • A member variable for the name...
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 constructor and appropriate accessors and mutators • A virtual print function that displays the ship’s name and the year it was built. Design a CruiseShip class that is derived from the Ship class. The CruiseShip class should have the following members: • A...
Problem Statement Design a class named Car that has the following fields: year: The year field...
Problem Statement Design a class named Car that has the following fields: year: The year field is an integer that holds the car’s year. make: the make field is a String that holds the make of the car. speed: the speed field is an integer that holds the car’s current speed. In addition, the class should have the following constructor and other methods: Constructor: the constructor should accept the car’s year >model and make as parameters. These values should be...
Java... Design a Payroll class with the following fields: • name: a String containing the employee's...
Java... Design a Payroll class with the following fields: • name: a String containing the employee's name • idNumber: an int representing the employee's ID number • rate: a double containing the employee's hourly pay rate • hours: an int representing the number of hours this employee has worked The class should also have the following methods: • Constructor: takes the employee's name and ID number as arguments • Accessors: allow access to all of the fields of the Payroll...
1.   Design a class called BankAccount. The member fields of the class are: Account Name, Account...
1.   Design a class called BankAccount. The member fields of the class are: Account Name, Account Number and Account Balance. There are also other variables called MIN_BALANCE=9.99, REWARDS_AMOUNT=1000.00, REWARDS_RATE=0.04. They look like constants, but for now, they are variables of type double Here is the UML for the class:                                                         BankAccount -string accountName // First and Last name of Account holder -int accountNumber // integer -double accountBalance // current balance amount + BankAccount()                     //default constructor that sets name to “”,...
Design a class named Student that contains the followingprivate instance variables: A string data field name...
Design a class named Student that contains the followingprivate instance variables: A string data field name for the student name. An integer data field id for the student id. A double data field GPA for the student GPA. An integer data field registeredCredits. It contains the following methods: An empty default constructor. A constructor that creates a student record with a specified id and name. The get and set methods for id, name, GPA, and registeredCredits. A method called registerCredit...
In java, design a class named Motorcycle that has the following fields: • year – The...
In java, design a class named Motorcycle that has the following fields: • year – The year field is an int that holds the motorcycle’s year • make – The make field references a String object that holds the make of the motorcycle. • speed – The speed field is an int that holds the motorcycle’s current speed The class should have the following constructor and other methods: • Constructor – the constructor should accept the motorcycle’s year and make...
Write a Data Element Class named Property that has fields tohold the property name, the...
Write a Data Element Class named Property that has fields to hold the property name, the city where the property is located, the rent amount, the owner's name, and the Plot to be occupied by the property, along with getters and setters to access and set these fields. Write a parameterized constructor (i.e., takes values for the fields as parameters) and a copy constructor (takes a Property object as the parameter). Follow the Javadoc file provided.Write a Data Element Class...
Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT