challenge program:
Create a class called MathTrick in the newly created project folder.
Complete the static methods in the starter code.
Utilize Math.random() and any other methods from the Math class as needed.
Utilize .substring() where appropriate.
Each method should return a value of the correct data type.
Call the completed static methods in the main method to complete a program that does the following:
Generate a random 3-digit number so that the first and third digits differ by more than one.
Now reverse the digits to form a second number.
Subtract the smaller number from the larger one.
Now reverse the digits in the answer you got in step c and add it to that number (String methods must be used to solve).
Multiply by one million.
Subtract 733,361,573.
Hint: since replaceLtr is expecting a String, you should use String.valueOf(number) to create a String variable from the integer variable before step g.
Then, replace each of the digits in your answer, with the letter it corresponds to using the following table:
0 --> Y
1 --> M
2 --> P
3 --> L
4 --> R
5 --> O
6 --> F
7 --> A
8 --> I
9 --> B
Now reverse the letters in the string to read your message backward.
Open the StarterCode407.java(shown below) file to begin your program.
Notice that these instructions double as pseudocode and including pseudocode in a program provides documentation.
/**
* This Math trick and many more can be found at: http://www.pleacher.com/handley/puzzles/mtricks.html
*
*/
public class MathTrick {
// Step 1) Creates a random 3-digit number where the first and third digits differ by more than one
// Hint: use modulus for the last digit and divide by 100 for the first digit.
public static int getRandomNum()
{ int num = 0;
int firstDigit = 0;
int lastDigit = 0;
// complete the method
return num;
}
// Step 2 & 4) reverse the digits of a number
public static int reverseDigits (int num) {
// complete the method
}
// Step 7) replace characters in a string according to the chart
public static String replaceLtr(String str)
{
// complete the method
}
// Step 8) reverse the letters in a string
public static String reverseString(String str) {
// complete the method
}
public static void main(String[] args)
{
// 1. Generate a random 3-digit number so that the first and third digits differ by more than one.
// 2. Now reverse the digits to form a second number.
// 3. Subtract the smaller number from the larger one.
// 4. Now reverse the digits in the answer you got in step 3 and add it to that number.
// 5. Multiply by one million.
// 6. Subtract 733,361,573.
// 7. Then, replace each of the digits in your answer, with the letter it corresponds to using the table in the instructions.
// 8. Now reverse the letters in the string to read your message backward.
} // end main
} // end class