English

Write a program for the following. Define a class Annual, described as below: Data Members: costPrice (int), sellPrice (int), profit (int), profitPct (double) Member Methods: (i) Annual

Advertisements
Advertisements

Question

Write a program for the following.

Define a class Annual, described as below:

Data Members:
costPrice (int), sellPrice (int), profit (int), profitPct (double)
Member Methods:
(i) Annual(int cp, int sp) constructor to initialize member data CostPrice to cp and SellPrice to sp and rest to null.
(ii) Calculate() to calculate the profit and profitPct using the formula profit = sellPrice - costPrice and
(iii) Display() to display all the member data.
Write a main() method to create an object of the class and call the above member methods.
Code Writing
Advertisements

Solution

class Annual
{
    int costPrice, sellPrice, profit;
    double profitPct;
    //Parameterized Constructor
    Annual(int cp, int sp)
    {
        costPrice = cp;
        sellPrice = sp;
        profit = 0;
        profitPct = 0.0;
    }
    //Calculate method
    void Calculate()
    {
        profit = sellPrice - costPrice;
        profitPct = (profit * 100.0) / costPrice;
    }
    //Display method
    void Display()
    {
        System.out.println("Cost Price =" + costPrice);
        System.out.println("Selling Price =" + sellPrice);
        System.out.println("Profit = " + profit);
        System.out.println("Profit Percentage =" + profitPct);
    }
    //Main method
    public static void main(String args[])
    {
        Annual obj = new Annual(5000, 6500);
        obj.Calculate();
        obj.Display();
    }
}
shaalaa.com
  Is there an error in this question or solution?
Chapter 4: Constructors - Exercises [Page 116]

APPEARS IN

Rupa Pandit Computer Applications [English] Class 10 ICSE
Chapter 4 Constructors
Exercises | Q 4. (c) | Page 116
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×