Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Looking for a second opinion

  1. #1
    Member
    Join Date
    Mar 2021
    Location
    Ontario, Canada
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Looking for a second opinion

    Hello Everyone,
    I have the following assignment to do.
    "
    (Recursive power Method) Write a recursive method power(base, exponent) that, when called, returns
    base exponent

    For Example, power(3,4) = 3*3*3*3. Assume that exponent is an integer greater than or equal to 1.
    Hint: The recursion step should use the relationship
    base exponent = base * base exponent-1

    and the terminating condition occurs when exponent is equal to 1, because
    base exponent 1 = base

    Incorporate this method into a program that enables the user to enter the base and exponent.
    "

    Here is the code I wrote,
    package recursionpower;
     
    import java.util.Scanner;
     
    public class RecursionPower {
        int baseNumber;
        int exponent;
        int count;
        int base; 
        int answer;
     
        public static void main(String[] args) {
            RecursionPower begin = new RecursionPower();
            begin.UserEntry();                        
        }
     
        public void UserEntry(){        
            Scanner input = new Scanner(System.in);
            System.out.print("Enter base number -> ");
            base = input.nextInt();
            System.out.print("Enter the exponent number -> ");
            exponent = input.nextInt(); 
     
            baseNumber = base;
            Power(base, exponent);
     
        }
     
        public int Power(int base, int count){        
            if(count==1){
                System.out.println("1 - The Answer is: " + base);
            }
            else{
                System.out.println(count + " - " + base + " * " + baseNumber + " = " + (baseNumber * base));
                base = baseNumber * base;
                count--;
                Power(base, count);
            }
            return base;
     
        }
    }

    The above code works and the correct answer is produced every time,
     

    run:
    Enter base number -> 9
    Enter the exponent number -> 5
    5 - 9 * 9 = 81
    4 - 81 * 9 = 729
    3 - 729 * 9 = 6561
    2 - 6561 * 9 = 59049
    1 - The Answer is: 59049
    BUILD SUCCESSFUL (total time: 6 seconds)




    I am just looking for a more experience java programmer who can critique this and let me know what or how to make it better, if any.

    Thanks,

  2. #2
    Junior Member
    Join Date
    Jul 2019
    Posts
    26
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Looking for a second opinion

    also it is possible to store temporary results to array
    and print it in separated method or in main()
    but this is more demonstrative

    import java.util.Scanner;
     
    public class RecursionPower {
      public static void main(String[] args) {
        int[] input = RecursionPower.UserEntry();
        int base = input[0], exponent = input[1];
     
        System.out.printf("\nThe Answer is: %,d\n",
          power(base, exponent));
      }
     
      public static int[] UserEntry() {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter base number:");
        int base = input.nextInt();
        System.out.println("Enter the exponent number: ");
        int exponent = input.nextInt();
        System.out.println();
     
        return new int[] {base,  exponent};
      }
     
      public static int power(int base, int count) {
        String format = "(%d) %,9d * %d = %,9d\n";
     
        if (count == 1) {
          System.out.printf(format, count, 1, base, 1 * base);
          return base;
        } else {
          //return power(base, count-1) * base;
     
          int before = power(base, count - 1),
            result = before * base;
          System.out.printf(format, count, before, base, result);
          return result;
        }
      }
    }

  3. #3
    Member
    Join Date
    Mar 2021
    Location
    Ontario, Canada
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Looking for a second opinion

    Thanks Zemiak, I tried your code and it looks a lot cleaner and the output was a lot nicer as well.. Is there an advantage of using an array to store the two variables as pose to how I did it. IE. Does it speed up the processing time, etc. This project was so small it was hard to tell, but I am looking long range, when we get into larger projects.

  4. #4
    Junior Member
    Join Date
    Jul 2019
    Posts
    26
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Looking for a second opinion

    Hiding informations is not about speed, it's about
    . maintaining a large projects,
    it is easier to rewrite or change part of code if not direct depending on others parts.
    change probably does not create hidden errors In other parts
    . easy testing changes
    . preventing to unwishing rewrite from other code with the same variable name used

    . readability

    also you can hide variables in to for() loops and other code blocks if it is possible

    member fields of an object are for important properties of object functionality that other programmers usually can set from outside by setters
    https://en.m.wikipedia.org/wiki/Information_hiding
    Last edited by zemiak; October 31st, 2021 at 01:06 AM.

Similar Threads

  1. Replies: 0
    Last Post: June 14th, 2021, 04:08 PM
  2. mining opinion necessary
    By loaa hamada in forum Paid Java Projects
    Replies: 4
    Last Post: October 18th, 2013, 06:33 AM
  3. mining opinion necessary
    By loaa hamada in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 17th, 2013, 09:34 PM

Tags for this Thread