Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: ICSE JAVA PROGRAMS 2019: SECTION B QUESTION 4

Monday, December 28, 2020

ICSE JAVA PROGRAMS 2019: SECTION B QUESTION 4

Design a class name ShowRoom with the following description :

Instance variables/ Data members :

String name – To store the name of the customer

long mobno – To store the mobile number of the customer

double cost – To store the cost of the items purchased

double dis – To store the discount amount

double amount – To store the amount to be paid after discount

Member methods: –

ShowRoom() – default constructor to initialize data members

void input() – To input customer name, mobile number, cost

void calculate() – To calculate discount on the cost of purchased items, based on following criteria

 

Cost                                                                     Discount (in percentage)

Less than or equal to ₹ 10000                                             5%

More than ₹ 10000 and less than or equal to ₹ 20000      10%

More than ₹ 20000 and less than or equal to ₹ 35000     15%

More than ₹ 35000                                                                20%

 

void display() – To display customer name, mobile number, amount to be paid after discount

Write a main method to create an object of the class and call the above member methods.

The calculation of the discount can easily be achieved by

Java if...else...if Statement

In Java, we have an  if...else...if ladder, which can be used to execute one block of code among multiple other blocks.

 

JAVA CODE

import java.util.*;

public class ICSE2019SECTIONBQ4 {

       public static void main(String[] args) {

          showRoom obj = new showRoom();

          obj.input();

          obj.calculate();

          obj.display();      

     }

}

class showRoom {

       public void display() {

              System.out.println("Customer's Name: "+name+" Mobile Number: "+mobileNumber);

              System.out.println("Amount to be paid after discount: "+amount);

       }

       public void calculate() {

              if (cost <= 10000) {

                     discount = (5*cost)/100;

                     amount = cost - discount;

              }

              else if (10000 < cost && cost <= 20000) {

                     discount = (10*cost)/100;

                     amount = cost - discount;

              }

              else if (20000 < cost && cost <= 35000) {

                     discount = (15*cost)/100;

                     amount = cost - discount;

              }

              else {

                     discount = (20*cost)/100;

                     amount = cost - discount;

              }

       }

       public void input() {

              Scanner input = new Scanner(System.in);

              System.out.println("Enter customer's name:");

              name = input.nextLine();

              System.out.println("Enter customer's mobile number:");

              mobileNumber = input.nextLong();

              System.out.println("Enter customer's bill (cost):");

              cost = input.nextDouble();

              input.close();

       }

       showRoom() {

              name ="";

              mobileNumber = 0;

              cost = 0;

              discount = 0;

              amount =0;

       }

       private String name;

       private long mobileNumber;

       private double cost;

       private double discount;

       private double amount;

       }

 

PYTHON CODE

 class showRoom:

    def display(self):
        print("Customer's Name: ",self.__name," Mobile Number: ",self.__mobileNumber)
        print("Amount to be paid after discount: ",self.__amount)
    def calculate(self):
        if self.__cost <= 10000:
            self.__discount = (5*self.__cost)/100
            self.__amount = self.__cost - self.__discount
        elif 10000 < self.__cost and self.__cost <= 20000:
            self.__discount = (10*self.__cost)/100
            self.__amount = self.__cost - self.__discount
        elif 20000 < self.__cost and self.__cost <= 35000:
            self.__discount = (15*self.__cost)/100
            self.__amount = self.__cost - self.__discount    
        else:
            self.__discount = (20*self.__cost)/100
            self.__amount = self.__cost - self.__discount
    def input(self):
        self.__name = str(input("Enter customer's name:"))
        self.__mobileNumber = int(input("Enter customer's mobile number:"))
        self.__cost = float(input("Enter customer's bill (cost):"))
    def __init__(self):
        self.__name=""
        self.__mobileNumber = 0
        self.__cost = 0.0
        self.__discount = 0.0
        self.__amount = 0.0
    __name =""
    __mobileNumber = 0
    __cost = 0.0
    __discount = 0.0
    __amount = 0.0
obj = showRoom()
obj.input()
obj.calculate()
obj.display()

No comments:

Post a Comment