हिंदी

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

प्रश्न

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.
कोड लेखन
Advertisements

उत्तर

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
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 4: Constructors - Exercises [पृष्ठ ११६]

APPEARS IN

रुपा पंडित Computer Applications [English] Class 10 ICSE
अध्याय 4 Constructors
Exercises | Q 4. (c) | पृष्ठ ११६
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×