Question

In: Computer Science

Create a class named Ship with a field that stores a collection of Shippable things and...

Create a class named Ship with a field that stores a collection of Shippable things and another that stores a maximum weight capacity. Include a constructor with a parameter for the max weight, and that gives the Ship an empty collection of Shippables. (Javascript)

Solutions

Expert Solution

Hello,

I have created Ship class based on your requirement. I have created colloection of Shippable things in class with both Set and arrays. You can use either of them as per your convenience. And also I haave provided code snippet to test Ship class. Please let me know if you need any changes required. Below is the code shippets.

Ship class with Collection Set used for holding shippables:

// Createing Ship class with constructor has one parameter that is maxWeightCapacity
class Ship {
    constructor(maxWeightCapacity) {
        // Set maxWeightCapacity value based on constructor parameter value
        this.maxWeightCapacity = maxWeightCapacity;
        // Assign empty set to shippables field value
        this.shippables = new Set();
    }
}

// Test Ship class
// Created ship1 object with maxWeightCapacity 1000 by using constructor
const ship1 = new Ship(1000);
// Print maxWeightCapacity value within ship1 object
console.log(ship1.maxWeightCapacity);
// Add new item to shippables Set
ship1.shippables.add({
    weight: 10
});
// Add new item to shippables Set
ship1.shippables.add({
    weight: 20
});
// Print shippables value within ship1 object
console.log(ship1.shippables);

Ship class with Arrays used for holding shippables:

// Createing Ship class with constructor has one parameter that is maxWeightCapacity
class Ship {
    constructor(maxWeightCapacity) {
        // Set maxWeightCapacity value based on constructor parameter value
        this.maxWeightCapacity = maxWeightCapacity;
        // Assign empty array to shippables field value
        this.shippables = [];
    }
}
// Test Ship class
// Created ship2 object with maxWeightCapacity 500 by using constructor
const ship2 = new Ship(500);
// Print maxWeightCapacity value within ship1 object
console.log(ship2.maxWeightCapacity);
// Add new object to shippables array
ship2.shippables.push({
    weight: 15
});
// Add new object to shippables array
ship2.shippables.push({
    weight: 35
});
// Print shippables value within ship2 object
console.log(ship2.shippables);

Related Solutions

Create a class named Ship with a field that stores a collection of Shippable things and...
Create a class named Ship with a field that stores a collection of Shippable things and another that stores a maximum weight capacity. Include a constructor with a parameter for the max weight, and that gives the Ship an empty collection of Shippables. (Javascript)
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant...
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant to mimic the ArrayList data structure. It will hold an ordered list of items. This list should have a variable size, meaning an arbitrary number of items may be added to the list. Most importantly this class should implement the interface SimpleArrayList provided. Feel free to add as many other functions and methods as needed to your class to accomplish this task. In other...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain 3 double variables containing the length of each of the triangles three sides. Create a constructor with three parameters to initialize the three sides of the triangle. Add an additional method named checkSides with method header - *boolean checkSides() throws IllegalTriangleSideException *. Write code so that checkSides makes sure that the three sides of the triangle meet the proper criteria for a triangle. It...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain 3 double variables containing the length of each of the triangles three sides. Create a constructor with three parameters to initialize the three sides of the triangle. Add an additional method named checkSides with method header - *boolean checkSides() throws IllegalTriangleSideException *. Write code so that checkSides makes sure that the three sides of the triangle meet the proper criteria for a triangle. It...
In Java, design a class named MyInteger. The class contains: An int data field named value...
In Java, design a class named MyInteger. The class contains: An int data field named value that stores the int value represented by this object. A constructor that creates a MyInteger object for the specified int A get method that returns the int Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively. Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively. Static...
In java: -Create a class named Animal
In java: -Create a class named Animal
Create a program that stores the information of up to 50 containers loaded on ship. The...
Create a program that stores the information of up to 50 containers loaded on ship. The program should contain a menu to do the following: A or a     to add a container. R or r      to retrieve the information of one container. T or t      to retrieve the information of all containers. W or w   to retrieve the total weight of the loaded containers. X or x      to exit the program Ask the user for the number of containers to...
Design a class named Account that contains: A private int data field named id for the...
Design a class named Account that contains: A private int data field named id for the account. A private double data field named balance for the account. A private double data field named annualInterestRate that stores the current interest rate. A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. The accessor and mutator methods for id, balance, and annualInterestRate. A method named getMonthlyInterestRate() that returns the monthly interest rate. A method named withdraw(amount)...
Design a class named Account that contains: A private String data field named accountNumber for the...
Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account. A constructor...
Design a class named BankAccount that contains: A private int data field named id for the...
Design a class named BankAccount that contains: A private int data field named id for the account. A private double data field named balance for the account. A constructor that creates an account with the specified id and initial balance. A getBalance() method that shows the balance. A method named withdraw that withdraws a specified amount from the account. Create a subclass of the BankAccount class named ChequingAccount. An overdraftlimit to be 1000 for ChequingAccount . Test your ChequingAccount class...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT