Results 1 to 6 of 6
 02272011, 01:52 AM #1Member
 Join Date
 Feb 2011
 Posts
 3
 Rep Power
 0
Help with Value Returning Methods
I'm working on a program that will calculate various mathematical problems pertaining to different shapes using formulas & user input. The problem that I am having is: writing the static method named "circleArea" that will take in the radius of the circle and return the area using the formula A=PI*Rsquared (3.14*(radius*radius)). I've added the lines in the main method to call the method but it still isn't calculating correctly. Where am I messing up at? Thanks for your support.
Java Code:import java.util.Scanner; import java.lang.Math; /** This program demonstrates static methods */ public class Task1 { private static void printMenu() { System.out.println("This is a geometry calculator"); System.out.println("Choose what you would like to calculate"); System.out.println("1. Find the area of a circle"); System.out.println("2. Find the area of a rectangle"); System.out.println("3. Find the area of a triangle"); System.out.println("4. Find the circumfrence of a circle"); System.out.println("5. Find the perimeter of a rectangle"); System.out.println("6. Find the perimeter of a triangle"); System.out.println("Enter the number of your choice:"); } public static void main (String [] args) { int choice; //the user's choice double value = 0; //the value returned from the method char letter; //the Y or N from the user's decision to exit double radius; //the radius of the circle double area; //the area of the circle double length; //the length of the rectangle double width; //the width of the rectangle double height; //the height of the triangle double base; //the base of the triangle double side1; //the first side of the triangle double side2; //the second side of the triangle double side3; //the third side of the triangle //create a scanner object to read from the keyboard Scanner keyboard = new Scanner (System.in); //do loop was chose to allow the menu to be displayed first do { //call the printMenu method printMenu(); choice = keyboard.nextInt(); switch (choice) { case 1: System.out.print("Enter the radius of the circle: "); radius = keyboard.nextDouble(); //call the circleArea method and store the result in the value variable value=circleArea(radius); double area = 5.0; circleArea = (3.14 * (r * r)); System.out.println("The area of the circle is " + value); break; case 2: System.out.print("Enter the length of the rectangle: "); length = keyboard.nextDouble(); System.out.print("Enter the width of the rectangle: "); width = keyboard.nextDouble(); //call the rectangleArea method and store the result in the value variable System.out.println("The area of the rectangle is " + value); break; case 3: System.out.print("Enter the height of the triangle: "); height = keyboard.nextDouble(); System.out.print("Enter the base of the triangle: "); base = keyboard.nextDouble(); //call the triangleArea method and store the result in the value variable System.out.println("The area of the triangle is " + value); break; case 4: System.out.print("Enter the radius of the circle: "); radius = keyboard.nextDouble(); //call the circumference method and store the result in the value variable System.out.println("The circumference of the circle is " + value); break; case 5: System.out.print("Enter the length of the rectangle: "); length = keyboard.nextDouble(); System.out.print("Enter the width of the rectangle: "); width = keyboard.nextDouble(); //call the perimeter method and store the result in the value variable System.out.println("The perimeter of the rectangle is " + value); break; case 6: System.out.print("Enter the length of side 1 of the triangle: "); side1 = keyboard.nextDouble(); System.out.print("Enter the length of side 2 of the triangle: "); side2 = keyboard.nextDouble(); System.out.print("Enter the length of side 3 of the triangle: "); side3 = keyboard.nextDouble(); //call the perimeter method and store the result in the value variable System.out.println("The perimeter of the triangle is " + value); break; default: System.out.println("You did not enter a valid choice."); } keyboard.nextLine(); //consumes the new line character after the number System.out.println("Do you want to exit the program (Y/N)?: "); String answer = keyboard.nextLine(); letter = answer.charAt(0); }while (letter != 'Y' && letter != 'y'); } private static double circleArea(double r) { return Math.PI*r*r; } }

Does this code compile? Or are there compilation errors occurring? Just quickly looking over suggests that it shouldn't and that you should see errors. If so, you really should be posting them here so we can help you knowledgeably.
If it does compile, then you should post the correct code and tell us more about what the code is or isn't doing.
 02272011, 02:07 AM #3Member
 Join Date
 Sep 2010
 Posts
 56
 Rep Power
 0
I see two errors. One, you declared the variable "area" twice. And two, in line 55, why are setting a method equal to a double?
AB
 02272011, 03:42 AM #4Member
 Join Date
 Feb 2011
 Posts
 3
 Rep Power
 0
@Fubarable No it won't compile and run. It's saying line 67 which is: "area = 3.14159 * radius * radius;" is the error. I'm not understanding how to input the formula properly. I'm supposed to call the circleArea method and store the result in the value variable. Which included putting the formula in for the app to read. That's the only error that is displayed.
@javaman1 I see what errors you're pointing out. I was testing out a different suggestion and it didn't work. I had added "double area" to line 28 because I thought I was supposed to declare it there first. How do I fix it to recognize the formula?
*thanks for helping*

get rid of any references to area, the variable. Use the circleArea method, just the method.
 02272011, 05:58 AM #6Member
 Join Date
 Feb 2011
 Posts
 3
 Rep Power
 0
Ah ok, I removed "double area = 5.0; & circleArea = (3.14 * (r * r));" and the formula worked. It appears that by establishing the circleArea method and the return math formula outside of the switch that it computed fine. Is this the method that you always use in a program such as this, or just the easiest way?
Similar Threads

Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04162012, 11:00 PM 
Help with returning
By Da1dmoney in forum EclipseReplies: 3Last Post: 12152010, 02:38 AM 
Returning An Array
By elektronika in forum New To JavaReplies: 2Last Post: 12072009, 03:43 PM 
[SOLVED] Beginner, need help with Returning Values from Methods
By bobmasta5 in forum New To JavaReplies: 11Last Post: 12082008, 12:29 AM 
text box listeners and returning multiple strings from methods
By int80 in forum New To JavaReplies: 5Last Post: 07182008, 04:30 PM
Bookmarks