Beruflich Dokumente
Kultur Dokumente
Download this document and solve them during the laboratory time.
You do not have to submit this document. Show your output to the instructor
once completed.
Do not copy code of other students.
Grading Scheme:
Problems:
1. Program in Java to design simple calculator for (+, -, *, and /) using switch
case
Source Code:
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter two numbers: ");
// nextDouble() reads the next double from the keyboard
double first = reader.nextDouble();
double second = reader.nextDouble();
System.out.print("Enter an operator (+, -, *, /): ");
char operator = reader.next().charAt(0);
double result;
switch(operator)
{
case '+':
result = first + second;
break;
case '-':
result = first - second;
break;
case '*':
result = first * second;
break;
case '/':
result = first / second;
break;
// operator doesn't match any case constant (+, -, *, /)
default:
System.out.printf("Error! operator is not correct");
return;
}
System.out.printf("%.1f %c %.1f = %.1f", first, operator, second,
result);
}
}
Output:
2. Program in Java to design accounts class and two functions withdraw () and
deposit ().
Source Code:
import java.util.Scanner;
class Acc {
public static int initiaBalance =100;
static void dep() {
Scanner s = new Scanner(System.in);
System.out.println("Enter Amount to be deposited");
int x = s.nextInt();
int dipositAmount = initiaBalance+x;
System.out.println("Net Amount:"+ dipositAmount);
}
static void wid() {
Scanner s = new Scanner(System.in);
System.out.println("Enter Amount to be withdrawn");
int x = s.nextInt();
int withdrownAmount = initiaBalance-x;
System.out.println("Net Amount:"+ withdrownAmount);
}
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
//Acc a = new Acc();
System.out.println("Chose option 1 or 2");
int ch = s.nextInt();
switch(ch) {
case 1:
dep();
break;
case 2:
wid();
break;
default:
System.out.println("wrong choice");
}
}
}
Output: