Alejandro Loaiza

Greenhorn

Posts: 9

posted 4 years ago

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

}

}

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

}

}

Alejandro Loaiza

Greenhorn

Posts: 9

posted 4 years ago

Objectives

1. To understand the functional decomposition strategy for program design.

2. To be able to implement a Java program using methods.

Overview

Write a program to spell a positive integer of up to 3 digits in words. For example:

3 Three 17 Seventeen

90 Ninety 48 Forty Eight

705 Seven Hundred Five 211 Two Hundred Eleven

839 Eight Hundred Thirty Nine 666 Six Hundred Sixty Six

The spelling combines ones-words, teen-words, tens-words and the word Hundred:

Digit Ones-Word Teen-Word Tens-Word

0 Zero Ten Zero

1 One Eleven Ten

2 Two Twelve Twenty

3 Three Thirteen Thirty

4 Four Fourteen Forty

5 Five Fifteen Fifty

6 Six Sixteen Sixty

7 Seven Seventeen Seventy

8 Eight Eighteen Eighty

9 Nine Nineteen Ninety

By splitting the integer into its three digits, each digit can be used to determine part of the spelling. For example, 839 splits into a hundreds-digit of 8, a tens-digit of 3 and a ones-digit of 9; thus the spelling parts are the ones-word Eight, the word Hundred, the tens-word Thirty, and the ones-word Nine - Eight Hundred Thirty Nine

Specific Requirements

1. A completed main() and several method stubs are provided. Run the program as is.

2. Complete the decimalDigit() method. It is called by the threeDigitSpelling() method, so there would be a good place to test decimalDigit(). How?

3. Complete the digit-spelling helper methods, onesWord(), teenWord() and tensWord(). Again, test them by calling them from the threeDigitSpelling() method.

4. Follow the comments to complete an implementation of the threeDigitSpelling() method. Your implementation must use the three digit-spelling helper methods.

5. Test your implementation on several single-digit, two-digit and three-digit integers. Special care for teens and 3-digit numbers like 704 that have 0 for the middle digit!

1. To understand the functional decomposition strategy for program design.

2. To be able to implement a Java program using methods.

Overview

Write a program to spell a positive integer of up to 3 digits in words. For example:

3 Three 17 Seventeen

90 Ninety 48 Forty Eight

705 Seven Hundred Five 211 Two Hundred Eleven

839 Eight Hundred Thirty Nine 666 Six Hundred Sixty Six

The spelling combines ones-words, teen-words, tens-words and the word Hundred:

Digit Ones-Word Teen-Word Tens-Word

0 Zero Ten Zero

1 One Eleven Ten

2 Two Twelve Twenty

3 Three Thirteen Thirty

4 Four Fourteen Forty

5 Five Fifteen Fifty

6 Six Sixteen Sixty

7 Seven Seventeen Seventy

8 Eight Eighteen Eighty

9 Nine Nineteen Ninety

By splitting the integer into its three digits, each digit can be used to determine part of the spelling. For example, 839 splits into a hundreds-digit of 8, a tens-digit of 3 and a ones-digit of 9; thus the spelling parts are the ones-word Eight, the word Hundred, the tens-word Thirty, and the ones-word Nine - Eight Hundred Thirty Nine

Specific Requirements

1. A completed main() and several method stubs are provided. Run the program as is.

2. Complete the decimalDigit() method. It is called by the threeDigitSpelling() method, so there would be a good place to test decimalDigit(). How?

3. Complete the digit-spelling helper methods, onesWord(), teenWord() and tensWord(). Again, test them by calling them from the threeDigitSpelling() method.

4. Follow the comments to complete an implementation of the threeDigitSpelling() method. Your implementation must use the three digit-spelling helper methods.

5. Test your implementation on several single-digit, two-digit and three-digit integers. Special care for teens and 3-digit numbers like 704 that have 0 for the middle digit!

posted 4 years ago

Alejandro,

Firstly, please enclose all code in brackets to make it easy for people to read and answer your question. Secondly, you should break this problem down into smaller parts.

I would say a good place to start solving this is reviewing the Java tutorials and getting your basics right. By 'Basics' I mean how to declare variables and the generic syntax for methods. You might want to look at:

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/index.html

http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html

After you have a better understanding of the basics, you can approach the first question regarding the decimalDigit() method. The Algorithm provides much of the answer for you.

This is an arithmetic operation. You already see the number declared (See http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html)

Steps 2 and 3 also use arithmetic operators to produce the logic

Once you understand how to use operators in methods you can look more closely at this method.

So you already see the number being passed as an argument of type integer. Now use the number in a statement within the method which conforms to the Java rules for operators. For example, do you need to begin the method by initializing the variable (ie: int number = 0;)? Can you jump straight into using arithmetic operators to declare the number according to the algorithm?

Before exploring this question further, confirm that you have a grasp of the basics and I think operators are part of your problem. Hope this helps

Firstly, please enclose all code in brackets to make it easy for people to read and answer your question. Secondly, you should break this problem down into smaller parts.

I would say a good place to start solving this is reviewing the Java tutorials and getting your basics right. By 'Basics' I mean how to declare variables and the generic syntax for methods. You might want to look at:

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/index.html

http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html

After you have a better understanding of the basics, you can approach the first question regarding the decimalDigit() method. The Algorithm provides much of the answer for you.

This is an arithmetic operation. You already see the number declared (See http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html)

Steps 2 and 3 also use arithmetic operators to produce the logic

Once you understand how to use operators in methods you can look more closely at this method.

So you already see the number being passed as an argument of type integer. Now use the number in a statement within the method which conforms to the Java rules for operators. For example, do you need to begin the method by initializing the variable (ie: int number = 0;)? Can you jump straight into using arithmetic operators to declare the number according to the algorithm?

Before exploring this question further, confirm that you have a grasp of the basics and I think operators are part of your problem. Hope this helps

It is sorta covered in the JavaRanch Style Guide. |