Question

In: Computer Science

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 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:
You entered: Chocolate_Chips

Enter the item price:
You entered: 3

Enter the item quantity:
You entered: 1



Item 2
Enter the item name:
You entered: Bottled_Water

Enter the item price:
You entered: 1

Enter the item quantity:
You entered: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

Solutions

Expert Solution

We will use the Scanner class for taking the input from the user.  

We will first write code for the first part, that is, for the ItemToPurchase.java file.

Please find all the explanation for the code within the code itself as comments.

import java.util.*;

public class ItemsToPurchase {
                
        // as specified in the problem statement, these are the private fields of the class
        private String itemName;
        private int itemPrice;
        private int itemQuantity;


        // default constructor
        public ItemsToPurchase () {
                this.itemName = "none";
                this.itemPrice = 0;
                this.itemQuantity = 0;
        }

        // as specified in the problem statement, these are the 
        // public member methods (mutators and accessors)

        // this function sets the name of the item
        public void setName (String itemName) {
                this.itemName = itemName;
        }

        // this function returns the name of the item
        public String getName () {
                return this.itemName;
        }

        // this function sets the price of the item
        public void setPrice (int itemPrice) {
                this.itemPrice = itemPrice;
        }

        // this function returns the price of the item
        public int getPrice () {
                return this.itemPrice;
        }

        // this function sets the quantity of the item
        public void setQuantity (int itemQuantity) {
                this.itemQuantity = itemQuantity;
        }

        // this function returns the quantity of the item
        public int getQuantity () {
                return this.itemQuantity;
        }
}

Now, for the second part, lets look at the ShoppingCartPrinter.java file.

In this file, we will prompt the user for the data of the ItemsToPurchase object and print what has been entered by the user like shown in the problem statement.

The code snippets are added for clarity. Please find the full code of ShoppingCartPrinter.java file at the end.

import java.util.*;

public class ShoppingCartPrinter {

        public static void main (String [] args) {
                        
                // FOR ITEM 1

                System.out.println ("Item 1");
                ItemsToPurchase i1 = new ItemsToPurchase ();

                // create an object of class Scanner to take the input from the user
                // the argument of the Scanner's constructor would be "System.in"
                // which specifies that from where the input has to be read. 
                // in our case, we are telling java that system input will be read from Standard input stream,
                // which is associated with keyboard
                Scanner scnr = new Scanner (System.in);

                // prompt the user for name
                System.out.print ("\nEnter the item name: ");

                // we will take the input using next() function of Scanner class
                String name = scnr.next();
                i1.setName (name);
                System.out.println ("You entered: " + i1.getName());


                // prompt the user for price
                System.out.print ("\nEnter the item price: ");

                // we will take the input using next() function of Scanner class
                int price = scnr.nextInt ();
                i1.setPrice (price);
                System.out.println ("You entered: " + i1.getPrice());


                // prompt the user for quantity
                System.out.print ("\nEnter the item quantity: ");

                // we will take the input using next() function of Scanner class
                int quantity = scnr.nextInt ();
                i1.setQuantity (quantity);
                System.out.println ("You entered: " + i1.getQuantity());


                // according to the problem statement, we need to call nextLine () once after taking the 
                // first item's input to allow the user to input a new string
                scnr.nextLine ();


                // FOR ITEM 2

                ItemsToPurchase i2 = new ItemsToPurchase ();

                // prompt the user for name
                System.out.println ("\n\nItem 2");
                System.out.print ("Enter the item name: ");

                // we will take the input using next() function of Scanner class
                name = scnr.next();
                i1.setName (name);
                System.out.println ("You entered: " + i1.getName());


                // prompt the user for price
                System.out.print ("\nEnter the item price: ");

                // we will take the input using next() function of Scanner class
                price = scnr.nextInt ();
                i1.setPrice (price);
                System.out.println ("You entered: " + i1.getPrice());


                // prompt the user for quantity
                System.out.print ("\nEnter the item quantity: ");

                // we will take the input using next() function of Scanner class
                quantity = scnr.nextInt ();
                i1.setQuantity (quantity);
                System.out.println ("You entered: " + i1.getQuantity());
        }
}


Now, the third part, we need to add the total cost so formed.

We can add the below code for that in our main function below the above code.

// printing the statements about the details of the shopping as mentioned in the problem statement

System.out.println ("\n\nTOTAL COST");
System.out.println (i1.getName () + " " + i1.getQuantity () + " @ $" + i1.getPrice () + " = " + "$" + (i1.getPrice () * i1.getQuantity ()));
System.out.println (i2.getName () + " " + i2.getQuantity () + " @ $" + i2.getPrice () + " = " + "$" + (i2.getPrice () * i2.getQuantity ()));

int item1Cost = (i1.getPrice () * i1.getQuantity ());
int item2Cost = (i2.getPrice () * i2.getQuantity ());

int totalCost = item1Cost + item2Cost;

System.out.println ("Total: $" + totalCost);

The full code is:

import java.util.*;

public class ShoppingCartPrinter {

        public static void main (String [] args) {
                        
                // FOR ITEM 1

                System.out.println ("Item 1");
                ItemsToPurchase i1 = new ItemsToPurchase ();

                // create an object of class Scanner to take the input from the user
                // the argument of the Scanner's constructor would be "System.in"
                // which specifies that from where the input has to be read. 
                // in our case, we are telling java that system input will be read from Standard input stream,
                // which is associated with keyboard
                Scanner scnr = new Scanner (System.in);

                // prompt the user for name
                System.out.print ("\nEnter the item name: ");

                // we will take the input using next() function of Scanner class
                String name = scnr.next();
                i1.setName (name);
                System.out.println ("You entered: " + i1.getName());


                // prompt the user for price
                System.out.print ("\nEnter the item price: ");

                // we will take the input using next() function of Scanner class
                int price = scnr.nextInt ();
                i1.setPrice (price);
                System.out.println ("You entered: " + i1.getPrice());


                // prompt the user for quantity
                System.out.print ("\nEnter the item quantity: ");

                // we will take the input using next() function of Scanner class
                int quantity = scnr.nextInt ();
                i1.setQuantity (quantity);
                System.out.println ("You entered: " + i1.getQuantity());


                // according to the problem statement, we need to call nextLine () once after taking the 
                // first item's input to allow the user to input a new string
                scnr.nextLine ();


                ItemsToPurchase i2 = new ItemsToPurchase ();

                // prompt the user for name
                System.out.println ("\n\nItem 2");
                System.out.print ("Enter the item name: ");

                // we will take the input using next() function of Scanner class
                name = scnr.next();
                i2.setName (name);
                System.out.println ("You entered: " + i2.getName());


                // prompt the user for price
                System.out.print ("\nEnter the item price: ");

                // we will take the input using next() function of Scanner class
                price = scnr.nextInt ();
                i2.setPrice (price);
                System.out.println ("You entered: " + i2.getPrice());


                // prompt the user for quantity
                System.out.print ("\nEnter the item quantity: ");

                // we will take the input using next() function of Scanner class
                quantity = scnr.nextInt ();
                i2.setQuantity (quantity);
                System.out.println ("You entered: " + i2.getQuantity());


        // CALCULATING THE TOTAL COST

                // printing the statements about the details of the shopping as mentioned in the problem statement

                System.out.println ("\n\nTOTAL COST");
                System.out.println (i1.getName () + " " + i1.getQuantity () + " @ $" + i1.getPrice () + " = " + "$" + (i1.getPrice () * i1.getQuantity ()));
                System.out.println (i2.getName () + " " + i2.getQuantity () + " @ $" + i2.getPrice () + " = " + "$" + (i2.getPrice () * i2.getQuantity ()));

                int item1Cost = (i1.getPrice () * i1.getQuantity ());
                int item2Cost = (i2.getPrice () * i2.getQuantity ());

                int totalCost = item1Cost + item2Cost;

                System.out.println ("Total: $" + totalCost);
                
        }
}


Please refer to the screenshots of the code for understanding the indentation of the code.

ItemsToPurchase.java file

ShoppingCartPrinter.java file

For the given input, the output of the code will be:


Related Solutions

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...
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()...
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()...
(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)...
Java Class Create a class with a main method. Write code including a loop that will...
Java Class Create a class with a main method. Write code including a loop that will display the first n positive odd integers and compute and display their sum. Read the value for n from the user and display the result to the screen.
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....
Create a new class called Account with a main method that contains the following: • A...
Create a new class called Account with a main method that contains the following: • A static variable called numAccounts, initialized to 0. • A constructor method that will add 1 to the numAccounts variable each time a new Account object is created. • A static method called getNumAccounts(). It should return numAccounts. Test the functionality in the main method of Account by creating a few Account objects, then print out the number of accounts
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class,...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. -Add a method addToCollection. In this method, add one to collected...
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT