You have been provided with the following class diagram:
The shipping company running the route wants to load a container for a product owned by a business tycoon.
You need to write the code for all the classes so that the main() method can provide this information.
You will need to apply your understanding of object interactions, and your solution must not result in new associations in the class diagram.
You may pass any values as arguments to the constructors/methods when creating your objects
(Hint: You may complete the entirety of the class to practise your coding).
Container.java
java
Owner.java
Product.java
Ship.java
/** * The Container class represents a shipping container * that contains an ID number, weight, owner information, * and the product stored inside. * * It provides constructors to initialize the container and * methods to access, modify, and display its details. * * @author Jimmy * @version 1.0 */ public class Container { // Fields private int no; private double weight; private Owner owner; private Product prod;
// Constructors
/** * Default constructor. * Initializes numeric values to 0 and creates new Owner and Product objects. */ public Container() { this.no = -1; this.weight = -1; this.owner = new Owner(); this.prod = new Product(); }
/** * Constructs a container with given number, weight, owner, and product. * * @param no the container number * @param weight the container weight * @param owner the owner of the container * @param prod the product stored inside the container */ public Container(int no, double weight, Owner owner, Product prod) { this.no = no; this.weight = weight; this.owner = owner; this.prod = prod; }
// Methods
/** * Displays container information including number and weight. */ public void display() { System.out.println("Container No : " + no); System.out.println("Container Weight : " + weight); }
/** * Gets the container number. * * @return the container number */ public int getNo() { return no; }
/** * Gets the owner of the container. * * @return the owner */ public Owner getOwner() { return owner; }
/** * Gets the product stored in the container. * * @return the product */ public Product getProd() { return prod; }
/** * Gets the weight of the container. * * @return the weight */ public double getWeight() { return weight; }
public void iniciateOwner(String name, double insured) { owner.setName(name); owner.setInsured(insured); owner.display(); } /** * Sets the container number. * * @param no the new container number */ public void setNo(int no) { this.no = no; }
/** * Sets the owner of the container. * * @param owner the new owner */ public void setOwner(Owner owner) { this.owner = owner; }
/** * Sets the product stored in the container. * * @param prod the new product */ public void setProd(Product prod) { this.prod = prod; }
/** * Sets the weight of the container. * * @param weight the new weight */ public void setWeight(double weight) { this.weight = weight; } }
java
/** * The Owner class represents the owner of a container. * It includes the owner's name and insured value. * * Provides constructors to initialize data, and * methods to display and access/modify the fields. * * @author Jimmy * @version 1.0 */ public class Owner { // Fields private String name; private double insured;
// Constructors
/** * Default constructor. * Initializes name as empty and insured value as 0. */ public Owner() { this.name = "Unknown"; this.insured = -1; }
/** * Constructs an owner with the specified name and insured value. * * @param name the name of the owner * @param insured the insured value */ public Owner(String name, double insured) { this.name = name; this.insured = insured; }
/** * Gets the insured value. * * @return the insured value */ public double getInsured() { return insured; }
/** * Gets the name of the owner. * * @return the name */ public String getName() { return name; }
/** * Sets the insured value. * * @param insured the new insured value */ public void setInsured(double insured) { this.insured = insured; }
/** * Sets the name of the owner. * * @param name the new name */ public void setName(String name) { this.name = name; } }
java
/** * The Product class represents a product with a name, brand, and unit price. * It provides constructors to initialize the product and methods to access, * modify, and display product details. * * @author Jimmy * @version 1.0 */ public class Product { // Fields private String name; private String brand; private double unitPrice;
// Constructors
/** * Default constructor. * Initializes name and brand to empty strings and unit price to 0.0. */ public Product() { this.name = "Unknown"; this.brand = "Unknown"; this.unitPrice = -1; }
/** * Constructs a product with the given name, brand, and unit price. * * @param name the product name * @param brand the product brand * @param unitPrice the price per unit */ public Product(String name, String brand, double unitPrice) { this.name = name; this.brand = brand; this.unitPrice = unitPrice; }
// Methods
/** * Displays the product information to the console. */ public void display() { System.out.println("Product Name : " + name); System.out.println("Product Brand : " + brand); System.out.println("Product UnitPrice: " + unitPrice); }
/** * Returns the product brand. * * @return the brand of the product */ public String getBrand() { return brand; }
/** * Returns the product name. * * @return the name of the product */ public String getName() { return name; }
/** * Returns the unit price of the product. * * @return the price per unit */ public double getUnitPrice() { return unitPrice; }
/** * Sets the product brand. * * @param brand the new brand of the product */ public void setBrand(String brand) { this.brand = brand; }
/** * Sets the product name. * * @param name the new name of the product */ public void setName(String name) { this.name = name; }
/** * Sets the unit price of the product. * * @param unitPrice the new unit price */ public void setUnitPrice(double unitPrice) { this.unitPrice = unitPrice; } }
java
/** * The Ship class represents a ship carrying a container. * It stores ship name, origin, and the container object. * Includes methods to set and display shipping information. * * Demonstrates object composition with Container, Product, and Owner. * * @author Jimmy * @version 1.0 */ public class Ship { // Fields private String name; private String origin; private Container container;
// Constructors
/** * Default constructor. * Initializes name and origin as empty strings, and * assigns a new Container. */ public Ship() { this.name = ""; this.origin = ""; this.container = new Container(); }
/** * Parameterized constructor to initialize all fields. * * @param name the name of the ship * @param origin the origin of the ship * @param container the container carried by the ship */ public Ship(String name, String origin, Container container) { this.name = name; this.origin = origin; this.container = container; }
// Methods (alphabetical order)
/** * Displays the ship's basic information. */ public void display() { System.out.println("Ship Name : " + this.name); System.out.println("Origin : " + this.origin); }
/** * Gets the container object. * * @return the container */ public Container getContainer() { return container; }
/** * Gets the ship's name. * * @return the name */ public String getName() { return name; }
/** * Gets the ship's origin. * * @return the origin */ public String getOrigin() { return origin; }
/** * Main method for testing the Ship and its components. * * @param args command-line arguments (not used) */ public static void main(String[] args) { // Create and configure ship Ship ship = new Ship(); ship.setName("FIT9131"); ship.setOrigin("CHINA");