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

CODE:

Text:

function printNumber(arr) {

    for (var i = 0; i < 10; i++) {

        var number = window.prompt("Enter a number");

        if (number > 10 && number < 100) {

            if (arr.find(aNumber => aNumber === number) == undefined) {

                arr.push(number);

                console.log(number + " ");

            }

        }

    }

}

class Ship{

    constructor(maxWeight){

        this.capacity = maxWeight;

        this.shippableItems = [];

    }

    addItems(item){

        if(this.shippableItems.length < this.capacity){

            this.shippableItems.push(item);

        }

    }

    printItems(){

        for(let item of this.shippableItems){

            console.log(item);

        }

    }

}

var ship = new Ship(5);

ship.addItems("Veg");

ship.addItems("Meat");

ship.addItems("Fish");

ship.addItems("colddrink");

ship.addItems("beverage");

ship.printItems();

OUTPUT:


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