- Home
- Chapter 1 - Object Oriented Programming Concepts
- Object Oriented Programming Concepts
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 2 - Introduction to Java
- Introduction to Java
- Multiple Choice Questions
- Assignment Questions
- Chapter 3 - Values and Data Types
- Values and Data Types
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 4 - Operators in Java
- Operators in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 5 - User-Defined Methods
- User-Defined Methods
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 6 - Input in Java
- Input in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 7 - Mathematical Library Methods
- Mathematical Library Methods
- Multiple Choice Questions
- Assignment Questions
- Chapter 8 - Conditional Constructs in Java
- Conditional Constructs in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 9 - Iterative Constructs in Java
- Iterative Constructs in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions and Programs
- Chapter 10 - Nested for loops
- Nested for loops
- Assignment Questions and Programs
- Chapter 11 - Constructors
- Constructors
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 12 - Library Classes
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 13 - Encapsulation and Inheritance
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 14 - Arrays
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 15 - String Handling
- Library Classes
- Multiple Choice Questions
- Assignment Questions
Constructors in Java
Chapter 11
Constructors in Java
Class 10 - Logix Kips ICSE Computer Applications with BlueJ
![]() Share with a Friend |
Java Program: Create a Pizza Class with Constructor and Cost Calculation
Create a class named Pizza that stores details about a pizza. It should contain the following:
Instance Variables:
String pizzaSize — to store the size of the pizza (small, medium, or large)
int cheese — the number of cheese toppings
int pepperoni — the number of pepperoni toppings
int mushroom — the number of mushroom toppings
Member Methods:
Constructor — to initialise all the instance variables
CalculateCost() — A public method that returns a double value, that is, the cost of the pizza.
Pizza cost is calculated as follows:
- Small: Rs.500 + Rs.25 per topping
- Medium: Rs.650 + Rs.25 per topping
- Large: Rs.800 + Rs.25 per topping
PizzaDescription() — A public method that returns a String containing the pizza size, quantity of each topping, and the pizza cost as calculated by CalculateCost().
Program Type 1: Java Program to Create a Pizza Class with Constructor and Cost Calculation
public class Pizza
{
String pizzaSize;
int cheese;
int pepperoni;
int mushroom;
public Pizza(String size,
int tc,
int tp,
int tm) {
pizzaSize = size;
cheese = tc;
pepperoni = tp;
mushroom = tm;
}
public double CalculateCost() {
double totalCost = 0;
double topCost = 25 * (cheese + pepperoni + mushroom);
if (pizzaSize == "small")
totalCost = 500 + topCost;
else if(pizzaSize == "medium")
totalCost = 650 + topCost;
else
totalCost = 800 + topCost;
return totalCost;
}
public String PizzaDescription() {
double cost = CalculateCost();
String desc = "Size: " + pizzaSize
+ " Cheese: " + cheese
+ " Pepperoni: " + pepperoni
+ " Mushroom: " + mushroom
+ " Cost: Rs. " + cost;
return desc;
}
public static void main(String args[]) {
Pizza p = new Pizza("medium", 15, 5, 10);
String desc = p.PizzaDescription();
System.out.println(desc);
}
}
Output
Sample Output Size: medium Cheese: 15 Pepperoni: 5 Mushroom: 10 Cost: Rs. 1400.0
Program Type 2: Pizza Class with Constructor & Cost Calculation
import java.util.Scanner;
public class Pizza
{
// Instance Variables
String pizzaSize;
int cheese;
int pepperoni;
int mushroom;
// Constructor
public Pizza(String size, int cheeseToppings, int pepperoniToppings, int mushroomToppings)
{
pizzaSize = size.toLowerCase();
cheese = cheeseToppings;
pepperoni = pepperoniToppings;
mushroom = mushroomToppings;
}
// Method to calculate cost
public double CalculateCost()
{
double basePrice = 0;
if(pizzaSize.equals("small"))
basePrice = 500;
else if(pizzaSize.equals("medium"))
basePrice = 650;
else if(pizzaSize.equals("large"))
basePrice = 800;
else
{
System.out.println("Invalid Pizza Size!");
return 0;
}
int totalToppings = cheese + pepperoni + mushroom;
return basePrice + (25 * totalToppings);
}
// Method to return description
public String PizzaDescription()
{
return "Pizza Size: " + pizzaSize +
"\nCheese Toppings: " + cheese +
"\nPepperoni Toppings: " + pepperoni +
"\nMushroom Toppings: " + mushroom +
"\nTotal Cost: Rs." + CalculateCost();
}
// Main Method (Single Program)
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Pizza Size (Small / Medium / Large): ");
String size = sc.next();
System.out.println("Enter number of Cheese toppings: ");
int cheese = sc.nextInt();
System.out.println("Enter number of Pepperoni toppings: ");
int pepperoni = sc.nextInt();
System.out.println("Enter number of Mushroom toppings: ");
int mushroom = sc.nextInt();
// Create Pizza Object
Pizza p = new Pizza(size, cheese, pepperoni, mushroom);
// Display Details
System.out.println("\n----- Pizza Details -----");
System.out.println(p.PizzaDescription());
sc.close();
}
}
Output
Sample Output Enter Pizza Size (Small / Medium / Large): Medium Enter number of Cheese toppings: 2 Enter number of Pepperoni toppings: 1 Enter number of Mushroom toppings: 3 ----- Pizza Details ----- Pizza Size: medium Cheese Toppings: 2 Pepperoni Toppings: 1 Mushroom Toppings: 3 Total Cost: Rs.800.0
