0% found this document useful (0 votes)
57 views7 pages

Lab 12 Exception Handling

The document discusses exception handling in Java. It provides three examples: 1) A simple program that divides two numbers and handles the ArithmeticException if there is an attempt to divide by zero. 2) A program that generates a random array and handles an ArrayIndexOutOfBoundsException if the user enters an invalid index. 3) A program that accepts a test score, assigns a letter grade, and is meant to throw an exception if an invalid score is entered.

Uploaded by

Javeria Shakil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views7 pages

Lab 12 Exception Handling

The document discusses exception handling in Java. It provides three examples: 1) A simple program that divides two numbers and handles the ArithmeticException if there is an attempt to divide by zero. 2) A program that generates a random array and handles an ArrayIndexOutOfBoundsException if the user enters an invalid index. 3) A program that accepts a test score, assigns a letter grade, and is meant to throw an exception if an invalid score is entered.

Uploaded by

Javeria Shakil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 7

lab 12

EXCEPTION HANDLING

QUESTION NO:1
[8:59 AM, 9/25/2019] javeriashakil999: Refining Your Calculator: Your primary task
for this lab is to restructure your calculator to handle exceptions. You can put the
method in a try catch clause.

class Excp

public static void main(String args[])

int a,b,c;

try

a = 0;

b = 10;

c = b/a;

System.out.println("This line will not be executed");

catch(ArithmeticException e)
{

System.out.println("Divided by zero");

System.out.println("After exception is handled");

output

zeroDivided by

After exception is handled

QUESTION NO:2

OUTPUT Creates an array with 100 randomly chosen integers. *

* ■ 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". *

***************************/

import java.util.*;

public class Exercise_12_03 {

public static void main(String[] args) {

// Create a Scanner
Scanner input = new Scanner(System.in);

// Invoke getArray method

int[] array = getArray();

// Prompt the user to enter the index of the array

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

try {

// Display the corresponding element value

System.out.println("The corresponding element value is " +

array[input.nextInt()]);

catch (ArrayIndexOutOfBoundsException ex) {

System.out.println("Out of Bounds.");

/** Returns an array with 100 randomly chosen integers */

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

Enter the index of the array :1

The corresponding value is : 3

Out of Bound:4

QUESTION NO:3

Write a program that accepts a test score, that is, a positive integer in the range 0
through 100 inclusive, and displays an equivalent letter grade: A (90), B (80–89), C
(70–79), D (60–69), F (under 60). Throw an exception if the mport
java.util.Scanner;

public class grade

public static void main(String[] args)

int A_grades = 0;

int B_grades = 0;

int C_grades = 0;

int D_grades = 0;
int F_grades = 0;

int count=0;

Scanner in = new Scanner(System.in);

System.out.println("Enter exam score as integer percentage in range 0 to


100 :");

int score = in.nextInt();

while(score>0)

count++;

if(score>=90 && score<=100)

A_grades++;

else if(score>=80 && score<=89)

B_grades++;

else if(score>=70 && score<=79)

C_grades++;

else if(score>=60 && score<=69)

D_grades++;

else if(score>=0 && score<=59)

F_grades++;

System.out.println("Enter exam score as integer percentage in range 0 to


100 :");

score = in.nextInt();
} //end while

System.out.println("Total number of grades :"+ count);

System.out.println("Total number of A grades :"+ A_grades);

System.out.println("Total number of B grades :"+ B_grades);

System.out.println("Total number of C grades :"+ C_grades);

System.out.println("Total number of D grades :"+ D_grades);

System.out.println("Total number of F grades :"+ F_grades);

} // end main

} // end class

Below is the Output of the program.

Enter exam score as integer percentage in range 0 to 100 :

88 98 78 68 55 45 -1

Enter exam score as integer percentage in range 0 to 100 :

Enter exam score as integer percentage in range 0 to 100 :

Enter exam score as integer percentage in range 0 to 100 :

Enter exam score as integer percentage in range 0 to 100 :


Enter exam score as integer percentage in range 0 to 100 :

Enter exam score as integer percentage in range 0 to 100 :

Total number of grades :6

Total number of A grades :1

Total number of B grades :1

Total number of C grades :1

Total number of D grades :1

Total number of F grades :2

In the output it keeps repeating "Enter exam score as integer percentage in range
0 to 100.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy