C Programs Tutorials | IT Developer
IT Developer

Java Programs - Solved 2019 ICSE Computer Science Paper



Share with a Friend

Class & Object Based Program - Java Programs

Design a class 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 the 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.

import java.util.Scanner; class ShowRoom{ String name; long mobNo; double cost; double dis; double amount; public ShowRoom(){ name = ""; mobNo = 0L; cost = 0.0; dis = 0.0; amount = 0.0; } public void input(){ Scanner in = new Scanner(System.in); System.out.print("Name: "); name = in.nextLine(); System.out.print("Mobile number: "); mobNo = Long.parseLong(in.nextLine()); System.out.print("Cost of items: "); cost = Double.parseDouble(in.nextLine()); } public void calculate(){ if(cost <= 10000) dis = 5.0; else if(cost <= 20000) dis = 10.0; else if(cost <= 35000) dis = 15.0; else dis = 20.0; dis = dis / 100 * cost; amount = cost - dis; } public void display(){ System.out.println("Name: " + name); System.out.println("Mobile number: " + mobNo); System.out.println("Amount: " + amount); } public static void main(String[] args){ ShowRoom obj = new ShowRoom(); obj.input(); obj.calculate(); obj.display(); } }

Output

 
 OUTPUT 1: 
Name: Anand Rao
Mobile number: 1234567890
Cost of items: 12000

Name: Anand Rao
Mobile number: 1234567890
Amount: 10800.0 

 OUTPUT 2: 
Name: Ajay Singh
Mobile number: 9087654321
Cost of items: 21000

Name: Ajay Singh
Mobile number: 9087654321
Amount: 17850.0