Question

[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 (an int) o 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) o 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

  • Design an interface called Militarizable with a method called fireWeapons

  • Design a Destroyer class that extends the Ship class and implements the Militarizable interface (show “Weapons Fired!” message when fireWeapons is called). The class should have the following members:
    • A field for the number of rockets available (an int) o A field for the number of weapon platforms (an int) o A constructor and appropriate accessors and mutators
    • A toString method that overrides the toString method in the base class. The Destroyer class's toString method should display its current state

  • Demonstrate the classes in a program that has a Ship array. Assign various Ship, CruiseShip, CargoShip, and Destroyer objects to the array elements. The program should then step through the array, calling each object's toString method.

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

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// Ship.java

public class Ship {
   // Declaring variables
   private String name;
   private int built_year;

   // Parameterized constructor
   public Ship(String name, int built_year) {
       this.name = name;
       this.built_year = built_year;

   }

   // Setters and Getters.
   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public int getBuilt_year() {
       return built_year;
   }

   public void setBuilt_year(int built_year) {
       this.built_year = built_year;
   }

   // toString() method which displays the contents of the object inside it
   @Override
   public String toString() {
       return "\nShip #\nName=" + name + ",\nBuild Year=" + built_year;
   }

}

______________________

// CargoShip.java

public class CargoShip extends Ship {
   //Declaring variables
       private int capacity_tonnage;
      
       //Parameterized constructor
       public CargoShip(String name, int built_year,int capacity) {
           super(name, built_year);
           this.capacity_tonnage=capacity;
          
       }
      
       //Setters and Getters.
       public int getCapacity_tonnage() {
           return capacity_tonnage;
       }
       public void setCapacity_tonnage(int capacity_tonnage) {
           this.capacity_tonnage = capacity_tonnage;
       }
      
       //toString() method which displays the contents of the object inside it
       @Override
       public String toString() {
          
           return "\nThis is CargoShip#\n"+super.getName()+"\nCapacity Tonnage=" + capacity_tonnage;
       }
      

}
_____________________

// CruiseShip.java

public class CruiseShip extends Ship {
     
       //Declaring variables
       private int capacity;
      
       //Parameterized constructor
       public CruiseShip(String name, int built_year,int capacity) {
           super(name, built_year);
           this.capacity=capacity;
       }
      
       //Setters and Getters.
       public int getCapacity() {
           return capacity;
       }
       public void setCapacity(int capacity) {
           this.capacity = capacity;
       }
      
       //toString() method which displays the contents of the object inside it
       @Override
       public String toString() {
           return "\nThis is CruiseShip#\nName="+super.getName()+"\nCapacity=" + capacity;
       }
}
_____________________

// Militarizable.java

public interface Militarizable {
   public void fireWeapons();
}
____________________

// Destroyer.java

public class Destroyer extends Ship implements Militarizable {

   private int noOfRockets;
   private int noOfWeaponPlatforms;

   public Destroyer(String name, int built_year, int noOfRockets,
           int noOfWeaponPlatforms) {
       super(name, built_year);
       this.noOfRockets = noOfRockets;
       this.noOfWeaponPlatforms = noOfWeaponPlatforms;
   }

   @Override
   public void fireWeapons() {
       System.out.println("Weapons Fired!");

   }

   @Override
   public String toString() {
       return "\nThis is Destroyer# [NoOfRockets=" + noOfRockets
               + ", noOfWeaponPlatforms=" + noOfWeaponPlatforms + "]"+super.toString();
   }

}
______________________

// Test.java

public class Test {

   public static void main(String[] args) {
       Ship shipArray[] = new Ship[4];
       shipArray[0] = new Ship("The Ship", 2009);
       shipArray[1] = new CargoShip("The CargoShip", 2000, 5000);
       shipArray[2] = new CruiseShip("The CruiseShip", 1999, 2000);
       shipArray[3] = new Destroyer("The BattleShip", 2009, 20, 4);
       for (int i = 0; i < shipArray.length; i++) {
           System.out.println(shipArray[i]);
       }

   }

}
___________________

Output:


Ship #
Name=The Ship,
Build Year=2009

This is CargoShip#
The CargoShip
Capacity Tonnage=5000

This is CruiseShip#
Name=The CruiseShip
Capacity=2000

This is Destroyer# [NoOfRockets=20, noOfWeaponPlatforms=4]
Ship #
Name=The BattleShip,
Build Year=2009


_______________Could you plz rate me well.Thank You

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

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

  • Help needed on C++ program: Program Description: Complete the Ship, CruiseShip, and CargoShip program (#12 in...

    Help needed on C++ program: Program Description: Complete the Ship, CruiseShip, and CargoShip program (#12 in the 9th edition of the text). Read the specific method requirements in the text. Specific Requirements: • Create the Ship class. • Create CruiseShip and CargoShip classes that are derived from Ship. • Create a small tester cpp file that has an array of Ship pointers (one each of Ship, CruiseShip, and CargoShip). The program steps through the array, calling each object’s print method....

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

  • Create a class House with the private fields: numberOfUnits of the type int, yearBuilt of the...

    Create a class House with the private fields: numberOfUnits of the type int, yearBuilt of the type int, assessedPrice of the type double. A constructor for this class should have three arguments for initializing these fields. Add accessors and mutators for all fields except a mutator for yearBuilt, and the method toString. Create a class StreetHouse which extends House and has additional fields: streetNumber of the type int, homeowner of the type String, and two neighbors: leftNeighbor and rightNeighbor, both...

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

  • python Design a class named Car that has the following fields: yearModel: The yearModel field is...

    python Design a class named Car that has the following fields: yearModel: The yearModel field is an Integer that holds the car’s year model. make: The make field references 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 arguments. These values should be...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

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

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