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.
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 int itemQuantity Initialized in default constructor to
Default constructor
Public member methods mutators & accessors
setName & getName ptssetPrice & getPrice ptssetQuantity & getQuantity pts
In main prompt the user for two items and create two objects of the ItemToPurchase class. Before prompting for the second item, call scnrnextLine; to allow the user to input a new string. 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.basejavautil.Scanner.throwForScannerjava: at java.basejavautil.Scanner.nextScannerjava: at java.basejavautil.Scanner.nextIntScannerjava: at java.basejavautil.Scanner.nextIntScannerjava: at ShoppingCartPrinter.mainShoppingCartPrinterjava:
Below is my current code
ShoppingCartPrinter.java
import java.util.Scanner;
public class ItemToPurchase
public static void mainString args
Create a scanner object to read input
Scanner scnr new ScannerSystemin;
Create two ItemToPurchase objects
ItemToPurchase item new ItemToPurchase;
ItemToPurchase item new ItemToPurchase;
Input details for Item
System.out.printlnItem ;
System.out.printEnter the item name: ;
itemsetNamescnrnextLine;
System.out.printEnter the item price: ;
itemsetPricescnrnextInt;
System.out.printEnter the item quantity: ;
itemsetQuantityscnrnextInt;
Consume the new line character from previous input
scnrnextLine;
Input details for Item
System.out.println
Item ;
System.out.printEnter the item name: ;
itemsetNamescnrnextLine;
System.out.printEnter the item price: ;
itemsetPricescnrnextInt;
System.out.printEnter the item quantity: ;
itemsetQuantityscnrnextInt;
Calculate total costs
int totalCostItem itemgetPrice itemgetQuantity;
int totalCostItem itemgetPrice itemgetQuantity;
int totalCost totalCostItem totalCostItem;
Output the total cost breakdown
System.out.println
TOTAL COST";
System.out.printlnitemgetName itemgetQuantity @ $ itemgetPrice $ totalCostItem;
System.out.printlnitemgetName itemgetQuantity @ $ itemgetPrice $ totalCostItem;
System.out.printlnTotal: $ 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 ;
this.itemQuantity ;
Mutator methods
public void setNameString name
this.itemName name;
public void setPriceint price
this.itemPrice price;
public void setQuantityint 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
