Finished Q3, alot of ships

This commit is contained in:
2025-04-15 22:14:32 -05:00
parent e9c9549b5a
commit 6fccf0cc20
5 changed files with 145 additions and 2 deletions

View File

@@ -1,17 +1,30 @@
// Import the BankAccount package
import BankAccount.*;
import Ship.*;
public class Main {
public static void main(String[] args) {
// For testing q1
/*
// Testing the rough version of SavingsAccount
SavingsAccount SA = new SavingsAccount(30, 2);
System.out.println(SA);
SA.withdraw(6);
System.out.println(SA);
SA.withdraw(5);
System.out.println(SA);
*/
// For testing q3
Ship[] ships = new Ship[3];
ships[0] = new Ship("Basic ship", 1970);
ships[1] = new CruiseShip("Caribbean Liner", 2001, 20000);
ships[2] = new CargoShip("USS CarrysALot", 2012, 5000000);
for(Ship ship : ships) {
System.out.println(ship);
}
}
}

35
src/Ship/CargoShip.java Normal file
View File

@@ -0,0 +1,35 @@
package Ship;
public class CargoShip extends Ship {
int maxTonnage; // Max cargo capacity of the ship
public CargoShip(String name, int year, int maxTonnage) {
super(name, year);
this.maxTonnage = maxTonnage;
}
// Setter and Getter
/**
* Mutator for max tonnage
* @param maxTonnage Maximum cargo of the ship
*/
void setMaxTonnage(int maxTonnage) {
this.maxTonnage = maxTonnage;
}
/**
* Accessor of max tonnage
* @return Maximum cargo of the ship
*/
int getMaxTonnage() {
return maxTonnage;
}
@Override
public String toString() {
return "Cargo Ship " + name + ". Max Tonnage: " + maxTonnage;
}
}

38
src/Ship/CruiseShip.java Normal file
View File

@@ -0,0 +1,38 @@
package Ship;
/**
* Cruise ship class extends Ship class, and it contains an integer value for the maximum amount of passengers
* on the ship
*/
public class CruiseShip extends Ship {
int maxPassengers; // Max number of passengers on ship
public CruiseShip(String name, int year, int maxPassengers) {
super(name, year);
this.maxPassengers = maxPassengers;
}
// Setter and Getter
/**
* maxPassenger Mutator
* @param maxPassengers New value for max passengers
*/
public void setMaxPassengers(int maxPassengers) {
this.maxPassengers = maxPassengers;
}
/**
* maxPassenger getter
* @return Max passengers of the Cruise Ship
*/
public int getMaxPassengers() {
return maxPassengers;
}
@Override
public String toString() {
return "Cruise Ship " + name + ". Max passengers: " + maxPassengers;
}
}

54
src/Ship/Ship.java Normal file
View File

@@ -0,0 +1,54 @@
package Ship;
public class Ship {
String name; // Name of the ship
int year; // Year the ship was built
public Ship(String name, int year) {
this.name = name;
this.year = year;
}
// Setters and Getters
/**
* Ship name mutator
* @param name New name of the ship
*/
public void setName(String name) {
this.name = name;
}
/**
* Ship year setter
* @param year New year of ship construction
*/
public void setYear(int year) {
this.year = year;
}
/**
* Ship name accessor
* @return Name of the ship
*/
public String getName() {
return name;
}
/**
* Ship year accessor
* @return Year the ship was built
*/
public int getYear() {
return year;
}
/**
* String representation of ship object includes name and year built
* @return String representation of ship object
*/
public String toString() {
return "Ship " + name + ", built " + year;
}
}

3
src/TestScores.java Normal file
View File

@@ -0,0 +1,3 @@
public class TestScores {
}