Question: Problem: Write a Java class (call it NumberString) which contains (note that each method below is an instance method, NOT static): - public static final
Problem: Write a Java class (call it NumberString) which contains (note that each method below is an instance method, NOT static):
- public static final int for the maximum numStrPalin size (assign 200 to it)
- private instance String called numStr (default is "0").
- private instance StringBuilder called numStrPalin which will hold a number palindrome generated by themakePalindrome (see below) method using numStr (default is a StringBuilder with "0")
- private instance StringBuilder called reverseStr (default is a StringBuilder with "0")
- a constructor which has a String parameter in which you CALL the mutator here (setNumStr) here, but if the mutator returns false, assign the defaults to the instance variables (if not already assigned defaults), EXCEPT assign "Invalid Parameter" to the numStrPalin instance variable
- a default constructor (assign or leave the instance variables to the defaults)
- accessors (public instance methods), one for numStr and an accessor for numStrPalin, but return a String for each including for numStrPalin)
- a method (I'm calling it makePalindrome) that has NO parameters, that creates a StringBuilder using the following algorithm:
+ store the characters in numStr in numStrPalin
+ clear the instance reverseStr StringBuilder and put the reverse of the chars in numStrPalin
+ If the chars in numStrPalin and the reverseStr are NOT the same, do the following:
* Add numStrPalin and the reverseStr using the addNumStr private method (see below)
* generate the reverse of the sum (chars in numStrPalin) and store the result in reverseStr (NOT a local nor new String/StringBuilder!)
* repeat from * until numStrPalin and the reverseStr have the same chars (USE THE EQUALS METHOD, NOT == ) OR the length of numStrPalin > maximum size (use the static final int)
+ if the length of numStrPalin > maximum size, change it to "No Solution < 200 digits" to it
- a private method (I'm calling it addNumStr) to add the values represented in reverseStr (StringBuilder parameter) to numStrPalin, storing the result in numStrPalin (instance variable), but DON'T convert the whole string to integers (or any number), since it may overflow! Only convert one char. at a time into an int and add corresponding chars in the same manner you would add by hand (keep track of the carry of a "column"!) Then store the "column" result in numStrPalin, converted back to a char.
- a public instance mutator method (setNumStr) that has a String parameter and returns a boolean. Check if the parameter is null, is empty, or doesn't have all digits and return false if so. If not, assign the parameter to the numStr private instance variable, then call makePalindrome, then return true. It is recommended to write your own method to check for all digits.
Note: NEVER ALLOW THE INSTANCE VARIABLES TO BE assigned nor left as null!
Write a Java application program in which main is in a separate class and file from the NumberString class (I'm calling it Prog4). In main, declare a NumberString ARRAY variable, and then in main:
? Call a static method in this Prog4 class that returns an array of NumberString objects based on user input (see 1. below), and assign to the local array declared in main.
? Call a static method in this Prog4 class that displays all the elements in the array of NumberString. (see 2. below)
1. In the method (no parameters) that returns a NumberString array, prompt the user for an integer for the array size, and read the integer into a local int (I'm calling size). If size <= 0, assign 1 to it. Instantiate an array of NumberString objects using the size. In a for loop, prompt the user for an integer, read the input into a String and assign a new instance of a NumberString (passing the input String) to the next element in the array. Return the array in a return statement.
2. In the method that displays the NumberString array (parameter), display to System.out EACH element's numStr and numStrPalin using its accessors (and display labels as shown in the test runs).
Notes:
? Do not declare any class-scope variables in the main (Prog4) class except the Scanner object for System.in.
? In ANY part of this program (including the NumberString class), EACH String and StringBuilder method MUST use the length method, NOT A HARD-CODED NUMBER nor a variable for the size !
? In ANY part of this program (including the NumberString class), when traversing the array of NumberStrings you MUST use the length variable (not a hard-coded number nor another variable).
? In ANY part of this program (including the NumberString class), DO NOT CONVERT ANY STRING OR STRINGBUILDER INTO AN ARRAY.
TEST RUN
Enter a credit card issuer number: 654321
The complete generated number from your input for the issuer ID: 6543 2107 7948 1437
Enter the number of elements in the array: 8
Enter an issuer ID# (6 digits) for element #0: 121212
Enter an account # (9 digits) for element #0: 123123123
Enter an issuer ID# (6 digits) for element #1: 454545
Enter an account # (9 digits) for element #1: 456456456
Enter an issuer ID# (6 digits) for element #2: 777777
Enter an account # (9 digits) for element #2: 888888888
Enter an issuer ID# (6 digits) for element #3: 123123
Enter an account # (9 digits) for element #3: 78978*789
Enter an issuer ID# (6 digits) for element #4: 787878
Enter an account # (9 digits) for element #4: 12345678
Enter an issuer ID# (6 digits) for element #5: 1234567
Enter an account # (9 digits) for element #5: 987654321
Enter an issuer ID# (6 digits) for element #6: 898989
Enter an account # (9 digits) for element #6: 1234512345
Enter an issuer ID# (6 digits) for element #7: 123?56
Enter an account # (9 digits) for element #7: 123123123
Credit Card # 0= 1212 1212 3123 1230
Credit Card # 1= 4545 4545 6456 4568
Credit Card # 2= 7777 7788 8888 8887
Credit Card # 3= 1231 2399 9999 9991
Credit Card # 4= 7878 7899 9999 9990
Credit Card # 5= 0000 0098 7654 3217
Credit Card # 6= 8989 8999 9999 9991
Credit Card # 7= 0000 0012 3123 1232
Test Run 2 (if input size <=0, changes input size to 1)
Enter a credit card issuer number: 321321
The complete generated number from your input for the issuer ID: 3213 2180 0971 9761
Enter the number of elements in the array: 0
Enter an issuer ID# (6 digits) for element #0: 232323
Enter an account # (9 digits) for element #0: 234234234
Credit Card # 0= 2323 2323 4234 2347
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
