Question

In: Computer Science

I have to complete all //to do comments for the following code: /** * A ShoppingBasket...

I have to complete all //to do comments for the following code:

/**
* A ShoppingBasket holds zero or more Products and can provide information
* about the Products. One can add Products to a ShoppingBasket during its
* lifetime, reset the ShoppingBasket, create a copy which contains Products of
* at least a certain value, and make various queries to the ShoppingBasket.
* (Thus, the number of Products that will be stored by a ShoppingBasket object
* is not yet known when the new object is created, and it may grow and shrink
* over the lifetime of a ShoppingBasket object.)
*
* @author Carsten Fuhs
*/
public class ShoppingBasket {
// TO DO instance variables
/* Constructors */
/**
* Constructs a new ShoppingBasket without any Products.
*/
public ShoppingBasket() {
// TO DO
}
/**
* Constructs a new ShoppingBasket containing the non-null Products in
* products. The products array may be modified by the caller afterwards
* without affecting this ShoppingBasket, and it will not be modified by
* this constructor.
*
* @param products must not be null; non-null elements are added to the
* constructed ShoppingBasket
*/
public ShoppingBasket(Product[] products) {
// TO DO
}
/* Modifiers */
/**
* Adds a Product e to this ShoppingBasket if e is not null; does not
* modify this ShoppingBasket otherwise. Returns true if e is not null,
* false otherwise.
*
* @param e an product to be added to this ShoppingBasket
* @return true if e is not null, false otherwise
*/
public boolean add(Product e) {
// TO DO
return false;
}
/**
* Adds all non-null Products in products to this ShoppingBasket.
*
* @param products contains the Product objects to be added to
* this ShoppingBasket; must not be null (but may contain null)
* @return true if at least one element of products is non-null;
* false otherwise
*/
public boolean addAll(Product[] products) {
// TO DO
return false;
}
/**
* Removes certain Products from this ShoppingBasket. Exactly those
* Products are kept whose price in pence is greater than or equal to the
* specified minimum price in pence.
*
* @param minProductPriceInPence the minimum price in pence for the
* Products that are kept
*/
public void keepOnlyProductsWith(long minProductPriceInPence) {
// TO DO
}
/* Accessors */
/**
* Returns the number of non-null Products in this ShoppingBasket.
*
* @return the number of non-null Products in this ShoppingBasket
*/

Solutions

Expert Solution

***Please upvote/thumbsup if you liked the answer***

N.B I do not have the description of the Product class so cannot run the code

Screenshot of the Java code:-

Java code to copy:-

public class ShoppingBasket {
    private int index = 0;
    private Product[] products;

    /* Constructors */
    /**
     * Constructs a new ShoppingBasket without any Products.
     */
    public ShoppingBasket() {
        //Initialising the instance variable to hold 100 products
        //but initially all positions are empty
        this.products = new Product[100];

    }
    /**
     * Constructs a new ShoppingBasket containing the non-null Products in
     * products. The products array may be modified by the caller afterwards
     * without affecting this ShoppingBasket, and it will not be modified by
     * this constructor.
     *
     * @param products must not be null; non-null elements are added to the
     * constructed ShoppingBasket
     */
    public ShoppingBasket(Product[] products) {

        for (Product e:products)
        {
            if(e != null) {
                this.products[index] = e;
                index++;
            }
        }
    }
    }
    /* Modifiers */
    /**
     * Adds a Product e to this ShoppingBasket if e is not null; does not
     * modify this ShoppingBasket otherwise. Returns true if e is not null,
     * false otherwise.
     *
     * @param e an product to be added to this ShoppingBasket
     * @return true if e is not null, false otherwise
     */
    public boolean add(Product e) {
        if (e != null)
        {
            products[index] = e;
            index++;
            return true;
        }
        return false;
    }
    /**
     * Adds all non-null Products in products to this ShoppingBasket.
     *
     * @param products contains the Product objects to be added to
     * this ShoppingBasket; must not be null (but may contain null)
     * @return true if at least one element of products is non-null;
     * false otherwise
     */
    public boolean addAll(Product[] products) {

        boolean flag = false;
        for (Product e:products) {
            {
                if (e != null)
                {
                    this.products[index] = e;
                    index++;
                    flag = true;
                }
            }
            }

        return flag;
    }
    /**
     * Removes certain Products from this ShoppingBasket. Exactly those
     * Products are kept whose price in pence is greater than or equal to the
     * specified minimum price in pence.
     *
     * @param minProductPriceInPence the minimum price in pence for the
     * Products that are kept
     */
    public void keepOnlyProductsWith(long minProductPriceInPence) {
        for (Product e:this.products) {
            {
                if (e.getProductPriceInPence() >= minProductPriceInPence)
                {
                    products[index] = e;
                    index++;
                    flag = true;
                }
            }
        }
    }
    /* Accessors */
    /**
     * Returns the number of non-null Products in this ShoppingBasket.
     *
     * @return the number of non-null Products in this ShoppingBasket
     */
    public Product[] getProducts() {
        return products;
    }
}

Related Solutions

I need to complete this C++ program. The instructions are in the comments inside the code...
I need to complete this C++ program. The instructions are in the comments inside the code below: ------------------------------------------------------------------------- Original string is: this is a secret! Encypted string is: uijt!jt!b!tfdsfu" Decrypted string is: this is a secret! //Encoding program //Pre-_____? //other necessary stuff here int main() { //create a string to encrypt using a char array cout<< "Original string is: "<<string<<endl; encrypt(string); cout<< "Encrypted string is: "<<string<<endl; decrypt(string); cout<<"Decrypted string is: "<<string<<endl; return 0; } void encrypt(char e[]) { //Write implementation...
Please complete the following code in C using the comments as instructions. Further instructions are below...
Please complete the following code in C using the comments as instructions. Further instructions are below the code. challenge.c // goal: print the environment variables to the file "env.txt", one per line // (If envp is NULL, the file should be empty, opening in write mode will do that.) // example: // inputs: // envp/environ = {"E1=2","E2=7",NULL} // outputs: // env.txt as a string would be "E1=2\nE2=7\n" // example: // inputs: // envp/environ = {NULL} or NULL // outputs: //...
JAVA CODE, BEGINERS; Please use comments to explain For all of the following words, if you...
JAVA CODE, BEGINERS; Please use comments to explain For all of the following words, if you move the first letter to the end of the word, and then spell the result backwards, you will get the original word: banana dresser grammar potato revive uneven assess Write a program that reads a word and determines whether it has this property. Continue reading and testing words until you encounter the word quit. Treat uppercase letters as lowercase letters.
complete this code for me, this is java question. // Some comments omitted for brevity import...
complete this code for me, this is java question. // Some comments omitted for brevity import java.util.*; /* * A class to demonstrate an ArrayList of Player objects */ public class ListOfPlayers { // an ArrayList of Player objects private ArrayList players; public ListOfPlayers() { // creates an empty ArrayList of Players players = new ArrayList<>(); } public void add3Players() { // adds 3 Player objects players.add(new Player("David", 10)); players.add(new Player("Susan", 5)); players.add(new Player("Jack", 25)); }    public void displayPlayers()...
Also please add comments on the code and complete in C and also please use your...
Also please add comments on the code and complete in C and also please use your last name as key. The primary objective of this project is to increase your understanding of the fundamental implementation of Vigenere Cipher based program to encrypt any given message based on the Vignere algorithm. Your last name must be used as the cipher key. You also have to skip the space between the words, while replicating the key to cover the entire message. Test...
Complete the code below by finishing up the tasks detailed in the comments. ArrayList<String> fruits =...
Complete the code below by finishing up the tasks detailed in the comments. ArrayList<String> fruits = new ArrayList<>(); Scanner sc = new Scanner(System.in); String fruit = "?"; while (!fruit.isEmpty()) { // prompt the user to enter a fruit // read the fruit using "sc", saving it into variable "fruit" // convert the fruit entered to lowercase // if the fruit is NOT empty and it is a new fruit, add it to ArrayList "fruits" } // display all fruits
Write code in SAS to do each of the following I have posted the data below...
Write code in SAS to do each of the following I have posted the data below from a pace delimited data set consisting of 66 randomly selected cars Upload the data set to SAS and store it as a SAS data set called cars. Make sure the full values are stored for all character variables. Create a comparative bar chart (with appropriate title and labels) displaying the brands of each car by fuel type (so that fuel type is on...
In Java: Complete the following methods in the template by adhering to the comments: // TO...
In Java: Complete the following methods in the template by adhering to the comments: // TO DO: add your implementation and JavaDoc public class BetterArray<T> { private static final int DEFAULT_CAPACITY = 2; //default initial capacity / minimum capacity private T[] data; //underlying array, you MUST use this for full credit // ADD MORE PRIVATE MEMBERS HERE IF NEEDED! @SuppressWarnings("unchecked") public BetterArray() { //constructor //initial capacity of the array should be DEFAULT_CAPACITY } @SuppressWarnings("unchecked") public BetterArray(int initialCapacity) { // constructor...
Hello I have this error in the code, I do not know how to fix it....
Hello I have this error in the code, I do not know how to fix it. It is written in C++ using a Eclipse IDE Error: libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: basic_string bus.h =========== #pragma once #include using namespace std; class Bus { private:    string BusId; // bus ID    string Manufacturer; // manufacturer of the bus    int BusCapacity; // bus capacity    int Mileage; // mileage of bus    char Status; // current status...
Can you complete the tasks inside the sample code. All the to-do task is inside the...
Can you complete the tasks inside the sample code. All the to-do task is inside the code commented out. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> typedef struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }node; /*Task 1 - Complete the function below, newNode() will return a tree node*/ node* newNode(int key){ } /*Task 2 - Complete the function below to return the size (number of elements stored) of a binary tree*/ int size(node* root){ }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT