Question

In: Computer Science

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

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

Item 1

Item = Chocolate Chips, Price = 3, Quantity =  1

Item 2

Item = Bottled Water, Price = 1, Quantity = 10

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


TOTAL COST Chocolate Chips 1 @ $3 = $3

Bottled Water 10 @ $1 = $10

Total: $13

Solutions

Expert Solution


package items;
import java.util.*;
public class ItemToBuy//public class
{
    public String itemName ;//data members
    public int itemPrice;
    public int itemQuantity;
    Scanner sc=new Scanner(System.in);
    public ItemToBuy()//functions
    {
        itemName="none";
        itemPrice=0;
        itemQuantity=0;
    }
    public void setName()
    {
        
        itemName=sc.nextLine();
    }
    public String getname()
    {
        return itemName;
    }
    public void setprice()
    {   
      
        itemPrice=sc.nextInt();
    }
    public int getprice()
    {
        return itemPrice;
    }
    public void setquantity()
    {
        itemQuantity=sc.nextInt();
    }


    public int getquantity()
    {
        return itemQuantity;
    }
}

save this file make it a packge by

javac -d . ItemToBuy.java

this above command in command prompt

import items.ItemToBuy;//imporing the class 
class ShoppingCartDriver
{
    public static void main(String[] args) {
        ItemToBuy obj=new ItemToBuy();//making the 2 objects
        ItemToBuy obj1=new ItemToBuy();
        System.out.println("item 1");//input from the user
        System.out.println("enter name");
        obj.setName();
        System.out.println("enter price");
        obj.setprice();
        System.out.println("enter quantity");
        obj.setquantity();
        System.out.println("item 2");
        System.out.println("enter name");
        obj1.setName();
        System.out.println("enter price");
        obj1.setprice();
        System.out.println("enter quantity");
        obj1.setquantity();
        System.out.println("Total cost"+obj.getname()+obj.getquantity()+"@ $"+obj.getprice());
        int i1=obj.getprice()*obj.getquantity();
        System.out.println(i1);
        System.out.println("Total cost"+obj1.getname()+obj1.getquantity()+"@ $"+obj1.getprice());
        int i2=obj1.getprice()*obj1.getquantity();
        System.out.println(i2);
        int total=i1+i2;//total price calculation
        System.out.println("total= $"+total);
        



    }
}

output:

item 1
enter name
Chocolate Chips
enter price
1
enter quantity
3
item 2
enter name
Bottled Water
enter price
1
enter quantity
3
Total costChocolate Chips3@ $1
3
Total costBottled Water3@ $1
3
total= $6


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...
(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)...
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....
C (1) Create three files to submit: ItemToPurchase.h - Struct definition and related function declarations ItemToPurchase.c...
C (1) Create three files to submit: ItemToPurchase.h - Struct definition and related function declarations ItemToPurchase.c - Related function definitions main.c - main() function Build the ItemToPurchase struct with the following specifications: Data members (3 pts) char itemName [ ] int itemPrice int itemQuantity Related functions MakeItemBlank() (2 pts) Has a pointer to an ItemToPurchase parameter. Sets item's name = "none", item's price = 0, item's quantity = 0 PrintItemCost() Has an ItemToPurchase parameter. Ex. of PrintItemCost() output: Bottled Water...
Create and submit a Python program (in a module) that contains a main function that prints...
Create and submit a Python program (in a module) that contains a main function that prints 17 lines of the following text containing your name: Welcome to third week of classes at College, <your name here>! can someone please show me how to do this step by step? I'm just confused and keep getting errors in idle.
Java program Create two classes based on the java code below. One class for the main...
Java program Create two classes based on the java code below. One class for the main method (named InvestmentTest) and the other is an Investment class. The InvestmentTest class has a main method and the Investment class consists of the necessary methods and fields for each investment as described below. 1.The Investment class has the following members: a. At least six private fields (instance variables) to store an Investment name, number of shares, buying price, selling price, and buying commission...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
Objectives: Use class inheritance to create new classes. Separate class definition and implementation in different files....
Objectives: Use class inheritance to create new classes. Separate class definition and implementation in different files. Use include guard in class header files to avoid multiple inclusion of a header. Tasks: In our lecture, we wrote a program that defines and implements a class Rectangle. The source code can be found on Blackboard > Course Content > Classes and Objects > Demo Program 2: class Rectangle in three files. In this lab, we will use class inheritance to write a...
submit the following files: Main.java Consider the following code: import java.util.Scanner; public class Main { public...
submit the following files: Main.java Consider the following code: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String input = ""; System.out.println("Enter the first number: "); input = in.nextLine(); int a = 0; int b = 0; a = Integer.parseInt(input); System.out.println("Enter the second number: "); input = in.nextLine(); b = Integer.parseInt(input); int result = 0; result = a/b; System.out.println("a/b : " + result); } Copy the code to your Main.java file...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT