Free2Code
 
Time: 2008-11-21, 03:08am
Triangle program
Subject: Triangle program  ·  Posted: 2006-09-13, 10:38am
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:
  1. import javax.swing.JOptionPane;
  2. public class AreaTriangle
  3. {
  4.    public static void main(String[] args)
  5.    {
  6.       double sideA=0;    // side a
  7.       double sideB=0;    // side b
  8.       double sideC=0;    //side c
  9.       double area=0;    //the triangle's area
  10.       double s=0;          // holds 
  11.       String input;      //holds user input
  12.       
  13.       input = JOptionPane.showInputDialog("What is the length of side a?");
  14.       sideA = Double.parseDouble(input);
  15.       
  16.       input = JOptionPane.showInputDialog("What is the length of side b?");
  17.       sideB = Double.parseDouble(input);
  18.       
  19.       input = JOptionPane.showInputDialog("What is the length of side c(the hypotenuse or longest side)?");
  20.       sideC = Double.parseDouble(input);
  21.             
  22.       JOptionPane.showMessageDialog(null, "The area of the triangle is "+area(sideA, sideB, sideC, area, s)+" and the triangle type is "+triangleType(sideA, sideB, sideC));
  23.    }
  24.    
  25.    public static double area(double sideA,double sideB,double sideC,double area,double s)
  26.    {      
  27.       s = sideA + sideB + sideC;
  28.       area = Math.sqrt(s * (s - sideA) * (s - sideB) * (s - sideC));
  29.       return area;
  30.    }
  31.    public static String triangleType(double sideA, double sideB, double sideC)
  32.    {
  33.      if(sideC == sideA || sideC == sideB)
  34.        return "Isosceles";
  35.      else if(Math.pow(sideA, 2.0) * Math.pow(sideB, 2.0) = Math.pow(sideC, 2.0))
  36.        return "right";
  37.      else
  38.        return "scalene";
  39.    }
  40. }


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.

 
  Reply to this ·  Post link ·  Top
Subject: Re: Triangle program  ·  Posted: 2006-09-14, 04:11am
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
 
  Reply to this ·  Post link ·  Top
Subject: Re: Triangle program  ·  Posted: 2006-09-14, 05:02am
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

 
  Reply to this ·  Post link ·  Top
Subject: Re: Triangle program  ·  Posted: 2006-09-14, 10:26pm
Rank: ? (119)
Member #: 28292
I write you a solution for processing of a sideA:
Code:
  1. do {
  2. input = JOptionPane.showInputDialog("What is the length of side a?");
  3. sideA = Double.parseDouble(input);
  4. } while(sideA < 0);


For other you should do it in the same way.

 
  Reply to this ·  Post link ·  Top
Subject: Re: Triangle program  ·  Posted: 2006-09-17, 05:37pm
Rank: ? (11)
Member #: 28462
Code:
  1. import javax.swing.JOptionPane;
  2. public class AreaTriangle2
  3. {
  4.    public static void main(String[] args)
  5.    {
  6.       int sideA=0;    // side a
  7.       int sideB=0;    // side b
  8.       int sideC=0;    //side c
  9.       String input;      //holds user input
  10.       
  11.       while(sideA < 0);
  12.       {
  13.         input = JOptionPane.showInputDialog("What is the length of side a?");
  14.         sideA = Integer.parseInt(input);
  15.       }
  16.         
  17.       while(sideB < 0); 
  18.       {
  19.         input = JOptionPane.showInputDialog("What is the length of side b?");
  20.         sideB = Integer.parseInt(input);
  21.       }
  22.             
  23.      while(sideC < 0);
  24.       {
  25.         input = JOptionPane.showInputDialog("What is the length of side C?");
  26.         sideC = Integer.parseInt(input);
  27.       }
  28.         
  29.             
  30.       JOptionPane.showMessageDialog(null, "The area of the triangle is "+area(sideA, sideB, sideC)+" and the triangle type is "+isosceles(sideA, sideB, sideC)+right(sideA, sideB, sideC)+equalateral(sideA, sideB, sideC));
  31.    }
  32.    
  33.    public static double area(int sideA,int sideB,int sideC)
  34.    {      
  35.       double area=0;    //the triangle's area
  36.       int s=0;          // holds 
  37.       s = sideA + sideB + sideC;
  38.       area = Math.sqrt(s * (s - sideA) * (s - sideB) * (s - sideC));
  39.       return area;
  40.    }
  41.    public static String isosceles(double sideA, double sideB, double sideC)
  42.    {
  43.      if(sideC == sideA || sideC == sideB || sideA == sideB)
  44.        return "Isosceles";
  45.      else
  46.       return "";
  47.    }
  48.    public static String right(double sideA, double sideB, double sideC)
  49.    {     
  50.      if(Math.pow(sideA, 2.0) + Math.pow(sideB, 2.0) == Math.pow(sideC, 2.0))
  51.        return "right";
  52.      else
  53.        return "";
  54.    }
  55.    public static String equalateral(double sideA, double sideB, double sideC)
  56.    {
  57.      if(sideA == sideB && sideA == sideC && sideB == sideC)
  58.        return "equalateral";
  59.      else
  60.        return "";
  61.    }
  62. }


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?

 
  Reply to this ·  Post link ·  Top
Subject: Re: Triangle program  ·  Posted: 2006-09-17, 08:13pm
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);

 
  Reply to this ·  Post link ·  Top
Subject: Re: Triangle program  ·  Posted: 2006-09-18, 06:18am
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

 
  Reply to this ·  Post link ·  Top
Subject: Re: Triangle program  ·  Posted: 2006-09-18, 06:31am
Rank: ? (119)
Member #: 28292
Use file FileReader and BufferedReader.

» Post edited 2006-09-18, 06:37am by igorok.

 
  Reply to this ·  Post link ·  Top
Subject: Re: Triangle program  ·  Posted: 2006-09-18, 06:41am
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.

 
  Reply to this ·  Post link ·  Top
Subject: Re: Triangle program  ·  Posted: 2006-09-18, 07:17am
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.

 
  Reply to this ·  Post link ·  Top
This post has been removed by the author
 
  Top
Subject: Re: Triangle program  ·  Posted: 2006-09-23, 02:46am
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:
  1. import javax.swing.JOptionPane;
  2. import java.io.*;
  3. public class AreaTriangle
  4. {
  5.    public static void main(String[] args) throws IOException
  6.    {
  7.      String str;
  8.       int sideA=0;    // side a
  9.       int sideB=0;    // side b
  10.       int sideC=0;    //side c
  11.       String input;      //holds user input
  12.       
  13.       FileReader freader = new FileReader("file.txt");
  14.       BufferedReader inputFile = new BufferedReader(freader);
  15.       
  16.       str = inputFile.readLine();
  17.         sideA = Integer.parseInt(str);
  18.       str = inputFile.readLine();
  19.         sideB = Integer.parseInt(str);
  20.       
  21.       str = inputFile.readLine();
  22.         sideC = Integer.parseInt(str);
  23.  
  24.       inputFile.close();
  25.             
  26.       JOptionPane.showMessageDialog(null, "The area of the triangle is "+area(sideA, sideB, sideC)+" and the triangle type is "+isosceles(sideA, sideB, sideC)+right(sideA, sideB, sideC)+equalateral(sideA, sideB, sideC));
  27.    }
  28.    
  29.    public static double area(int sideA,int sideB,int sideC)
  30.    {      
  31.       double area=0;    //the triangle's area
  32.       int s=0;          // holds 
  33.       s = sideA + sideB + sideC;
  34.       area = Math.sqrt(s * (s - sideA) * (s - sideB) * (s - sideC));
  35.       return area;
  36.    }
  37.    public static String isosceles(double sideA, double sideB, double sideC)
  38.    {
  39.      if(sideC == sideA || sideC == sideB || sideA == sideB)
  40.        return "Isosceles";
  41.      else
  42.       return "";
  43.    }
  44.    public static String right(double sideA, double sideB, double sideC)
  45.    {     
  46.      if(Math.pow(sideA, 2.0) + Math.pow(sideB, 2.0) == Math.pow(sideC, 2.0))
  47.        return "right";
  48.      else
  49.        return "";
  50.    }
  51.    public static String equalateral(double sideA, double sideB, double sideC)
  52.    {
  53.      if(sideA == sideB && sideA == sideC && sideB == sideC)
  54.        return "equalateral";
  55.      else
  56.        return "";
  57.    }
  58. }


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



 
  Reply to this ·  Post link ·  Top

Pages: 1

Please login or register to post a reply.

icons