Results 1 to 9 of 9
  1. #1
    TeacherZ is offline Member
    Join Date
    Feb 2017
    Posts
    24
    Rep Power
    0

    Default 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:
    Java 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
    
          }
        
    }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,144
    Rep Power
    12

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    TeacherZ is offline Member
    Join Date
    Feb 2017
    Posts
    24
    Rep Power
    0

    Default Re: How to think properly about classes

    Quote Originally Posted by jim829 View Post
    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.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,144
    Rep Power
    12

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    TeacherZ is offline Member
    Join Date
    Feb 2017
    Posts
    24
    Rep Power
    0

    Default 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!

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,144
    Rep Power
    12

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,144
    Rep Power
    12

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

    Java 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    TeacherZ is offline Member
    Join Date
    Feb 2017
    Posts
    24
    Rep Power
    0

    Default Re: How to think properly about classes

    Jim,

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

    Java 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...

    Thanks for your helpful commenting!

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,144
    Rep Power
    12

    Default Re: How to think properly about classes

    You can't follow me because I made a silly mistake (mea culpa). I mis-read the code.

    Regards,
    Jim
    Last edited by jim829; 12-10-2017 at 03:32 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. not compiling classes to target/ classes
    By vandunen in forum Apache Maven
    Replies: 0
    Last Post: 11-12-2013, 05:55 PM
  2. Classes Help!!! two classes with object question.
    By stuckonjava in forum New To Java
    Replies: 16
    Last Post: 02-10-2012, 02:39 AM
  3. How do you properly use break?
    By louist in forum New To Java
    Replies: 3
    Last Post: 03-08-2011, 03:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •