Results 1 to 17 of 17
Thread: Java program error
- 06-06-2011, 04:28 AM #1
Member
- Join Date
- May 2011
- Posts
- 29
- Rep Power
- 0
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:
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.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)); } }
- 06-06-2011, 04:47 AM #2
- 06-06-2011, 04:49 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 23
- Rep Power
- 0
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.
- 06-06-2011, 04:55 AM #4
Member
- Join Date
- May 2011
- Posts
- 29
- Rep Power
- 0
- 06-06-2011, 04:58 AM #5
Member
- Join Date
- Jan 2011
- Posts
- 23
- Rep Power
- 0
- 06-06-2011, 05:02 AM #6
- 06-06-2011, 05:02 AM #7
Member
- Join Date
- May 2011
- Posts
- 29
- Rep Power
- 0
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);"
- 06-06-2011, 05:05 AM #8
/me predicts the future.
- 06-06-2011, 05:11 AM #9
Member
- Join Date
- May 2011
- Posts
- 29
- Rep Power
- 0
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.
my new error is "BookExample.java:41: missing return statementJava 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)); } }
}
^"
- 06-06-2011, 05:12 AM #10
Member
- Join Date
- May 2011
- Posts
- 29
- Rep Power
- 0
- 06-06-2011, 05:13 AM #11
Member
- Join Date
- Jan 2011
- Posts
- 23
- Rep Power
- 0
- 06-06-2011, 05:16 AM #12
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?
- 06-06-2011, 05:23 AM #13
Member
- Join Date
- May 2011
- Posts
- 29
- Rep Power
- 0
Being that i'm new to java i'm actually lost right now.
- 06-06-2011, 05:27 AM #14
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?Java Code:public static double getArea(int a,int b,int c)
- 06-06-2011, 05:39 AM #15
Member
- Join Date
- May 2011
- Posts
- 29
- Rep Power
- 0
- 06-06-2011, 05:42 AM #16
Member
- Join Date
- Jan 2011
- Posts
- 23
- Rep Power
- 0
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)
- 06-06-2011, 05:59 AM #17
Member
- Join Date
- May 2011
- Posts
- 29
- Rep Power
- 0
Similar Threads
-
error in java program..
By mudit222 in forum New To JavaReplies: 2Last Post: 12-17-2010, 09:32 AM -
Error running java program using URL
By gio123bg in forum New To JavaReplies: 6Last Post: 06-30-2009, 06:26 PM -
error running java program
By bdasilva in forum New To JavaReplies: 1Last Post: 06-29-2009, 01:46 AM -
Same error msg with every program Java Textpad
By peterdfl in forum New To JavaReplies: 7Last Post: 04-17-2009, 04:49 AM -
How to get error codes using java program
By kasipandian in forum Web FrameworksReplies: 10Last Post: 05-25-2008, 05:00 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks