Results 1 to 6 of 6
  1. #1
    SoCal.SD is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default 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; 
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    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

  4. #4
    SoCal.SD is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    @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*

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  6. #6
    SoCal.SD is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    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

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. Help with returning
    By Da1dmoney in forum Eclipse
    Replies: 3
    Last Post: 12-15-2010, 03:38 AM
  3. Returning An Array
    By elektronika in forum New To Java
    Replies: 2
    Last Post: 12-07-2009, 04:43 PM
  4. Replies: 11
    Last Post: 12-08-2008, 01:29 AM
  5. Replies: 5
    Last Post: 07-18-2008, 05:30 PM

Posting Permissions

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