Menu
Topics Index
...
`

Icecream Prices - Java Example Program


Icecream Prices
class IcecreamPrices
{
    public static void main(String arg[])
    {
        IceCream ic = new IceCream();
        ic.flavor = "Pista";
        ic.numberOfScoops = 2;
        
        System.out.println(ic.numberOfScoops + " scoops of " + ic.flavor + " flavor price is : " + ic.getIceCreamPrice());
        
        FruitSaladWithIceCream fs = new FruitSaladWithIceCream();
        fs.flavor = "Chocolate";
        fs.numberOfScoops = 1;
        fs.gramsOfFruitSalad = 50;
        
        System.out.print(fs.gramsOfFruitSalad + " grams of fruit salad and ");
        System.out.println(fs.numberOfScoops + " scoops of " + fs.flavor + " flavor price is : " + fs.getFruitSaladWithIceCreamPrice());
        
        KhubaniKaMeetaWithIceCream kkm = new KhubaniKaMeetaWithIceCream();
        kkm.flavor = "Vanila";
        kkm.numberOfScoops = 1;
        kkm.gramsOfKhubaniKaMeeta = 75;
        
        System.out.print(kkm.gramsOfKhubaniKaMeeta + " grams of khubani ka meeta and ");
        System.out.println(kkm.numberOfScoops + " scoops of " + kkm.flavor + " flavor price is : " + kkm.getKhubaniKaMeetaWithIceCreamPrice());
    
    }
}

class IceCream
{
    String flavor;
    int numberOfScoops;
    
    double getIceCreamPrice()
    {
        double pricePerScoop = 35.0;
        return numberOfScoops * pricePerScoop;
    }
}

class FruitSaladWithIceCream extends IceCream
{
    int gramsOfFruitSalad;

    double getFruitSaladWithIceCreamPrice()
    {
        double iceCreamPrice = getIceCreamPrice();
        double pricePerGram = 0.75;

        return gramsOfFruitSalad * pricePerGram + iceCreamPrice;
    }
}

class KhubaniKaMeetaWithIceCream extends IceCream
{
    int gramsOfKhubaniKaMeeta;

    double getKhubaniKaMeetaWithIceCreamPrice()
    {
        double iceCreamPrice = getIceCreamPrice();
        double pricePerGram = 1.25;

        return gramsOfKhubaniKaMeeta * pricePerGram + iceCreamPrice;
    }
}
OUTPUT

2 scoops of Pista flavor price is : 70.0
50 grams of fruit salad and 1 scoops of Chocolate flavor price is : 72.5
75 grams of khubani ka meeta and 1 scoops of Vanila flavor price is : 128.75

DESCRIPTION

Here we have created three classes - IceCream, FruitSaladWithIceCream and KhubaniKaMeetaWithIceCream and implemented the methods which provide the price of each item. The method getIceCreamPrice in IceCream class gives the price of the ice cream. The method getFruitSaladWithIceCreamPrice in FruitSaladWithIceCream class gives the price of ice cream and fruit salad. This method internally calls getIceCreamPrice to get the ice cream price and adds it to the price of the fruit salad. Similarly the method getKhubaniKaMeetaWithIceCreamPrice in KhubaniKaMeetaWithIceCream class gives the price of khubani ka meeta and ice cream.

THINGS TO TRY
  • Define a new class BadamMilkWithIceCream and add member variable milliLitersOfBadamMilk and add a method getBadamMilkWithIceCreamPrice. Assume that the price of milli liter of badam milk is Rs. 0.20.

© meritcampus 2019

All Rights Reserved.

Open In App