Insy 4305 Lab 3 Instructionsship Cruiseship Cargoship Classes Anddi ✓ Solved
INSY 4305 Lab 3 Instructions Ship, CruiseShip, CargoShip Classes, and Displayable Interface Design an abstract 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 accessor and mutators. · A toString method that displays the ships 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, a copy 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, a copy constructor, and appropriate accessors and mutators. · 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. In a driver program (shipDemo.java) · Demonstrate the classes in a Ship array.
Assign various CruiseShip and CargoShip objects to the array elements. The program should then step through the array, calling each object's display method. (polymorphism) · Create an ArrayList of Ship objects and add various CruiseShip and CargoShip objects to the array list. The program should then step through the array list, calling each object's toString method. (polymorphism) · Test your copy constructors in both CruiseShip and CargoShip in shipDemo.java Interface to your lab 3 (Ship implements Displayable). public interface Displayable { void display(); } Resource: · UML · Sample Program (OOP_example_2) Deliverables: · Displayable.java (10%) · Ship.java (20 %) · CargoShip.java (15%) · CruiseShip.java (15%) · ShipDemo.java (30%) · Screenshot (10%) Note: Sparingly comment your java source code, save all the files in your_lastname_lab_3 folder, zip it, and upload for grading.
Thank you! image1.tiff MBA 699 Employee Survey Response Key The organization has collected employee data since its inception in 1999. The following information is available: 1. Status • Current • Former 2. Age ___________________ 3. Business Travel • Nontravel • Travel Frequently • Travel Rarely 4.
Department • Human Resources • Research & Development • Sales 5. Education • High School or Equivalent • Associate Degree • Undergraduate Degree • Graduate Degree • Doctorate 6. Education Field • Life Sciences • Medical • Technical Degree • Marketing • Other 7. Employee Number ______________ 8. Gender • Male • Female 9.
Job Role • Healthcare Representative • Human Resources • Laboratory Technician • Manager • Manufacturing Director • Research Director • Research Scientist • Sales Executive • Sales Representative 10. Job Satisfaction • Unsatisfied • Somewhat Satisfied • Satisfied • Very Satisfied 11. Marital Status • Single • Married • Divorced 12. Monthly Income ______________ 13. Number of Companies Worked ______________ 14.
Percent Salary Increase Last Year ______________ (11% to 25%) 15. Performance Rating • 1 (low) • 2 • 3 • 4 (high) 16. Total Working Years ______________ 17. Training Times Last Year • 0 • 1 • 2 • 3 • 4 • 5 • . Work-Life Balance • 1 (low) • 2 • 3 • 4 (high) 19.
Years at Company ______________ [0-22] 20. Years in Current Role ______________ [0-18] 21. Years Since Last Promotion ______________ [0-15]
Paper for above instructions
Ship, CruiseShip, CargoShip Classes, and Displayable Interface - Assignment Solution
1. Abstract Class: Ship
```java
// Ship.java
public abstract class Ship implements Displayable {
private String name;
private String yearBuilt;
public Ship(String name, String yearBuilt) {
this.name = name;
this.yearBuilt = yearBuilt;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getYearBuilt() {
return yearBuilt;
}
public void setYearBuilt(String yearBuilt) {
this.yearBuilt = yearBuilt;
}
@Override
public String toString() {
return "Ship Name: " + name + ", Year Built: " + yearBuilt;
}
@Override
public void display() {
System.out.println(this.toString());
}
}
```
2. Class: CruiseShip
```java
// CruiseShip.java
public class CruiseShip extends Ship {
private int maxPassengers;
public CruiseShip(String name, String yearBuilt, int maxPassengers) {
super(name, yearBuilt);
this.maxPassengers = maxPassengers;
}
public CruiseShip(CruiseShip cruiseShip) {
super(cruiseShip.getName(), cruiseShip.getYearBuilt());
this.maxPassengers = cruiseShip.maxPassengers;
}
public int getMaxPassengers() {
return maxPassengers;
}
public void setMaxPassengers(int maxPassengers) {
this.maxPassengers = maxPassengers;
}
@Override
public String toString() {
return "Cruise Ship Name: " + getName() + ", Max Passengers: " + maxPassengers;
}
}
```
3. Class: CargoShip
```java
// CargoShip.java
public class CargoShip extends Ship {
private int cargoCapacity;
public CargoShip(String name, String yearBuilt, int cargoCapacity) {
super(name, yearBuilt);
this.cargoCapacity = cargoCapacity;
}
public CargoShip(CargoShip cargoShip) {
super(cargoShip.getName(), cargoShip.getYearBuilt());
this.cargoCapacity = cargoShip.cargoCapacity;
}
public int getCargoCapacity() {
return cargoCapacity;
}
public void setCargoCapacity(int cargoCapacity) {
this.cargoCapacity = cargoCapacity;
}
@Override
public String toString() {
return "Cargo Ship Name: " + getName() + ", Cargo Capacity: " + cargoCapacity + " tons";
}
}
```
4. Interface: Displayable
```java
// Displayable.java
public interface Displayable {
void display();
}
```
5. Driver Program: ShipDemo
```java
import java.util.ArrayList;
public class ShipDemo {
public static void main(String[] args) {
Ship[] shipsArray = new Ship[4];
shipsArray[0] = new CruiseShip("Carnival Glory", "2003", 3000);
shipsArray[1] = new CargoShip("Maersk Triple E", "2013", 180000);
shipsArray[2] = new CruiseShip("Royal Caribbean", "2001", 4000);
shipsArray[3] = new CargoShip("Ever Given", "2018", 220000);
// Displaying ships using array
for (Ship ship : shipsArray) {
ship.display();
}
// Using ArrayList
ArrayList
shipsList.add(new CruiseShip("Norwegian Bliss", "2018", 4000));
shipsList.add(new CargoShip("MSC Oscar", "2015", 197000));
// Displaying ships using ArrayList
for (Ship ship : shipsList) {
System.out.println(ship.toString());
}
// Testing copy constructors
CruiseShip copyCruise = new CruiseShip((CruiseShip) shipsArray[0]);
CargoShip copyCargo = new CargoShip((CargoShip) shipsArray[1]);
System.out.println("Copy of Cruise Ship: " + copyCruise.toString());
System.out.println("Copy of Cargo Ship: " + copyCargo.toString());
}
}
```
Conclusion
The implementation of Ship, CruiseShip, and CargoShip classes, along with a Displayable interface, is designed to demonstrate the principles of Object-Oriented Programming, including abstraction, inheritance, encapsulation, and polymorphism. The use of an abstract class for the Ship allows for shared properties and methods, while specific classes like CruiseShip and CargoShip extend its functionality. The driver program illustrates how these concepts work in practice, providing clear output for each ship type and demonstrating the effectiveness of polymorphism with both an array and an ArrayList.
References
1. Deitel, P. J., & Deitel, H. M. (2019). Java: How to Program (11th ed.). Pearson.
2. Eckel, B. (2006). Thinking in Java (4th ed.). Prentice Hall.
3. Bloch, J. (2018). Effective Java (3rd ed.). Addison-Wesley.
4. Myers, G. J. (2011). The Art of Software Testing (3rd ed.). Wiley.
5. Schildt, H. (2018). Java: The Complete Reference (11th ed.). Oracle Press.
6. Horstmann, C. S. (2019). Core Java Volume I: Fundamentals (11th ed.). Pearson.
7. Freeman, S., & Robson, E. (2017). Head First Java (2nd ed.). O'Reilly Media.
8. Bloch, J., & Gafter, N. (2008). Java Puzzlers: Traps, Pitfalls, and Corner Cases. Addison-Wesley.
9. Liang, Y. D. (2016). Introduction to Java Programming (11th ed.). Pearson.
10. Arnold, K., Gosling, J., & Holmes, D. (2005). The Java Programming Language (4th ed.). Addison-Wesley.