Results 1 to 17 of 17
  1. #1
    Taszk is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default Java program error

    i want to use at least 2 methods

    i want to write a program that inputs 3 integers, which are sides of a triangle
    and determines the type of triangle it is:

    isosceles
    equilateral
    scalene
    not valid sides of a triangle

    If it is a valid triangle print the type of triangle it is, its area and perimeter.
    the program will then repeat for another set of sides, until a 0 is entered for the 1st side of the triangle (the other 2 sides should not be read in for this case)

    This is what i got so far:
    Java Code:
    import java.util.*;
    public class BookExample{
    	public static void main(String[] args)
    		{int a, b, c, p;
    			System.out.println("Enter the length of side \"a\" of the triangle to begin:");
    			Scanner in=new Scanner(System.in);
    			a=in.nextInt();			
    			
    	if(a==0)
    		System.exit(0);
    		
    			b=in.nextInt();
    			c=in.nextInt();
    			
    	getTriType();
    	getArea();
    	p = a+b+c;
    		}
    	
    public static void getTriType()
    	{if(a==0||b==0||c==0||a<0||b<0||c<0||a+b>=c||b+c>=a||a+c>=b)
    		{System.out.println("According to the given sides these are invalid sides of a Triangle");
    		}
    	else if(a==b&&a==c&&b==c)
    		{System.out.print("According to the given sides, since a,b and c are equal ");
    		 System.out.print("the triangle is an Equilateral triangle.");
    		}
    	else if(a==b||a==c||b==a||b==c||c==a||c==b)
    		{System.out.print("According to the given sides, since two sides are equal ");
    		 System.out.print("the triangle is an Isosceles triangle.");
    		}
    	else
    		{System.out.print("According to the given sides, since a,b and c are not equal ");
    		 System.out.print("the triangle is a Scalene triangle.");
    		}
    	}
    	
    public static double getArea(int a,int b,int c)
    	{double s = 0.5 * (a + b + c);
    	 double area = Math.sqrt(s*(s-a)*(s-b)*(s-c));
    	}
    }
    I know i'm still missing to output the area and perimeter but i'm trying to get rid of my error(s) first. My first error is "BookExample.java:16: getArea(int,int,int) in BookExample cannot be applied to ()" i know what it has to do with i just can't figure out how to fix the problem. Thanks for the help.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by Taszk View Post
    i know what it has to do with i just can't figure out how to fix the problem.
    That statement is contradictory. If you know what is has to do then you know how to fix it. How many parameters does the getArea method want? How many parameters are you passing to it?

  3. #3
    airowe is offline Member
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by Taszk View Post
    My first error is "BookExample.java:16: getArea(int,int,int) in BookExample cannot be applied to ()" i know what it has to do with i just can't figure out how to fix the problem. Thanks for the help.
    You need to make the parameters of the function definition the same as the parameters in the function call within your main.

    getArea(); is not the same as

    public static double getArea(int a, int b, int c)

    when you call getArea from main, make sure you include the values that you inputted from the console.

  4. #4
    Taszk is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by airowe View Post
    You need to make the parameters of the function definition the same as the parameters in the function call within your main.

    getArea(); is not the same as

    public static double getArea(int a, int b, int c)

    when you call getArea from main, make sure you include the values that you inputted from the console.
    so what your saying is when i call getArea from main i would need to call it as is getArea(int a,int b,int c); ?

  5. #5
    airowe is offline Member
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by Taszk View Post
    so what your saying is when i call getArea from main i would need to call it as is getArea(int a,int b,int c); ?
    Yep. Your function call needs to match some function definition, somewhere.

    In this problem, you want to pass the values that you received from the console (Your user) to the getArea() function so you can calculate the area of the user's triangle.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by Taszk View Post
    so what your saying is when i call getArea from main i would need to call it as is getArea(int a,int b,int c); ?
    You could try, although you would get a different error message. The compiler already knows what types your variables are, no need to tell it again.

  7. #7
    Taszk is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default

    Thanks. I knew more errors were going to follow up right behind that one. Now i'm getting something like "BookExample.java:16: '.class' expected
    getArea(int a,int b,int c);"

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    /me predicts the future.

  9. #9
    Taszk is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    You could try, although you would get a different error message. The compiler already knows what types your variables are, no need to tell it again.
    I see what your talking about i changed it and just called the variables instead and did the same to my other function which began getting an error that said something like 'can't find a' etc...(a,b,c); now my error is completely different.
    Java Code:
    import java.util.*;
    public class BookExample{
    	public static void main(String[] args)
    		{int a, b, c, p;
    			System.out.println("Enter the length of side \"a\" of the triangle to begin:");
    			Scanner in=new Scanner(System.in);
    			a=in.nextInt();			
    			
    	if(a==0)
    		System.exit(0);
    		
    			b=in.nextInt();
    			c=in.nextInt();
    			
    	getTriType(a,b,c);
    	getArea(a,b,c);
    	p = a+b+c;
    		}
    	
    public static void getTriType(int a, int b, int c)
    	{if(a==0||b==0||c==0||a<0||b<0||c<0||a+b>=c||b+c>=a||a+c>=b)
    		{System.out.println("According to the given sides these are invalid sides of a Triangle");
    		}
    	else if(a==b&&a==c&&b==c)
    		{System.out.print("According to the given sides, since a,b and c are equal ");
    		 System.out.print("the triangle is an Equilateral triangle.");
    		}
    	else if(a==b||a==c||b==a||b==c||c==a||c==b)
    		{System.out.print("According to the given sides, since two sides are equal ");
    		 System.out.print("the triangle is an Isosceles triangle.");
    		}
    	else
    		{System.out.print("According to the given sides, since a,b and c are not equal ");
    		 System.out.print("the triangle is a Scalene triangle.");
    		}
    	}
    	
    public static double getArea(int a,int b,int c)
    	{double s = 0.5 * (a + b + c);
    	 double area = Math.sqrt(s*(s-a)*(s-b)*(s-c));
    	}
    }
    my new error is "BookExample.java:41: missing return statement
    }
    ^"

  10. #10
    Taszk is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    /me predicts the future.
    Lol. Well i did mention above that after that error i'm pretty sure there was going to be more.

  11. #11
    airowe is offline Member
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by Taszk View Post
    my new error is "BookExample.java:41: missing return statement
    What method do you have written that needs a return statement?

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    What does that error message suggest to you? I think it is telling you that there is supposed to be a return statement but it is missing. Do you have any methods that SHOULD have a return statement but DON'T?

  13. #13
    Taszk is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default

    Being that i'm new to java i'm actually lost right now.

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Java Code:
    public static double getArea(int a,int b,int c)
    This is your getArea method. It's return type is double. This means you are promising that the method will return a double. Are you?

  15. #15
    Taszk is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Java Code:
    public static double getArea(int a,int b,int c)
    This is your getArea method. It's return type is double. This means you are promising that the method will return a double. Are you?
    no. I have a question being that in that method i have s and area as a type double does it matter if i change the type double to void?

    Edit:
    Java Code:
    public static void getArea(int a,int b,int c)
    Last edited by Taszk; 06-06-2011 at 06:43 AM.

  16. #16
    airowe is offline Member
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default

    The method is named "getArea". You don't want to change it to void or it won't "get" anything.

    You want that method to return a value of type double which is the area of the triangle.

    Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  17. #17
    Taszk is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by airowe View Post
    The method is named "getArea". You don't want to change it to void or it won't "get" anything.

    You want that method to return a value of type double which is the area of the triangle.

    Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Thanks a lot to the both of you i got the errors out the way now to try and finish it up. Hopefully i won't run into anymore mistakes but if i do i hope i could comeback.

Similar Threads

  1. error in java program..
    By mudit222 in forum New To Java
    Replies: 2
    Last Post: 12-17-2010, 10:32 AM
  2. Error running java program using URL
    By gio123bg in forum New To Java
    Replies: 6
    Last Post: 06-30-2009, 07:26 PM
  3. error running java program
    By bdasilva in forum New To Java
    Replies: 1
    Last Post: 06-29-2009, 02:46 AM
  4. Same error msg with every program Java Textpad
    By peterdfl in forum New To Java
    Replies: 7
    Last Post: 04-17-2009, 05:49 AM
  5. How to get error codes using java program
    By kasipandian in forum Web Frameworks
    Replies: 10
    Last Post: 05-25-2008, 06:00 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
  •