Вы находитесь на странице: 1из 7

Implement a class calculator that models handheld calculator.

It should have (atleast) the following functionality :

Addition, subtraction, multiplication, integer division, remainder.

Class Calculator 
Public void Calculator ( double n1, double n2, char chr ) 
double result = 0.0; 
System.out.println(First Number = “ +n1);
System.out.println(“Second Number = “ =n2);

Switch(chr) 
Case ‘+’ : result = n1 + n2;
System.out.println(“Addition of number is = “+result);
break;
Case ‘-‘ : result = n1 - n2 ;
System.out.print (“Subtraction of Number is =” + result) ;
break;
Case ‘*’ : result = n1 * n2 ;
System.out.print(“Multiplicatioin of number is =” =result) ;
break;
Case ‘/’ : result = n1 / n2 ;
System.out.print(“Result of Division is =” +result) ;
break;
Case’%’ : result = n1 % n2 ;
System.out.print(“Result of Modulus Operator is =” +result) ;
break;
default: System.out.print(“Operator Selected is Wrong”);


To convert the numerals into equivalent words using switch statement.


For example :
Inpput-124
Output-One Two Four

Class switchCase 
Public void main(int num ) 
Int reverse = 0, remainder ;
While(num > 0 ) 
remainder = num % 10;
reverse = reverse * 10 + remainder;
num = num / 10 ;

String result = “ “ ;
While(reverse > 0 ) 
remainder = reverse % 10;
reverse = reverse / 10;
switch(remainder ) 
case 0 :
result = result + “ Zero “;
break;
case 1 :
result = result + “ One “ ;
break;
case 2:
result = result + “ Two “ ;
break;
case 3 :
result = result + “ Three “ ;
break;
case 4 :
result = result + “ Four “ ;
break;
case 5:
result = result + “ Five “ ;
break;
case 6:
result = result + “ Six “ ;
break;
case 7 :
result = result + “ Seven “ ;
break;
case 8 :
result = result + “ Eight “ ;
break;
case 9:
result = result + “ Nine “ ;
break;
default:
result = “ “ ;


System.out.print(result) ;


3. A cloth showroom has announced the following festival discounts on the purchase items,
based on the total cost of the items purchased :
Total Cost Discount (in percentage)
Less than 2000 5%
2001 to 5000 25%
5001 to 10000 35%
Above 10000 50%
Write a program to input the total cost and display the amount to be paid by the customer
availing the discount.
Answer. Class Amount
Public void main (double cost)
System.out.println(“Amount of purchase : “ +cost);
double discountRate, discountAmt, finalAmt;
discountRate = 2.0;
if (( cost > 2000) && (cost < = 10000))
discountRate = 25.0;
else if ((cost > 5000) && (cost <= 10000))
discountRate = 35.0;
else
discountRate = 50.0;
discountRate = cost * discountRate / 100.0;
finalAmt = cost * discontAmt;
// display the results
System.out.println(“Total cost : “+cost);
System.out.println(“Discount % : “ +discountRate);
System.out.println(“Discount Amt. : “ +discountAmt);
System.out.println(“Final Price : “ +finalAmt);
}
}

5. Write a program which finds whether a given year is a leap year or not.

Answer. Public class Leap year {

Public void find (int year) {

If (year% 100==0) {

If(year% 400==0) {

System. Out . println (year+ is a leap year”);}

else {

System. Out . println (year+ “is not a leap year”);

Else {

If (year %4==0) {

System. Out . println (year+” is a leap year”);


}

Else{

System. Out. Println(year + “is not a leap year”);

6. An electronics shop has announced the following seasonal discounts on the purchase of certain items:

Import java. Io.*;

Class laptop PC {

Public static void main (String args[]) throws IOException {

Double p, d, net;

String name, address;

System. Out. Println(“*********BILL PROGRAM **********”);

System. Out. Println (“Enter Name of the Customer::”);

Input Stream Reader reder =new InputStreamReader(Systeem.in);

Name= input.readLine();

System.out.println (“Enter address ::”);

Address=input.readLine ();

Systeam. Out. Println (“Enter Value of purchases= “);

String X =input. readLine ();

P=Double. Parse Double (x);

System. Out.println (Name of the Customer :: “ +name);

System. Out. Println (“Address of the Customer :: “+ address);

System .Out. println (“L.Enter L for Laptop “ );

System.out. println (“D. Enter D for Laptop “);

System. Out. Println ();

System. Out. Println (“Enter your choice::”);

Char choice;
Choice= (char) System.in.read();

System. Out. Println(“Name of the Customer::”+name);

System. Out. Println(“Address of the Customer ::”+address);

Switch (choice) {

Case’L’ :if (p<=25000){

D=0;

Net=p-d;

System.out.Println(“Amount to be paid”+net);

Else if (p>25000&&P <=57000){

d =(5.0/100)*p;

net=p-d;

System.out.println(“Amount to be paid “ +net);

Else if (p> 57000&& p < =100000){

d = (7.5/100) * p;

net = p-d;

System.out.println (“Amount to be paid “+net);

Else if(p> 100000) {

d =(10.0/100) * p;

net = p-d;

System.out.println ( “Amount to be paid” +net0;

Break;

Case ‘D’ : if (p<=25000) {

d = (5.0/100) * p;

net = p-d;

System.out.println(“Amount to be paid “+net);

}
Else if (p> 25000&& p < = 57000) {

d = (7.5/100) * p;

net = p-d;

System.out.println (“Amount to be paid “ +net);

Else if (p>57000&& p<=100000) {

Net =p-d;

System.out.println(“Amount to be paid”+net);

Else if (p> 100000) {

d = (15.0/100) * p;

net= p-d;

System.out.println(“Amount to be paid” + net);

break;

Output of the program

7) Using a switch statement, write a menu driven program to convert a given


temperature from Fahrenheit to Celsius and vice versa. For an incorrect choice, an
appropriate error message should be displayed.

(HINT: c = 5/9x (f-32) and


F = 1.8xc +32)
Import java.io.*;
Class temperature {
Public void main (int choice) throws ioExeption {
Float c = o;
Float f = 0;
Inputstreamreader reader = new inputstreamreader (system. In);
Buffered reader input = new buffered reader (reader);
System.out.println (“enter 1. Fahrenheit to Celsius”);
System.out.println (“enter 2. Celsius to Fahrenheit”);
Switch (choice) {
Case 1:
System.out.println (“enter temperature in Fahrenheit “);
String a = input .redline ();
F = float. Parse float (a);
C = 5/ 9f * (f- 32);
System.out.println (“equivalent temperature in Celsius is = “+c);
Break;
Case2:
System.out.println (“enter temperature in Celsius”);
String b = input. Read line ();
C= float. Parse float (b);
F= (1.8f * c) +32;
System. Out println (“equivalent temperature in Fahrenheit is = “+f);
Break;
Default:
System.out.println (“incorrect choice”);
Break;
}
}
}

Вам также может понравиться