Question

In: Computer Science

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 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 ship’s name and the maximum number of passengers. 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 ship’s name and the ship’s cargo capacity.

I need user input aka asking the user to input a ship name, the number of passengers, and the year.

Solutions

Expert Solution

Hi,

Please find the answer below:
-------------------------------

Java Classes :

Ship.java

public class Ship {
   protected String shipName;
   protected String year;

   // Ship Constructor
   public Ship(String ship, String year) {
       this.shipName = ship;
       this.year = year;
   }

   // Ship get methods
   public String getShipName()   {
       return shipName;
   }

   public String getYear(){
       return year;
   }

   // Override toString method
   public String toString(){
       return "Ship Name : " + getShipName() +
               "\nBuilt Year : " + getYear();
   }
}

////////////////////

CruiseShip.java

public class CruiseShip extends Ship {
   private int maxPassengers;

   //CruiseShip Constructor
   public CruiseShip(String ship,String year, int maxPassengers){
       super(ship, year);
       this.maxPassengers = maxPassengers;
   }

   //get method
   public int getMaxPassengers(){
       return maxPassengers;
   }

   //overridden toString
   public String toString(){
       return "CruiseShip Name : " + getShipName() +
               "\nMaximum Number of Passengers : " + getMaxPassengers();
   }
}

//////////////////

CargoShip.java

public class CargoShip extends Ship {
   private int cargoCapacityTons;
  
   //CargoShip Constructor
public CargoShip(String name, String year, int tons) {
   super(name, year);
   this.cargoCapacityTons = tons;
}

//cargo get method
public int getCargoCapacityTons() {
   return cargoCapacityTons;
}

//Overridden toString method
public String toString() {
   return "CargoShip Name : " + getShipName() +
           "\nShip Cargo Capacity : " + getCargoCapacityTons();
   }
}

////////////////////

ShipMain.java

import java.util.Scanner;

public class ShipMain {
   public static void main(String[] args) {
       String name;
       int maxNoPass;
       String year;
       try {
           //Take Input from user
           Scanner console = new Scanner(System.in);
           System.out.println("Enter the name of the ship: ");
           name=console.nextLine();
           System.out.println("Enter the number of passengers: ");
           maxNoPass=Integer.parseInt(console.nextLine());
           System.out.println("Enter the year the ship was built: ");
           year=console.nextLine();
           //Build a ship object
           CruiseShip cruiseShipObj= new CruiseShip(name,year,maxNoPass);
           System.out.println(cruiseShipObj.toString());
       }catch(Exception e) {
           e.printStackTrace();
       }
   }
}

///////////////////////////////


Sample Program Output:

Enter the name of the ship:
Pacific Liner
Enter the number of passengers:
1000
Enter the year the ship was built:
1999
CruiseShip Name : Pacific Liner
Maximum Number of Passengers : 1000


Screenshot:

-------------------------------


Hope this helps.
Let me know if you need more help with this.
Kindly, like the solution if you find it useful
Thanks.


Related Solutions

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...
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...
In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance:...
In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance: balance held in the account interestRate: annual interest rate. accountID: unique 3 digit account number assigned to each BankAccount object. Use a static data member to generate this unique account number for each BankAccount count: A static data member to track the count of the number of BankAccount objects created. Member Functions void withdraw(double amount): function which withdraws an amount from accountBalance void deposit(double...
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...
Create a class named Ship with a field that stores a collection of Shippable things and...
Create a class named Ship with a field that stores a collection of Shippable things and another that stores a maximum weight capacity. Include a constructor with a parameter for the max weight, and that gives the Ship an empty collection of Shippables. (Javascript)
Create a class named Ship with a field that stores a collection of Shippable things and...
Create a class named Ship with a field that stores a collection of Shippable things and another that stores a maximum weight capacity. Include a constructor with a parameter for the max weight, and that gives the Ship an empty collection of Shippables. (Javascript)
This question demonstrates the use of inheritance and polymorphism. Design a Ship class that has the...
This question demonstrates the use of inheritance and polymorphism. Design a Ship class that has 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 overrides the toString method in the Object class. The Ship class toString method should display the ship’s name and the year it was built. Design...
Program Specification Design an inventory class that stores the following members: serialNum: An integer that holds...
Program Specification Design an inventory class that stores the following members: serialNum: An integer that holds a part's serial number. manufactDate: A member that holds the date the part was manufactured. lotNum: An integer that holds the part's lot number. The class should have appropriate setter and getter functions. Next, design a stack class that can hold objects of the class described above. If you wish, you may use the linked list from program 5 as a basis for designing...
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT