For this assignment your job is to create a two class application that examines the concept of a numerical palindrome.
A numerical palindrome is a non-negative whole number that is the
same forwards and backwards, e.g. 2332, 12321, etc. Here's the
catch: suppose you do the following. Start with any positive whole
number. If it's a palindrome, you're done; if it isn't, reverse the
number, add the reversal to the original value, and test this new
result. It it's a palindrome, you're done; otherwise repeat:
reverse, add, and test again, and so forth. This process almost
always leads, very quickly, to a palindrome.
Example:
Example:
Your job is to code this process, using the JOptionPane class to
accept an initial (String) input value.
Here is sample output for 552.
start value [552] 552 reverse-> 255 [output of toString method] new sum: 552 + 255 = 807 807 reverse-> 708 [output of toString method] new sum: 807 + 708 = 1515 1515 reverse-> 5151 [output of toString method] new sum: 1515 + 5151 = 6666 final value: 6666 number of steps: 3
To help you code the solution, we've provided the driver class (see
link below), which you MAY NOT alter. Your job here is to code the
NumPal class.
/JavaCS1/src/numericpalindrome/NumPalDriver.java
Here is a sample output with 997 as the input:
start value [997]
997 reverse-> 799
new sum: 997 + 799 = 1796
1796 reverse-> 6971
new sum: 1796 + 6971 = 8767
8767 reverse-> 7678
new sum: 8767 + 7678 = 16445
16445 reverse-> 54461
new sum: 16445 + 54461 = 70906
70906 reverse-> 60907
new sum: 70906 + 60907 = 131813
131813 reverse-> 318131
new sum: 131813 + 318131 = 449944
final value: 449944
number of steps: 6
As we indicated above, this process doesn't always work. For
example, 196 seems to go on and on. Thus it's necessary to cap the
number of iterations. Here we've capped them at 10. Also, because
numbers may grow large, you will need to use the whole number type
long rather than int. Thus you will need to use
the method Long.parseLong from the Long wrapper class, instead of
Integer.parseInt, to convert a string of digits into an actual
number.
Make sure you have a thorough pencil and paper understanding of the
problem before you even begin to think about coding.
A note on how to structure your solution. A NumPal object MUST have
four attributes: the current number, its reverse, the String
version of the current number, and String version of the reverse of
the current number. The numbers should be of type long, not
int.
How does a solution evolve? The driver code generates a sequence of
distinct objects that represent successive constructions of new
NumPal objects. The next() method in NumPal is the key. It
generates a new NumPal object from an old one, and your coding of
this method is the key activity of the assignment. Generating the
next numerical value from the current value is easy: you're just
adding the current value and the value of its reverse from the
current object. It's the other three object attributes for the next
NumPal object that require some thought. Notice, by the way, that
while each successive NumPal object is distinct, the variable
reference in each case is the same: p. The diagram below
illustrates the first two steps of NumPal object evolution for one
of the examples above. An important observation: while it's not
obvious how to generate the reverse of a number directly, it's
relatively easy to 1) turn a number into a String; 2) reverse a
String; and 3) turn a String into a number.
Here's a detailed account of what you need to do for this
assignment. You must develop a complete class, NumPal, which works
properly with the driver class above, and in that way allows you to
turn integer inputs into numerical palindromes. Your submitted code
must implement the following methods:
One final comment. There are clearly many ways to
structure a solution to this problem. We've chosen one, and you
need to adhere to the pattern we've spelled out above. You may not
alter the driver class in any way.
Enter you NumPal code in the box below. Do not include any import
statements.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// NumPal.java
public class NumPal {
// attributes
private String currentNumberString;
private long currentNumber;
private String reverseNumberString;
private long reverseNumber;
// constructor taking current number as String
public NumPal(String currentNumberString) {
// assigning to currentNumberString
this.currentNumberString = currentNumberString;
// converting to long and assigning to currentNumber
this.currentNumber = Long.parseLong(currentNumberString);
// reversing the text and assigning to reverseNumberString
this.reverseNumberString = reverseString(currentNumberString);
// parsing reverseNumberString to long to set reverseNumber
this.reverseNumber = Long.parseLong(reverseNumberString);
}
// method to reverse a String
private String reverseString(String str) {
String rev = "";
// looping in reverse order, appending characters to rev
for (int i = str.length() - 1; i >= 0; i--) {
rev += str.charAt(i);
}
// returning reversed string
return rev;
}
// checks if the number is palindrome
public boolean pal() {
// number is palindrome if currentNumberString and reverseNumberString
// are same
return this.currentNumberString.equals(this.reverseNumberString);
}
// returns current number
public long getCur() {
return currentNumber;
}
// returns reversed number
public long getRev() {
return reverseNumber;
}
// returns next NumPal object
public NumPal next() {
// adding current number with reverse number
long sum = currentNumber + reverseNumber;
// creating and returning a new NumPal object with the above sum
NumPal numPal = new NumPal(String.valueOf(sum));
return numPal;
}
@Override
public String toString() {
// returning the current number and reverse Strings in proper format
return currentNumberString + " reverse-> " + reverseNumberString;
}
}
/*OUTPUT*/
start value [997]
997 reverse-> 799
new sum: 997 + 799 = 1796
1796 reverse-> 6971
new sum: 1796 + 6971 = 8767
8767 reverse-> 7678
new sum: 8767 + 7678 = 16445
16445 reverse-> 54461
new sum: 16445 + 54461 = 70906
70906 reverse-> 60907
new sum: 70906 + 60907 = 131813
131813 reverse-> 318131
new sum: 131813 + 318131 = 449944
final value: 449944
number of steps: 6
For this assignment your job is to create a two class application that examines the concept...
// Client application class and service class Create a NetBeans project named StudentClient following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard. Add a class named Student to the StudentClient project following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard. After you have created your NetBeans Project your application class StudentC lient should contain the following executable code: package studentclient; public class...
Stack help. I need help with my lab assignment. Complete a method for a class named Palindrome that evaluates a string phrase to determine if the phrase is a palindrome or not. A palindrome is a sequence of characters that reads the same both forward and backward. When comparing the phrase to the same phrase with the characters in reverse order, an uppercase character is considered equivalent to the same character in lowercase, and spaces and punctuation are ignored. The...
Using C# Create an application class named LetterDemo that instantiates objects of two classes named Letter and CertifiedLetter and that demonstrates all their methods. The classes are used by a company to keep track of letters they mail to clients. The Letter class includes auto-implemented properties for the Name of the recipient and the Date mailed (stored as strings). Next, include a ToString() method that overrides the Object class’s ToString() method and returns a string that contains the name of...
java Generics Objectives: OOWorking with a Generic Class 1. Create a class called Node that is Generic. The class has one class attribute that is an element. It will need 2 Constructors, a setter and getter for the class attribute, and a toString method. 2. Write a Lab10Driver that will: Instantiate a Node Integer object with no value. I. Il. Ill. IV. a. Then call the set method to set the value to 5. Instantiate a Node String object with...
How can help me to create this program? PartADriver public class PartADriver { public static void main (String [] args) { // create two martians Martian m1 = new Martian(); Martian m2 = new Martian("Grey", 3); // display both martians Output.showMessage("After instantiation.\n" + "Martian #1: " + m1.toString() + "\nMartian #2: " + m2.toString()); // make the first martian speak 3 times for (int count = 0; count < 3; count++) m1.speak(); // make the second martian...
Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will have four instance variables: • An instance variable called “year” which will be of type int • An instance variable called “month” which will be of type int • An instance variable called “day” which will be of type int • An instance variable called “description” which will be of type String The “Appointment” class must also implement the following methods: • A getter...
Create a class called Restaurant that is the base class for all restaurants. It should have attributes for the restaurant's name{protected) and seats (private attribute that represents the number of seats inside the restaurant). Use the UML below to create the methods. Note, the toString prints the name and the number of seats. Derive a class Fastfood from Restaurant This class has one attribute - String slogan (the slogan that the restaurants uses when advertising - e.g., "Best Burgers Ever!")....
In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...
Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...
Last picture is the tester program! In this Assignment, you will create a Student class and a Faculty class, and assign them as subclasses of the superclass UHPerson. The details of these classes are in the UML diagram below: UHPerson - name : String - id : int + setName(String) : void + getName(): String + setID(int) : void + getID(): int + toString(): String Faculty Student - facultyList : ArrayList<Faculty - rank: String -office Hours : String - studentList...