Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this assignment your job is to create a two class application that examines

ID: 3621012 • Letter: F

Question

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 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:


152 (no)
152 + 251 = 403 (no)
403 + 304 = 707 (yes)

Example:

552 (no)
552 + 255 = 807 (no)
807 + 708 = 1515 (no)
1515 + 5151 = 6666 (yes)

Your job is to code this process, using the JOptionPane class to accept an initial input value.

Here is sample output for 552.



552 reverse-> 255
new sum: 552 + 255 = 807
807 reverse-> 708
new sum: 807 + 708 = 1515
1515 reverse-> 5151
new sum: 1515 + 5151 = 6666
final value: 6666
number of steps: 3


To help you code the solution, we'll provide the driver class (see link below). Your job, then, is to code the NumPal class.


import javax.swing.JOptionPane;
public class NumPalDriver {

public static void main(String[] args){
String start = JOptionPane.showInputDialog("Enter a number: ");
System.out.println("start value ["+ start + "]");
NumPal p = new NumPal(start);
int ctr = 0;
while (!p.pal() && (ctr < 10)){
System.out.println(p.toString());
System.out.print("new sum: ");
System.out.println(p.getCur()+" + "+p.getRev()+ " = " +
(p.getCur()+p.getRev() ));
p = p.next();
ctr++;
}
System.out.println("final value: " + p.getCur());
System.out.println("number of steps: " + ctr);
}
}


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, we've used the whole number type long rather than int, and thus we've used the method Long.parseLong from the Long wrapper class, instead of Integer.parseInt.


Make sure you you have a thorough pencil and paper understanding of the problem before you even begin to think about coding. Also, the key idea here is having a clear understanding of various helper methods that will help with the coding of NumPal. What are they?


A note on how to structure your solution. A NumPal object should 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 version of the "current" value from the old one is easy: you're just adding cur and rev from the old object. It's the other three object attributes 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 NumPal object evolution for one of the examples above. An important observation: while it's not obvious how to generate the reverse of number directly, it's relatively easy to 1) to turn a number into a String; and 2) reverse a String.



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 indicated pattern.




Explanation / Answer

please rate - thanks

message me if any problems

public class NumPal
{private long num,rev;
private String snum,srev;
public NumPal(String ns){
int digit,n;
n=Integer.parseInt(ns);
num=n;
rev=0;
while(n>0)
   {digit=n%10;
    rev=rev*10+digit;
    n/=10;
}
snum= new Long(num).toString();
srev= new Long(rev).toString();
}
public boolean pal()
{if(num==rev)
     return true;
else
    return false;
}
public long getCur()
{return num;
}
public long getRev()
{return rev;
}
public NumPal next()
{
return new NumPal(Long.toString(rev+num));
}
public String toString()
{return snum+" reverse -> "+srev;
}
}

------------------------------------------

import javax.swing.JOptionPane;
public class NumPalDriver {

public static void main(String[] args){
String start = JOptionPane.showInputDialog("Enter a number: ");
System.out.println("start value ["+ start + "]");
NumPal p = new NumPal(start);
int ctr = 0;
while (!p.pal() && (ctr < 10)){
System.out.println(p.toString());
System.out.print("new sum: ");
System.out.println(p.getCur()+" + "+p.getRev()+ " = " +
(p.getCur()+p.getRev() ));
p = p.next();
ctr++;
}
System.out.println("final value: " + p.getCur());
System.out.println("number of steps: " + ctr);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote