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 = side1 intArray = 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.
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:
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.
Joined: Oct 27, 2008
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
Joined: Oct 27, 2008
After some editing i m facing this error. What i m doing wrong here? Thanks in advance
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 ]
Joined: Oct 13, 2005
Agree the acceptThreeIntegers method should go, but the poster has been told to use JOptionPane throughout.
Joined: May 12, 2008
Yea, I would use a scanner otherwise...I just meant remove it from the Triangle object.