You are on page 1of 8

Programming

Fundamentals
Lab Task  18/05/2021

Name : Kantesh Kumar


Reg no: FA20-BSE-101
Submitted To : Azfar Shakeel Khan

S SUBMITTED BY: KANTESH KUMAR


2

Q12.1: (NumberFormatException) Listing 7.9, Calculator.java, is a simple commandline


calculator. Note that the program terminates if any operand is nonnumeric. Write a program with
an exception handler that deals with nonnumeric operands; Your program should display a message
that informs the user of the wrong operand type before exiting (see Figure 12.12).

CODE:
import java.util.InputMismatchException;

import java.util.Scanner;

class Calculator1 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        String option;
3

        do{

            try{

                System.out.println("*Welcome to calculator**");

                System.out.print("Enter first number:: ");

                int first = in.nextInt();

                System.out.print("Enter the operator('+','-','*' or '/')::");

                String operator = in.next();

                System.out.print("Enter second number");

                int second = in.nextInt();

                double result = 0;

                if(operator.equals("+")){

                    result = first+second;

                }

                else if(operator.equals("-")){

                    result = first-second;

                }

                else if(operator.equals("*")){

                    result = first*second;

                }

                else if(operator.equals("/")){

                    result = first/second;

                }

                String resultFormatted = String.format("%.2f",result);
4

                System.out.println("Result for "+first+operator+second+" = "+
result);

            }

            catch(InputMismatchException e){

                System.err.println("Non numeric value entered");

                System.exit(0);

            }

            System.out.println("Do you want to continue?(Y/N)");

            option = in.next();

        }

        while(option.equalsIgnoreCase("Y"));

        in.close();

    }

OUTPUT:
5

Q12.2: (InputMismatchException) Write a program that prompts the user to read two
integers and displays their sum. Your program should prompt the user to read the number again
if the input is incorrect.

CODE:
import java.util.Scanner;
import java.util.Arrays;
public class hello {
    public static void main(String[] args){
        Scanner in = new java.util.Scanner(System.in);
        int a = 0;
        int b = 0;
        while(true) {
            System.out.print("Please enter in 2 integers to get their sum: ");
            try {
                a = in.nextInt();
                b = in.nextInt();

                break;
            }
            catch (java.util.InputMismatchException ime) {
                System.out.printf("You must enter 2 integers!%n%n");
                in.nextLine();
            }

        }
        System.out.printf("The sum is %d%n", (a+b));
    }
}

OUTPUT:

Q12.3:  (ArrayIndexOutOfBoundsException) Write a program that meets the following


requirements:

■ Creates an array with 100 randomly chosen integers.


6

■ Prompts the user to enter the index of the array, then displays the corresponding
element value. If the specified index is out of bounds, display the message Out of Bounds.
CODE:
import java.util.Scanner;
import java.util.Arrays;
public class hello {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);

        int[] array = getArray();

        System.out.print("Enter the index of the array: ");
        try {

            System.out.println("The corresponding element value is " +
                    array[input.nextInt()]);
        }
        catch (ArrayIndexOutOfBoundsException ex) {
            System.out.println("Out of Bounds.");
        }
    }

    public static int[] getArray() {
        int[] array = new int[100];
        for (int i = 0; i < array.length; i++) {
            array[i] = (int)(Math.random() * 100) + 1;
        }
        return array;
    }
}

OUTPUT:
7
8

You might also like