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
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
