Question

Design a Ship class that has the following class members: A field for the name of the ship...

Design a Ship class that has the following class members:


A field for the name of the ship


A field for the year the ship was built


A constructor


Appropriate accessors and mutators


toString method that displays the ships name and the year it was built


Design a CruiseShip class the inherits from the Ship class. Include the following members:

A field for the max number of passengers


A constructor


Appropriate accessors and mutators


toString method that overrides the Ship toString method. The method should display the ship name and the max number of passengers


Design a CargoShip class that inherits from the Ship class. Include the following members:

A field for cargo capacity (tons)


A constructor


Appropriate accessors and mutators


toString method that overrides the base class. The method should display ships name and cargo capacity.


Demonstrate these classes in a program that creates two objects from each class and stores them in an ArrayList of Ship objects. Use the following information:

Ship - NSU Vic , 1884


Ship - NSU Column, 1902


Cruise Ship - Java Queen 1, 2003, 450


Cruise Ship - CIS Minnow, 2010, 250


Cargo Ship - Demon 1, 1999, 7000


Cargo Ship - Demon 2, 1993, 10000


Step through the ArrayList and call the toString method for each object.


0 0
Add a comment Improve this question Transcribed image text
Answer #1


import java.util.ArrayList;

/**
*
* @author Yourname
*/
//base class
class Ship
{
//variables to store name and year
String name;
String year;
//constructor of class ship
//in this class we havent used the constructor
//variables are initialized by using setter method
public Ship()
{

}
  
//getter and setter methods
public void setName(String n)
{
name =n;
}
public String getName()
{
return name;
}
public void setYear(String y)
{
year =y;
}
public String getYear()
{
return year;
}
  
//tostring method
@Override
public String toString()
{
String detail = "Ship - "+name+", "+year+"\n";
return detail;
}
}

//cargo ship class
class CargoShip extends Ship
{
double capacity;
//in this class variables are initialized by constructor
public CargoShip(String n, String y)
{
name =n;
year = y;
}
  
//getter and setter methods
public void setCapacity(double c)
{
capacity =c;
}
public double getCapacity()
{
return capacity;
}
  
//toString method overriden
@Override
public String toString()
{
String detail = "Cargo Ship - "+name+", "+year+", "+capacity+"\n";
return detail;
}
  
}

//cruise ship class
class CruiseShip extends Ship
{
int max;
//variables initialized by using constructor
public CruiseShip(String n, String y)
{
name =n;
year=y;
}
  
//getter and setter methods
public void setMax(int m)
{
max = m;
}
public int getMax()
{
return max;
}
//override tostring method
@Override
public String toString()
{
String detail = "Cruise Ship - "+name+", "+year+", "+max+"\n";
return detail;
}
}

//driver class
public class Driver {
//main method
public static void main(String args[])
{
//make array list of type Ship class
ArrayList<Ship> al = new ArrayList<Ship>();
  
//create object of ship class and add values into it
Ship ob1 = new Ship();
ob1.setName("NSU Vic");
ob1.setYear("1884");
//insert object into array list
al.add(ob1);
  
//create second object
Ship ob2 = new Ship();
ob2.setName("NSU Column");
ob2.setYear("1902");
al.add(ob2);
  
//create object of cruise ship class and assign values into constructor
CruiseShip ob3 = new CruiseShip("Java Queen 1","2003");
ob3.setMax(450);
al.add(ob3);
  
//create second object of cruise ship class
CruiseShip ob4 = new CruiseShip("CIS Minnow","2010");
ob4.setMax(250);
al.add(ob4);
  
  
//create two objects of cargo ship class and add
//objects into array list
CargoShip ob5 = new CargoShip("Demon 1","1999");
ob5.setCapacity(7000);
al.add(ob5);
  
CargoShip ob6 = new CargoShip("Demon 2","1993");
ob6.setCapacity(10000);
al.add(ob6);
  
//print content of the array list by calling to string method
for(int i =0;i<al.size();i++)
{
System.out.println(al.get(i).toString());
}
  
}
}

========================

Add a comment
Know the answer?
Add Answer to:
Design a Ship class that has the following class members: A field for the name of the 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
  • [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...

  • Python: Design a Ship class that has the following members: A member variable for the name...

    Python: Design a Ship class that has the following members: A member variable for the name of the ship. A member variable for the year that the ship was built. An init method and appropriate accessors and mutators. A 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 member variable for the maximum number of passengers....

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

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

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

  • IN JAVA Write a class Store which includes the attributes: store name, city. Write another class...

    IN JAVA Write a class Store which includes the attributes: store name, city. Write another class encapsulating an Art Gallery, which inherits from Store. An Art Gallery has the following additional attributes: how many paintings are sold every year and the number of artists submitting artwork. Code the constructor, accessors, mutators, toString and equals method of the super class Store. Code the constructor, accessors and mutators for the subclass Art Gallery. In the Art Gallery class, also code a method...

  • Write a class Store which includes the attributes: store name, city. Write another class encapsulating an...

    Write a class Store which includes the attributes: store name, city. Write another class encapsulating an Art Gallery, which inherits from Store. An Art Gallery has the following additional attributes: how many paintings are sold every year and the number of artists submitting artwork. Code the constructor, accessors, mutators, toString and equals method of the super class Store. Code the constructor, accessors and mutators for the subclass Art Gallery. In the Art Gallery class, also code a method returning the...

  • The object class should be a Student with the following attributes: id: integer first name: String...

    The object class should be a Student with the following attributes: id: integer first name: String last name: String write the accessors, mutators, constructor, and toString(). In your main test class you will write your main method and do the following things: Create an array of Student objects with at least 5 students in your array. Write a sort method that must be your original work and included in the main class. The sort method will sort based on student...

  • Create the following program in java please Write a class Store which includes the attributes: store...

    Create the following program in java please Write a class Store which includes the attributes: store name and sales tax rate. Write another class encapsulating a Book Store, which inherits from Store. A Book Store has the following additional attributes: how many books are sold every year and the average price per book. Code the constructor, accessors, mutators, toString and equals method of the super class Store and the subclass Book Store; In the Book Store class, also code a...

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