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

Formative4 Naong Machine Problem

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

Formative4 Naong Machine Problem

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

COLLEGE OF COMPUTER STUDEIS

INFORMATION TECHNOLOGY DEPARTMENT

CCS0023L
(Object Oriented Programming)

Machine Problem

8
How to implement exception and assertion

Student Name:
Joshua Miguel Naong
Section:
Tb21
Professor:
Sir Kirk Awat
I.

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.

SOURCE CODE:

import java.util.InputMismatchException;
import java.util.Scanner;
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);
}
}
}

PART II

public class NumberToWord {

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


}
}

SCREENSHOTS OF THE CODES:

You might also like