Triangle program
|
|||
|
Rank: ? (11)
Member #: 28462 |
i am making a program that ask a user for 3 sides of a triangle and then calculates the area and determines whether it is a right, isosceles or scalene triangle but i am having a problem
Code:
the * in the if statement between Math.pow(sideA, 2.0) and Math.pow(sideB, 2.0) is the problem. this is the error: File: C:\Documents and Settings\Owner\My Documents\java programs\AreaTriangle.java [line: 36] Error: unexpected type required: variable found : value what is wrong, it was working before and i started to adjust it and it just stopped working. and dont get on me for not documenting my program, i havent gotten to that yet lol » Post edited 2006-09-14, 03:56am by misterhaan.
|
||
|
|||
|
|||
|
Rank: ? (4821)
Member #: 3416 |
the * in the if statement between Math.pow(sideA, 2.0) and Math.pow(sideB, 2.0) is the problem.
actually, the problem is that you have = where you want == the reason you get a compiler error is because on the left of the = you have something that's not a variable. i also noticed a couple of other things: public static double area(double sideA,double sideB,double sideC,double area,double s)
you should not pass area or s to the function -- instead declare them inside the function since they are only used there. their declarations should also be removed from main(). if(sideC == sideA || sideC == sideB)
return "Isosceles"; what if side A and B are equal? also, are you just considering an equilateral triangle to be isosceles? Math.pow(sideA, 2.0) * Math.pow(sideB, 2.0) = Math.pow(sideC, 2.0)
this is the pythagorean theorem, right? so maybe the * is wrong, since it's a^2 + b^2 = c^2
my mind is like a steel trap! it only hangs on to the big stuff. visit my forums at track7.org
|
||
|
|||
|
|||
|
Rank: ? (11)
Member #: 28462 |
there were a lot of little errors in that program. i find it really hard to find the little errors in the programs i write. i fixed them and changed a few things around and now it runs perfect. thanks a lot. i just have one more question. how do i throw error in case the user puts in a negative number? my teacher was not clear at all on how to do that
|
||
|
|||
|
|||
|
Rank: ? (119)
Member #: 28292 |
I write you a solution for processing of a sideA:
Code:
For other you should do it in the same way. |
||
|
|||
|
|||
|
Rank: ? (11)
Member #: 28462 |
Code:
thats what i came up with to try and catch user errors but it doesnt work. if i type in 0 for a side, it calculates the triangle with 1 side being 0, it doesnt re-ask for that side. what now i do now? |
||
|
|||
|
|||
|
Rank: ? (119)
Member #: 28292 |
You have from beginning sideA - sideB equal to 0. Your while blocks while(sideA < 0) etc. will never be performed because your side is not lesser as 0. I suppose you need that the sides of triangle should be more than 0. Than do it so while(sideA<= 0). If you want that the side could be 0, than try do{}while(sideA < 0);
|
||
|
|||
|
|||
|
Rank: ? (11)
Member #: 28462 |
ok i got it to run without errors. now i have to write another one that reads input from a file instead of asking the user. help would be appreciated, i get more help here than i do anywhere else
|
||
|
|||
|
|||
|
Rank: ? (119)
Member #: 28292 |
|||
|
|||
|
|||
|
Rank: ? (11)
Member #: 28462 |
ok i understand using filereader to a point. say that the sides of a triangle are listed in the txt file that the program is readin and the 3 sides in that file are all on one line like this: '3', '4', '5' how do i get side a to be 3, side b to be 4 and so on? and the txt file is going to have more than 1 set of sides for a triangle, it will have many. so i want it to read the first set of sides, display all the info for that triangle and move on to the next set of sides.
» Post edited 2006-09-18, 06:42am by youwish16.
|
||
|
|||
|
|||
|
Rank: ? (119)
Member #: 28292 |
use the function read() to read one character. Create a loop which will read chars till space symbol. Every read symbol accumulate in a string using operator +=. If space symbol is reached convert the first readed string with function parsetointeger into an integer and than pass them to a sideA. For other sides do the same. End so on till you reach the end of file. Try to do it yourself.
The format of file you can define yourself. I would recommend you using for every side the particular value in each line. It will make it easier to realize the reading of values for the sides. |
||
|
|||
|
This post has been removed by the author
|
|||
|
|||
|
|||
|
Rank: ? (11)
Member #: 28462 |
ok so i got to read data from the input file, but it only read the first set of sides. heres my code:
Code:
all i need now is for it to all the data from the file and not the first set. i need it to read the data, display results then read more data and display new results. how do i do this? my input file format is as follows: 3 4 5 5 7 8 |
||
|
Please login or register to post a reply.