Question

In: Computer Science

I need this code in java. Task (1) Create two files to submit: ItemToPurchase.java - Class...

I need this code in java.

Task

(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. (2 pts)

Ex:

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


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

Ex:

TOTAL COST
Chocolate Chips 1 @ $3 = $3
Bottled Water 10 @ $1 = $10

Total: $13

This is the given code so far:

import java.util.Scanner;

public class ShoppingCartPrinter {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int i = 0;
String productName;
int productPrice = 0;
int productQuantity = 0;
int cartTotal = 0;
  
// TODO Create new item1 and new item2

// Prompt for item 1 details from user, create itemToPurchase object
System.out.println("Item 1");
System.out.println("Enter the item name:");
productName = scnr.nextLine();
  
System.out.println("Enter the item price:");
productPrice = scnr.nextInt();
  
System.out.println("Enter the item quantity:");
productQuantity = scnr.nextInt();
System.out.println("");
  
// TODO: Set item1 fields here
  

// Promptr for item 2 details from user, create itemToPurchase object
System.out.println("Item 2");
System.out.println("Enter the item name:");
scnr.nextLine(); // DO NOT OMIT THIS LINE
productName = scnr.nextLine();
  
System.out.println("Enter the item price:");
productPrice = scnr.nextInt();
  
System.out.println("Enter the item quantity:");
productQuantity = scnr.nextInt();
System.out.println("");
  
// TODO set item2 here

  
// Add costs of two items and print total
cartTotal = (item1.getPrice() * item1.getQuantity()) +
(item2.getPrice() * item2.getQuantity());
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("");
System.out.println("Total: $" + cartTotal);
  
return;
}
}

Solutions

Expert Solution

ItemToPurchase.java :

//Java class
public class ItemToPurchase {
   //private variables
   private String itemName;
   private int itemPrice;
   private int itemQuantity;
   //Constructor
   public ItemToPurchase() {
       itemName = "none";
       itemPrice = 0;
       itemQuantity = 0;
   }
   //getter method
   public String getName() {
       return itemName;
   }
   //setter method
   public void setName(String in) {
       this.itemName = in;
   }
   public int getPrice() {
       return itemPrice;
   }

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

   public int getQuantity() {
       return itemQuantity;
   }

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

}

========================

ShoppingCartPrinter.java :

package sample;

import java.util.Scanner;//java class

public class ShoppingCartPrinter {
   public static void main(String[] args) {
       Scanner scnr = new Scanner(System.in);
       int i = 0;
       String productName;
       int productPrice = 0;
       int productQuantity = 0;
       int cartTotal = 0;

// TODO Create new item1 and new item2
       ItemToPurchase item1 = new ItemToPurchase();
       ItemToPurchase item2 = new ItemToPurchase();
// Prompt for item 1 details from user, create itemToPurchase object
       System.out.println("Item 1");
       System.out.println("Enter the item name:");
       productName = scnr.nextLine();

       System.out.println("Enter the item price:");
       productPrice = scnr.nextInt();

       System.out.println("Enter the item quantity:");
       productQuantity = scnr.nextInt();
       System.out.println("");

// TODO: Set item1 fields here
       item1.setName(productName);
       item1.setPrice(productPrice);
       item1.setQuantity(productQuantity);
// Promptr for item 2 details from user, create itemToPurchase object
       System.out.println("Item 2");
       System.out.println("Enter the item name:");
       scnr.nextLine(); // DO NOT OMIT THIS LINE
       productName = scnr.nextLine();

       System.out.println("Enter the item price:");
       productPrice = scnr.nextInt();

       System.out.println("Enter the item quantity:");
       productQuantity = scnr.nextInt();
       System.out.println("");

// TODO set item2 here
       item2.setName(productName);
       item2.setPrice(productPrice);
       item2.setQuantity(productQuantity);
// Add costs of two items and print total
       cartTotal = (item1.getPrice() * item1.getQuantity()) + (item2.getPrice() * item2.getQuantity());
       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("");
       System.out.println("Total: $" + cartTotal);

       return;
   }
}
**************************************

Output :


Related Solutions

In Java: (1) Create two files to submit: ItemToBuy.java - Class definition ShoppingCartDriver.java - Contains main()...
In Java: (1) Create two files to submit: ItemToBuy.java - Class definition ShoppingCartDriver.java - Contains main() method Build the ItemToBuy class with the following specifications: Private fields String itemName - Initialized in the nor-arg constructor to "none" int itemPrice - Initialized in default constructor to 0 int itemQuantity - Initialized in default constructor to 0 No-arg Constructor (set the String instance field to "none") Public member methods (mutators & accessors) setName() & getName() setPrice() & getPrice() setQuantity() & getQuantity() toString()...
JAVA (1) Create two files to submit: Payroll.java - Class definition PayrollClient.java - Contains main() method...
JAVA (1) Create two files to submit: Payroll.java - Class definition PayrollClient.java - Contains main() method Build the Payroll class with the following specifications: 4 private fields String name - Initialized in default constructor to "John Doe" int ID - Initialized in default constructor to 9999 doulbe payRate - Initialized in default constructor to 15.0 doulbe hrWorked - Initialized in default constructor to 40 2 constructors (public) Default constructor A constructor that accepts the employee’s name, ID, and pay rate...
JAVA (1) Create two files to submit: ItemToPurchase.java - Class definition ShoppingCartPrinter.java - Contains main() method...
JAVA (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...
HOMEWORK PROJECT #1 – SHOPPING CART Part I. Create two files to submit: ItemToPurchase.java – Class...
HOMEWORK PROJECT #1 – SHOPPING CART Part I. Create two files to submit: ItemToPurchase.java – Class Definition ShoppingCartPrinter.java – Contains main() method Build the ItemToPurchase class with the following specifications: Specifications Description ItemToPurchase(itemName) itemName – The name will be a String datatype and Initialized in default constructor to “none”. ItemToPurchase(itemPrice) itemPrice – The price will be integer datatype and Initialized in default constructor to 0. ItemToPurchase(itemQuantity) itemQuantity – The quantity will be integer datatype Initialized in default constructor to 0....
(1) Create three files to submit. ContactNode.h - Class declaration ContactNode.cpp - Class definition main.cpp -...
(1) Create three files to submit. ContactNode.h - Class declaration ContactNode.cpp - Class definition main.cpp - main() function (2) Build the ContactNode class per the following specifications: Parameterized constructor. Parameters are name followed by phone number. Public member functions InsertAfter() (2 pts) GetName() - Accessor (1 pt) GetPhoneNumber - Accessor (1 pt) GetNext() - Accessor (1 pt) PrintContactNode() Private data members string contactName string contactPhoneNum ContactNode* nextNodePtr Ex. of PrintContactNode() output: Name: Roxanne Hughes Phone number: 443-555-2864 (3) In main(),...
(1) Create three files to submit: ItemToPurchase.h - Class declaration ItemToPurchase.cpp - Class definition main.cpp -...
(1) Create three files to submit: ItemToPurchase.h - Class declaration ItemToPurchase.cpp - Class definition main.cpp - main() function Build the ItemToPurchase class with the following specifications: Default constructor Public class functions (mutators & accessors) SetName() & GetName() (2 pts) SetPrice() & GetPrice() (2 pts) SetQuantity() & GetQuantity() (2 pts) Private data members string itemName - Initialized in default constructor to "none" int itemPrice - Initialized in default constructor to 0 int itemQuantity - Initialized in default constructor to 0 (2)...
In java program format Submit your completed UML class diagram and Java file. Part I: Create...
In java program format Submit your completed UML class diagram and Java file. Part I: Create a UML diagram for this assignment PartII: Create a program that implements a class called  Dog that contains instance data that represent the dog's name and age.   define the Dog constructor to accept and initialize instance data.   create a method to compute and return the age of the dog in "person-years" (note: dog age in person-years is seven times a dog's age).   Include a toString...
In java program format Submit your completed UML class diagram and Java file. Part I: Create...
In java program format Submit your completed UML class diagram and Java file. Part I: Create a UML diagram for this assignment PartII: Create a program that implements a class called Dog that contains instance data that represent the dog's name and age. define the Dog constructor to accept and initialize instance data. create a method to compute and return the age of the dog in "person-years" (note: dog age in person-years is seven times a dog's age). Include a...
I need this in java on textpad. There are two files, both have instructions in them...
I need this in java on textpad. There are two files, both have instructions in them on what to add in the code. They are posted below. public class MyArrayForDouble { double[] nums; int numElements; public MyArrayForDouble() { // Constructor. automatically called when creating an instance numElements = 0; nums = new double[5]; } public MyArrayForDouble(int capacity) { // Constructor. automatically called when creating an instance numElements = 0; nums = new double[capacity]; } public MyArrayForDouble(double[] nums1) { nums =...
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to...
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to use all methods, also switch statements. Write a Temperature class. The class will have three conversion methods: toCelsius(), toKelvin() and toFahrenheit(). These methods will return a Temperature in those three scales equal to the this temperature. Note that the value of this is not changed in these conversions. In addition to these three conversion methods the class will have methods add(Temperature), subtract(Temperature), multiply(Temperature), and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT