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

in case of further query please type in comment box.
A grocery list is provided below: Create three different classes named Milk. Bread, and Eggs, where...
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 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 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 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) 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 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 (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 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 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, 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,...