Jump to content

Recommended Posts

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:

 


package Triangle;

/*
* MyApplication.java
*/

import javax.swing.JOptionPane;

public class MyApplication {

/** Creates a new instance of myApplication */
public MyApplication() {
}

public static void main(String[] args  )
{
String message = "The Triangle Problem";
JOptionPane.showMessageDialog( null, message );

// obtain three integer values representing side lengths
int[ ] intArray = new int[ 3 ];
for( int i = 0; i < intArray.length; i++ )
{
message = "Enter an integer value for triangle side "
+ ( i + 1 ) + ":";

        // gather integer value representing side here…
        String input = JOptionPane.showInputDialog(null, message );
        intArray[i] = Integer.parseInt(input);
}
        // construct and evaluate the triangle
        Triangle Triangle = new Triangle(intArray[0], intArray[1], intArray[2]);
        String outcome = Triangle.evaluate( );

          intArray[0] = Side1; //or s1,s2,s3 don't work either  here
          intArray[1] = Side2; 
          intArray[2] = Side3;
          Triangle Triangle = new Triangle(intArray);
          Triangle.evaluate();

// present the outcome to the user
JOptionPane.showMessageDialog( null, outcome );
}
}

 

&

 

package Triangle;
/**
* Triangle.java
*/
import javax.swing.JOptionPane;


public class Triangle
{
   	private static int Side1, Side2, Side3;

      
        
        public Triangle (int s1 ,int s2, int s3)
{
     Side1 = s1; 
     Side2 = s2;
     Side3 = s3;
}
        
    	
        private static void acceptThreeIntegers( )
    {
    	String strSide1 = JOptionPane.showInputDialog( "Enter side 1: ");
        Side1 = Integer.parseInt( strSide1 );
        
    	String strSide2 = JOptionPane.showInputDialog( "Enter Side 2: ");
        Side2 = Integer.parseInt( strSide2 );
        
    	String strSide3 = JOptionPane.showInputDialog( "Enter Side 3: ");
        Side3 = Integer.parseInt( strSide3 );
    }
    
        private static void useMethod( )
    {
    	if( checkForNegatives( ) == false )
    	{
    		if (checkForTriangle() == true)
    		{    
       			String result = evaluate( );
       			JOptionPane.showMessageDialog( null, result );
    		}
    		else
       		JOptionPane.showMessageDialog( null, "Impossible", "Results", JOptionPane.PLAIN_MESSAGE );   
    	}
   		else
       	JOptionPane.showMessageDialog( null, "Impossible", "Results", JOptionPane.PLAIN_MESSAGE  );   
    	
   	}
   	private static boolean checkForNegatives( )
    {
    if (Side1 <=0 || Side2 <=0 || Side3 <=0)
       return true;
    else return false; 
    }
    
/** evaluate if a triangle is physically possible*/
    private static boolean checkForTriangle( )
    {
    if (Side1 + Side2 - Side3 <=0 ||
        Side1 + Side3 - Side2 <=0 ||
        Side2 + Side3 - Side1 <=0)
    return false;
    else return true;
    }


    public static String evaluate( )
    {
    if (Side1 == Side2 && Side2 == Side3)
    {
    return "Equilateral";
    }
    if (Side1 == Side2 && Side2 != Side3 ||
        Side1 == Side3 && Side2 != Side3 ||
        Side2 == Side3 && Side1 != Side2)
    {
    return "Isosceles ";
    }
    if (Side1 != Side2 && Side2 != Side3 ||
        Side1 != Side2 && Side1 != Side3)
    {
    return "Scalene";
    }
    return "test value";
    }
    
    public static void main (String[] args)
    {
    	acceptThreeIntegers( );
    	useMethod( );
    }
  
    public static void Triangle (int[]intArray) {
        Side1 = intArray[0] ;
        Side2 = intArray[1] ;
        Side3 = intArray[2] ;
    }

}
    

 

 

 

I m not good at Java so  im not 100% sure on setting the values into an array and then passing it to the triangle class would work.

 

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. I seem to have problems with the constructor i think.

 

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

 

Many thanks

You need to look at what static functions/variables are.  Also:

 

        // construct and evaluate the triangle

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

        String outcome = Triangle.evaluate( );

 

          intArray[0] = Side1; //or s1,s2,s3 don't work either  here

          intArray[1] = Side2;

          intArray[2] = Side3;

          Triangle Triangle = new Triangle(intArray);

          Triangle.evaluate();

 

 

Isn't very logical.

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.