Question: Create a program using classes that does the following in the zyLabs developer below. For this lab, you will be working with two different class

Create a program using classes that does the following in the zyLabs developer below. For this lab, you will be working with two different class files. To switch files, look for where it says "Current File" at the top of the developer window. Click the current file name, then select the file you need.
(1) Create two files to submit:
ItemToPurchase.java - Class definition
ShoppingCartPrinter.java - 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 0int itemQuantity - Initialized in default constructor to 0
Default constructor
Public member methods (mutators & accessors)
setName() & getName()(2 pts)setPrice() & getPrice()(2 pts)setQuantity() & getQuantity()(2 pts)
(2) In main(), prompt the user for two items and create two objects of the ItemToPurchase class. Before prompting for the second item, call scnr.nextLine(); to allow the user to input a new string. (2 pts)
*** I keep getting the same errors no matter what I do to edit the code, the error is listed below ***
Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor(Scanner.java:937) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at ShoppingCartPrinter.main(ShoppingCartPrinter.java:14)
*** Below is my current code ***
// ShoppingCartPrinter.java
import java.util.Scanner;
public class ItemToPurchase {
public static void main(String[] args){
// Create a scanner object to read input
Scanner scnr = new Scanner(System.in);
// Create two ItemToPurchase objects
ItemToPurchase item1= new ItemToPurchase();
ItemToPurchase item2= new ItemToPurchase();
// Input details for Item 1
System.out.println("Item 1");
System.out.print("Enter the item name: ");
item1.setName(scnr.nextLine());
System.out.print("Enter the item price: ");
item1.setPrice(scnr.nextInt());
System.out.print("Enter the item quantity: ");
item1.setQuantity(scnr.nextInt());
// Consume the new line character from previous input
scnr.nextLine();
// Input details for Item 2
System.out.println("
Item 2");
System.out.print("Enter the item name: ");
item2.setName(scnr.nextLine());
System.out.print("Enter the item price: ");
item2.setPrice(scnr.nextInt());
System.out.print("Enter the item quantity: ");
item2.setQuantity(scnr.nextInt());
// Calculate total costs
int totalCostItem1= item1.getPrice()* item1.getQuantity();
int totalCostItem2= item2.getPrice()* item2.getQuantity();
int totalCost = totalCostItem1+ totalCostItem2;
// Output the total cost breakdown
System.out.println("
TOTAL COST");
System.out.println(item1.getName()+""+ item1.getQuantity()+" @ $"+ item1.getPrice()+"= $"+ totalCostItem1);
System.out.println(item2.getName()+""+ item2.getQuantity()+" @ $"+ item2.getPrice()+"= $"+ totalCostItem2);
System.out.println("Total: $"+ totalCost);
}
}
// ItemToPurchase.java
public class ItemToPurchase {
// Private fields
private String itemName;
private int itemPrice;
private int itemQuantity;
// Default constructor
public ItemToPurchase(){
this.itemName = "none";
this.itemPrice =0;
this.itemQuantity =0;
}
// Mutator methods
public void setName(String name){
this.itemName = name;
}
public void setPrice(int price){
this.itemPrice = price;
}
public void setQuantity(int quantity){
this.itemQuantity = quantity;
}
// Accessor methods
public String getName(){
return this.itemName;
}
public int getPrice(){
return this.itemPrice;
}
public int getQuantity(){
return this.itemQuantity;
}
}
Thank you for the assist.

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!