Hello,
So I have been looking at this weeks code and I am completely dumbfounded. I have added the checklist for this assignment (Assignment4) below as I had trouble attaching them as files. The code you are looking at is what I am supposed to work on. The teacher just added his own work and we are supposed to add whatever the checklist is asking for. So to start the first problem is asking :
2.Complete the decimalDigit() method. It is called by the threeDigitSpelling() method, so there would be a good place to
test decimalDigit(). How?
I honestly don't know how to answer this. I have looked through RomanNumeral and I don't see what can help me out here. I need some guidance please. I need some helpful links that is related to this or something. I have crashed twice on this problem. Thank you!
//Program to spell positive 3-digit numberss
//
import java.util.*;
import javax.swing.*;
public class Assignment4
{
public static void main(
String[] args)
{
String input; //input buffer
boolean anotherInput = true; //repeat flag
while ( anotherInput )
{
//User enters a 3-digit number, or Cancel to quit
input = JOptionPane.showInputDialog(null, "Enter a Positive Number, 1 ... 999" +
"\nOR click on \"Cancel\" to quit" );
if (input == null) //User hit "Cancel"
anotherInput = false;
else //Number entered
{
//Parse input as an integer
int positive = Integer.parseInt( input );
//Reject integers that are non-positive, or more than 3 digits
if (positive <= 0 || positive > 999)
throw new RuntimeException("Invalid input: " + positive);
//Display the spelling (in words) of the input integer
String spelling = threeDigitSpelling( positive );
JOptionPane.showMessageDialog(null, positive + " " + spelling);
}
}
}
//Return the spelling (in words) of any positive number up to 3 digits
//Parameter number : any positive whole number 1 ... 999
private static String threeDigitSpelling(int number)
{
//Obtain the individual digits of the number
int hundreds = decimalDigit(2, number);
int tens = decimalDigit(1, number);
int ones = decimalDigit(0, number);
//The spelling that will be returned
String words = "";
//Intrepret the hundreds digit to extend the spelling
//Interpret the tens digit to extend the spelling.
//There are three different cases:
// a) tens > 1, b) tens == 1, c) tens == 0
//Interpret the ones digit to extend the spelling
//Return the spelling
String[] verse = {"Hickory Dickory Dock!",
"The Mouse Ran Up The Clock!",
"The Clock Struck One!",
"The Mouse Ran Down!",
"Hickory Dickory Dock!" };
return verse[ (new Random()).nextInt(verse.length) ];
}
//Helper method: Returns the ones-word spelling of a single digit
// Zero, One, Two, ..., Nine
//Parameter digit : 0 ... 9, identifies the desired ones-word
private static String onesWord(int digit)
{
return "" + digit;
}
//Helper method: Return the teen-word spelling of a single digit
// Ten, Eleven, Twelve, ..., Nineteen
//Parameter digit : 0 ... 9, identifies the desired teen-word
private static String teenWord(int digit)
{
return "" + digit;
}
//Helper method: Return the tens-word spelling of a single digit
// Zero, Ten, Twenty, ..., Ninety
//Parameter digit : 0 ... 9, identifies the desired tens-word
private static String tensWord(int digit)
{
return "" + digit;
}
//Helper method: Return a digit of any positive integer
//Parameter index : indexes the desired digit, 0 = least significant
//Parameter number : the integer whose digit is required
//Example: decimalDigit(3, 57419) returns 7
private static int decimalDigit(int index, int number)
{
//ALGORITHM:
//Step 1: Divide number by 10, index times
//Step 2: Find the remainder of number from Step 1 with 10
//Step 3: Return the remainder from Step 2
return (new Random()).nextInt(10);
}
}