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

JAVA CODING Needs to compilable and match the examples exactly. (1) Create two f

ID: 3780062 • Letter: J

Question

JAVA CODING

Needs to compilable and match the examples exactly.

(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 0

int 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.

Ex:


(3) Add the costs of the two items together and output the total cost.

Ex:

*****************************************************************************************************************************************************************************************
Here is my code that DID NOT work/compile.

ItemToPurchase.java

public class ItemToPurchase{
private String itemName;
private int itemPrice;
private int itemQuantity;

public ItemToPurchase(){
this.itemName="none";
this.itemPrice=0;
this.itemQuantity=0;
}
public void setname(String itemName){
this.itemName=itemName;
}
public String getName(){
return this.itemName;
}
public void setPrice(int itemPrice){
this.itemPrice=itemPrice;
}
public int getPrice(){
return this.itemPrice;
}
public void setQuantity(int itemQuantity){
this.itemQuantity=itemQuantity;
}
public int getQuantity(){
return this.itemQuantity;
}
}

ShoppingCartPrinter.java

import java.util.*;
public class ShoppingCartPrinter{

public static void main(String []args){
Scanner sc=new Scanner(System.in);
ItemToPurchase item1=new ItemToPurchase();
ItemToPurchase item2=new ItemToPurchase();
System.out.println("Item 1");
System.out.print("Enter the item name:");
item1.setName(sc.nextLine());
System.out.print("Enter the item price:");
item1.setPrice(sc.nextInt());
sc.nextLine();
System.out.print("Enter the item quantity:");
item1.setQuantity(sc.nextInt());
sc.nextLine();
System.out.println("Item 2");
System.out.print("Enter the item name:");
item2.setName(sc.nextLine());
System.out.print("Enter the item price:");
item2.setPrice(sc.nextInt());
sc.nextLine();
System.out.print("Enter the item quantity:");
item2.setQuantity(sc.nextInt());
sc.nextLine();

System.out.println("TOTAL COST");
System.out.println(item1.getName()+" "+item1.getQuantity()+"@ $"+item1.getPrice()+" = $"+(item1.getPrice()*item1.getQuantity()));
System.out.println(item2.getName()+" "+item2.getQuantity()+"@ $"+item2.getPrice()+" = $"+(item2.getPrice()*item2.getQuantity()));
System.out.println("Total:$"+(item1.getPrice()*item1.getQuantity()+item2.getPrice()*item2.getQuantity()));
}
}

Explanation / Answer

ItemToPurchase.java

public class ItemToPurchase {
   //Declaring instance variables
   private String itemName;
   private int itemPrice;
   private int itemQuantity;

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

   //setters and getters
   public void setName(String itemName) {
       this.itemName = itemName;
   }

   public String getName() {
       return this.itemName;
   }

   public void setPrice(int itemPrice) {
       this.itemPrice = itemPrice;
   }

   public int getPrice() {
       return this.itemPrice;
   }

   public void setQuantity(int itemQuantity) {
       this.itemQuantity = itemQuantity;
   }

   public int getQuantity() {
       return this.itemQuantity;
   }
}

____________________

ShoppingCartPrinter.java

import java.util.*;

public class ShoppingCartPrinter {
   public static void main(String[] args) {

       // Scanner class object is used to get the inputs entered by the user
       Scanner sc = new Scanner(System.in);

       // Creating the class objects to ItemToPurchase
       ItemToPurchase item1 = new ItemToPurchase();
       ItemToPurchase item2 = new ItemToPurchase();

       // Getting the Item name entered by the user
       System.out.println("Item 1");
       System.out.print("Enter the item name:");
       item1.setName(sc.nextLine());

       // getting the item price entered by the user
       System.out.print("Enter the item price:");
       item1.setPrice(sc.nextInt());
       sc.nextLine();

       // getting the item quantity entered by the user
       System.out.print("Enter the item quantity:");
       item1.setQuantity(sc.nextInt());
       sc.nextLine();

       // Getting the Item name entered by the user
       System.out.println("Item 2");
       System.out.print("Enter the item name:");
       item2.setName(sc.nextLine());

       // getting the item price entered by the user
       System.out.print("Enter the item price:");
       item2.setPrice(sc.nextInt());
       sc.nextLine();

       // getting the item quantity entered by the user
       System.out.print("Enter the item quantity:");
       item2.setQuantity(sc.nextInt());
       sc.nextLine();

       // Calculating and displaying the total cost of all items
       System.out.println("TOTAL COST");
       System.out.println(item1.getName() + " " + item1.getQuantity() + "@ $"+ item1.getPrice() + " = $"+ (item1.getPrice() * item1.getQuantity()));
       System.out.println(item2.getName() + " " + item2.getQuantity() + "@ $"+ item2.getPrice() + " = $"+ (item2.getPrice() * item2.getQuantity()));
       System.out.println("Total:$"+ (item1.getPrice() * item1.getQuantity() + item2.getPrice()* item2.getQuantity()));
   }
}

________________________

Output:

Item 1
Enter the item name:Pens
Enter the item price:15
Enter the item quantity:25
Item 2
Enter the item name:Book
Enter the item price:40
Enter the item quantity:20
TOTAL COST
Pens 25@ $15 = $375
Book 20@ $40 = $800
Total:$1175

________Thank You