Question: on another. code, each container will be given a CargoStrength value, ranging from FRAGILE, to MODERATE, to STURDY. The rules for these CargoSt rength types







on another. code, each container will be given a CargoStrength value, ranging from FRAGILE, to MODERATE, to STURDY. The rules for these CargoSt rength types are described as follows: 1. FRAGLIE containers can only support other FRAGILE containers. 2. MODERATE containers can support other MODERATE containers, as well as FRAGILE containers. 3. STURDY containers can support all other containers. type of cargo). Lastly, the program should print a neatly formatted tabular representation of the ship in it's current state after each user operation. Required Classes UML Diagram 1. CargoStrength Enum Write a simple Enum named Cargostrength, which lists the strength values a cargo container may have. If you are unfamiliar with Java Enum types, you can read about them in this Java tutorial. Java Enums are very similar to Java classes, though they are typically used to encapsulate a set of related constants. In this case, we use it to represent the strength of the cargo, which can be either FRAGILE, MODERATE, or STURDY. The rules for the Cargostrength type is described as follows: 1. FRAGLIE containers can only support other FRAGILE containers. 2. MODERATE containers can support other MODERATE Containers, as well as FRAGILE containers. 3. STURDY containers can support all other containers. 4. The number of containers above any given containers does not matter. The only restrictions are the three rules above (Though this is a naive assumption, the simplification will make the assignment easier). 2. Cargo Class Write a fully documented class named cargo. This class represents a container which holds the 'cargo' the program will place on the ship. It contains the following private member variables: name (Str ing), weight (double) and strength (Cargost rength). The Cargo class itself is actually immutable. This means onoe it has been constructed, no data within can he changed. From the UML diagram, note that the public constructor takes all the member variables as arguments. Data can still be read via the getter methods. - private string name - private double weight - private Cargostrength strength - public Cargo(String initNane, double initweight, Cargostrength initstrength) - Brief: - Default Constructor. - Parameters: - initName - Non-null name for the cargo item. - initweight - The weight for the cargo. The weight should be greater than 0 , so an exception should be thrown if initweight (We do not deal with massless cargo). - initstrength - Either FRAgILE, MODERATE, or STURDY. - Preconditions: - initNatre is not null. - initweight >0. 3. CargoStack class (or java.util.Stack) Sinoe Cargo objects can be physically stacked on top of onc another, we can store our Cargo objects within a Stack ADT. In this assignment, you can write your own Stack elass or you may use the standard Stack class that is in the java. ut il package. Go to the HEL.P section and vicw the online documentation for Stack from the Java API. CAUTION: Although the Stack class has push and pop methods, the Stack class is a subclass of Vector. Therefore, all of the methods of vector are also accessible in the Stack class. However, if you use any of the inherited Vector methods in your solution, you will be penalized in this assignment, since some of the Vector methods are not supposed to apply to a Stack ADT in general. (That is, the designers of Java basically define that a Stack is a special type of Vector, but it really isn't.) 4. Cargoship class Write a fully documented class named Cargoship. This class represents a container ship which holds stacks of containers. It must contain the following private member variables: stacks (Cargostack []\}, maxHeight (int), and maxweight (double). The Cargoship class must also feature the following public methods: void pushCargo(Cargo cargo, int stack), Cargo popCargo(int stack), and void findAndPrint (String name). - private Cargostack[] (or java.util.stack[]) stacks - Array of container stacks on the ship. The array should be initialized in the constructor to the size indicated by the parameter numstacks. - private int maxHeight - private double maxWeight - public CargoShip(int numstacks, initMaxHeight, initMaxweight) - Brief: - Default Constructor. - Parameters: - num5tacks - The number of stacks this ship can support. This parameter should be used to initialize the stacks array to a fixed size. - init MaxHeight - The maximum height of any stack on this ship. - initMaxweight - The maximum weight for all the cargo on the ship. - Precondiions: - numstacks >0. - initMaxHeight >0. - initMaxweight >0. - Postcondinions: - This object has been initialized to a Cargoship object with the indicated number of stacks, maxHeight, and maxweight. - Throws: - Illegalargumentexception o Throws: - IllegalargumentException - if either of the parameters are now within the appropriate bounds. - public void pushCargo(Cargo cargo, int stack) throws FullstackException, Ship0verweightException, CargostrengthException - Brief: - Pushes a cargo container to the indicated stack on the cargo ship Parameters: - cargo - The container to place on the stack. - stack - The index of the stack on the ship to push cargo onto. - Note: you may choose to do your actual indexing starting at 0 , but from the user's perspective, the leftmost stack must be number 1 . - Preconditions: - This Cargoship is initialized and not null. - cargo is initialized and not null. - 1 stack stacks. length. - The size of the stack at the desired index is less than maxHeight. - The total weight of all Cargo on the ship and cargo.getweight () is less than max'eight. - Posiconditions. - Throws. - Illegalargumentexception - If cargo is null or stack is not in the appropriate bounds. - FullstackException - If the stack being pushed to is at the max height. - Shipoverweightexception - If cargo would make the ship exceed maxielght and sink. (imaginary bonus points for "yellow subroarine" reticaces in the exeeption message). - Cargostrengthexception - If the cargo would be stacked on top of a weaker cargo (see the Cargostrength rules above for reference). - public Cargo popCargo(int stack) throws EmptyStackException - Brief: - Pops a cargo from one of the specified stack. - Parameters: - stack - The index of the stack to remove the cargo from. - Note: Again. from the user's perspective, the leftmost stack must be indexed 1. - Preconditiors: - This Cargoship is initialized and not null. - 1 stack stacks. length. o Throws: - IllegalA rgumentException - If stack is not in the appropriate bounds. - EmptyStackException - If the stack being popped from is empty. - public void findAndPrint(String name) - Brief: - Parameters: - name:The name of the cargo to find and print. - Preconditions: - This Cargoship is initialized and not null. - Posiconditions: - The details of the cargo with the given name have been found and printed, or the user has been notified that no such cargo has been found. - The state of the cargo ship should remain unchanged. 5. Shiploader class be provided: - public static void main(String[] args) - Brief: required information is then requested from the user based on the selected operation. Following is the list of menu options and their required information: A sample menu is provided below indicating the functionality your shiploader program should support: C) Create new cargo enames sweights sstrengths L) Load cargo from dock U) Unload cargo from ship esrestackIndex> M) Move cargo between stacks > K) Clear dock P) Print ship stacks S) Search for cargo enames Q) Quit as necessary, as long as the Cargostrength rules are not violated. Please see the sample IO for an example. - Any time cargo is created or moved between stacks, the program should automatically print the ship stacks immediately after performing the reqested operation. - For option S), the program should only search the stacks on the ship (the dock should not be included in the search). 6. Exception classes 1. FullstackException - Should he thrown if the user attempts to push a Cargo object onto a stack which is currently at the maximum height. 2. EmptystackException - Should be thrown if the user attempts to pop a Cargostack that currently has no Cargo items on it. 3. ShipoverweightException - Should be thrown if the user attempts to push a Cargo object on to any stack of the CargoShip which would put it over it's maximum weight limit. 4. CargostrengthException o Should be thrown if the user attempts to push a Cargo object onto another Cargo object that violates the CargoStrength rules indicated above. gracefully and prevent a crash. Your program should NOT crash from any of the above exceptions (it should not crash from any exception, but especially not one that you ihrow yourself). Extra Credit: items should be the ONLY cargo on the dock). Note that the resulting contiguration of the cargo ship does not matter, so long as no rules are violated and the desired cargo is on the dock. For this operation, you can make the following assumptions: 1. The ship will have 3 stacks and a max height of 3 , for a potential maximum of 9 cargo objects on the ship. 2. All instances of the cargo with the indicated name will have the same Cargostrength (i.e. they can all be stacked on lop of one another). Input Format: before placing it on the dock (see sample LO for examples)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
