Advertisements
Advertisements
Question
An electronics shop has announced a special discount on the purchase of laptops as given below:
Category | Discount on Laptop |
Up to ₹ 25,000 | 5.0% |
₹ 25,001 − ₹ 50,000 | 7.5% |
₹ 50,001 − ₹ 1,00,000 | 10.0% |
More than ₹ 1,00,000 | 15.0% |
Define a class Laptop described as follows:
Data members/Instance variables: name, price, dis, amt;
Member methods:
- a parameterised constructor to initialize the data members.
- to accept the details (name of the customer and the price).
- to compute the discount.
- to display the name, discount and amount to be paid after discount.
Write a main method to create an object of the class and call the member methods.
Code Writing
Solution
import java.util.*;
public class Laptop
{ Scanner ob = new Scanner(System.in);
String name; 2
double price, dis, amt;
Laptop (String a, double b, double c, double d)
{name = a;
price = b;
dis = c;
amt = d;
}
void input()
{System.out.println("Enter the name:");
name = ob.nextLine();
System.out.println("Enter the price");
price = ob.nextFloat();
}
void discount()
{if (price <= 25000)
dis = 5.0 / 100;
else if (price >= 25001 && price <= 50000)
dis = 7.5 / 100;
else if (price >= 50001 && price <= 100000)
dis = 10.0 / 100;
else if (price > 100000)
dis = 15.0 / 100;
dis = price * dis; // calculate discount
amt = price - dis; // amount after discount
}
void display()
{System.out.println("***Output***");
System.out.println("Name: " + name);
System.out.println("Discount: " + dis);
System.out.println("Amount Payable: " + amt);
}
public static void main(String args[])
{Laptop L = new Laptop ("", 0.0, 0.0, 0.0);
L.input();
L.discount();
L.display();
}}
shaalaa.com
Is there an error in this question or solution?