0% found this document useful (0 votes)
66 views5 pages

Week 7 - Laboratory Activity: A. Using Java Arrays Problem

The document describes a laboratory activity where the objective is to write a Java program that takes a numeric input from 0-999999 and converts it to words. The program instructions provide details on using arrays, conditional statements, and input/output classes to write the number conversion program. Sample outputs are included to demonstrate converting inputs like 123 to "One Hundred Twenty-Three".

Uploaded by

John Rey Ubas
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)
66 views5 pages

Week 7 - Laboratory Activity: A. Using Java Arrays Problem

The document describes a laboratory activity where the objective is to write a Java program that takes a numeric input from 0-999999 and converts it to words. The program instructions provide details on using arrays, conditional statements, and input/output classes to write the number conversion program. Sample outputs are included to demonstrate converting inputs like 123 to "One Hundred Twenty-Three".

Uploaded by

John Rey Ubas
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

Week 7 - Laboratory Activity

Name: _John Rey J. Ubas_____________________________________________ Score:


Student Number: _202150559_________ Date: _18/03/2023___________
Section/Schedule _202/29052__________________ ______
Subject: Object Oriented Programming Lab Professor:_Mr. Archie G. Santiago_ 50

Objectives:
a. Declare and create one-dimensional arrays
b. Access array elements
c. Determine the number of elements in an array

A. Using Java Arrays

Problem: Write a Java Application that will let the user enter a Number from 0 – 999999 (Note:
Maximum of six digits only) and converts the number to its word form. For
example, if the number entered is 123, the java program should print “One
Hundred Twenty-Three” and so on.

Notes:
a. You may use any of the following classes for User Input: BufferedReader, JOptionPane or
Scanner.
b. Use Conditional Statements, Looping Statements (if necessary) and Arrays.
c. For the output, you may also use any of the classes mentioned in letter “a”.

See sample screenshot:


Directions:

Step 1. Open your NetBeans IDE, then create a New Project.

Step 2. Write the intended program for the problem. Refer to the “Notes” above for the Input
and Output requirements.

Step 3. Compile and Run your Java Application.

Step 4. Provide the following:

a. Source Code Listing. (20 points)

package week7labact;

import java.util.Scanner;

public class Week7LabAct {

public static void main(String[] args) {


int xintnumbr = 0;
Scanner scanner = new Scanner(System.in);
System.out.print("Choose and enter a number starting from 0 to 999999: ");
try{
// Reads the number
xintnumbr = scanner.nextInt();
if (xintnumbr == 0) {
System.out.print("Inputted number into words: Zero");
}
else if(xintnumbr > 999999){
System.out.println("System Failed, Incorrect inputted number! Maximum of 6 Digits
only!");
}
else{
System.out.println("Inputted number into words: " +
numberToWord(xintnumbr));
}
} catch (Exception e) {
System.out.println ("System Error! Incorrect Input..");
}
// ending the reader
scanner.close();
}
private static String numberToWord(int xintnumbr) {
// Variables which holds strings to represent the numbers
String words = "";
String unitsArray[] = { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten",
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen",
"Eighteen", "Nineteen"};
String tensArray[] = { "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy",
"Eighty", "Ninety"};

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

if (xintnumbr < 0) {
// converts number to a string
String numberStr = "" + xintnumbr;

numberStr = numberStr.substring(1);
// add minus before the number and convert the rest of number
return "minus " + numberToWord(Integer.parseInt(numberStr));
}

// divisible by 1 thousand
if ((xintnumbr / 1000) > 0) {
words += numberToWord(xintnumbr / 1000) + " thousand ";
xintnumbr %= 1000;
}
// divisible by 1 hundred
if ((xintnumbr / 100) > 0) {
words += numberToWord(xintnumbr / 100) + " hundred ";
xintnumbr %= 100;
}

if (xintnumbr > 0) {

if (xintnumbr < 20) {


words += unitsArray[xintnumbr];
} else {
words += tensArray[xintnumbr / 10];
if ((xintnumbr % 10) > 0) {
words += "-" + unitsArray[xintnumbr % 10];
}
}
}
return words;
}
}

b. Screenshots of the Final Output (At least FIVE sample output) (25 points)
Note: Include in the screenshots one sample output for “Incorrect Input (Max. of 6 digits only!)”.

c. Selfie Picture with the output. Note: Be sure the screen output is visible and not
blurred or too dark. (5 points)

You might also like