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...
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...
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.
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
Part A Java netbeans ☑ Create a project and in it a class with a main....
Part A Java netbeans ☑ Create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class , after the package statement, paste import java.util.Scanner; As the first line inside your main method, paste Scanner scan = new Scanner(System.in); Remember when you are getting a value from the user, first print a request, then use the Scanner to read the right type...
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT