Question
This is my code:: public class NumPal{ String current; String reverse; boolean d; public NumPal(String s){ current=s; for (int k=0; k 799 new sum: 997
This is my code::
public class NumPal{ String current; String reverse; boolean d;
public NumPal(String s){ current=s; for (int k=0; k if(current==reverse) d=true; else d=false; } public boolean pal(){ return d; } public String toString(){ String t = current+"reverse->" +reverse; return t; } public String getCur(){ return current; } public String getRev(){ return reverse; } public NumPal next(){ String newNum= current+reverse; NumPal q = new NumPal(""+newNum); return q; } } Here is the driver: What my code produces: start value [997] NumPal@67424e82 new sum: 997 + 799 = 800796 NumPal@42110406 new sum: 800796 + 697008 = 697009497804 NumPal@531d72ca new sum: Exception in thread "main" java.lang.NumberFormatException: For input string: "408794900796408794900796" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Long.parseLong(Long.java:592) at java.lang.Long.parseLong(Long.java:631) at NumPal.getRev(NumPal.java:37) at NumPalDriver.main(NumPalDriver.java:13) What it should produce: 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 I would appreciate any help or cleaning up my code. Thank you.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); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started