Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help with java Triangle code

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help with java Triangle code

    Good evening everyone! Im in a tight spot right now with my java programming assignment.. i will attach my code below but so far i'm having difficulties with the step (Create a method that returns a String indicating the type of triangle), I tried many ways to do this and i have no syntax errors but i just know its wrong because when i call the method in my TriangleTest class to return me the type of triangle it just doesnt work :/ Helppp please!!!

    public class TriangleAnalyzer  {
     
        private int side1;
        private int side2;
        private int side3; 
     
     
    public TriangleAnalyzer(int sideLength1, int  sideLength2, int sideLength3)  
    {
        this.side1 = sideLength1;
        this.side2 = sideLength2;
        this.side3 = sideLength3;
     
    }
     
    public int getside1()
    {
        return side1;
    }
     
    public int getside2()
    {
        return side2;
    }
     
    public int getside3()
    {
        return side3;
    }
     
    public boolean isTriangle()
    {
        if (side1 + side2 > side3 || side2 + side3 > side1 || side1 + side3 > side2)
        {
           return true;
        }
        else
        {
           return false;
        }
    }
     
    public boolean isEquilateral(){
     
         if (side1 == side2 && side1 == side3)
            {
             return true;
            }
         else   
            {  
             return false;
            }
    }
     
    public boolean isRight(){
     
     //(If right angle) if side1 is hypotenuse
        int hypotenuse = (int) Math.sqrt (Math.pow(side2,2)+ Math.pow(side3,2)); 
          if(hypotenuse == side1) {
               return true;
            }
     
            //if side2 is the hypotenuse 
        hypotenuse = (int) Math.sqrt (Math.pow(side1,2)+ Math.pow(side3,2)); 
          if(hypotenuse == side2) {
                return true;
            }
     
            //if side3 is hypotenuse 
         hypotenuse = (int) Math.sqrt (Math.pow(side1,2)+ Math.pow(side2,2)); 
          if(hypotenuse == side3) 
            {
              return true;
            }
          else
            {
              return false;
            }
    }
     
    public boolean isIsosceles(){
     
            if (side1 == side2 || side1 == side3 || side2 == side3) 
            {
              return true;
            }
            else 
            {
              return false;
            }
    }
     
    public boolean isObtuse(){
     
          if (Math.sqrt(side1 + side2) < Math.sqrt (side3))
            {
                return true;
            }
          else 
            { 
                return false;
            }
    }
     
     
     
     
    //create a method that returns a string indicating the type of triangle (equilateral, isosceles, right, obtuse).
     
    public void toString(boolean isEquilateral, boolean isObtuse, boolean isIsosceles, boolean isRight){
     
            String equilateral = "equilateral";
            if  (isEquilateral)   {
            System.out.println(equilateral);
            }
     
            String obtuse = "obtuse";
            if  (isObtuse)   {
            System.out.println(obtuse);
            }
     
            String right = "right";
            if  (isRight)   {
            System.out.println(right);
            }
     
            String isosceles = "isosceles";
            if  (isIsosceles)  {
            System.out.println(isosceles);
            }
     
            else    {
            System.out.println("not a triangle");
            }
     
        }
     
     
     
     
     
    //and here below is my test class...
     
     
     
     
    import java.lang.String;
    import java.util.Scanner;
    import javax.swing.JOptionPane;
     
    /* This is a test class for tha Triangle Analyzer class.  Make no changes to 
     * this class other than adding the required statements in the TO DO section,
     * below.
     */
    public class TriangleTester
    {
        public static void main(String[] args)
        {
            String input = JOptionPane.showInputDialog(
                        "Enter the lengths of the 3 sides of the first triangle, "
                        + "separated by spaces\n(or click Cancel to quit)");
     
            while (input != null)
            {
                Scanner inputScan = new Scanner(input);
                int side1 = inputScan.nextInt();    // length of first side
                int side2 = inputScan.nextInt();    // length of second side
                int side3 = inputScan.nextInt();    // length of third side
     
                 // TO DO:
     
                 // 1. Create a Triangle Analyzer object using side1, side2, 
                 //    and side3
            TriangleAnalyzer triangle = new TriangleAnalyzer(side1, side2, side3);
     
     
                  // 2. Call the accessor mathods to get the lengths of the sides 
                 //    and print them
            System.out.println("The length of side 1 is :" + triangle.getside1());
            System.out.println("The length of side 2 is :" + triangle.getside2());
            System.out.println("The length of side 3 is :" + triangle.getside3());
     
                // 3. Call the method that returns the type of triangle (or an 
                 //    appropriate message if the sides do not form a triangle) 
                 //    and print it
                 // 
            System.out.println("The type of triangle is " + triangle.);
    //stuck on this part cant seem to figure out how to call the method that returns the strings..
     
                 // 4. If the side lengths do form a triangle, call the method that 
                 //    computes and returns the area and print it, rounded to 2 
                 //    decimal places
     
     
                input = JOptionPane.showInputDialog(
                    "Enter the lengths of the 3 sides of the next triangle, "
                    + "separated by spaces\n(or click Cancel to quit)");
            }
        }
    }


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Help with java Triangle code

    in your code:
    //create a method that returns a string indicating the type of triangle (equilateral, isosceles, right, obtuse).

    public void toString(boolean isEquilateral, boolean isObtuse, boolean isIsosceles, boolean isRight){


    it will not return String since the return type you have provided is void:
    public void toString..........
    Change void with String
    and make sure to return a string

    in your code:
    System.out.println("The type of triangle is " + triangle.);
    //stuck on this part cant seem to figure out how to call the method that returns the strings..

    just write the name of the method with its arguments after dot operator.
    if you are not familiar on how to create a method, please see this link:
    Java - Methods

    does your program compile with no error?

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,848
    My Mood
    Mellow
    Thanks
    239
    Thanked 721 Times in 707 Posts

    Default Re: Help with java Triangle code

    . . . i have no syntax errors . . (and) . . when i call the method . . it just doesnt work
    You surely know that a program without syntax errors may have compiler errors or runtime logic errors. Next time, describe what "doesn't work" means, and post your errors, copied exactly from what you're seeing at your end.

Similar Threads

  1. How to make this triangle upside down in java?
    By namenamename in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 21st, 2013, 04:02 PM
  2. java triangle programming
    By babbigoyal in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2012, 07:36 AM
  3. Replies: 5
    Last Post: November 14th, 2012, 09:47 AM
  4. Triangle Printing - Java Program
    By mparthiban in forum Java Programming Tutorials
    Replies: 1
    Last Post: January 5th, 2012, 09:00 AM
  5. Triangle Printing - Java Program
    By mparthiban in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: January 18th, 2010, 04:35 AM

Tags for this Thread