Question

In: Computer Science

public class GroceryCart { private static final int DEFAULT_CAPACITY = 10; /* * Default constructor with...

public class GroceryCart {

private static final int DEFAULT_CAPACITY = 10;

/*

* Default constructor with zero arguments. This constructs a grocery

* cart with a default capacity of ten items.

*/

public GroceryCart() {

}

/*

* Alternate constructor which takes in a maxCapacity. This maxCapacity

* determines how many items can fit inside of this groceryCart.

*/

public GroceryCart(int maxCapacity) {

}

/*

* Adds an item to the grocery cart. Returns true if the item was added

* successfully (i.e. there was still room in the cart to add it.

*/

public boolean addItem(String item, Double cost) {

return false;

}

/*

* Removes the specified item from the cart. Returns true if the item was

* successfully removed (i.e. the item existed in the cart).

*/

public boolean removeItem(String item) {

return false;

}

/*

* Empties the cart of all contents.

*/

public void emptyCart() {

}

/*

* Returns a string representation of the carts contents. The contents

* should be printed out in alphabetical order. It should be of

* the form, "item:price, item:price, item:price". An example:

* "Green Beans:2.99, Milk:41.99, Rolled Oats:1.99, Zucchini:2.67"

*/

public String toString() {

return "";

}

}

Solutions

Expert Solution

NOTE: Since you have not enclosed any description of the problem, I am providing the definitions of all the methods as per my understanding.

CODE

class Grocery {

private String item;

private double cost;

public Grocery(String item, double cost) {

this.item = item;

this.cost = cost;

}

public String getItem() {

return item;

}

public double getCost() {

return cost;

}

}

public class GroceryCart {

private static final int DEFAULT_CAPACITY = 10;

private Grocery[] groceries;

private int size;

/*

* Default constructor with zero arguments. This constructs a grocery

* cart with a default capacity of ten items.

*/

public GroceryCart() {

groceries = new Grocery[DEFAULT_CAPACITY];

size = 0;

}

/*

* Alternate constructor which takes in a maxCapacity. This maxCapacity

* determines how many items can fit inside of this groceryCart.

*/

public GroceryCart(int maxCapacity) {

groceries = new Grocery[maxCapacity];

size = 0;

}

/*

* Adds an item to the grocery cart. Returns true if the item was added

* successfully (i.e. there was still room in the cart to add it.

*/

public boolean addItem(String item, Double cost) {

if (size >= groceries.length) {

return false;

}

groceries[size ++] = new Grocery(item, cost);

}

/*

* Removes the specified item from the cart. Returns true if the item was

* successfully removed (i.e. the item existed in the cart).

*/

public boolean removeItem(String item) {

for (int i=0; i<groceries.length; i++) {

if (groceries[i].getItem().equals(item)) {

for (int j=i; j<groceries.length; j++) {

groceries[j] = groceries[j+1];

}

size --;

return true;

}

}

return false;

}

/*

* Empties the cart of all contents.

*/

public void emptyCart() {

groceries = new Grocery[DEFAULT_CAPACITY];

}

/*

* Returns a string representation of the carts contents. The contents

* should be printed out in alphabetical order. It should be of

* the form, "item:price, item:price, item:price". An example:

* "Green Beans:2.99, Milk:41.99, Rolled Oats:1.99, Zucchini:2.67"

*/

public String toString() {

String res = "";

for(int i=0; i<groceries.length; i++) {

res += groceries[i].getItem() + ":" + groceries[i].getCost() + ", ";

}

return res.substring(0, res.length() - 2);

}

}


Related Solutions

public class ProductThread { static class ProductThreads extends Thread{ private int begin, end; int[] v1, v2;...
public class ProductThread { static class ProductThreads extends Thread{ private int begin, end; int[] v1, v2; long ris; public ProductThreads(String name, int [] v1, int [] v2, int begin, int end) { setName(name); this.v1 = v1; this.v2 = v2; this.begin = begin; this.end = end; this.ris = 0; } public void run() { System.out.println("Thread " + Thread.currentThread().getName() + "[" + begin + "," + end + "] started"); ris = 1; for(int i = begin; i <= end; i++) ris...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first < second) return "less than"; else if (first == second) return "equal to"; else return "greater than";       }    // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter first integer: "); int first = input.nextInt(); System.out.print("Enter second integer: "); int second = input.nextInt(); System.out.println("The first integer is " + comparison(first, second) + " the...
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
Define the following class: class XYPoint { public: // Contructors including copy and default constructor //...
Define the following class: class XYPoint { public: // Contructors including copy and default constructor // Destructors ? If none, explain why double getX(); double getY(); // Overload Compare operators <, >, ==, >=, <=, points are compare using their distance to the origin: i.e. SQR(X^2+Y^2) private: double x, y; };
public class P2 { public static int F(int x[], int c) { if (c < 3)...
public class P2 { public static int F(int x[], int c) { if (c < 3) return 0; return x[c - 1] + F(x, c - 1); } public static int G(int a, int b) { b = b - a; a = b + a; return a; } public static void main(String args[]) { int a = 4, b = 1; int x[] = { 3, 1, 4, 1, 5 }; String s = "Problem Number 2"; System.out.println(x[2 +...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the array to have the following property:        Suppose the first element in the original array has the value x.        In the new array, suppose that x is in position i, that is data[i] = x.        Then, data[j] <= x for all j < I and data[j] > x for all j > i.        Thus, informally, all the...
import java.util.NoSuchElementException; public class ArrayBasedDeque<AnyType> implements deque<AnyType> {       private static int MAX_SIZE = 5;...
import java.util.NoSuchElementException; public class ArrayBasedDeque<AnyType> implements deque<AnyType> {       private static int MAX_SIZE = 5; // initial array size    //DO NOT CHANGE this, it is set to 5 to make sure your code    //will pass all the tests and works with no issue.       // add all data fields which are required    /**    * ArrayBasedDeque() constructs an empty deque.    */    public ArrayBasedDeque(){        //complete    }       /**    *...
public class StringTools {    public static int count(String a, char c) {          ...
public class StringTools {    public static int count(String a, char c) {           }
In the following class public class Single { private float unique; ... } Create constructor, setter...
In the following class public class Single { private float unique; ... } Create constructor, setter and getter methods, and toString method. Write a program that request a time interval in seconds and display it in hours, minutes, second format. NEED THESE ASAP
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class BowlerReader { private static final String FILE_NAME =...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class BowlerReader { private static final String FILE_NAME = "bowler.txt"; public static void main(String[] args) throws FileNotFoundException { System.out.println("Reading Data from file"); Scanner fileReader = new Scanner(new File(FILE_NAME)); System.out.printf("%-20s%-10s%-10s%-10s%-10s\n", "Sample Data", "Game 1", "Game 2", "Game 3", "Average"); int bowler = 1; while (fileReader.hasNext()) { String scores[] = fileReader.nextLine().split("\\s+"); double average = Integer.parseInt(scores[0]) + Integer.parseInt(scores[1]) + Integer.parseInt(scores[2]); average /= 3; System.out.printf("%-20s%-10s%-10s%-10s%-10.2f\n", "Bowler " + bowler, scores[0], scores[1], scores[2], average); bowler += 1; }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT