aspose file tools*
The moose likes Beginning Java and the fly likes Triangle Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Triangle Problem" Watch "Triangle Problem" New topic
Author

Triangle Problem

Menios Menelaos
Greenhorn

Joined: Oct 27, 2008
Posts: 6
Hey guys.

I hope someone can help me on this very irritating problem i cant seem to solve. Heres the brief of what i have to do:

Design and implement a program that accepts three integer values from the user and then evaluates them to see if they would correspond to an equilateral, isosceles, or scalene triangle. (An equilateral triangle has three sides of equal length, an isosceles triangle has two sides of equal length, and a scalene triangle has no sides of equal length.) The result of the evaluation is then revealed to the user.

*Note -
you got to make use of the javax.swing class JOptionPane for all user interaction.

two classes (in two files): the first being a “MyApplication” class that contains a main() method which instantiates an instance of a Triangle; and a second, separate class “Triangle”, that encapsulates the methods and data of a Triangle.

Ok so basically from this, i have come up with this:


&



My problem is im confused on what parts i need to cut over to the myapplication file and the triangle file. I know this may seem basic stuff, but im not used to doing this in java.

Also, im not 100% sure on setting the values into an array and then passing it to the triangle class. As you can see from the lines ive commented out, i tried something like this:
intArray[0] = side1 intArray[1] = side2 etc...then call the triangle using New Triangle = triangle1(intArray) then Triangle.evaluate().
but this didnt work.

So yes, please can someone help me out, ive been trying and trying and i really wanna get this to work.

Many thanks
Menios
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11498
    
  16

looking at your code for about 2 seconds, two things popped out:



1) you need semicolons at the end of each line


2) in your method declaration, you need to tell it what intArray IS:



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Welcome to JavaRanch

I wouldn't have told you to use the old-fashioned JOptionPane data entry rather than the more recent Scanner? Pass System.in to the Scanner constructor then you get methods which read input directly into primitives. They are usually called nextSomething. You can the print: "Enter the three sides" and enter 3 4 5 and when you push enter all three values are set up.
You need to watch Scanner; you may need to call nextLine() and discard the line every now and again otherwise it gets ahead of you, but I don't think you will have that problem in your little application.

I would have preferred to call the class with the main method in TriangleDemo or similar. But you are stuck with that name.

Set up a Triangle class with non-static fields for the three sides. Don't use an empty constructor. Set up the three sides in the constructor. You can do this simply by putting the 3 JOptionPane.showInputDialog calls in the constructor.
You will probably need to pass null as the first parameter to the input dialog, before "Enter side 1:".
If you pass negative lengths or values like 1 2 4 which won't make a triangle, throw an Exception from the constructor. If you don't know how to throw an Exception, show a JOptionPane with an error message and set the triangle to a default size) eg 3, 4, 5. Using private methods to test the lengths as you have done is a good idea; if those private methods access the fields, then they mustn't be "static."

Now you have reached the Triangle class with its class invariant intact (ie 3 sides with "real" lengths).

Never, never write == false or == true. You can get all sorts of serious errors if you write = instead of == by mistake.

Now you can set up a method which tests the type of triangle, and either returns it or displays it on a JOptionPane. You have got most of the logic in the evaluate() method, but you can make it much simpler with else if and else. Don't call the evaluate method static. Once you have tested that all three sides are equal, an else if makes it easier to test whether two sides are equal, then an else will assume all three sides are different lengths.
If you change your evaluate method to public void displayType(), you can then reduce your main method to a single line:

new Triangle().displayType();

You can make your boolean methods much simpler by looking at the Java Style Guidelines �10.5.2. Most of the members of the Triangle class which you have called static ought not to be static.

Hope I haven't confused you too much and good luck with it.
Menios Menelaos
Greenhorn

Joined: Oct 27, 2008
Posts: 6
Thanks for all the corrections up to now. I have to use the javax.swing class JOptionPane for all user interaction. After fixing the code a bit Triangle.java does what its suppossed to do but since i uncommented the rest of the code in MyApplication i get all type of errors. Any clues about what i m doing wrong?


*all italics are shown as errors in Netbeans
Here is the Compile errors also
Menios Menelaos
Greenhorn

Joined: Oct 27, 2008
Posts: 6
After some editing i m facing this error. What i m doing wrong here?
Thanks in advance




fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11498
    
  16

I just may not be seeing it, but I don't see a constructor for a triangle that takes three ints, as you are calling here:

Triangle triangle = new Triangle(intArray[0], intArray[1], intArray[2]);

In fact, that is what your first error says, too:

C:\Users\Tier\Documents\NetBeansProjects\Triangle_Project\src\MyApplication.java:33: cannot find symbol
symbol : constructor Triangle(int,int,int)
location: class Triangle


Basically, you are calling a method that you never defined/declared. You have a no-arg constructor declared in the triangle class:

public Triangle() {}

You need something like this:

Menios Menelaos
Greenhorn

Joined: Oct 27, 2008
Posts: 6
ACtually i can't figure out what i m doing so wrong. After some minor changes i get this errors




I ve also added the constructor but still no change.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11498
    
  16

It's very difficult to help you at this point, because we don't have access to what your code currently is. Even when you DO post it, it's hard to follow since there is so much of it.

Generally, the best way to code it to write as little as possible, compile it, and test it until you are SURE it works. THEN add another line or two of code.

You seem to have written 200 or so lines, and now it doesn't work, so it's VERY hard to track where the issues are.

Now, having said all that... The compiler seems to be complaining that on line 41 of the MyApplication.java file, you are trying to use the Side1 variable. WHERE is the Side1 variable declared?
Menios Menelaos
Greenhorn

Joined: Oct 27, 2008
Posts: 6
As a single program it works but i must have 2 java files.

My code right now



&
The triangle.java is doing the math but i can't pass the values from Sides to intArrays back in MyApplication.java
Side1 = intArray[0] ;
Side2 = intArray[1] ;
Side3 = intArray[2] ;


Hope i made it as clear as possible
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
Originally posted by Menios Menelaos:
� accepts three integer values from the user and then evaluates them to see if they would correspond to an equilateral, isosceles, or scalene triangle. �


what do the three integers represent?
the lengths of the three sides, or are the inputs the 2D or 3D coordinates of the endpoints of the triangle's sides?


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Menios Menelaos
Greenhorn

Joined: Oct 27, 2008
Posts: 6
No its just the length of the sides
Paul Yule
Ranch Hand

Joined: May 12, 2008
Posts: 229
couple things...

you probably want to remove static from your triangle object everywhere it is used. I personally don't see a place where class level evaluations or variables should be used.
Once you make the methods and such specific to an instance of your triangle object I think your application class that holds your main will make more sense to you.


I think this is suppose to be another constructor? Constructors don't have a return type and being static doesn't make a whole lot of sense. You may want to take some time to review this concept. I would think about how you want your object to be created and go with it, either via 3 ints or an int array holding 3 ints.
I would also remove all of the UI code in your Triangle object. Such as AcceptThreeIntegers and the JOptionPanes you have in there.
The following is what I have for your application class...notice the validateTriangle and evaluateType is tied to an instance of the object that was created.


[ October 28, 2008: Message edited by: Paul Yule ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Agree the acceptThreeIntegers method should go, but the poster has been told to use JOptionPane throughout.
Paul Yule
Ranch Hand

Joined: May 12, 2008
Posts: 229
Yea, I would use a scanner otherwise...I just meant remove it from the Triangle object.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Yes, I see what you mean.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Triangle Problem