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 Square {    public static final int NUM_OF_SIDES = 4;    private float length;...
public class Square {    public static final int NUM_OF_SIDES = 4;    private float length;       public Square(float l) {        setLength(l);    }       public void setLength(float l) {        if(l >= 0) {            length = l;        }    }       public float getLength() {        return length;    }           //TODO - Add method to calculate add return area          //TODO -...
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...
public class Clock { private int hr; private int min; private int sec; public Clock() {...
public class Clock { private int hr; private int min; private int sec; public Clock() { setTime(0, 0, 0); } public Clock(int hours, int minutes, int seconds) { setTime(hours, minutes, seconds); } public void setTime(int hours, int minutes, int seconds) { if (0 <= hours && hours < 24) hr = hours; else hr = 0; if (0 <= minutes && minutes < 60) min = minutes; else min = 0; if(0 <= seconds && seconds < 60) sec =...
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 Lab1 { public static void main(String[] args) { int array [] = {10, 20,...
public class Lab1 { public static void main(String[] args) { int array [] = {10, 20, 31, 40, 55, 60, 65525}; System.out.println(findPattern(array)); } private static int findPattern(int[] arr) { for (int i = 0; i < arr.length - 2; i++) { int sum = 0; for (int j = i; j < i + 2; j++) { sum += Math.abs(arr[j] - arr[j + 1]); } if (sum == 20) return i; } return -1; } } QUESTION: Modify the given...
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...
public class Point { protected double x, y; // coordinates of the Point //Default constructor public...
public class Point { protected double x, y; // coordinates of the Point //Default constructor public Point() { setPoint( 0, 0 ); } //Constructor with parameters public Point(double xValue, double yValue ) { setPoint(xValue, yValue ); } // set x and y coordinates of Point public void setPoint(double xValue, double yValue ) { x = xValue; y = yValue; } // get x coordinate public double getX() { return x; } // get y coordinate public double getY() { return...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT