Question: Show me the steps to solve Problem 3 : Implementing a Shopping Cart Implement a simple shopping cart using an array of objects. Files Provided:

Show me the steps to solve Problem 3: Implementing a Shopping Cart
Implement a simple shopping cart using an array of
objects.
Files Provided:
- : A class to represent an item with name, price, and quantity.
- ShoppingCart.java: A class representing the shopping cart with an array of objects.
Item.java
import java.text.NumberFormat;
public class Item \{
private String name;
private double price;
private int quantity;
//
// Create a new item with the given attributes.
//
public Item (String itemName, double itemPrice, int numPurchased)\{
name = itemName;
price = itemPrice;
quantity = numpurchased;
\}
//
// Return a string with the information about the item
//
public string tostring ()\{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
return (name +"\t"+ fmt.format(price)+"\t"+ quantity +"\t"
+ fmt.format(price * quantity));
\}
//
// Returns the unit price of the item
//
public double getPrice()\{
return price;
\}
//
// Returns the name of the item
//
public string getName()\{ return name;
\}
//
// Returns the quantity of the item
//
public int getquantity()\{
return quantity;
ShoppingCart.java
```
import java.text.NumberFormat;
```
public class ShoppingCart {
private int itemCount; // total number of items in the cart
private double totalprice; // total price of items in the cart
private int capacity; // current cart capacity
//
// Creates an empty shopping cart with a capacity of 5 items.
//
public ShoppingCart(){
capacity =5;
itemCount =0;
totalPrice =0.0;
}
//
// Adds an item to the shopping cart.
//
public void addToCart(String itemName, double price, int quantity){
// Implementation of adding item to cart should go here
}
//
// Returns the contents of the cart together with summary information.
//
public string toString(){
NumberFormat fmt = NumberFormat.getCurrencyInstance();
string contents ="
shopping Cart
";
contents +="
Item\t\tUnit Price\tQuantity\tTotal
";
for (int i =0; i itemCount; i++)
contents += cart[i].tostring()+"
"; ```
contents +="
Total Price: "+ fmt.format(totalPrice);
contents +="
";
return contents;
}
//
// Increases the capacity of the shopping cart by 3
//
private void increaseSize(){
// Implementation of increasing the cart size should go here
```
Instructions:
1. In , declare an array of Item objects and initialize it with a capacity of 5.
2. Implement to expand the cart's capacity by 3 if more space is needed.
3. Implement to add items to the cart and update the total price.
Example:
- Input:
Name: Pen, Price: 1.5, Quantity: 2
- Output:
The file contains a program to test the variable length method average from Section 7.5 of the text. Note that average must be a static method since it is called from the static method main. Write a program to test variable-length parameter lists.
Parameters.java
import java.util.scanner;
public class Parameters
// Calls the average and minimum methods with
// different numbers of parameters.
public static void main(String[] args)\{
double mean1, mean2;
mean \(1=\) average \((42,69,37)\);
mean2\(=\) average(\(35,43,93,23,40,21,75)\);
System.out.println("mean1="+ mean1);
system.out.println("mean2="+ mean2);
3
// Returns the average of its parameters.
public static double average (int ... list)\{
double result \(=0.0\);
if (list. length !=0)\{
int sum \(=0\);
for (int num : list)
sum \(+=\) num;
result \(=\)(double) sum / list.length;
3
return result;
3
(3) Steps:
1. Implement a method that calculates the average of any number of integer arguments.
2. Test the method with different sets of arguments, including a single integer and an empty parameter list.
3. Add an interactive part that accepts a series of nonnegative integers (up to 20) from the user and stops when a negative number is entered.
4. Add a method that finds the minimum value in the passed parameters and use it to test each set of arguments.
Instructions:
- Input:
- Output:
Mean: 8.5
Minimum: 4
Show me the steps to solve Problem 3 :

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!