Object Oriented Programming Assignment
Object Oriented Programming Assignment
Object Oriented Programming Assignment
Hey! Now that you’re familiar with various object oriented concepts of Java, so it’s time to
polish our skills by doing an assignment.
Problem Statement: Mr. Sharma owns a bakery shop and is quite popular for baking
variety of cakes and pastries. Each day he comes up with Today’s Special Menu that
includes two special cakes and two special pastries. He prepares Today’s Special
Menu every day and puts it for the display. But with every passing day, it has become
painful for Mr. Sharma to prepare the hand written menu.
Let’s help Mr. Sharma by writing a small Java program that prints the menu in this
format.
Special Cakes
---------------------------------------------
Chocolate Brownie : ₹ 250.0 per pound Use For Each loop to
Chocolate Maple : ₹ 300.0 per pound print all cakes
Special Pastries
---------------------------------------------
Black Forest : ₹ 35.0 per piece Use For Each loop to
Choco Truffle : ₹ 40.0 per piece print all pastries
Approach: Follow the steps and complete the assignment
1. Let us consider Cake and Pastry. Both of these can be drafted as a separate model
classes. So lets create an empty class of Cake and Pastry.
• Firstly, within your project package create a class Cake.
• Secondly within your project package create another class Pastry.
Hint: To create a new class, right click on your package, go to New —> Java File and
enter your class name as Cake and Pastry one by one. Your project should look like this:
2. Next, we need to define properties of Cake and Pastry. Firstly what are the similarities
between these two? Well, a pastry is also a kind of Cake. So we can apply the concept of
inheritance.
• Make class Pastry extend properties of class Cake. So Cake becomes super class and
pastry becomes sub class or child class.
3. Now within class Cake define following properties as instance variables.
• Define name as String. [ A cake should have a name ]
• Define price as float. [ A cake should have a price ]
4. Define Getter and Setter within class Cake.
• To create Getter and Setter you can either write all methods manually or auto-generate
it as we did in the topic of Getter and Setter.
5. Define a display() method within class Cake that contains a print statement to print the
cake details in the following format:
6. Now, what about the code of class Pastry. Since, class Pastry inherits from class Cake so it
automatically gets all the instance variable as well as getters and setters methods. But
still we need to write some code within class Pastry
• Let’s override display() method from super class and modify the print statement to get
the output in this format:
Special Cakes
---------------------------------------------
10. It’s time to use for each loop and iterate through ArrayList of Cake. With each iteration call
the display() method. So this will print the following:
11. Finally, let’s write print statements for Pastries. Follow step number 9 and 10 for the same.
Henceforth, you will get this output:
Special Pastries
---------------------------------------------
Black Forest : ₹ 35.0 per piece Use For Each loop to
Choco Truffle : ₹ 40.0 per piece print all pastries
12. Lastly, run your code, verify the output and submit the project.