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 10 of 10

Thread: Help with Class Methods(Probably simple)

  1. #1
    Junior Member ShakeyJakey's Avatar
    Join Date
    May 2010
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Help with Class Methods(Probably simple)

    Jake here, im new to this forum, im also new to java and recently got a bit stuck in a programming exercise for my module. I know it'll probably be really easy to some people but i thought id share it because im still only learning.The question is as follows:

    Write a class method which calculates the power of one number raised to another. The method would be used as follows:
    	int x = getPower(2, 5); // 2 to the power of 5 = 32
    	int x = getPower(3, 4); // 3 to the power of 4 = 81
    Use this method to print the first 20 powers of 2.

    The class method i will use will be something like this:
    static int getPower(int num, int power){
     
    }

    Im basically just confused about how i would go about doing this and whether or not i need to use a for loop. Any help would be greatly appreciated.
    Thanks and il look forward to talking with you all throughout the forum
    Last edited by ShakeyJakey; May 26th, 2010 at 11:46 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with Class Methods(Probably simple)

    How would you do it manually?
    For example:
    if power = 1 return the number.
    Mult the number by itself
    if power = 2 return the product
    otherwise mult the above result by the number
    incr the power number
    if power number = desired, done
    otherwise go back and do it again

  3. #3
    Junior Member ShakeyJakey's Avatar
    Join Date
    May 2010
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with Class Methods(Probably simple)

    Thanks for your reply . Ive tried working it by what you've said. This is what i have so far
    class Powers{
    	static int getPower(int num, int power){
    		if(power==1){
    			num = num;
    		}
    		else if(power==2){
    			num = num*num;
    		}
                       ????
    		return num;
    	}
     
    	public static void main(String [] args){
    		int x = getPower(2, 2);
    		System.out.println(x);
    	}
    }
    This works fine and everything, but im still confused as to what to do next in the getPower() method. The question states that I have to list the first 20 powers of 2 so im presuming it requires a for loop somewhere (maybe where the ??? are) instead of using more if statements. Could you or anybody else help me with this bit of code? Apologies if im being noobish ha like i said im still learnin

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with Class Methods(Probably simple)

    First suggestion, use a new variable to hold the results, don't reuse num.
    You need a loop where you multiply the result by num while using the value of power to control the number of times thru the loop.

    Look at this:
    power=2 result= num*num
    power=3 result=num*num*num
    power=4 result=num*num*num*num
    See how for each higher value of power you multiply the result by num one more time.
    Last edited by Norm; May 26th, 2010 at 05:06 PM.

  5. The Following User Says Thank You to Norm For This Useful Post:

    ShakeyJakey (May 27th, 2010)

  6. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Help with Class Methods(Probably simple)

    Alternatively, you can do something like this:

    a^b = c
    b * ln(a) = ln(c)

    where ln is the natural logarithm.

    Now all you have to do is use a logarithm table and extrapolate to an answer, or use the "nearest neighbor" method with a logarithm table (alternatively you can call the exp() method to do the first method for you).

    Without using this, though, you would want to use a loop of some kind (either a while or a for loop), and iterate from 0 to the exponent number. Inside the loop you just need to multiply the result (originally 1) by the base.

  7. The Following User Says Thank You to helloworld922 For This Useful Post:

    ShakeyJakey (May 27th, 2010)

  8. #6
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Help with Class Methods(Probably simple)

    I'd suggest doing the multiplication in a loop, as Norm suggests.

    If you're allowed to use the Java library, you could just call Math.pow(num, power)

  9. The Following User Says Thank You to dlorde For This Useful Post:

    ShakeyJakey (May 27th, 2010)

  10. #7
    Junior Member ShakeyJakey's Avatar
    Join Date
    May 2010
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with Class Methods(Probably simple)

    Thanks alot i solved it just there with all of your help It was just a small question out of an exercise sheet that confused me and I hate moving on and leaving questions unsolved. Incase you were wondering this was my solution.
    class Powers{		
    	static int pow(int x, int y){
    		int result = 1;
    		for(int z = 0; z < y; z++){
    			result = result * x;		
    		}
    		return result;
    	}
     
    	public static void main(String [] args){
    		int num;
    		for(int i = 1; i < 21; i++){
    			num = pow(2, i);
    			System.out.println("2 to the power of " + i + " = " + num);
    		}
    	} 
    }

    Feel free to suggest some tips for future coding as i am here to continue learning more Thanks again.
    Last edited by ShakeyJakey; May 27th, 2010 at 09:01 AM.

  11. #8
    Junior Member ShakeyJakey's Avatar
    Join Date
    May 2010
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with Class Methods(Probably simple)

    Quote Originally Posted by dlorde View Post
    I'd suggest doing the multiplication in a loop, as Norm suggests.

    If you're allowed to use the Java library, you could just call Math.pow(num, power)
    Yea i only read that after i used the first method. This is how i done it the other way
    import java.math.*;
     
    class PowersAlt{
    	static int pow(int num, int power){
    		int x = (int)Math.pow(num,power);
    		return x;
    	}
    	public static void main(String [] args){
    		int num;
    		for(int i=1;i<21;i++){
    			num = pow(2, i);
    			System.out.println("2 to the power of " + i + " = " + num);
    		}
    	}
    }

  12. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with Class Methods(Probably simple)

    Your second method of coding it only calls another method. It doesn't do anything.

  13. #10
    Junior Member ShakeyJakey's Avatar
    Join Date
    May 2010
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with Class Methods(Probably simple)

    Quote Originally Posted by Norm View Post
    Your second method of coding it only calls another method. It doesn't do anything.
    I know, I didnt use that, I used the first method. I only posted it to show how I would get the same answer as the previous method. Because the question stated the method to be in the form num = pow(2, i); or num = getPower(2, i); when being called in the main method

Similar Threads

  1. Use of Exceptions and Methods of The String Class
    By cagataylina in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2011, 01:56 AM
  2. Methods in java class files are not recognized.
    By Girish in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 18th, 2010, 05:44 AM
  3. Replies: 0
    Last Post: April 11th, 2010, 08:56 AM
  4. using the setSize and setLocation methods of Rectangle class
    By etidd in forum Java Theory & Questions
    Replies: 1
    Last Post: March 6th, 2010, 04:03 PM
  5. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 03:47 AM

Tags for this Thread