Homework 04 –Pirates and Plunder Authors:Will, Nathaniel, Faris, Brandon, Matthew, Allan Topics: polymorphism, dynamic binding, casting Problem Description Please make sure to read all parts of this...

1 answer below »
Homework 04 –Pirates and Plunder Authors:Will, Nathaniel, Faris, Brandon, Matthew, Allan Topics: polymorphism, dynamic binding, casting Problem Description Please make sure to read all parts of this document carefully. Ahoy matey, The year be 1700. A group of pirates recently acquired a computer from the British Navy (just go with it) in a surprise raid on a British fort. Despite their affinity for tea, the British had recently installed Java on the computer before it was plundered by the pirates. The pirates, being business savvy as they are, have offered a contract worth one-thousand doubloonsto you to help modernize their logistical systems. You choose to ignore the ethical issues of piracy for those sweet, sweet doubloons and the opportunity to apply your polymorphism, dynamic binding, and casting knowledge to a real-world problem. SolutionDescription Create files Loot.java, Coin.java, Sugar.java, Plunderable.java, and Ship.java. You will be creating several fields and methods for each file. Some classes will inherit from other classes, and some will implement an interface. Follow the instructions, paying close attention to keywords, to determine what extends and implements what. Loot.java This class represents the spoils gained from plundering a ship or fort. Loot is an abstract class. Variables: All variables must not be allowed to be directly modified outside the class in which they are declared, unless stated otherwise: • double value – the monetary value of an item. Constructor(s): • Constructorthat takes in a valueand ensures that the value of the item is greater than 0. • No-param constructor that sets value to 0. Methods: All methods in this class should be public, unless stated otherwise: • Getter and setter for value. We want both to be public, since the value of a piece of loot might change based on supply and demand. • toString() o This method should properly override Object’s toString()method o This method should return “A piece of loot worth {value}” • equals() o This method should properly override Object’s equals()method o If two Loot objects have the same value we say they are equal Coin.java This class represents a piece of loot in the form of a coin. Variables: All variables must not be allowed to be directly modified outside the class in which they are declared, unless otherwise stated in the description of the variable. Coin inherits all variables from Loot. • boolean heads – represents whether or not the coin is heads up or tails up. • int yearMade – represents the year the coin was manufactured in. yearMademust be positive • String material – represents the material the Coin is made of, such as gold, silver, etc. Constructor(s): • A constructor that takes in value, heads, yearMade, and material o Reuse any code if you can o Ensure yearMadeis valid, in the range [0, 1700]. If not, set it to 1700. • A constructor that takes in heads and yearMade o Use constructor chaining if possible o For value, set it to (3000 – yearMade) / 100 o Will use a default material of “Gold” Methods: All methods must have the proper visibility to be used where it is specified they are used. • toString() o This method should properly override Object’s and Loot’s toString()methods o It should print the following: “A {material} coin made in {yearMade}. Heads side is up: {heads}. • equals() o This method should properly override Object’s equals()method o If two Coinobjects have the same year, value, and material, they are equal o Reuse code. Hint: how can you use Loot’s equals()method? Sugar.java This class represents the valuable cash crop of sugar as loot, a frequently pirated commodity. Variables: All variables must not be allowed to be directly modified outside the class in which they are declared, unless otherwise stated in the description of the variable. Sugar inherits all variables from Loot. • double amount – unlike a coin, we don’t have “one sugar”. Instead we have an amount represented by a double • double sweetness – This value represents the sweetness of the sugar. It must be in range 0 to 100 inclusive - [0, 100] Constructor(s): • A constructor that takes in amount and sweetness o Ensure sweetnessvalue is within acceptable range. If not, set to 0. o After determining the appropriate sweetnessabove, the valueof the sugar should be determined by its amount times its sweetness Methods: All methods must have the proper visibility to be used where it is specified they are used. • toString() o This method should properly override Object’s toString()method o It should print the following: “A pile of sugar of size {amount} and sweetness {sweetness}.” • equals() o This method should properly override Object’s equals()method o If two Sugar objects have the same amount, value, and sweetness, they are equal o Reuse code. Hint: how can you use Loot’s equals()method? Plunderable.java Plunderable is an interface which is implemented when an object can be plundered. Methods: Plunder only has one method, bePlundered. • bePlundered() o This method should not be implemented in plunderable. It should, when implemented, return an array of Loot objects, representing what the object plundered had on hand. Ship.java This class represents a ship on the high seas with cargo on board. Ship should implement Plunderable.java Variables: All variables must not be allowed to be directly modified outside the class in which they are declared, unless otherwise stated in the description of the variable. • Loot[] cargo – This is the cargo a ship has aboard. Initially, this array should be of length 10. • double totalCargoValue – This is the total value of the cargo on the ship. You should continually update this whenever new cargo is added or removed. • String name – Every ship needs a good name, these are no exception Constructor(s): • A one-param constructor which takes in a name and sets the initializes cargo’s length to the value specified is required. • A no-parameter constructor chained to the one above, with default name Black Pearl Methods: All methods must have the proper visibility to be used where it is specified they are used. • toString() o This method should properly override Object’s toString()method o It should print the following: “A ship called {name} with cargo {cargo[0]}, {cargo[1]}..., which has a total value of {totalCargoValue}.” o Only slots in the array that have something in them should have their values displayed o Round totalCargoValue to 2 decimal places in the string • addCargo(Loot newItem) o This method should be responsible for the logic of adding cargo to a ship. This new cargo should take the first available slot in the array. Additionally, update the totalCargoValue of the ship. o If there are no slots left in the array, create a new array with twice the current one’s length, copy over the old one’s cargo, and add the new cargo according to the previous rule. • removeCargo() o This method will set the first filled cargo slot in the array to null, then return that cargo item. o It should also update totalCargoValue. o If the array is empty, return null • removeCargo(Loot cargo) o This method will iterate through the array, checking if that cargo is on the ship. If it is, perform the operation specified in removeCargo()on that item. Else, return null. Note: check for object equality, not reference equality when determining if an item is in the array. • bePlundered() o Return an array of the Loot objects in the ship’s cargo, then set all the items in the old array to null. E.g. if a ship with 3 coins is plundered, you should return an array of 3 coins and that ship’s cargo array should be empty. o This method should also account for the totalCargoValue’s changes. Testing Reuse your code when possible. Certain methods can be reused using certain keywords. These tests and the ones on Gradescope are by no means comprehensive, so be sure to create your own! Checkstyle You mustrun checkstyle on yoursubmission (To learn more about Checkstyle, check out cs1331-styleguide.pdf under CheckStyle Resources in the Modules section of Canvas.) The Checkstyle cap for this assignment is 25 points. This means there is a maximum point deduction of 20. If you don't have Checkstyle yet, download it from Canvas -> Modules -> CheckStyle Resources -> checkstyle-8.28.jar. Place it in the same folder as the files you want to run Checkstyle on. Run checkstyle on your code like so: $ java -jar checkstyle-8.28.jar yourFileName.java Starting audit... Audit done. The message above meansthere were no Checkstyle errors. If you had any errors,they would show up above this message, and the number atthe end would be the points we would take off (limited by the checkstyle cap mentioned in the Rubric section). In future homeworks we willbe increasing this cap, so getinto the habit of fixing these style errors early! Additionally, you must Javadoc your code. Run the following to only check your Javadocs: $ java -jar checkstyle-8.28.jar -j yourFileName.java Run the following to check both Javadocs and Checkstyle: $ java -jar checkstyle-8.28.jar -a yourFileName.java For additional help with Checkstyle see the CS 1331 Style Guide. Turn-In Procedure Submission To submit, upload the files listed below to the corresponding assignment on Gradescope: • Loot.java • Coin.java • Sugar.java • Plunderable.java • Ship.java Make sure you see the message stating the assignment was submitted successfully. From this point, Gradescope will run a basic autograder on your submission as discussed in the next section. Any autograder test are provided as a courtesy to help “sanity check” your work and you may not see all the test cases used to grade your work. You are responsible for thoroughly testing your submission on your own to ensure you have fulfilled the requirements of this assignment. If you have questions about the requirements given, reach out to a TA or Professor via Piazza for clarification. You can submit as many times as you want before the deadline, so feel free to resubmit as you make substantial progress on the homework. We will only grade your latest submission. Be sure to submit every file each time you resubmit.
Answered 1 days AfterOct 19, 2021

Answer To: Homework 04 –Pirates and Plunder Authors:Will, Nathaniel, Faris, Brandon, Matthew, Allan Topics:...

Kshitij answered on Oct 21 2021
125 Votes
oct19_21/Coin.java
oct19_21/Coin.java
/*   Created by IntelliJ IDEA.
 *   Author: Kshitij Varshney (kshitijvarshne1)
 *   Date: 19-Oct-21
 *   Time: 11:24 PM
 *   File: Coin.java
 */
package October.oct19_21;
import java.util.Objects;
// extends from abstract class Loot
public
 class Coin extends Loot {
    // instance variable
    private int yearMade;
    private String material;
    private boolean heads;
    // parameterised constructor -> 4 parameter
    public Coin(double value, boolean heads, int yearMade, String material) {
        super(value);
        this.heads = heads;
        if (yearMade < 0 || yearMade > 1700) {
            this.yearMade = 1700;
        } else {
            this.yearMade = yearMade;
        }
        this.material = material;
    }
    // parameterised constructor -> 2 parameter
    public Coin(boolean heads, int yearMade) {
        this.yearMade = (3000 - yearMade) / 100;
        this.material = "GOLD";
    }
    //getter and setter
    public int getYearMade() {
        return yearMade;
    }
    public void setYearMade(int yearMade) {
        this.yearMade = yearMade;
    }
    public String getMaterial() {
        return material;
    }
    public void setMaterial(String material) {
        this.material = material;
    }
    public boolean isHeads() {
        return heads;
    }
    public void setHeads(boolean heads) {
        this.heads = heads;
    }
    // toString methods-> print the data
    @Override
    public String toString() {
        return "A " + this.material + " coin made in " + this.yearMade + ". Heads side is up: " + this.heads + ".";
    }
    // equals method to check Object
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;
        Coin coin = (Coin) o;
        return getYearMade() == coin.getYearMade() && isHeads() == coin.isHeads() && Objects.equals(getMaterial(), coin.getMaterial());
    }
}
oct19_21/Loot.java
oct19_21/Loot.java
/*   Created by IntelliJ IDEA.
 *   Author: Kshitij Varshney (kshitijvarshne1)
 *   Date: 19-Oct-21
 *   Time: 11:18 PM
 *   File: Loot.java
 */
package October.oct19_21;
// abstract class
public abstract class Loot {
    private double value;
    // constructor
    public Loot(double value) {
        this.value = value;
    }
    public Loot() {
        this.value = 0;
    }
    // getter and setter
    public double getValue() {
        return value;
    }
    public void setValue(double value) {
        this.value = value;
    }
    // toString method to print the data
    @Override
    public String toString() {
        return "A piece of loot worth " + this.value;
    }
    @Override
    public bo...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here