# How to think properly about classes

• 12-05-2017, 08:39 AM
TeacherZ
How to think properly about classes
Below is code I wrote to solve a problem on ProjectEuler.net. I am using the problems to help me learn Java.
I believe the logic in this code is sound. It does work, although maybe it is not the most elegant solution.

In any case, I can solve this, but I cannot seem to wrap my brain around how to create a class and make this a true piece of object-oriented programming. I am not asking for help to code this. I simply want some pointers on how to apply the concept of classes to my code. As a long time FORTRAN programmer, I think in terms of functions and subroutines, not classes.

Thanks for helping me to think differently!

The code:
Code:

```class Palindrome2 {     public static void main(String[] args){         int i; //outer loop     int j; // inner loop     int k; // loop for creating pSequence1     int loop; // loop counter for finding difference     int difference = 0;     int[] pSequence1 = new int[6];     int ijProduct;     boolean loopStatus = true;             for(i=999; i>99; i--){                 for(j=999; j>99; j--){                     difference = 0;           ijProduct = i*j;                     for (k=0; k<6; k++) {             pSequence1[k] = (int)(ijProduct%Math.pow(10,k+1)/Math.pow(10,k));           }                     for (loop=0;loop<3 ;loop++ ) {             difference = difference + Math.abs((pSequence1[loop] - pSequence1[5-loop]));           }                     if (difference == 0) {             System.out.println("The largest palindrome from multiplying 2 3-digit numbers is " + ijProduct);             System.out.println("The two factors for this palindrom are " + i + " and " + j);             loopStatus = false;             break;           } // end of if                   }             if (loopStatus == false) {             break;             } // end of if       }     } }```
• 12-05-2017, 03:39 PM
jim829
Re: How to think properly about classes
Even in an object oriented language, not all programming tasks require the use of classes. In Java you must have
an enclosing class to run a program. But if I just wanted to sum up the first 10 cubes using a loop (there is a formula),
I would do it like I would in Fortran.

A class is basically a user defined type and may include methods to examine,
display, or manipulate that type. An example would be complex numbers. They are in the form of a + bi where a and b
are real numbers and i is the imaginary number (square root of -1). There is no built in types in Java to handle them.
So you build your own. You would create it simply by passing a and b to the constructor. Then you write methods to
do basic math operations (add, etc) on different instances of that type and method(s) to display them.

But it can be much more than that. This could be a length topic to explain in a single thread.
So I suggest you do a web search on Object oriented programming, and read up on several sources. Then come back and
ask specific questions. We will be here.

Regards,
Jim
• 12-06-2017, 08:40 AM
TeacherZ
Re: How to think properly about classes
Quote:

Originally Posted by jim829
Even in an object oriented language, not all programming tasks require the use of classes. In Java you must have
an enclosing class to run a program. But if I just wanted to sum up the first 10 cubes using a loop (there is a formula),
I would do it like I would in Fortran.

But it can be much more than that. This could be a length topic to explain in a single thread.
So I suggest you do a web search on Object oriented programming, and read up on several sources. Then come back and
ask specific questions. We will be here.

Regards,
Jim

Jim,

Forgive me for editing your quote. My concern is not what are classes and objects. Insofar as I think I can at this stage of development, I do believe I understand the concept. My concern is that the mathematical projects I have been using to learn java have not lent themselves easily to object oriented programming.

My concern is I am being too closed-minded in terms of my previous experience with FORTRAN. Am I missing anything obvious within this code to make it an OO piece of code. If I understand you correctly, some problems do not lend themselves easily to an OO approach. If this problem is one of them, I am relieved. While I am sure the subtleties of OO programming will raise later questions, for now all I am looking for is either projects that can be interesting and have an OO aspect. If you know of any good web sites for this, please do pass them on.

Thanks again for letting me know I probably did not miss anything in terms of making this OO.
• 12-06-2017, 02:09 PM
jim829
Re: How to think properly about classes
In this case, I'm not certain how you would use a class just for finding the largest palindromic numbers.
But look at the Math class in the Java API. That class could have had a static method to ascertain whether
two supplied numbers resulted in a a palindromic number when multiplied. It could return true or false.

BTW, I decided to run your own program and then I wrote one myself. I didn't analyze your program but
I found the largest to be the following which is different than your output.

906609
913 993

Regards,
Jim
• 12-06-2017, 03:41 PM
TeacherZ
Re: How to think properly about classes
Jim,

There was a logical bug in my program. Once i allowed the loops to keep running without a break, I posted the answer to Project Euler and had that result (what you also posted) confirmed.

Thanks for the good advice about perusing the Math class. It beats re-inventing the wheel. But solving the problem w/o that was still a good learning exercise. Thanks for your patience with someone who is slow to think!
• 12-06-2017, 03:47 PM
jim829
Re: How to think properly about classes
Glad you got it working! BTW, the Math class does NOT contain a method to determine if a number is palindromic. I was just using that as an example. In any event you did take advantage of the class when using the Math.pow() method.

Regards,
Jim
• 12-08-2017, 05:15 PM
jim829
Re: How to think properly about classes
Well, I couldn't resist. I decided to take a quick look at your algorithm. And without offering
a different approach I thought I would point out one thing.
The following line:

Code:

`pSequence1[k] = (int)(ijProduct%Math.pow(10,k+1)/Math.pow(10,k));`
uses unnecessary calls to the Math.pow() method. Remember that for any exponent k, the following
holds (Note: in this case, ^ means exponentiation)

b^(k+1)/b^k == b^(k+1-k) == b^1 == b.

In your case, b is 10 so just put it there and eliminate the Math.pow() method.

Regards,
Jim
• 12-09-2017, 06:06 AM
TeacherZ
Re: How to think properly about classes
Jim,

Forgive me for not following you. For the code snippet:

Code:

`pSequence1[k] = (int)(ijProduct%Math.pow(10,k+1)/Math.pow(10,k));`
you suggest

b^(k+1)/b^k == b^(k+1-k) == b^1 == b and eliminate the call to Math.pow.
I am trying to isolate the value of a single digit within the 6 digit number I generate.
To that end I take the modulus of the number based on 10^(k+1). I divide that result by 10^k to get a single digit.
So I am not sure how to apply your suggestion. Since I could get numbers with 4 or 5 digits instead of 6, this seemed like the best way to avoid using a case or conditional to test for length of the int.

It's been my experience in FORTRAN that conditionals slow down code. Is it different in Java? I realize performance for this small problem is not an issue, but old habits die hard. But again, maybe I am missing something obvious...