Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please dont waste my question if you cant do it. Thank you. Above you should see

ID: 3822910 • Letter: P

Question

Please dont waste my question if you cant do it. Thank you.

Above you should see a .java file: RecursionDemo. You are to create GUI for this program. It must allow user input and program output. Design your GUI to follow the functionality of the the original programs. In other words, familiarize yourself with how each of the programs work before attempting your GUI version. This time try to make your GUI user friendly. Tell the user what they must do and what outcome to expect.

Add comments to code please.

Explanation / Answer

Hi,

This program is a recursive implementation of Numbers to Word Program. It accepts a single or multiple digit number from the user and converts it into word.
Example- Input : 23 , Output : two three

Please find below the comments for the given code-

import java.util.Scanner;

public class RecursionDemo
{
public static void main(String[] args)
{
/* step 1 - Taking User input for the number which needs to be converted to words*/
System.out.println("Enter an integer:");
Scanner keyboard = new Scanner(System.in);;
int number = keyboard.nextInt( );
System.out.println("The digits in that number are:");
/*step 2 - Calling displayAsWords function which takes the given inout number as an argument*/
displayAsWords(number);
System.out.println( );

System.out.println("If you add ten to that number, ");
System.out.println("the digits in the new number are:");
number = number + 10;
displayAsWords(number);
System.out.println( );
}

/**
Precondition: number >= 0
Displays the digits in number as words.
*/
// step 3- The program will now jump into definition of displayAsWords function
public static void displayAsWords(int number)
{
// step 4- if the number is less than 10, we need to call the getWordFromDigit function.
if (number < 10)
System.out.print(getWordFromDigit(number) + " ");
else //If the number has two or more digits, we need take the modulus of the number and get the value for unit place of the number.
// Then we need to pass this number to the displayAsWords as it is converted to single digit.
//Likewise, the values will be calculated for the tens place and hundredth place digit as well.
{
displayAsWords(number / 10);
System.out.print(getWordFromDigit(number % 10) + " ");
}
}
/*Example- Lets say the number user entered is 123. Then, the if condition fails as number>10.
The else part is executed now. Displaywords(123/10) which is 12 is used to call the displayAsWords function again. So displayAsWords(number / 10) just stripes out the last digit of the number.
Now, getWordFromDigit is called with getWordFromDigit(123%10) which is 3. Hence, getWordFromDigit(3) is passed and it returns "three"
In the same way, all other steps can be iterated.

*/
//step 5(b)- The program will just into getWordFromDigit now from step 4 if number is less than 10
// In this function getWordFromDigit, we have a case statement which maps each digit with its equivalent word value. This function can handle only single digit values
private static String getWordFromDigit(int digit)
{
String result = null;
switch (digit)
{
case 0: result = "zero"; break;
case 1: result = "one"; break;
case 2: result = "two"; break;
case 3: result = "three"; break;
case 4: result = "four"; break;
case 5: result = "five"; break;
case 6: result = "six"; break;
case 7: result = "seven"; break;
case 8: result = "eight"; break;
case 9: result = "nine"; break;
default:
System.out.println("Fatal Error.");
System.exit(0);
break;
}
return result;
}
}