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 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.
Implement programming challenge 10. Demonstrate the classes in a separate ShipDemo.java that:
1. Create an inventory of ships owned by a company from a data file.
2. Print out the ship inventory.
3. Compute and print out the total number of people the company can evacuate in a disaster with all of its appropriate ships and the total tonnage of personal belongings that the company can carry for the evacuees.
4. Must demonstrate the use of inheritance and polymorphism: define an array of Ship to track the inventory.
5. The totals in the output are to be calculated in main method of ShipDemo.
6. Each of the detail line about the ship is returned from toString. ie. for cargo, String.format “%-20s Cargo:%d”
7. The Default data file name is myShips.txt. If a file is specified in the first command line, then that name will be used. The format of the data file:
Company name:String
# of ship:int
type:char Ship name:String yearbuilt:String param:int
…
Example
All American Shipping Company
2
c Liberty 2010 100
C Challenger 2001 20000
For the name:String, the underscore character is used for space and must be converted before use. The type is case sensitive can be ‘c’ for Cruiseship and ‘C’ for CargoShip. Exception handling of file not found is mandatory, data validation, optional.
Required Output:
blank line
Welcome to Ship by your-name
blank line
Ship name Type
-------------------- ---------------
Liberty Cruise:100
American Challenger Cargo:1000
…
Total Ships = #
Total Passengers = #
Total Tonnage = #
Reading in the inventory should be in the main method of this class.
This class maintains the inventory of ship
main:
String companyName;
Ship[] shipInventory;
Open myShips.txt
Read in the company name
Read in the number of ships
Loop the number of ships
Read each ship information and add to the inventory
Print the report
The instanceof operator can be used for this:
Ship[] myShips = { new CruiseShip(…), new CargoShip(…), … };
for (Ship ship : myShips) {
if (ship[i] instanceof CruiseShip) {
// ship[i] reference a CruiseShip
}
}
You will need this to compute the total passengers and total tonnage
//Ship.java
public class Ship {
private String name;
private int year;
public Ship(String name, int year)
{
this.name = name;
this.year = year;
}
public void setName(String name)
{
this.name = name;
}
public void setYear(int year)
{
this.year = year;
}
public String getName()
{
return name;
}
public int getYear()
{
return year;
}
public String toString()
{
return String.format("%-20s : %d",name,year);
}
}
//end of Ship.java
//CruiseShip.java
public class CruiseShip extends Ship{
private int num_passengers;
public CruiseShip(String name, int year, int num_passengers) {
super(name, year);
this.num_passengers = num_passengers;
}
public void setPassengers(int num_passengers)
{
this.num_passengers = num_passengers;
}
public int getPassengers()
{
return num_passengers;
}
public String toString()
{
return String.format("%-20s : %d",getName()+" Cruise",num_passengers);
}
}
//end of CruiseShip.java
//CargoShip.java
public class CargoShip extends Ship{
private int tonnage;
public CargoShip(String name, int year, int tonnage) {
super(name, year);
this.tonnage = tonnage;
}
public void setTonnage(int tonnage)
{
this.tonnage = tonnage;
}
public int getTonnage()
{
return tonnage;
}
public String toString()
{
return String.format("%-20s : %d",getName()+" Cargo",tonnage);
}
}
//end of CargoShip.java
//ShipDemo.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ShipDemo {
public static void main(String[] args) {
String companyName;
int num_ships, total_passengers=0, total_tonnage=0;
Ship[] shipInventory;
try {
if(args.length == 1)
{
File file = new File(args[0]);
Scanner fileScan = new Scanner(file);
companyName = fileScan.nextLine();
num_ships = fileScan.nextInt();
shipInventory = new Ship[num_ships];
int n=0;
String type,name;
int value,year;
System.out.println("\nWelcome to ship by your-name");
System.out.printf("\n %-20s","Ship name Type");
System.out.println("\n-------------------------------");
while(fileScan.hasNextLine() && n<num_ships)
{
type = fileScan.next();
name = fileScan.next();
year = fileScan.nextInt();
value = fileScan.nextInt();
if(type.equals("c"))
{
shipInventory[n] = new CruiseShip(name.replaceAll("_", " "),year,value);
}else if(type.equals("C"))
{
shipInventory[n] = new CargoShip(name.replaceAll("_", " "),year,value);
}
n++;
}
for (Ship ship : shipInventory)
{
if (ship instanceof CruiseShip)
{
total_passengers += ((CruiseShip) ship).getPassengers();
System.out.println(((CruiseShip) ship));
}else
{
total_tonnage += ((CargoShip) ship).getTonnage();
System.out.println(((CargoShip) ship));
}
}
System.out.println("\nTotal Ships : "+shipInventory.length);
System.out.println("Total Passengers : "+total_passengers);
System.out.println("Total Tonnage : "+total_tonnage);
}else
System.out.println(" Pass the filename from command line argument");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//end of ShipDemo.java
Output:
Program Challenge 10 Design a Ship class that the following members: · A field for the...
[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...
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 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 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....
I keep getting the compilation error: pass the filename from command line arguement... Can you please tell me what im doing wrong? here is the code: import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ShipDemo { public static void main(String[] args) { String companyName; int num_ships, total_passengers=0, total_tonnage=0; Ship[] shipInventory; try { if(args.length == 1) { File file = new File(args[0]); Scanner fileScan = new Scanner(file); companyName = fileScan.nextLine(); num_ships = fileScan.nextInt(); shipInventory = new Ship[num_ships]; int n=0; String type,name;...
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 encapsulating a restaurant,which inherits from Store. A restaurant has the following additional attributes: how many people are served every year and the average price per person. code the constructor, accessors, mutators, toString and equals method of the new subclass; also code a method returning the average taxes per year. You also need to include a client class to test your code for both the parent class and the subclass. Code for Store below(Super class aka...
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...
Java Problem 2: Employee (10 points) (Software Design) Create an abstract class that represents an Employee and contains abstract method payment. Create concrete classes: SalaryEmployee, CommissionEmployee, HourlyEmployee which extend the Employee class and implements that abstract method. A Salary Employee has a salary, a Commision Employee has a commission rate and total sales, and Hourly Employee as an hourly rate and hours worked Software Architecture: The Employee class is the abstract super class and must be instantiated by one of...
Hello I need help completing this java task publicclassCruise { // Class Variables privateString cruiseName; privateString cruiseShipName; privateString departurePort; privateString destination; privateString returnPort; // Constructor - default Cruise() { } // Constructor - full Cruise(String tCruiseName, String tShipName, String tDeparture, String tDestination, String tReturn) { cruiseName = tCruiseName; cruiseShipName = tShipName; departurePort = tDeparture; destination = tDestination; returnPort = tReturn; } // Accessors publicString getCruiseName() { returncruiseName; } publicString getCruiseShipName() { returncruiseShipName; } publicString getDeparturePort() { returndeparturePort; } publicString getDestination()...