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...
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.
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){ }...
Programming. Write only code for the following. Do not write any comments. You can submit your...
Programming. Write only code for the following. Do not write any comments. You can submit your answer as .java file(s). #1. Design a Java JProduct class for a product which implements both cloneable and comparable interfaces The class should have the following private member variables: m_id: an integer that holds the product ID m_name: a string that holds the product name m_wholesaleprice: a double that holds the wholesale price m_retailers: a String array that holds all retailers who sell the...
I'm very confused on how to complete this code. There are 5 total classes. I have...
I'm very confused on how to complete this code. There are 5 total classes. I have some written but it isn't quite working and the Band class especially is confusing me. I posted the entire thing since the band and manager class refer to the other classes' constructors and thought it would be helpful to post those as well. Person class is a base class: Private variables String firstname, String lastname, int age Constructor has the parameters String firstname, String...
Please do it in C++. Please comment on the code, and comments detail the run time...
Please do it in C++. Please comment on the code, and comments detail the run time in terms of total operations and Big O complexities. 1. Implement a class, SubstitutionCipher, with a constructor that takes a string with the 26 uppercase letters in an arbitrary order and uses that as the encoder for a cipher (that is, A is mapped to the first character of the parameter, B is mapped to the second, and so on.) Please derive the decoding...
write the code in MATLAB with comments and show the inputs and results of the code...
write the code in MATLAB with comments and show the inputs and results of the code for the question below. Write an .m file in MATLAB, that records audio (you can record your own voice for 20 seconds that was recorded using your phone), then take Fourier transform of the signal (probably FFT).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT