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 method returning a double

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Location
    Louisiana
    Posts
    8
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with method returning a double

    Hello Programmers,

    This is my first post on my quest to self study Java more in depth since my recent graduation for my BSIT/Software Engineering Degree. I'm stuck on this exercise. I'm having trouble understanding why my method will not recognize that I am returning a double. Here is the requirements. And my code is below. I am using the acm libraries solely at this point and shouldn't use the Math libraries as this project is basically about reinventing the wheel. Finally, all my method projects to this point have been with int variable only. Thanks in advance.


    Write a method raiseRealToPower that takes a floating-point value x and an integer
    k and returns x^k. Implement your method so that it can correctly calculate the result
    when k is negative, using the relationship
    x^-k = 1 / x^k
    Use your method to display a table of values of πk for all values of k from –4 to 4.
    import acm.program.*;
     
    public class raiseRealToPower extends ConsoleProgram{
     
    	double PI = 3.1417;
    	int p;
    	double temp = 1.0;
     
     
    	public void run(){
     
     
    		for (int i = -4; i < 5; i++){
     
    			double result = power(PI, i);
    			println("The result of PI to the power of" + i + " is : " + result);
     
    		}
     
     
    	}
     
    	private double power(double x, int k){
    		double num = x;
     
    		if (k<0) {
    			p = -k;
     
    		}
    		if (k == 0) {
    			return 1.0;
     
    		}
     
    		for (int i = 1; i < p + 1; i++){
    			temp = num * temp;
     
    		}
     
    		if (k < 0) return (1/temp);
    		if (k > 0) return temp;
     
    	}
     
     
    	}


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help with method returning a double

    A few pointers that help us to help you.

    When posting code you should wrap it in code tags. Click the BB code link at the bottom of the page for more information.

    "It doesn't work" which is basically the gist of your post provides us with zero information. Does your code compile? If not post the exact and full error message. Does it produces incorrect output? Then provide a sample of what the actual output is and the desired output should be.

    In general the more relevant information you provide the easier it is for someone to spot your problem and guide you towards a solution.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Location
    Louisiana
    Posts
    8
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with method returning a double

    Thanks for the quick reply. Ok, here goes. If there is anything else i need, I will post for you quickly. This is my first programming forum and I'm new to the format.

    The error message during compiling is:
    Severity and Description: This method must return a result of type double
    Path: raiseIntToPower
    Resource : raiseRealToPower.java
    Location: line 23
    Creation Time: 1311275429260
    Id: 338

    import acm.program.*;
     
    public class raiseRealToPower extends ConsoleProgram{
     
    	double PI = 3.1417;
    	int p;
    	double temp = 1.0;
     
     
    	public void run(){
     
     
    		for (int i = -4; i < 5; i++){
     
    			double result = power(PI, i);
    			println("The result of PI to the power of" + i + " is : " + result);
     
    		}
     
     
    	}
     
    	private double power(double x, int k){
    		double num = x;
     
    		if (k<0) {
    			p = -k;
     
    		}
    		if (k == 0) {
    			return 1.0;
     
    		}
     
    		for (int i = 1; i < p + 1; i++){
    			temp = num * temp;
     
    		}
     
    		if (k < 0) return (1/temp);
    		if (k > 0) return temp;
     
    	}
     
     
    	}

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help with method returning a double

    What happens when k == 0?
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Location
    Louisiana
    Posts
    8
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with method returning a double

    It should return 1 as its answer since any number raised to the power of 0 = 1.

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help with method returning a double

    You know that.
    I know that.

    But the compiler doesn't care about what happened earlier in the method. All it knows is that it can get to the end of your method, past the 2 if statements, and there is no return statement.
    Improving the world one idiot at a time!

  7. #7
    Junior Member
    Join Date
    Jul 2011
    Location
    Louisiana
    Posts
    8
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with method returning a double

    Quote Originally Posted by Junky View Post
    You know that.
    I know that.

    But the compiler doesn't care about what happened earlier in the method. All it knows is that it can get to the end of your method, past the 2 if statements, and there is no return statement.
    ok, I will refigure the logic. Any suggestions(or hints to think about?)on how to run the iteration from -4 to 4 without using the Math library and its absolute value, power methods?

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help with method returning a double

    Quote Originally Posted by Mike_Chase View Post
    ok, I will refigure the logic.
    Use an if/else statement rather than separate if statements.
    Any suggestions(or hints to think about?)on how to run the iteration from -4 to 4 without using the Math library and its absolute value, power methods?
    I'm not sure what you are asking. What is wrong with how you have coded it? Other than magic numbers are frowned upon.

    By the way you failed the very first requirement of the assignment: "Write a method raiseRealToPower "
    Improving the world one idiot at a time!

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

    Mike_Chase (July 22nd, 2011)

  10. #9
    Junior Member
    Join Date
    Jul 2011
    Location
    Louisiana
    Posts
    8
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with method returning a double

    Thank you Junky for your advice, I have it working fine now. Here is my code now....
    I was incorrectly coding the if else sequence. I put in some booleans to make it cleaner looking. Once the neg and zero are defined, like the switch statement, the last is the default and doesn't need an IF.

    import acm.program.*;
     
    public class raiseRealToPower extends ConsoleProgram{
     
    	float PI = 3.1417f;
    	float temp = 1.0f;
     
     
    	public void run(){
     
     
    		for (int i = -4; i < 5; i++){
     
    			float result = raiseRealToPower(PI, i);
    			println("The result of PI to the power of " + i + " is : " + result);
     
    		}
    	}
     
    	private float raiseRealToPower(float x, int k){
     
    		float num = x;
    		int exp = k;
    		boolean neg = exp < 0;
    		boolean pos = exp > 0;
    		boolean zero = exp == 0;
    		float temp = 1f;
     
    		if (neg) {
     
    			for (int i = 1; i < -exp + 1; i++){
    				temp = temp * num;
    			}
    			return (1 / temp);
     
    		} else if (zero) {
    			return temp;
     
    		} else {
    			for (int i = 1; i < exp + 1; i++){
    			temp = temp * num;
    			}
    			return temp;
    		}
    	}
     
    }

  11. #10
    Junior Member
    Join Date
    Jul 2011
    Location
    Louisiana
    Posts
    8
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with method returning a double

    Quote Originally Posted by Junky View Post
    By the way you failed the very first requirement of the assignment: "Write a method raiseRealToPower "
    Yes..no matter how trivial, I will pay strict attention to the requirements of the assignment. Notice I have changed my variables to float primitives as well. Thanks again Junky.

Similar Threads

  1. [SOLVED] Read double from console without having to read a string and converting it to double.
    By Lord Voldemort in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 26th, 2011, 08:08 AM
  2. Method returning 0 for everything
    By JJTierney in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 4th, 2010, 08:51 PM
  3. Boolean method returning a boolean value
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 10:56 AM
  4. Method returning boolean
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 13th, 2010, 06:45 PM
  5. Error using public static double Method
    By stommy989 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 13th, 2010, 03:01 PM