Computer Applications

Define a class Discount having the following description:

Class name : Discount

Data MembersPurpose
int costto store the price of an article
String nameto store the customer's name
double dcto store the discount
double amtto store the amount to be paid
Member methodsPurpose
void input()Stores the cost of the article and name of the customer
void cal()Calculates the discount and amount to be paid
void display()Displays the name of the customer, cost, discount and amount to be paid

Write a program to compute the discount according to the given conditions and display the output as per the given format.

List PriceRate of discount
Up to ₹5,000No discount
From ₹5,001 to ₹10,00010% on the list price
From ₹10,001 to ₹15,00015% on the list price
Above ₹15,00020% on the list price

Output:

Name of the customer  Discount    Amount to be paid
....................  ........    .................
....................  ........    .................

Java

Java Classes

86 Likes

Answer

import java.util.Scanner;

public class Discount
{
    private int cost;
    private String name;
    private double dc;
    private double amt;
    
    public void input() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter customer name: ");
        name = in.nextLine();
        System.out.print("Enter article cost: ");
        cost = in.nextInt();
    }
    
    public void cal() {
        if (cost <= 5000)
            dc = 0;
        else if (cost <= 10000)
            dc = cost * 0.1;
        else if (cost <= 15000)
            dc = cost * 0.15;
        else
            dc = cost * 0.2;
            
        amt = cost - dc;
    }
    
    public void display() {
        System.out.println("Name of the customer\tDiscount\tAmount to be paid");
        System.out.println(name + "\t" + dc + "\t" + amt);   
    }
    
    public static void main(String args[]) {
        Discount obj = new Discount();
        obj.input();
        obj.cal();
        obj.display();
    }
}

Variable Description Table

Program Explanation

Output

Answered By

44 Likes


Related Questions