Question

1. A grocery list is provided below: Item Unit price Quantity Milk $4.75 Bread $2.50 Eggs $4.00 Create three different classes named Milk, Bread, and Eggs where each has the following members private data member unit price private data member quantity. Default and overloaded constructors Provide properties for the private data members public total price method which returns the total price (e.g., unit price x quantity) A ToStringo method that returns the info of the object Create a class named Grocery, which has the following members my milk of type Milk my bread of type Bread r my eggs of type Eggs An overloaded constructor which takes all the items above as inputs An expense method which calculates the total price of all items. This function must call the individual total price methods reside in the classes Milk, Bread, and Eggs A String( function Develop a driver main function to demonstrate all of the above
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

Just solved your problem as per your reuirement , please find the below code and also attached sample output.

I have wrriten the code in JAVA.

Milk.Java

public class Milk {

  
   private float item_price;
   private int quantity;
  
   //Default Constructor
   public Milk(){
          
       }
      
   //Parameterized constructor
   public Milk(float item_price, int quantity){
           this.item_price= item_price;
           this.quantity = quantity;
       }
  
   //setters and getters
   public float getItem_price() {
       return item_price;
   }
   public void setItem_price(float item_price) {
       this.item_price = item_price;
   }
   public int getQuantity() {
       return quantity;
   }
   public void setQuantity(int quantity) {
       this.quantity = quantity;
   }
   public float total_price(){
       return item_price*quantity;
   }
   @Override
   public String toString() {
       return "Milk [item_price=" + item_price + ", quantity=" + quantity + "]";
   }
  
   }

Bread.java

public class Bread {

  
   private float item_price;
   private int quantity;
  
   //Default constructor
  
   public Bread(){
          
       }
       //Parameterized constructor
   public Bread(float item_price,int quantity){
           this.item_price= item_price;
           this.quantity = quantity;
          
       }
   public float getItem_price() {
       return item_price;
   }
   public void setItem_price(float item_price) {
       this.item_price = item_price;
   }
   public int getQuantity() {
       return quantity;
   }
   public void setQuantity(int quantity) {
       this.quantity = quantity;
   }
   public float total_price(){
       return item_price*quantity;
   }
   @Override
   public String toString() {
       return "Milk [item_price=" + item_price + ", quantity=" + quantity + "]";
   }
  

}

Eggs.java

public class Eggs {

   private float item_price;
   private int quantity;
  
   //Default constructor
  
       public Eggs(){
              
           }
           //Parameterized constructor
       public Eggs(float item_price,int quantity){
               this.item_price= item_price;
               this.quantity = quantity;
              
           }
      
   public float getItem_price() {
       return item_price;
   }
   public void setItem_price(float item_price) {
       this.item_price = item_price;
   }
   public int getQuantity() {
       return quantity;
   }
   public void setQuantity(int quantity) {
       this.quantity = quantity;
   }
   public float total_price(){
       return item_price*quantity;
   }
   @Override
   public String toString() {
       return "Milk [item_price=" + item_price + ", quantity=" + quantity + "]";
   }
  

}

Grocery.java

public class Grocery {

  
   private Milk myMilk;
   private Bread myBread;
   private Eggs myEggs;
  
   public Grocery(Milk myMilk, Bread myBread, Eggs myEggs) {
      
       this.myMilk = myMilk;
       this.myBread = myBread;
       this.myEggs = myEggs;
   }
   public String toString(){
       System.out.println("Item \tUnit price \tQuantity");
       return "Milk \t$"+myMilk.getItem_price()+" \t\t"+myMilk.getQuantity()+
               "\nBread \t$"+myBread.getItem_price()+" \t\t"+myBread.getQuantity()+
               "\nEggs \t$"+myEggs.getItem_price()+" \t\t"+myEggs.getQuantity();
   }
   public void expense(){
      
       System.out.println("---------------------------------------------------");
       System.out.println("\nCalculating total price per individually item");
       System.out.println("\nItem \tTotal Price");
       System.out.println("Milk \t$"+myMilk.total_price());
       System.out.println("Bread \t$"+myBread.total_price());
       System.out.println("Eggs \t$"+myEggs.total_price());
      
       System.out.println("------------------------------------------------------");
       System.out.println("\nCalculating total expense including all items");
       System.out.println("Total Price \t$"+(myMilk.total_price()+myBread.total_price()+myEggs.total_price()));
      
   }
  
  

}

For demonstration of above written a Driver class

Driver.java

//Driver class to demonstrate functionality

public class Driver {

   public static void main(String[] args) {
       Milk milk = new Milk(3, 3);
       Bread bread = new Bread(4,6);
       Eggs eggs = new Eggs(5,9);
       Grocery gro = new Grocery(milk, bread, eggs);
       System.out.println(gro.toString());
       gro.expense();
   }

}

Output

Item Milk Bread $4.0 Eggs Unit price $3.0 Quantity $5.0 Calculating total price per individually item Total Price $9.0 Item M

in case of further query please type in comment box.

Add a comment
Know the answer?
Add Answer to:
A grocery list is provided below: Create three different classes named Milk. Bread, and Eggs, where...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help to create general class Classes Data Element - Ticket Create an abstract class called...

    Need help to create general class Classes Data Element - Ticket Create an abstract class called Ticket with: two abstract methods called calculateTicketPrice which returns a double and getld0 which returns an int instance variables (one of them is an object of the enumerated type Format) which are common to all the subclasses of Ticket toString method, getters and setters and at least 2 constructors, one of the constructors must be the default (no-arg) constructor. . Data Element - subclasses...

  • Help with this coding assignment for C++! Add any comments for better understanding. Create a class...

    Help with this coding assignment for C++! Add any comments for better understanding. Create a class named CupCake. It contains: • Has private instance variables 1. egg 2. butter 3. baking powder 4. sugar 5. flour 6. milk • All members must have public methods: getter() and setter(). • A constructor - a default constructor with no arguments. The constructor will initial all member variable to a default value 0. • One public pure virtual function showingredients() that returns void....

  • Create the header file named “Complex.h” that contains the following class: The class Complex represents a...

    Create the header file named “Complex.h” that contains the following class: The class Complex represents a complex number which is a number of the form a + bi where a and b are real numbers and i2 = −1. The class should contain: Private double field named real. Private double field named imaginary. Public default constructor that assigns 1 to real and 0 to imaginary. Public overloaded constructor that takes a double as a parameter named real. It assigns real...

  • Using C#: Create an application class named BillDemo that instantiates objects of two classes named Bill...

    Using C#: Create an application class named BillDemo that instantiates objects of two classes named Bill and OverdueBill, and that demonstrates all their methods. The Bill class includes auto-implemented properties for the name of the company or person to whom the bill is owed and for the amount due. Also, include a ToString() method and returns a string that contains the name of the class (using GetType()) and the Bill’s data fields values. Create a child class named OverdueBill that...

  • 1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files)...

    1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files) 1b. Add the following private member variables - a int variable named itemNumber - an int variable named quantity - a double variable named cost - a double variable named totalCost. Total cost is defined as quantity X cost. - updateTotalCost() which takes no arguments and sets the values of the variable totalCost. 1c. Add the following public functions: - Accessor and Mutators for...

  • Please submit a .cpp file or upload zip if you have more than one file before...

    Please submit a .cpp file or upload zip if you have more than one file before the time up. No library function is allowed. ***The code must contain your name and has proper format. Create a class named CupCake. It contains: • Has private instance variables 1. egg 2. butter 3. baking powder 4. sugar 5. flour 6. milk • All members must have public methods: getter() and setter(). • A constructor - a default constructor with no arguments. The...

  • using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship...

    using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship (all attributes private) String attribute for the name of ship float attribute for the maximum speed the of ship int attribute for the year the ship was built Add the following behaviors constructor(default and parameterized) , finalizer, and appropriate accessor and mutator methods Override the toString() method to output in the following format if the attributes were name="Sailboat Sally", speed=35.0f and year built of...

  • Using C# Create an application class named LetterDemo that instantiates objects of two classes named Letter...

    Using C# Create an application class named LetterDemo that instantiates objects of two classes named Letter and CertifiedLetter and that demonstrates all their methods. The classes are used by a company to keep track of letters they mail to clients. The Letter class includes auto-implemented properties for the Name of the recipient and the Date mailed (stored as strings). Next, include a ToString() method that overrides the Object class’s ToString() method and returns a string that contains the name of...

  • In this assignment, you will implement Address and Residence classes. Create a new java project. Part...

    In this assignment, you will implement Address and Residence classes. Create a new java project. Part A Implementation details of Address class: Add and implement a class named Address according to specifications in the UML class diagram. Data fields: street, city, province and zipCode. Constructors: A no-arg constructor that creates a default Address. A constructor that creates an address with the specified street, city, state, and zipCode Getters and setters for all the class fields. toString() to print out all...

  • Create class definitions using the following: Classes: Circle, Cube, Shape, Sphere, Square, ThreeDimensionalShape, TwoDimensionalShape Functions: Draw,...

    Create class definitions using the following: Classes: Circle, Cube, Shape, Sphere, Square, ThreeDimensionalShape, TwoDimensionalShape Functions: Draw, GetArea, GetDimensions, GetHeight, GetLength, GetSurfaceArea, GetVolume Members: height, length, width – all of type double Don’t actually write function implementations, just define the classes as you would in a header file. Use inheritance as appropriate. Use keywords such as “const” and “virtual” as appropriate. Make functions and members public, private, or protected as appropriate. Don’t forget constructors and destructors. Some classes may be abstract,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT