Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

[JAVA]: Hello! I\'m working on a java program and I\'m having some difficulties

ID: 3757279 • Letter: #

Question

[JAVA]:

Hello! I'm working on a java program and I'm having some difficulties in getting it to work.

Thanks.

Part 1:

Part 2:

20.7 Program 3a: Online shopping cart (1) Create two files to submit ItemToPurchase.java Class definition ShoppingCartPrinterjava- Contains main method Build the ItemToPurchase class with the following specifications . Private fields String itemName- Initialized in default constructor to none int itemPrice-Initialized in default constructor to 0 int itemQuantity - Initialized in default constructor to o Default constructor Public member methods (mutators & accessors) setName) & getName0 (2 pts) setPrice)&getPrice0 (2 pts) setQuantity0& getQuantity0 (2 pts) toStringo (1 pt) (2) In main0. (ShoppingCartPrinterjava) prompt the user for two items and create two objects of the ItemToPurchase class. Before prompting for the second item, call scnr.nextLine0; to allow the user to input a new string. (2 pts) Ex: Item 1 Enter the item name Chocolate Chips Enter the item price: Enter the item quantity: Item 2 Enter the item name: Bottled Water Enter the item price: Enter the item quantity: 10

Explanation / Answer

Answer :


1)
public class ItemToPurchase {


private String itemName;
private int itemPrice;
private int itemQuantity;
  

public ItemToPurchase() {
this.itemName = "none";
this.itemPrice = 0;
this.itemQuantity = 0;
}

public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public int getItemPrice() {
return itemPrice;
}
public void setItemPrice(int itemPrice) {
this.itemPrice = itemPrice;
}
public int getItemQuantity() {
return itemQuantity;
}
public void setItemQuantity(int itemQuantity) {
this.itemQuantity = itemQuantity;
}
}
_________________
ShoppingCartPrinter.java
import java.util.Scanner;
public class ShoppingCartPrinter {
public static void main(String[] args) {

String name;
int price, qty, itmTotPrice, tot = 0;

Scanner scnr = new Scanner(System.in);

ItemToPurchase itp[] = new ItemToPurchase[2];
for (int i = 0; i < itp.length; i++) {
itp[i] = new ItemToPurchase();
System.out.println("Item " + (i + 1));
System.out.println("Enter the item name:");
name = scnr.nextLine();
itp[i].setItemName(name);
System.out.println("Enter the item price:");
price = scnr.nextInt();
itp[i].setItemPrice(price);
System.out.println("Enter the item quantity:");
qty = scnr.nextInt();
itp[i].setItemQuantity(qty);
scnr.nextLine();
}
System.out.println("TOTAL COST");

for (int i = 0; i < itp.length; i++) {
itmTotPrice = itp[i].getItemQuantity() * itp[i].getItemPrice();
System.out.println(itp[i].getItemName() + " " + itp[i].getItemQuantity() + " @ $" + itp[i].getItemQuantity() + " = $" + (itmTotPrice));
tot += itmTotPrice;
}
System.out.println("Total: $" + tot);
}
}

Output:

Item 1
Enter the item name:
Chocolate Chips
Enter the item price:
3
Enter the item quantity:
1
Item 2
Enter the item name:
Bottled Water
Enter the item price:
1
Enter the item quantity:
10
TOTAL COST
Chocolate Chips 1 @ $1 = $3
Bottled Water 10 @ $10 = $10
Total: $13



Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote