Computer Applications

An electronics shop has announced a special discount on the purchase of Laptops as given below:

CategoryDiscount on Laptop
Up to ₹25,0005.0%
₹25,001 - ₹50,0007.5%
₹50,001 - ₹1,00,00010.0%
More than ₹1,00,00015.0%

Define a class Laptop described as follows:

Data members/instance variables:

  1. name
  2. price
  3. dis
  4. amt

Member Methods:

  1. A parameterised constructor to initialize the data members
  2. To accept the details (name of the customer and the price)
  3. To compute the discount
  4. 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.

Java

Java Constructors

106 Likes

Answer

import java.util.Scanner;

public class Laptop
{
    private String name;
    private int price;
    private double dis;
    private double amt;

    public Laptop(String s, int p)
    {
        name = s;
        price = p;
    }

    public void compute() {
        if (price <= 25000)
            dis = price * 0.05;
        else if (price <= 50000)
            dis = price * 0.075;
        else if (price <= 100000)
            dis = price * 0.1;
        else
            dis = price * 0.15;
            
        amt = price - dis;
    }
    
    public void display() {
        System.out.println("Name: " + name);
        System.out.println("Discount: " + dis);
        System.out.println("Amount to be paid: " + amt);
    }
    
    public static void main(String args[]) {
        
        Scanner in = new Scanner(System.in);
        System.out.print("Enter Customer Name: ");
        String str = in.nextLine();
        System.out.print("Enter Price: ");
        int p = in.nextInt();
        
        Laptop obj = new Laptop(str,p);
        obj.compute();
        obj.display();
    }
}

Variable Description Table

Program Explanation

Output

Answered By

58 Likes


Related Questions