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

Machine Problem 8 How To Implement Exception and Assertion

The document discusses implementing exception handling and assertions in a program. It creates a FindArray class to iterate through an array and catch ArrayIndexOutOfBoundsException, NullPointerException, and RuntimeException. It also includes a Main3 class with a try-catch block that takes user input, checks it is an integer between 1-3999, and catches InputMismatchException and RuntimeException. A NumberToWord class converts a number to words, catching exceptions if the number is outside the valid range.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views

Machine Problem 8 How To Implement Exception and Assertion

The document discusses implementing exception handling and assertions in a program. It creates a FindArray class to iterate through an array and catch ArrayIndexOutOfBoundsException, NullPointerException, and RuntimeException. It also includes a Main3 class with a try-catch block that takes user input, checks it is an integer between 1-3999, and catches InputMismatchException and RuntimeException. A NumberToWord class converts a number to words, catching exceptions if the number is outside the valid range.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

COLLEGE OF COMPUTER STUDEIS

INFORMATION TECHNOLOGY DEPARTMENT

CCS0023L
(Object Oriented Programming)

Machine Problem

8
How to implement exception and assertion

Student Name:
Guballa, Ramon Chino L.
Section:
1-A
Professor:
Doc Kirk Awat
I.

This study source was downloaded by 100000831283672 from CourseHero.com on 10-27-2022 22:49:17 GMT -05:00

https://www.coursehero.com/file/72609695/Machine-Problem-8-How-to-implement-exception-and-assertiondocx/
Create a Class FindArray that will try to iterate in the array displaying the content
in each index. The program should be able to catch an
ArrayIndexOutOfBoundsException, a NullPointerException and a RunTimeException.

package main3;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
*
* @author chinoguballa
*/
public class Main3 {

public static void main(String[] args) {


NumberToWord obj = new NumberToWord();
Scanner sc = new Scanner(System.in);
try{
System.out.println("Enter number from 1 to 3999");
int number = sc.nextInt();
if (number <1||number > 3999){
throw new RuntimeException("The range must be from 1 to 3999");
}
System.out.println(obj.convert(number));
}
catch(InputMismatchException e){
System.out.println("The input number must be a Integer");
}
catch(RuntimeException e){
System.out.println("Illegal command");
System.out.println(e);
}

package main3;
This study source was downloaded by 100000831283672 from CourseHero.com on 10-27-2022 22:49:17 GMT -05:00

https://www.coursehero.com/file/72609695/Machine-Problem-8-How-to-implement-exception-and-assertiondocx/
/**
*
* @author chinoguballa
*/
public class NumberToWord {

static {
private static final String[] specialNames = {
"",
" thousand",
};

private static final String[] tensNames = {


"",
" ten",
" twenty",
" thirty",
" forty",
" fifty",
" sixty",
" seventy",
" eighty",
" ninety"
};

private static final String[] numNames = {


"",
" one",
" two",
" three",
" four",
" five",
" six",
" seven",
" eight",
" nine",
" ten",
" eleven",
" twelve",
" thirteen",
" fourteen",
" fifteen",
" sixteen",
" seventeen",
" eighteen",
" nineteen"
};

private String toWord(int number) {


String current;
This study source was downloaded by 100000831283672 from CourseHero.com on 10-27-2022 22:49:17 GMT -05:00

https://www.coursehero.com/file/72609695/Machine-Problem-8-How-to-implement-exception-and-assertiondocx/
if (number % 100 < 20){
current = numNames[number % 100];
number /= 100;
}
else {
current = numNames[number % 10];
number /= 10;

current = tensNames[number % 10] + current;


number /= 10;
}
if (number == 0) return current;
return numNames[number] + " hundred" + current;
}

public String convert(int number) {

if (number == 0) { return "zero"; }

String prefix = "";

if (number < 0) {
number = -number;
prefix = "negative";
}

String current = "";


int place = 0;

do {
int n = number % 1000;
if (n != 0){
String s = toWord(n);
current = s + specialNames[place] + current;
}
place++;
number /= 1000;
} while (number > 0);

return (prefix + current).trim();


}
}

This study source was downloaded by 100000831283672 from CourseHero.com on 10-27-2022 22:49:17 GMT -05:00

https://www.coursehero.com/file/72609695/Machine-Problem-8-How-to-implement-exception-and-assertiondocx/
This study source was downloaded by 100000831283672 from CourseHero.com on 10-27-2022 22:49:17 GMT -05:00

https://www.coursehero.com/file/72609695/Machine-Problem-8-How-to-implement-exception-and-assertiondocx/
Powered by TCPDF (www.tcpdf.org)

You might also like