0% found this document useful (0 votes)
3 views

assignment7

The document contains a series of Java programming assignments focused on exception handling, including examples of using try-catch blocks, defining custom exceptions, and handling specific cases like division by zero and negative array indices. It also includes tasks for implementing character counting with error handling for non-alphabetic characters, calculating factorials with overflow checks, and outputting country capitals with exception handling for unmatched inputs. Each assignment is accompanied by code snippets demonstrating the required functionality.

Uploaded by

Cactus
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

assignment7

The document contains a series of Java programming assignments focused on exception handling, including examples of using try-catch blocks, defining custom exceptions, and handling specific cases like division by zero and negative array indices. It also includes tasks for implementing character counting with error handling for non-alphabetic characters, calculating factorials with overflow checks, and outputting country capitals with exception handling for unmatched inputs. Each assignment is accompanied by code snippets demonstrating the required functionality.

Uploaded by

Cactus
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

-Akshara Suhane

‭ASSIGNMENT-7‬
‭1.‬ ‭Write‬ ‭a‬ ‭Java‬ ‭program‬ ‭to‬ ‭show‬ ‭the‬ ‭use‬ ‭of‬ ‭all‬
‭keywords‬ ‭for‬ ‭exception‬ ‭handling.‬
‭public‬‭class‬‭sample‬‭{‬
‭public‬‭static‬‭void‬‭main‬‭(‭S ‬ tring‬‭[]‬‭args‬‭) {‬
‭try‬‭{‬
‭int‬‭result‬‭=‬‭divide‬‭(‭1 ‬ 0‬‭,‬‭0‬‭);‬
‭System‬‭.‭o ‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ Result: "‬‭+‬‭result‬‭);‬
‭}‬‭catch‬‭(‬‭ArithmeticException‬‭e‬‭) {‬
‭System‬‭.‭o ‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ ArithmeticException caught: "‬‭+‬‭e‬‭.‭g ‬ etMessage‬‭());‬
‭}‬‭catch‬‭(‬‭Exception‬‭e‬‭) {‬
‭System‬‭.‭o‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ Exception caught: "‬‭+‬‭e‬‭.‭g
‬ etMessage‬‭());‬
‭}‬‭finally‬‭{‬
‭System‬‭.‭o
‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ Finally block executed"‬‭);‬
‭}‬
‭}‬
‭public‬‭static‬‭int‬‭divide‬‭(‭i‬nt‬‭num1‬‭,‬‭int‬‭num2‬‭)‬‭throws‬‭ArithmeticException‬‭{‬
‭if‬‭(‭n
‬ um2‬‭==‬‭0‭)‬ {‬
‭throw‬‭new‬‭ArithmeticException‬‭(‭"‬ Division by zero is not allowed"‬‭);‬
‭}‬
‭return‬‭num1‬‭/‬‭num2‬‭;‬
‭}‬
‭}‬
‭Output:‬
‭ArithmeticException caught: Division by zero is not allowed‬
‭Finally block execute‬

‭ . Write‬
2 ‭a Java program using try and catch to generate NegativeArrayIndex‬
‭Exception and‬ ‭Arithmetic Exception.‬
‭public‬‭class‬‭sample‬‭{‬
‭public‬‭static‬‭void‬‭main‬‭(‭S ‬ tring‬‭[]‬‭args‬‭) {‬
‭try‬‭{‬
‭int‬‭[]‬‭arr‬‭= {‬‭1‭,‬‬‭2‬‭,‬‭3‭}‬ ;‬
‭System‬‭.‭o ‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ Element at index 3: "‬‭+‬‭arr‬‭[‬‭3‭]‬);‬
‭}‬‭catch‬‭(‬‭NegativeArraySizeException‬‭e‬‭) {‬
‭System‬‭.‭o ‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ NegativeArrayIndexException caught: "‬‭+‬
‭e‬‭.‭g
‬ etMessage‬‭());‬
‭}‬

‭try‬‭{‬
‭int‬‭result‬‭=‬‭divide‬‭(‭1‬ 0‬‭,‬‭0‬‭);‬
‭System‬‭.‭o
‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ Result: "‬‭+‬‭result‬‭);‬
‭}‬‭catch‬‭(‬‭ArithmeticException‬‭e‬‭) {‬
‭System‬‭.‭o
‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ ArithmeticException caught: "‬‭+‬‭e‬‭.‭g
‬ etMessage‬‭());‬
‭}‬
‭}‬

‭public‬‭static‬‭int‬‭divide‬‭(‭i‬nt‬‭num1‬‭,‬‭int‬‭num2‬‭)‬‭throws‬‭ArithmeticException‬‭{‬
‭if‬‭(‭n
‬ um2‬‭==‬‭0‭)‬ {‬
‭throw‬‭new‬‭ArithmeticException‬‭(‭"‬ Division by zero is not allowed"‬‭);‬
‭}‬
‭return‬‭num1‬‭/‬‭num2‬‭;‬
‭}‬
‭}‬

‭3. Define an exception called “NoMatchFoundException” that is thrown when a string‬


‭isnot‬
‭equal to‬ ‭“University”.‬ ‭Write‬ ‭a program that uses this exception.‬
‭class‬‭NoMatchFoundException‬‭extends‬‭Exception‬‭{‬
‭public‬‭NoMatchFoundException‬‭(‬‭String‬‭message‬‭) {‬
‭super‬‭(‬‭message‬‭);‬
‭}‬
‭}‬

‭public‬‭class‬‭sample‬‭{‬
‭public‬‭static‬‭void‬‭main‬‭(‭S ‬ tring‬‭[]‬‭args‬‭) {‬
‭try‬‭{‬
‭String‬‭inputString‬‭=‬‭"College"‬‭;‬
‭if‬‭(!‬‭inputString‬‭.‭e ‬ quals‬‭(‭"‬ University"‬‭)) {‬
‭throw‬‭new‬‭NoMatchFoundException‬‭(‬‭"String does not match 'University'"‬‭);‬
‭}‬
‭System‬‭.‭o‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ String matches 'University'"‬‭);‬
‭}‬‭catch‬‭(‬‭NoMatchFoundException‬‭e‬‭) {‬
‭System‬‭.‭o
‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ NoMatchFoundException caught: "‬‭+‬‭e‬‭.‭g
‬ etMessage‬‭());‬
‭}‬
‭}‬
‭}‬
‭Output:‬
‭NoMatchFoundException caught: String does not match 'University'‬

‭ . Write‬ ‭a class that keeps a running total of all characters passed to it‬
4
‭(one at a time)‬ ‭and‬ ‭throws‬ ‭an‬ ‭exception‬ ‭if‬ ‭it‬ ‭is‬ ‭passed‬
‭a‬ ‭non-alphabetic‬ ‭character.‬
‭class‬‭NonAlphabeticCharacterException‬‭extends‬‭Exception‬‭{‬
‭public‬‭NonAlphabeticCharacterException‬‭(‭S‬ tring‬‭message‬‭) {‬
‭super‬‭(‬‭message‬‭);‬
‭}‬
}‭ ‬
‭public‬‭class‬‭sample‬‭{‬
‭private‬‭int‬‭total‬‭;‬

‭public‬‭sample‬‭() {‬
‭this‬‭.‭t‬otal‬‭=‬‭0‬‭;‬
‭}‬

‭public‬‭void‬‭addChar‬‭(‭c‬ har‬‭c‬‭)‬‭throws‬‭NonAlphabeticCharacterException‬‭{‬
‭if‬‭(!‬‭Character‬‭.‬‭isLetter‬‭(‭c‬ ‬‭)) {‬
‭throw‬‭new‬‭NonAlphabeticCharacterException‬‭(‭"‬ Non-alphabetic‬‭character‬
‭detected: "‬‭+‬‭c‬‭);‬
‭}‬
‭this‬‭.‭t‬otal‬‭++;‬
‭}‬

‭public‬‭int‬‭getTotal‬‭() {‬
‭return‬‭this‬‭.‭t‬otal‬‭;‬
‭}‬

‭public‬‭static‬‭void‬‭main‬‭(‭S
‬ tring‬‭[]‬‭args‬‭) {‬
‭sample‬‭runningTotal‬‭=‬‭new‬‭sample‬‭();‬

‭try‬‭{‬
‭runningTotal‬‭.‭a ‬ ddChar‬‭(‬‭'A'‬‭);‬
‭runningTotal‬‭.‭a ‬ ddChar‬‭(‬‭'B'‬‭);‬
‭runningTotal‬‭.‭a ‬ ddChar‬‭(‬‭'C'‬‭);‬
‭runningTotal‬‭.‭a ‬ ddChar‬‭(‬‭'1'‬‭);‬
‭runningTotal‬‭.‭a ‬ ddChar‬‭(‬‭'D'‬‭);‬
‭}‬‭catch‬‭(‬‭NonAlphabeticCharacterException‬‭e‭)‬ {‬
‭System‬‭.‭o
‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ NonAlphabeticCharacterException‬‭caught: "‬‭+‬
‭e‭.‬‬‭getMessage‬‭());‬
‭}‬

‭System‬‭.‬‭out‬‭.‭p
‬ rintln‬‭(‬‭"Total alphabetic characters: "‬‭+‬‭runningTotal‬‭.‭g
‬ etTotal‬‭());‬
‭}‬
‭}‬

‭ . Write‬
5 ‭ program called Factorial.java that computes factorials and catches‬
a
‭the result‬ ‭in‬ ‭an array of type long for reuse. The long type of variable has its‬
‭ wn range. For‬ ‭example‬ ‭20! Is as high as the range of long type. So check the‬
o
‭argument‬ ‭passes and‬ ‭“throw‬ ‭an‬ ‭exception”,‬ ‭if‬ ‭it‬ ‭is‬
‭too‬ ‭big‬ ‭or‬ ‭too‬ ‭small.‬

‭ ).If‬
a ‭x is less than 0 throw an IllegalArgumentException with a‬
‭message‬ ‭“Value‬ ‭of‬ ‭x‬ ‭must‬ ‭be‬
‭positive”.‬
‭b).If‬ ‭x‬ ‭is‬ ‭above‬ ‭the‬ ‭length‬
‭of‬ ‭the‬ ‭array‬ ‭throw‬ ‭an‬
‭IllegalArgumentException‬ ‭with‬ ‭a‬ ‭message‬
‭“Result‬ ‭will‬ ‭overflow”.‬

‭ ere x is the value for which we want to find the factorial.‬


H
‭class‬‭FactorialException‬‭extends‬‭Exception‬‭{‬
‭public‬‭FactorialException‬‭(‭S
‬ tring‬‭message‬‭) {‬
‭super‬‭(‬‭message‬‭);‬
‭}‬
‭}‬

‭public‬‭class‬‭sample‬‭{‬
‭private‬‭static‬‭final‬‭int‬‭MAX_FACTORIAL‬‭=‬‭20‬‭;‬
‭private‬‭static‬‭long‬‭[]‬‭factorials‬‭=‬‭new‬‭long‬‭[‭M
‬ AX_FACTORIAL‬‭];‬

‭public‬‭static‬‭void‬‭main‬‭(‭S ‬ tring‬‭[]‬‭args‬‭) {‬
‭try‬‭{‬
‭int‬‭x‬‭=‬‭5‬‭;‬
‭long‬‭result‬‭=‬‭factorial‬‭(‭x‬ ‭)‬ ;‬
‭System‬‭.‭o ‬ ut‬‭.‭p
‬ rintln‬‭(‭x‬ ‬‭+‬‭"! = "‬‭+‬‭result‬‭);‬

x‭ ‬‭=‬‭21‬‭;‬
‭result‬‭=‬‭factorial‬‭(‭x‬ ‬‭);‬
‭System‬‭.‭o ‬ ut‬‭.‭p
‬ rintln‬‭(‭x‬ ‬‭+‬‭"! = "‬‭+‬‭result‬‭);‬
‭}‬‭catch‬‭(‬‭FactorialException‬‭e‬‭) {‬
‭System‬‭.‭o‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ FactorialException caught: "‬‭+‬‭e‭.‬‬‭getMessage‬‭());‬
‭}‬
‭}‬

‭public‬‭static‬‭long‬‭factorial‬‭(‭i‬nt‬‭x‭)‬ ‬‭throws‬‭FactorialException‬‭{‬
‭if‬‭(‭x‬ ‬‭<‬‭0‬‭) {‬
‭throw‬‭new‬‭IllegalArgumentException‬‭(‬‭"Value of x must be positive"‬‭);‬
‭}‬

‭if‬‭(‭x‬ ‬‭>=‬‭MAX_FACTORIAL‬‭) {‬
‭throw‬‭new‬‭FactorialException‬‭(‭"‬ Result will overflow"‬‭);‬
‭}‬

‭if‬‭(‭f‬actorials‬‭[‭x‬ ‬‭] !=‬‭0‬‭) {‬


‭return‬‭factorials‬‭[‭x‬ ‬‭];‬
‭}‬

‭if‬‭(‭x‬ ‬‭==‬‭0‬‭||‬‭x‬‭==‬‭1‭)‬ {‬
‭factorials‬‭[‭x‬ ‬‭] =‬‭1‭;‬‬
‭}‬‭else‬‭{‬
‭factorials‬‭[‭x‬ ‬‭] =‬‭x‬‭*‬‭factorial‬‭(‭x‬ ‬‭-‬‭1‬‭);‬
‭}‬

‭return‬‭factorials‬‭[‬‭x‭]‬;‬
‭}‬
}‭ ‬
‭Output:‬
‭5! = 120‬
‭FactorialException caught: Result will overflow‬

‭ . Write‬ ‭a program that outputs the name of the capital of the country‬
6
‭entered at the‬ ‭command line. The program should throw a‬
‭“NoMatchFoundException” when it‬ ‭fails‬ ‭to‬ ‭print‬ ‭the‬ ‭capital‬ ‭of‬
‭the‬ ‭country‬ ‭entered‬ ‭at‬ ‭the‬ ‭command‬ ‭line.‬
‭class‬‭NoMatchFoundException‬‭extends‬‭Exception‬‭{‬
‭public‬‭NoMatchFoundException‬‭(‬‭String‬‭message‬‭) {‬
‭super‬‭(‬‭message‬‭);‬
‭}‬
‭}‬
‭public‬‭class‬‭sample‬‭{‬
‭public‬‭static‬‭void‬‭main‬‭(‭S ‬ tring‬‭[]‬‭args‬‭) {‬
‭try‬‭{‬
‭if‬‭(‬‭args‬‭.‬‭length‬‭==‬‭0‬‭) {‬
‭throw‬‭new‬‭IllegalArgumentException‬‭(‬‭"Country name not provided"‬‭);‬
‭}‬

‭ tring‬‭country‬‭=‬‭args‬‭[‬‭0‭]‬;‬
S
‭String‬‭capital‬‭=‬‭findCapital‬‭(‬‭country‬‭);‬

‭if‬‭(‬‭capital‬‭==‬‭null‬‭) {‬
‭throw‬‭new‬‭NoMatchFoundException‬‭(‬‭"No capital found for country: "‬‭+‬
‭country‬‭);‬
‭}‬

‭ ystem‬‭.‭o
S ‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ Capital of "‬‭+‬‭country‬‭+‬‭": "‬‭+‬‭capital‬‭);‬
‭}‬‭catch‬‭(‬‭IllegalArgumentException‬‭e‭)‬ {‬
‭System‬‭.‭o‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ IllegalArgumentException caught: "‬‭+‬‭e‬‭.‭g ‬ etMessage‬‭());‬
‭}‬‭catch‬‭(‬‭NoMatchFoundException‬‭e‬‭) {‬
‭System‬‭.‭o
‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ NoMatchFoundException caught: "‬‭+‬‭e‬‭.‭g ‬ etMessage‬‭());‬
‭}‬
‭}‬

‭public‬‭static‬‭String‬‭findCapital‬‭(‭S
‬ tring‬‭country‬‭) {‬
‭switch‬‭(‭c‬ ountry‬‭.‭t‬oLowerCase‬‭()) {‬
‭case‬‭"united states"‬‭:‬
‭return‬‭"Washington, D.C."‬‭;‬
‭case‬‭"united kingdom"‬‭:‬
‭return‬‭"London"‬‭;‬
‭case‬‭"france"‬‭:‬
‭return‬‭"Paris"‬‭;‬
‭default:‬
‭return‬‭null‬‭;‬
‭}‬
‭}‬
}‭ ‬
‭Output:‬
‭IllegalArgumentException caught: Country name not provided‬

‭ . Write‬
7 ‭a program that takes a value at the command line for which factorial‬ ‭is‬
‭to be‬ ‭computed.‬ ‭The program must convert the string to its integer equivalent.‬
‭There‬ ‭are‬ ‭three‬‭possible‬ ‭user‬ ‭input‬ ‭errors‬ ‭that‬ ‭can‬ ‭prevent‬ ‭the‬
‭program‬ ‭from‬ ‭executing‬ ‭normally.‬

‭ ). The‬
a ‭first‬ ‭error‬ ‭is‬ ‭when‬ ‭the‬
‭user‬ ‭provides‬ ‭no‬ ‭argument‬ ‭while‬ ‭executing‬
‭the‬ ‭program‬ ‭and an ArrayIndexOutOfBoundsException is raised.‬
‭You must write a‬ ‭catch‬ ‭block‬ ‭for‬ ‭this.‬

‭b). The‬ ‭ econd error is NumberFormatException that is raised in case the‬


s
‭user‬ ‭provides‬ ‭a non-integer‬ ‭(float‬ ‭double)‬
‭value‬ ‭at‬ ‭the‬ ‭command‬ ‭line.‬

‭c). The‬ ‭third error is IllegalArgumentException. This‬ ‭needs‬


t‭o be thrown manually if‬ t‭he‬ ‭value‬ ‭at‬ ‭the‬
‭command‬ ‭line is‬ ‭0.‬

‭class‬‭FactorialException‬‭extends‬‭Exception‬‭{‬
‭public‬‭FactorialException‬‭(‭S
‬ tring‬‭message‬‭) {‬
‭super‬‭(‬‭message‬‭);‬
‭}‬
‭}‬

‭public‬‭class‬‭sample‬‭{‬
‭public‬‭static‬‭void‬‭main‬‭(‭S ‬ tring‬‭[]‬‭args‬‭) {‬
‭try‬‭{‬
‭if‬‭(‬‭args‬‭.‬‭length‬‭==‬‭0‬‭) {‬
‭throw‬‭new‬‭ArrayIndexOutOfBoundsException‬‭(‭"‬ No argument provided"‬‭);‬
‭}‬

‭int‬‭x‬‭=‬‭Integer‬‭.‭p
‬ arseInt‬‭(‭a
‬ rgs‬‭[‭0
‬ ‬‭]);‬

‭if‬‭(‬‭x‬‭==‬‭0‭)‬ {‬
‭throw‬‭new‬‭IllegalArgumentException‬‭(‬‭"Value of x must be greater‬‭than 0"‬‭);‬
‭}‬

l‭ong‬‭result‬‭=‬‭factorial‬‭(‭x‬ ‭)‬ ;‬
‭System‬‭.‭o ‬ ut‬‭.‭p
‬ rintln‬‭(‭x‬ ‬‭+‬‭"! = "‬‭+‬‭result‬‭);‬
‭}‬‭catch‬‭(‬‭ArrayIndexOutOfBoundsException‬‭e‭)‬ {‬
‭System‬‭.‭o ‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ ArrayIndexOutOfBoundsException caught: "‬‭+‬
‭e‭.‬‬‭getMessage‬‭());‬
‭}‬‭catch‬‭(‬‭NumberFormatException‬‭e‬‭) {‬
‭System‬‭.‭o ‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ NumberFormatException caught: Invalid argument‬
‭provided"‬‭);‬
‭}‬‭catch‬‭(‬‭IllegalArgumentException‬‭e‭)‬ {‬
‭System‬‭.‭o‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ IllegalArgumentException caught: "‬‭+‬‭e‬‭.‭g‬ etMessage‬‭());‬
‭}‬‭catch‬‭(‬‭FactorialException‬‭e‬‭) {‬
‭System‬‭.‭o
‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ FactorialException caught: "‬‭+‬‭e‭.‬‬‭getMessage‬‭());‬
‭}‬
‭}‬

‭public‬‭static‬‭long‬‭factorial‬‭(‭i‬nt‬‭x‭)‬ ‬‭throws‬‭FactorialException‬‭{‬
‭if‬‭(‭x‬ ‬‭<‬‭0‬‭) {‬
‭throw‬‭new‬‭IllegalArgumentException‬‭(‬‭"Value of x must be positive"‬‭);‬
‭}‬
‭if‬‭(‭x‬ ‬‭==‬‭0‬‭||‬‭x‬‭==‬‭1‭)‬ {‬
‭return‬‭1‭;‬‬
‭}‬

‭long‬‭result‬‭=‬‭x‬‭*‬‭factorial‬‭(‭x‬ ‬‭-‬‭1‬‭);‬

‭if‬‭(‭r‬ esult‬‭<‬‭0‭)‬ {‬
‭throw‬‭new‬‭FactorialException‬‭(‭"‬ Result will overflow"‬‭);‬
‭}‬

‭return‬‭result‬‭;‬
‭}‬
}‭ ‬
‭Output:‬
‭ArrayIndexOutOfBoundsException caught: No argument provided‬

‭ . Create‬ ‭a user-defined exception named CheckArgument to check the‬


8
‭number of‬ ‭arguments‬ ‭passed through the command line. If the number of‬
‭argument is less‬ ‭than‬ ‭5, throw‬ ‭the‬ ‭CheckArgumentexception,else‬
‭print‬ ‭the‬ ‭addition‬ ‭of‬ ‭all‬ ‭the‬ ‭five‬ ‭numbers.‬
‭class‬‭CheckArgumentException‬‭extends‬‭Exception‬‭{‬
‭public‬‭CheckArgumentException‬‭(‭S
‬ tring‬‭message‬‭) {‬
‭super‬‭(‭m
‬ essage‬‭);‬
‭}‬
‭}‬

‭public‬‭class‬‭sample‬‭{‬
‭public‬‭static‬‭void‬‭main‬‭(‬‭String‬‭[]‬‭args‬‭) {‬
‭try‬‭{‬
‭if‬‭(‭a
‬ rgs‬‭.‭l‬ength‬‭<‬‭5‬‭) {‬
‭throw‬‭new‬‭CheckArgumentException‬‭(‭"‬ At least 5 arguments‬
‭required"‬‭);‬
‭}‬

‭int‬‭sum‬‭=‬‭0‬‭;‬

‭for‬‭(‭S
‬ tring‬‭arg‬‭:‬‭args‬‭) {‬
‭sum‬‭+=‬‭Integer‬‭.‭p ‬ arseInt‬‭(‬‭arg‬‭);‬
‭}‬
‭ ystem‬‭.‭o
S ‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ Sum of all arguments: "‬‭+‬‭sum‬‭);‬
‭}‬‭catch‬‭(‭C‬ heckArgumentException‬‭e‭)‬ {‬
‭System‬‭.‭o‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ CheckArgumentException caught: "‬‭+‬
‭e‭.‬‬‭getMessage‬‭());‬
‭}‬‭catch‬‭(‭N
‬ umberFormatException‬‭e‬‭) {‬
‭System‬‭.‭o
‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ NumberFormatException caught: Invalid‬‭argument‬
‭provided"‬‭);‬
‭}‬
‭}‬
‭}‬
‭Output:‬
‭CheckArgumentException caught: At least 5 arguments required‬

‭ . Consider‬
9 ‭a‬ ‭Student‬ ‭examination‬ ‭database‬ ‭system‬
‭that‬ ‭prints‬ ‭the‬ ‭mark‬‭sheet‬ ‭of‬ ‭students.‬ ‭Input‬‭the‬
‭following‬ ‭from‬ ‭the‬ ‭command‬ ‭line.‬
‭1.Student’s‬ ‭Name‬
‭2.Marks‬ ‭in six‬‭subjects‬
‭These marks should be between 0 to 50. If the marks are not in the specified‬
‭range, raise a RangeException, else find the total marks and prints the‬
‭percentage of the students.‬
‭class‬‭RangeException‬‭extends‬‭Exception‬‭{‬
‭public‬‭RangeException‬‭(‬‭String‬‭message‬‭) {‬
‭super‬‭(‭m
‬ essage‬‭);‬
‭}‬
‭}‬

‭public‬‭class‬‭sample‬‭{‬
‭public‬‭static‬‭void‬‭main‬‭(‬‭String‬‭[]‬‭args‬‭) {‬
‭try‬‭{‬
‭if‬‭(‭a
‬ rgs‬‭.‭l‬ength‬‭<‬‭7‬‭) {‬
‭throw‬‭new‬‭IllegalArgumentException‬‭(‬‭"At least 7 arguments‬
‭required"‬‭);‬
‭}‬

‭ tring‬‭name‬‭=‬‭args‬‭[‭0
S ‬ ‬‭];‬
‭int‬‭[]‬‭marks‬‭=‬‭new‬‭int‬‭[‬‭6‭]‬;‬

‭for‬‭(‭i‬nt‬‭i‬‭=‬‭1‭;‬‬‭i‬‭<=‬‭6‬‭;‬‭i‬‭++) {‬
‭int‬‭mark‬‭=‬‭Integer‬‭.‬‭parseInt‬‭(‭a ‬ rgs‬‭[‭i‬‬‭]);‬
‭if‬‭(‭m
‬ ark‬‭<‬‭0‬‭||‬‭mark‬‭>‬‭50‬‭) {‬
‭throw‬‭new‬‭RangeException‬‭(‬‭"Marks should be between‬‭0 to 50"‬‭);‬
‭}‬

‭marks‬‭[‭i‬‬‭-‬‭1‭]‬ =‬‭mark‬‭;‬
‭}‬

‭int‬‭totalMarks‬‭=‬‭0‬‭;‬

‭for‬‭(‭i‬nt‬‭mark‬‭:‬‭marks‬‭) {‬
‭totalMarks‬‭+=‬‭mark‬‭;‬
‭}‬

‭ ouble‬‭percentage‬‭= (‬‭double‬‭)‬‭totalMarks‬‭/‬‭300‬‭*‬‭100‬‭;‬
d
‭System‬‭.‭o ‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ Name: "‬‭+‬‭name‬‭);‬
‭System‬‭.‭o ‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ Total Marks: "‬‭+‬‭totalMarks‬‭);‬
‭System‬‭.‭o ‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ Percentage: "‬‭+‬‭percentage‬‭+‬‭"%"‬‭);‬
‭}‬‭catch‬‭(‭I‬llegalArgumentException‬‭e‭)‬ {‬
‭System‬‭.‭o‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ IllegalArgumentException caught: "‬‭+‬
‭e‭.‬‬‭getMessage‬‭());‬
‭}‬‭catch‬‭(‭R ‬ angeException‬‭e‭)‬ {‬
‭System‬‭.‭o
‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ RangeException caught: "‬‭+‬‭e‬‭.‭g‬ etMessage‬‭());‬
‭}‬
‭}‬
‭}‬
‭Output:‬
‭IllegalArgumentException caught: At least 7 arguments required‬

‭ 0. Create‬ ‭two user-defined exceptions named “TooHot” and “TooCold” to‬


1
‭check the‬ ‭temperature‬ ‭(in Celsius) given by the user passed through‬
‭the command line is‬ ‭too‬ ‭hot‬ ‭or too‬ ‭cold.‬
‭1. If‬ ‭temperature‬ ‭>‬ ‭35,‬ ‭throw‬ ‭exception‬ ‭“TooHot”.‬
‭2. If‬ ‭temperature <5, throw exception “TooCold”.‬
‭3. Otherwise,‬ ‭print‬ ‭“Normal”‬ ‭and‬ ‭convert‬ ‭it to‬ ‭Farenheit.‬
‭class‬‭TooHotException‬‭extends‬‭Exception‬‭{‬
‭public‬‭TooHotException‬‭(‭S ‬ tring‬‭message‬‭) {‬
‭super‬‭(‭m
‬ essage‬‭);‬
‭}‬
‭}‬

‭class‬‭TooColdException‬‭extends‬‭Exception‬‭{‬
‭public‬‭TooColdException‬‭(‬‭String‬‭message‬‭) {‬
‭super‬‭(‭m
‬ essage‬‭);‬
‭}‬
‭}‬

‭public‬‭class‬‭sample‬‭{‬
‭public‬‭static‬‭void‬‭main‬‭(‬‭String‬‭[]‬‭args‬‭) {‬
‭try‬‭{‬
‭if‬‭(‭a
‬ rgs‬‭.‭l‬ength‬‭==‬‭0‬‭) {‬
‭throw‬‭new‬‭IllegalArgumentException‬‭(‬‭"No argument provided"‬‭);‬
‭}‬

‭int‬‭temperature‬‭=‬‭Integer‬‭.‭p
‬ arseInt‬‭(‬‭args‬‭[‬‭0‭]‬);‬

‭if‬‭(‭t‬emperature‬‭>‬‭35‬‭) {‬
‭throw‬‭new‬‭TooHotException‬‭(‭"‬ Temperature is too hot"‬‭);‬
‭}‬‭else‬‭if‬‭(‭t‬emperature‬‭<‬‭5‬‭) {‬
‭throw‬‭new‬‭TooColdException‬‭(‬‭"Temperature is too cold"‬‭);‬
‭}‬‭else‬‭{‬
‭System‬‭.‬‭out‬‭.‭p ‬ rintln‬‭(‭"‬ Normal"‬‭);‬
‭double‬‭fahrenheit‬‭= (‬‭temperature‬‭*‬‭9‬‭/‬‭5‬‭) +‬‭32‬‭;‬
‭System‬‭.‬‭out‬‭.‭p
‬ rintln‬‭(‭"‬ Temperature in Fahrenheit: "‬‭+‬‭fahrenheit‬‭);‬
‭}‬
‭}‬‭catch‬‭(‭I‬llegalArgumentException‬‭e‭)‬ {‬
‭System‬‭.‭o ‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ IllegalArgumentException caught: "‬‭+‬
‭e‭.‬‬‭getMessage‬‭());‬
‭}‬‭catch‬‭(‭T ‬ ooHotException‬‭e‭)‬ {‬
‭System‬‭.‭o ‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ TooHotException caught: "‬‭+‬‭e‬‭.‭g
‬ etMessage‬‭());‬
‭}‬‭catch‬‭(‭T ‬ ooColdException‬‭e‭)‬ {‬
‭System‬‭.‭o ‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ TooColdException caught: "‬‭+‬‭e‬‭.‭g‬ etMessage‬‭());‬
‭}‬
‭}‬
‭}‬
‭Output:‬
‭IllegalArgumentException caught: No argument provided‬‭o‬

‭11.Write‬ ‭an application that displays a series of at least five student ID‬
‭numbers (that‬ ‭you‬ ‭have‬
‭stored in an array) and asks the user to enter a numeric test‬ ‭score for the‬
‭student.‬ ‭Create‬
‭ScoreException‬ ‭class,‬ ‭and‬ ‭throw‬ ‭a‬ ‭ScoreException‬ ‭for‬ ‭the‬
‭class‬‭if‬
‭the user does not enter a valid score (zero to 100). Catch the ScoreException and‬
‭then display an appropriate‬
‭message. In addition, store a 0 for the student’s score. At the end of the application,‬
‭display all the student IDs and scores.‬

‭class‬‭ScoreException‬‭extends‬‭Exception‬‭{‬
‭public‬‭ScoreException‬‭(‬‭String‬‭message‬‭) {‬
‭super‬‭(‬‭message‬‭);‬
‭}‬
‭}‬

‭public‬‭class‬‭sample‬‭{‬
‭public‬‭static‬‭void‬‭main‬‭(‭S‬ tring‬‭[]‬‭args‬‭) {‬
‭String‬‭[]‬‭studentIDs‬‭= {‬‭"S001"‬‭,‬‭"S002"‬‭,‬‭"S003"‬‭,‬‭"S004"‬‭,‬‭"S005"‬‭};‬
‭int‬‭[]‬‭scores‬‭=‬‭new‬‭int‬‭[‭5
‬ ‬‭];‬

‭for‬‭(‭i‬nt‬‭i‬‭=‬‭0‭;‬‬‭i‬‭<‬‭studentIDs‬‭.‬‭length‬‭;‬‭i‬‭++) {‬
‭try‬‭{‬
‭System‬‭.‬‭out‬‭.‭p ‬ rint‬‭(‭"‬ Enter score for student "‬‭+‬‭studentIDs‬‭[‭i‬‬‭] +‬‭": "‬‭);‬
‭int‬‭score‬‭=‬‭Integer‬‭.‬‭parseInt‬‭(‬‭System‬‭.‭c‬ onsole‬‭().‬‭readLine‬‭());‬

‭if‬‭(‬‭score‬‭<‬‭0‬‭||‬‭score‬‭>‬‭100‬‭) {‬
‭throw‬‭new‬‭ScoreException‬‭(‬‭"Score should be between 0 to 100"‬‭);‬
‭}‬

s‭ cores‬‭[‭i‬‬‭] =‬‭score‬‭;‬
‭}‬‭catch‬‭(‬‭ScoreException‬‭e‭)‬ {‬
‭System‬‭.‬‭out‬‭.‭p ‬ rintln‬‭(‭"‬ ScoreException caught: "‬‭+‬‭e‬‭.‭g
‬ etMessage‬‭());‬
‭scores‬‭[‭i‬‬‭] =‬‭0‭;‬‬
‭}‬‭catch‬‭(‬‭NumberFormatException‬‭e‬‭) {‬
‭System‬‭.‬‭out‬‭.‭p ‬ rintln‬‭(‭"‬ NumberFormatException caught: Invalid‬‭input‬
‭provided"‬‭);‬
‭scores‬‭[‭i‬‬‭] =‬‭0‭;‬‬
‭}‬
‭}‬

‭ ystem‬‭.‬‭out‬‭.‭p
S ‬ rintln‬‭(‬‭"Student IDs and Scores:"‬‭);‬
‭for‬‭(‭i‬nt‬‭i‬‭=‬‭0‭;‬‬‭i‬‭<‬‭studentIDs‬‭.‬‭length‬‭;‬‭i‬‭++) {‬
‭System‬‭.‭o ‬ ut‬‭.‭p ‬ rintln‬‭(‭s‬ tudentIDs‬‭[‬‭i‬‭] +‬‭": "‬‭+‬‭scores‬‭[‭i‬‭]‬);‬
‭}‬
‭}‬
}‭ ‬
‭Output:‬
‭ nter score for student S001: 34‬
E
‭Enter score for student S002: 56‬
‭Enter score for student S003: 34‬
‭Enter score for student S004: 67‬
‭Enter score for student S005: 89‬
‭Student IDs and Scores:‬
‭S001: 34‬
‭S002: 56‬
‭S003: 34‬
‭S004: 67‬
‭S005: 89‬

‭or‬

‭ nter score for student S001: 234‬


E
‭ScoreException caught: Score should be between 0 to 100‬

‭12.Write a‬ J‭ ava‬ ‭program‬ ‭in‬ ‭which‬ ‭total‬ ‭4‬ ‭threads‬ ‭should‬


‭run.‬ ‭Set‬
‭different‬ ‭priorities‬ ‭to‬ ‭the‬ ‭thread.‬

‭public‬‭class‬‭sample‬‭{‬
‭public‬‭static‬‭void‬‭main‬‭(‭S
‬ tring‬‭[]‬‭args‬‭) {‬
‭Thread‬‭thread1‬‭=‬‭new‬‭Thread‬‭(‬‭new‬‭MyRunnable‬‭(),‬‭"Thread 1"‬‭);‬
‭Thread‬‭thread2‬‭=‬‭new‬‭Thread‬‭(‬‭new‬‭MyRunnable‬‭(),‬‭"Thread 2"‬‭);‬
‭Thread‬‭thread3‬‭=‬‭new‬‭Thread‬‭(‬‭new‬‭MyRunnable‬‭(),‬‭"Thread 3"‬‭);‬
‭Thread‬‭thread4‬‭=‬‭new‬‭Thread‬‭(‬‭new‬‭MyRunnable‬‭(),‬‭"Thread 4"‬‭);‬

t‭hread1‬‭.‭s‬ etPriority‬‭(‭T ‬ hread‬‭.‭M


‬ IN_PRIORITY‬‭);‬
‭thread2‬‭.‭s‬ etPriority‬‭(‭T ‬ hread‬‭.‭N
‬ ORM_PRIORITY‬‭);‬
‭thread3‬‭.‭s‬ etPriority‬‭(‭T‬ hread‬‭.‭N
‬ ORM_PRIORITY‬‭);‬
‭thread4‬‭.‭s‬ etPriority‬‭(‭T
‬ hread‬‭.‭M
‬ AX_PRIORITY‬‭);‬

t‭hread1‬‭.‭s‬ tart‬‭();‬
‭thread2‬‭.‭s‬ tart‬‭();‬
‭thread3‬‭.‭s‬ tart‬‭();‬
‭thread4‬‭.‭s‬ tart‬‭();‬
‭}‬

‭static‬‭class‬‭MyRunnable‬‭implements‬‭Runnable‬‭{‬
‭@‬‭Override‬
‭public‬‭void‬‭run‬‭() {‬
‭System‬‭.‭o
‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ Running thread: "‬‭+‬‭Thread‬‭.‬‭currentThread‬‭().‬‭getName‬‭());‬
‭}‬
‭}‬
}‭ ‬
‭Output:‬
‭Running thread: Thread 4‬
‭Running thread: Thread 3‬
‭Running thread: Thread 2‬
‭Running thread: Thread 1‬

‭13. Create‬ 4 ‭‬ ‭threads‬ ‭with‬ ‭priority‬ ‭1,3,5,7‬ ‭respectively.‬ ‭ pdate‬


U
‭a‬ ‭counter‬ ‭in‬ ‭each‬ ‭of‬ ‭the‬ ‭threads‬ ‭for‬ ‭10‬ ‭ms.‬
‭Print‬ ‭the‬ ‭final‬ ‭value of‬ ‭count‬ ‭for‬ ‭each‬ ‭thread.‬
‭public‬‭class‬‭sample‬‭{‬
‭public‬‭static‬‭void‬‭main‬‭(‭S‬ tring‬‭[]‬‭args‬‭) {‬
‭CounterThread‬‭thread1‬‭=‬‭new‬‭CounterThread‬‭(‬‭"Thread 1"‬‭,‬‭1‬‭);‬
‭CounterThread‬‭thread2‬‭=‬‭new‬‭CounterThread‬‭(‬‭"Thread 2"‬‭,‬‭3‬‭);‬
‭CounterThread‬‭thread3‬‭=‬‭new‬‭CounterThread‬‭(‬‭"Thread 3"‬‭,‬‭5‬‭);‬
‭CounterThread‬‭thread4‬‭=‬‭new‬‭CounterThread‬‭(‬‭"Thread 4"‬‭,‬‭7‬‭);‬

t‭hread1‬‭.‭s‬ tart‬‭();‬
‭thread2‬‭.‭s‬ tart‬‭();‬
‭thread3‬‭.‭s‬ tart‬‭();‬
‭thread4‬‭.‭s‬ tart‬‭();‬

‭try‬‭{‬
‭thread1‬‭.‭j‬oin‬‭();‬
‭thread2‬‭.‭j‬oin‬‭();‬
‭thread3‬‭.‭j‬oin‬‭();‬
‭thread4‬‭.‭j‬oin‬‭();‬
‭}‬‭catch‬‭(‬‭InterruptedException‬‭e‬‭) {‬
‭e‬‭.‭p
‬ rintStackTrace‬‭();‬
‭}‬

‭ ystem‬‭.‬‭out‬‭.‭p
S ‬ rintln‬‭(‬‭"Final value of count for each thread:"‬‭);‬
‭System‬‭.‬‭out‬‭.‭p ‬ rintln‬‭(‬‭thread1‬‭.‬‭getName‬‭() +‬‭": "‬‭+‬‭thread1‬‭.‭g
‬ etCount‬‭());‬
‭System‬‭.‬‭out‬‭.‭p ‬ rintln‬‭(‬‭thread2‬‭.‬‭getName‬‭() +‬‭": "‬‭+‬‭thread2‬‭.‭g
‬ etCount‬‭());‬
‭System‬‭.‬‭out‬‭.‭p‬ rintln‬‭(‬‭thread3‬‭.‬‭getName‬‭() +‬‭": "‬‭+‬‭thread3‬‭.‭g
‬ etCount‬‭());‬
‭System‬‭.‬‭out‬‭.‭p
‬ rintln‬‭(‬‭thread4‬‭.‬‭getName‬‭() +‬‭": "‬‭+‬‭thread4‬‭.‭g
‬ etCount‬‭());‬
‭}‬

‭static‬‭class‬‭CounterThread‬‭extends‬‭Thread‬‭{‬
‭private‬‭int‬‭count‬‭;‬

‭public‬‭CounterThread‬‭(‭S ‬ tring‬‭name‬‭,‬‭int‬‭priority‬‭) {‬
‭super‬‭(‬‭name‬‭);‬
‭setPriority‬‭(‬‭priority‬‭);‬
‭}‬

‭public‬‭int‬‭getCount‬‭() {‬
‭return‬‭count‬‭;‬
‭}‬

‭ ‬‭Override‬
@
‭public‬‭void‬‭run‬‭() {‬
‭for‬‭(‬‭int‬‭i‬‭=‬‭0‭;‬‬‭i‬‭<‬‭10‬‭;‬‭i‬‭++) {‬
‭count‬‭++;‬
‭try‬‭{‬
‭Thread‬‭.‭s‬ leep‬‭(‭1 ‬ 0‬‭);‬
‭}‬‭catch‬‭(‬‭InterruptedException‬‭e‬‭) {‬
‭e‭.‬‭p
‬ rintStackTrace‬‭();‬
‭}‬
‭}‬
‭}‬
‭}‬
}‭ ‬
‭Output:‬
‭Final value of count for each thread:‬
‭Thread 1: 10‬
‭Thread 2: 10‬
‭Thread 3: 10‬
‭Thread 4: 10‬

‭14. Write‬ ‭a‬ ‭Java‬ ‭Program‬ ‭to‬ ‭Use‬ ‭Method‬ ‭Level‬


‭Synchronization.‬

‭public‬‭class‬‭sample‬‭{‬
‭private‬‭int‬‭count‬‭=‬‭0‭;‬‬

‭public‬‭synchronized‬‭void‬‭increment‬‭() {‬
‭count‬‭++;‬
‭}‬

‭public‬‭synchronized‬‭void‬‭decrement‬‭() {‬
‭count‬‭--;‬
‭}‬

‭public‬‭synchronized‬‭int‬‭getCount‬‭() {‬
‭return‬‭count‬‭;‬
‭}‬

‭public‬‭static‬‭void‬‭main‬‭(‭S
‬ tring‬‭[]‬‭args‬‭)‬‭throws‬‭InterruptedException‬‭{‬
‭sample‬‭example‬‭=‬‭new‬‭sample‬‭();‬

‭Thread‬‭thread1‬‭=‬‭new‬‭Thread‬‭(()‬‭->‬‭{‬
‭for‬‭(‬‭int‬‭i‬‭=‬‭0‭;‬‬‭i‬‭<‬‭1000‬‭;‬‭i‭+
‬ +) {‬
‭example‬‭.‭i‬ncrement‬‭();‬
‭}‬
‭});‬

‭Thread‬‭thread2‬‭=‬‭new‬‭Thread‬‭(()‬‭->‬‭{‬
‭for‬‭(‬‭int‬‭i‬‭=‬‭0‭;‬‬‭i‬‭<‬‭1000‬‭;‬‭i‭+
‬ +) {‬
‭example‬‭.‭d ‬ ecrement‬‭();‬
‭}‬
‭});‬

t‭hread1‬‭.‭s‬ tart‬‭();‬
‭thread2‬‭.‭s‬ tart‬‭();‬

t‭hread1‬‭.‭j‬oin‬‭();‬
‭thread2‬‭.‭j‬oin‬‭();‬

‭System‬‭.‬‭out‬‭.‭p
‬ rintln‬‭(‬‭"Final count: "‬‭+‬‭example‬‭.‭g
‬ etCount‬‭());‬
‭}‬
}‭ ‬
‭Output:‬
‭Final count: 0‬

‭ 5. Write‬ ‭a‬
1 ‭Java‬ ‭Program‬ ‭to‬ ‭Use‬ ‭Block‬ ‭Level‬
‭Synchronization.‬
‭public‬‭class‬‭sample‬‭{‬
‭private‬‭int‬‭count‬‭=‬‭0‬‭;‬
‭private‬‭final‬‭Object‬‭lock‬‭=‬‭new‬‭Object‬‭();‬
‭public‬‭void‬‭increment‬‭() {‬
‭synchronized‬‭(‭l‬ock‬‭) {‬
‭count‬‭++;‬
‭}‬
‭}‬

‭public‬‭void‬‭decrement‬‭() {‬
‭synchronized‬‭(‭l‬ock‬‭) {‬
‭count‬‭--;‬
‭}‬
‭}‬

‭public‬‭int‬‭getCount‬‭() {‬
‭synchronized‬‭(‭l‬ock‬‭) {‬
‭return‬‭count‬‭;‬
‭}‬
‭}‬

‭public‬‭static‬‭void‬‭main‬‭(‬‭String‬‭[]‬‭args‬‭)‬‭throws‬‭InterruptedException‬‭{‬
‭sample‬‭example‬‭=‬‭new‬‭sample‬‭();‬

‭Thread‬‭thread1‬‭=‬‭new‬‭Thread‬‭(()‬‭->‬‭{‬
‭for‬‭(‭i‬nt‬‭i‬‭=‬‭0‭;‬‬‭i‬‭<‬‭1000‬‭;‬‭i‭+
‬ +) {‬
‭example‬‭.‭i‬ncrement‬‭();‬
‭}‬
‭});‬

‭Thread‬‭thread2‬‭=‬‭new‬‭Thread‬‭(()‬‭->‬‭{‬
‭for‬‭(‭i‬nt‬‭i‬‭=‬‭0‭;‬‬‭i‬‭<‬‭1000‬‭;‬‭i‭+
‬ +) {‬
‭example‬‭.‭d ‬ ecrement‬‭();‬
‭}‬
‭});‬

t‭hread1‬‭.‭s‬ tart‬‭();‬
‭thread2‬‭.‭s‬ tart‬‭();‬

t‭hread1‬‭.‭j‬oin‬‭();‬
‭thread2‬‭.‭j‬oin‬‭();‬

‭System‬‭.‭o
‬ ut‬‭.‭p
‬ rintln‬‭(‭"‬ Final count: "‬‭+‬‭example‬‭.‭g
‬ etCount‬‭());‬
‭}‬
‭}‬
‭ utput:‬
O
‭Final count : 0‬

You might also like