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)
Design a Ship class that the following members: A field for the name of the ship...
Design a Ship class that the following members: A field for the name of the ship (a string). A field for the year that the ship was built (a string). A constructor and appropriate accessors and mutators. A toString method that displays the ship’s name and the year it was built. Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members: A field for the maximum number of passengers (an int). A constructor...
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...
In a package named "oop" create a Scala class named "Score" with the following: • A...
In a package named "oop" create a Scala class named "Score" with the following: • A constructor that takes an Int and stores it in a member variable named score • A method named scoreGoal that takes no parameters and has return type Unit that increments the score by 1 • A method named isWinner that takes a Score object as a parameter and returns a Boolean. The method returns true if this instances score is strictly greater than the...
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)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT