Sie sind auf Seite 1von 4

Laboratory Assignment Sheet

Assignment ID Assignment Date of Submission


(as per the Title- Submission Assessment Release- Deadline –
policy Assignment Mode- Method- Group/ 04-sept 04-sept 2020,
guidelines) 1 Online Internal Individual Weightage 2020 23:59

Instructions (Sample provided below, please change as necessary):

 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.

Due Date: 04-sept 2020

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);

ROLL NO SAP ID: 1000013909 NAME: Mohit Singh


Laboratory Assignment Sheet

        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);
    }
}

ROLL NO SAP ID: 1000013909 NAME: Mohit Singh


Laboratory Assignment Sheet

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);

ROLL NO SAP ID: 1000013909 NAME: Mohit Singh


Laboratory Assignment Sheet

            }

    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:

ROLL NO SAP ID: 1000013909 NAME: Mohit Singh

Das könnte Ihnen auch gefallen