0

How do I approach this?

The palindromic number 595 is interesting because it can be written as the sum of consecutive squares: 6^2 + 7^2 + 8^2 + 9^2 + 10^2 + 11^2 + 12^2. There are exactly eleven palindromes below one thousand that can be written as consecutive square sums. Note that 1 = 02 + 12 has not been included as this problem is concerned with the squares of positive integers. Write a Java program to produce an array of all positive palindromic numbers smaller than 1000. In this project, besides the main method, you should create the following methods: 1. A Boolean method "isPalindromic" will return True when the input integer is palindromic and False otherwise. 2. A method "display" to print the elements of the palindromic array. 3. A method "sumOfSquares" with two integer input arguments i and j, and returns the sum of squares of integers between i and j (including both).

23rd Apr 2021, 7:54 PM
J.C
6 Respuestas
+ 2
I rearranged your code according to the task requirements. For explanation you can refer to my earlier post. https://code.sololearn.com/cIMBI3H0jzfi/?ref=app
24th Apr 2021, 11:09 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 2
The second half of the task actually are tips on how to approach this. 1. Is quite easy. I suggest you convert the number to a string (array of characters) , but you can also do it arithmetically 2. Is a no brainer 3. Is simple math. Use a loop from i to j Then you have to use those methods in main, probably using different values for i and j in a double loop
23rd Apr 2021, 11:18 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 1
First of all your program produces the correct solution, so congrats on that. Now you basically just need to move stuff around to fulfill the structural requirements (if you take them as that). 1. Is already fulfilled. Looks like it can be optimized but leave that aside for now. 2. Signature should be: static void display(ArrayList<Integer> list) Then just move line 53 to display 3. Signature should be: static Integer sumOfSquares(int i, int j) This method should do only what the name suggests: calculate and return the sum of squares of integers between i and j. So the majority of the code has to be moved to main and instead of calculating n in the loops call sumOfSquares. You might think it is slower this way but that's not important. Allways optimize your code for readability, not performance, until performance becomes a real problem!
24th Apr 2021, 7:48 AM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 1
Thank you so much for that, I appreciate it so much! Have a great day man!
24th Apr 2021, 11:20 PM
J.C
0
https://code.sololearn.com/cA7A51A10a5A here is what I have so far, I don't know how to properly incorporate the display method. My "display" is under sumOfSquares. I am a beginner so apologies if I am overcomplicating this
24th Apr 2021, 2:10 AM
J.C
0
I'm sorry! I guess I dont understand what is wrong with my step 3 part of code now. https://code.sololearn.com/cA7A51A10a5A I'm lost on how to create a proper sum of squares with I and j now that I incorporate the array list
24th Apr 2021, 10:05 PM
J.C