11 Replies - 573 Views - Last Post: 23 January 2012 - 06:41 PM Rate Topic: -----

#1 VIPERHlr  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 76
  • Joined: 05-August 11

Getting errors! triangle program

Posted 23 January 2012 - 07:33 AM

i am getting 2 errors with the following code:

  public class triangle {
   public static void main(String[]args)
   {
   double a;
   double b;
   double c;
   double area;
   }
   
      public static double area(double a,double b,double c,double area){
         a = 20;
         b = 12;
         c = 15;
         area = (b*c)/2;
         return area;
      	System.out.println(area);
      	
      }
     
   }


triangle.java:16: error: unreachable statement
System.out.println(area);
^
triangle.java:18: error: missing return statement
}
^
2 errors

What am i doing wrong ?

This post has been edited by VIPERHlr: 23 January 2012 - 07:34 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Getting errors! triangle program

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9035
  • View blog
  • Posts: 33,508
  • Joined: 27-December 08

Re: Getting errors! triangle program

Posted 23 January 2012 - 07:43 AM

Your design doesn't make a lot of sense. You shouldn't be assigning the variables values in your area() method. The whole point of parameters is so you can work with variables without knowing their values. So if the user passes 3, 4, and 5 as the side lengths, it will return a different result than if the user passes 12, 15, and 20.

Also, it doesn't make sense to pass area to a method that calculates the area. You should assign the side lengths in the main() method and pass them to the area() method, which returns the result.

As for your first error, you can't have any code after a return statement, since a return statement end the method call.

public static void main(String[] args){

   area = getArea(20, 15, 12);
}

public static double getArea(int a, int b, int c){
    //calculate based on a, b, and c
}


Was This Post Helpful? 1
  • +
  • -

#3 Amatore  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 47
  • Joined: 27-December 11

Re: Getting errors! triangle program

Posted 23 January 2012 - 07:47 AM

View PostVIPERHlr, on 23 January 2012 - 07:33 AM, said:

i am getting 2 errors with the following code:

  public class triangle {
   public static void main(String[]args)
   {
   double a;
   double b;
   double c;
   double area;
   }
   
      public static double area(double a,double b,double c,double area){
         a = 20;
         b = 12;
         c = 15;
         area = (b*c)/2;
         return area;
      	System.out.println(area);
      	
      }
     
   }


triangle.java:16: error: unreachable statement
System.out.println(area);
^
triangle.java:18: error: missing return statement
}
^
2 errors

What am i doing wrong ?



Ok, just a few things to point out

1) when naming things like methods and variables try not to use the same names. And when I say try not to I mean dont =]
so instead of
public static double area(double .. , ... )

use something like
public static double findArea(double .. , ...)

or something of that nature.

2) your problem with an unreachable statement, keep in mind that anything after a
return
statement in a method will not be executed, so this line is letting you know that.

3) I am unsure as to what the point of your main method is?
Was This Post Helpful? 1
  • +
  • -

#4 VIPERHlr  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 76
  • Joined: 05-August 11

Re: Getting errors! triangle program

Posted 23 January 2012 - 09:35 AM

Sorry i am still new to java, i sorted the problem out. I now want to add user input to change the variables being calculated in my methods. How would i do that ? sorry for the stupid questions, im still learning java.

public class triangle {
      public static void main(String[]args)
      {
         double a = 20;
         double b = 15;
         double c = 12;
         double perimeter = 0;
         double area = 0;
      
         perimeter = calcPerimeter(a,b,c,perimeter);
         area = calcArea(a,b,c,perimeter,area);
         System.out.println("The area of triangle A,B,C is: " + area);
      }
   
      public static double calcPerimeter(double a,double b,double c,double perimeter){
         perimeter = (a+b+c)/2;
         return perimeter;
      }
   		
      public static double calcArea(double a,double b,double c,double perimeter,double area){
         area = Math.sqrt(perimeter*(perimeter-a)*(perimeter-B)/>*(perimeter-c));
         return area;
      	
      	
      }
     
   }

Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9035
  • View blog
  • Posts: 33,508
  • Joined: 27-December 08

Re: Getting errors! triangle program

Posted 23 January 2012 - 09:41 AM

You can use the Scanner class for user input:
http://www.dreaminco...ortant-classes/
http://docs.oracle.c...il/Scanner.html

You don't know area when you invoke this method- that's why you're invoking it. So it doesn't make sense to have an area parameter. Remove it, and then return the result of your calculation. Parameters are for values you know ahead of time.
public static double calcArea(double a,double b,double c,double perimeter,double area){
    area = Math.sqrt(perimeter*(perimeter-a)*(perimeter-B)/>*(perimeter-c));
    return area;
}


Was This Post Helpful? 0
  • +
  • -

#6 VIPERHlr  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 76
  • Joined: 05-August 11

Re: Getting errors! triangle program

Posted 23 January 2012 - 09:48 AM

I used that method to calculate the area for the variables listed in the main method
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9035
  • View blog
  • Posts: 33,508
  • Joined: 27-December 08

Re: Getting errors! triangle program

Posted 23 January 2012 - 09:50 AM

Yes, but your method calcArea() should NOT have a parameter area. That's what I've been telling you for the past three posts. You don't know the area ahead of time, which is why you have the method. Parameters are for things you know ahead of time. Since you don't know area ahead of time, you shouldn't have an area parameter. Same goes for your calcPerimeter() method.
Was This Post Helpful? 0
  • +
  • -

#8 VIPERHlr  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 76
  • Joined: 05-August 11

Re: Getting errors! triangle program

Posted 23 January 2012 - 10:00 AM

Sorry i get it now, so i should just remove the parameter area completely ?
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9035
  • View blog
  • Posts: 33,508
  • Joined: 27-December 08

Re: Getting errors! triangle program

Posted 23 January 2012 - 10:02 AM

Yes.
Was This Post Helpful? 0
  • +
  • -

#10 VIPERHlr  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 76
  • Joined: 05-August 11

Re: Getting errors! triangle program

Posted 23 January 2012 - 10:19 AM

When i remove it, it gives me an errors:

triangle.java:16: error: cannot find symbol
perimeter = (a+b+c)/2;
^
symbol: variable perimeter
location: class triangle
triangle.java:16: error: cannot find symbol
perimeter = (a+b+c)/2;
^
symbol: variable a
location: class triangle
triangle.java:16: error: cannot find symbol
perimeter = (a+b+c)/2;
^
symbol: variable b
location: class triangle
triangle.java:16: error: cannot find symbol
perimeter = (a+b+c)/2;
^
symbol: variable c
location: class triangle
triangle.java:17: error: cannot find symbol
return perimeter;
^
symbol: variable perimeter
location: class triangle
triangle.java:21: error: cannot find symbol
area = Math.sqrt(perimeter*(perimeter-a)*(perimeter-B)*(perimeter-c));
^
symbol: variable area
location: class triangle
triangle.java:21: error: cannot find symbol
area = Math.sqrt(perimeter*(perimeter-a)*(perimeter-B)*(perimeter-c));
^
symbol: variable perimeter
location: class triangle
triangle.java:21: error: cannot find symbol
area = Math.sqrt(perimeter*(perimeter-a)*(perimeter-B)*(perimeter-c));
^
symbol: variable perimeter
location: class triangle
triangle.java:21: error: cannot find symbol
area = Math.sqrt(perimeter*(perimeter-a)*(perimeter-B)*(perimeter-c));
^
symbol: variable a
location: class triangle
triangle.java:21: error: cannot find symbol
area = Math.sqrt(perimeter*(perimeter-a)*(perimeter-B)*(perimeter-c));
^
symbol: variable perimeter
location: class triangle
triangle.java:21: error: cannot find symbol
area = Math.sqrt(perimeter*(perimeter-a)*(perimeter-B)*(perimeter-c));
^
symbol: variable b
location: class triangle
triangle.java:21: error: cannot find symbol
area = Math.sqrt(perimeter*(perimeter-a)*(perimeter-B)*(perimeter-c));
^
symbol: variable perimeter
location: class triangle
triangle.java:21: error: cannot find symbol
area = Math.sqrt(perimeter*(perimeter-a)*(perimeter-B)*(perimeter-c));
^
symbol: variable c
location: class triangle
triangle.java:22: error: cannot find symbol
return area;
^
symbol: variable area
location: class triangle
14 errors

This post has been edited by VIPERHlr: 23 January 2012 - 10:21 AM

Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9035
  • View blog
  • Posts: 33,508
  • Joined: 27-December 08

Re: Getting errors! triangle program

Posted 23 January 2012 - 10:21 AM

Declare the variable as a local variable rather than a parameter.
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,133
  • Joined: 06-March 08

Re: Getting errors! triangle program

Posted 23 January 2012 - 06:41 PM

or just

return (a+b+c)/2;

on the other hand I don't understand why the perimeter of a triangle would be the sum of its sides divided by 2
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1