• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Triangle Problem

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After some editing i m facing this error. What i m doing wrong here?
Thanks in advance




 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Menios Menelaos
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No its just the length of the sides
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree the acceptThreeIntegers method should go, but the poster has been told to use JOptionPane throughout.
 
Paul Yule
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yea, I would use a scanner otherwise...I just meant remove it from the Triangle object.
 
Campbell Ritchie
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I see what you mean.
 
Space seems cool in the movies, but once you get out there, it is super boring. Now for a fascinating tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic