Skip to main content
deleted 395 characters in body; edited tags; edited title; deleted 1 character in body
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

Methods for a Class Triangle classifier in a Java assignment

I just started learning Java and am having difficulty with understanding classes. One of my most recent assignments was to create a class called Triangle that would - given user input from the main method - take in the input to test if the hypothetical "triangle" is equilateral, isosceles, or scalene and then return that to main. I was able to get the program to work but.

However, I did not 100% within the guidelines offollow the assignment's requirements (whichimplementation guidelines, which specified having two additional methods that calculated largest and smallest side-length). I am wondering, after the fact, if anyone can look at what I did and let me know if they can see and or explain - in beginner terms - why having those two methods would be needed if it works without them (i.e. is my program somehow at a disadvantage by nature of NOT having them) and if so, what was I doing wrong that kept throwing errors when I tried to implement those methods?

  • Prints the sides of the triangle."

Again - I have already done this assignment and I don't want anyone to do this for me, but I would really, REALLY appreciate some help in understanding what I did wrong in my helper methods (commented out but still within the class) and why they would be needed if the program can still function without them? I'm new to this and classes just aren't coming as easy as other things did. Thanks!

Methods for a Class in a Java assignment

I just started learning Java and am having difficulty with understanding classes. One of my most recent assignments was to create a class called Triangle that would - given user input from the main method - take in the input to test if the hypothetical "triangle" is equilateral, isosceles, or scalene and then return that to main. I was able to get the program to work but not 100% within the guidelines of the assignment's requirements (which specified having two additional methods that calculated largest and smallest side-length). I am wondering, after the fact, if anyone can look at what I did and let me know if they can see and or explain - in beginner terms - why having those two methods would be needed if it works without them (i.e. is my program somehow at a disadvantage by nature of NOT having them) and if so, what was I doing wrong that kept throwing errors when I tried to implement those methods?

  • Prints the sides of the triangle."

Again - I have already done this assignment and I don't want anyone to do this for me, but I would really, REALLY appreciate some help in understanding what I did wrong in my helper methods (commented out but still within the class) and why they would be needed if the program can still function without them? I'm new to this and classes just aren't coming as easy as other things did. Thanks!

Triangle classifier in Java

I just started learning Java and am having difficulty with understanding classes. One of my most recent assignments was to create a class called Triangle that would - given user input from the main method - take in the input to test if the hypothetical "triangle" is equilateral, isosceles, or scalene and then return that to main. I was able to get the program to work.

However, I did not follow the assignment's implementation guidelines, which specified having two additional methods that calculated largest and smallest side-length. I am wondering, after the fact, if anyone can look at what I did and let me know if they can see and or explain - in beginner terms - why having those two methods would be needed if it works without them (i.e. is my program somehow at a disadvantage by nature of NOT having them) and if so, what was I doing wrong that kept throwing errors when I tried to implement those methods?

  • Prints the sides of the triangle.
Made question title more clearly match content of question
Link

Helper methods in Methods for a Class forin a Java assignment

Changed formatting of the guidelines to separate it clearly from the code. Removed irrelevant tag for extension methods.
Source Link

I just started learning Java and am having difficulty with understanding classes. One of my most recent assignments was to create a class called Triangle that would - given user input from the main method - take in the input to test ifif the hypothetical "triangle" is equilateral, isosceles, or scalene and then return that to main. I was able to get the program to work but not 100% within the guidelines of the assignment's requirements (which specified having two additional methods that calculated largest and smallest side-length). I am wondering, after the fact, if anyone can look at what I did and let me know if they can see and or explain - in beginner terms - why having those two methods would be needed if it works without them (i.e. is my program somehow at a disadvantage by nature of NOT having them) and if so, what was I doing wrong that kept throwing errors when I tried to implement those methods?

"Your assignment is to write a class definition (not a program, there is no main method) named Triangle (saved in a file Triangle.java). Triangle has 3 instance variables: int side1, side2, side3; The class Triangle must include the following constructors and methods: (If your class does not contain any of the following methods, points will be deducted).The assignment guidelines:

public Triangle (int s1, int s2, int s3) 

Your assignment is to write a class definition (not a program, there is no main method) named Triangle (saved in a file Triangle.java). Triangle has 3 instance variables: int side1, side2, side3; The class Triangle must include the following constructors and methods: (If your class does not contain any of the following methods, points will be deducted).

public Triangle (int s1, int s2, int s3) 
  • Sets up a triangle with the specified side lengths.

    private int largest()

  • Returns the length of the longest side of the triangle. This is a helper method.

    private int shortest()

  • Returns the length of the shortest side of the triangle. This is a helper method.

    public boolean is_equilateral()

  • Determines whether a triangle is equilateral. If the longest side is equal to the shortest side, then the triangle is equilateral.

    public boolean is_isosceles()

  • Determines whether a triangle is isosceles. Any (and at least) two sides must be equal.

    public boolean is_scalene()

  • Determines whether a triangle is scalene. No two sides are equal.

    public String toString()

  • Prints the sides of the triangle."

    public class Triangle {

    private int side1, side2, side3; // 3 sides for user-input of triangle side length

    public Triangle (int s1, int s2, int s3){ // instance variables

          side1 = s1;
    
          side2 = s2;
    
          side3 = s3;
    

    }

    // My largest and shortest helper methods never got off the ground // I left them in for kicks and giggles as comments

    // private int largest(int s1, int s2, int s3) { <------ Why are these supposed to be declared as private? // // int max = Math.max(Math.max(s1, s2), s3); // and is_scalene to determine what type of
    // return max; // }

    // private int shortest(int s1, int s2, int s3) { // // all the other is_x methods below
    // int min = Math.min(Math.min(s1, s2), s3);
    // return min; // }

      public boolean is_equilateral() { // These methods will return a true or false back to main()
    
          int max = Math.max(Math.max(side1, side2),side3);  // Because I couldn't get largest/smallest
          int min = Math.min(Math.min(side1, side2), side3); // to work, I opted to use Java's Math.max/min
                                                             // methods instead.  
          boolean answer = false;     // default answer set to false                              
    
          if (max == min)     // if the largest # is also equal to the smallest # (i.e. all equal) 
                              // then true else false
          {
              answer = true;
          }
    
          else
          {
              answer = false;
          }
    
          return answer;
      }
    
    
      public boolean is_isosceles() {
    
          boolean answer = false; 
    
          if(side1 == side2 || side1 == side3 || side2 == side3)
          {
              answer = true;  // if any 2 sides are equal then true, else false
          }
    
          else
          {
              answer = false;
          }
    
          return answer;
    
      }
    
      public boolean is_scalene() {
    
          boolean answer = false;
    
          if(side1 != side2 && side2 != side3) 
          {
              answer = true;  // if all #'s are different, then true, else false
          }
    
          else 
          {
              answer = false;
          }
    
          return answer;
      }
    
      public String toString() {  // print results on same line
    
          return "" + this.side1 + " " + this.side2 + " " + this.side3 + "\n\n";
    
      }
    

    }

    // Then the test file that we use to ensure that the class is working is below

    import java.util.Scanner; public class Assignment4 {

    //=========================================================== // Create and determine properties of various triangles. //=========================================================== public static void main (String[] args) { Scanner console = new Scanner(System.in); int num1, num2, num3; String another;

    // repeat until the user enter 'n'
    do
    {
       // get the input
       System.out.println("Enter the sides of the triangle: ");
       num1 = console.nextInt();
       num2 = console.nextInt();
       num3 = console.nextInt();
    
    
       // create the Triangle
       Triangle myTriangle = new Triangle (num1, num2, num3);
    
       // print the Triangle
       System.out.println(myTriangle.toString() + " triangle:");
    
       //check the isosceles
       if (myTriangle.is_isosceles())
         System.out.println("It is isosceles");
       else
         System.out.println("It is not isosceles");
    
       //check the equilateral
       if (myTriangle.is_equilateral())
         System.out.println("It is equilateral");
       else
         System.out.println("It is not a equilateral");
    
       //check the scalene
       if (myTriangle.is_scalene())
         System.out.println("It is scalene");
       else
         System.out.println("It is not scalene");
    
       // find if the user want to repeat
       System.out.println();
       System.out.print("Check another Triangle (y/n)? ");
       another = console.next();
    
    } while (another.equalsIgnoreCase("y"));
    
    
    }  // method main
    

    } // class Assignment4

    Sets up a triangle with the specified side lengths.
private int largest() 
  • Returns the length of the longest side of the triangle. This is a helper method.
private int shortest() 
  • Returns the length of the shortest side of the triangle. This is a helper method.
public boolean is_equilateral() 
  • Determines whether a triangle is equilateral. If the longest side is equal to the shortest side, then the triangle is equilateral.
public boolean is_isosceles() 
  • Determines whether a triangle is isosceles. Any (and at least) two sides must be equal.
public boolean is_scalene() 
  • Determines whether a triangle is scalene. No two sides are equal.
public String toString() 
  • Prints the sides of the triangle."

My code:

public class Triangle {

private int side1, side2, side3;   // 3 sides for user-input of triangle side length    

public Triangle (int s1, int s2, int s3){  // instance variables
    
        side1 = s1;
    
        side2 = s2;
    
        side3 = s3;
    
    
}

// My largest and shortest helper methods never got off the ground
// I left them in for kicks and giggles as comments

//      private int largest(int s1, int s2, int s3) {   <------ Why are these supposed to be declared as private?
//
//          int max = Math.max(Math.max(s1, s2), s3);                                                                   // and is_scalene to determine what type of     
//          return max;
//          }

//      private int shortest(int s1, int s2, int s3) { 
//        // all the other is_x methods below   
//          int min = Math.min(Math.min(s1, s2), s3);   
//          return min;
//          }
    
    
    public boolean is_equilateral() { // These methods will return a true or false back to main()
        
        int max = Math.max(Math.max(side1, side2),side3);  // Because I couldn't get largest/smallest
        int min = Math.min(Math.min(side1, side2), side3); // to work, I opted to use Java's Math.max/min
                                                           // methods instead.  
        boolean answer = false;     // default answer set to false                              
        
        if (max == min)     // if the largest # is also equal to the smallest # (i.e. all equal) 
                            // then true else false
        {
            answer = true;
        }
        
        else
        {
            answer = false;
        }

        return answer;
    }
    
    
    public boolean is_isosceles() {
        
        boolean answer = false; 
        
        if(side1 == side2 || side1 == side3 || side2 == side3)
        {
            answer = true;  // if any 2 sides are equal then true, else false
        }
        
        else
        {
            answer = false;
        }
        
        return answer;
        
    }
    
    public boolean is_scalene() {
        
        boolean answer = false;
        
        if(side1 != side2 && side2 != side3) 
        {
            answer = true;  // if all #'s are different, then true, else false
        }
        
        else 
        {
            answer = false;
        }
        
        return answer;
    }
    
    public String toString() {  // print results on same line
        
        return "" + this.side1 + " " + this.side2 + " " + this.side3 + "\n\n";
        
    }



}

// Then the test file that we use to ensure that the class is working is below

import java.util.Scanner;
public class Assignment4 
{

//===========================================================
// Create and determine properties of various triangles.
//===========================================================
public static void main (String[] args) 
{
  Scanner console = new Scanner(System.in);
  int num1, num2, num3;
  String another;
  
  // repeat until the user enter 'n'
  do
  {
     // get the input
     System.out.println("Enter the sides of the triangle: ");
     num1 = console.nextInt();
     num2 = console.nextInt();
     num3 = console.nextInt();
  

     // create the Triangle
     Triangle myTriangle = new Triangle (num1, num2, num3);

     // print the Triangle
     System.out.println(myTriangle.toString() + " triangle:");

     //check the isosceles
     if (myTriangle.is_isosceles())
       System.out.println("It is isosceles");
     else
       System.out.println("It is not isosceles");
    
     //check the equilateral
     if (myTriangle.is_equilateral())
       System.out.println("It is equilateral");
     else
       System.out.println("It is not a equilateral");
    
     //check the scalene
     if (myTriangle.is_scalene())
       System.out.println("It is scalene");
     else
       System.out.println("It is not scalene");

     // find if the user want to repeat
     System.out.println();
     System.out.print("Check another Triangle (y/n)? ");
     another = console.next();

  } while (another.equalsIgnoreCase("y"));


  }  // method main

}  // class Assignment4

I just started learning Java and am having difficulty with understanding classes. One of my most recent assignments was to create a class called Triangle that would - given user input from the main method - take in the input to test if the hypothetical "triangle" is equilateral, isosceles, or scalene and then return that to main. I was able to get the program to work but not 100% within the guidelines of the assignment's requirements (which specified having two additional methods that calculated largest and smallest side-length). I am wondering, after the fact, if anyone can look at what I did and let me know if they can see and or explain - in beginner terms - why having those two methods would be needed if it works without them (i.e. is my program somehow at a disadvantage by nature of NOT having them) and if so, what was I doing wrong that kept throwing errors when I tried to implement those methods?

"Your assignment is to write a class definition (not a program, there is no main method) named Triangle (saved in a file Triangle.java). Triangle has 3 instance variables: int side1, side2, side3; The class Triangle must include the following constructors and methods: (If your class does not contain any of the following methods, points will be deducted).

public Triangle (int s1, int s2, int s3) 
  • Sets up a triangle with the specified side lengths.

    private int largest()

  • Returns the length of the longest side of the triangle. This is a helper method.

    private int shortest()

  • Returns the length of the shortest side of the triangle. This is a helper method.

    public boolean is_equilateral()

  • Determines whether a triangle is equilateral. If the longest side is equal to the shortest side, then the triangle is equilateral.

    public boolean is_isosceles()

  • Determines whether a triangle is isosceles. Any (and at least) two sides must be equal.

    public boolean is_scalene()

  • Determines whether a triangle is scalene. No two sides are equal.

    public String toString()

  • Prints the sides of the triangle."

    public class Triangle {

    private int side1, side2, side3; // 3 sides for user-input of triangle side length

    public Triangle (int s1, int s2, int s3){ // instance variables

          side1 = s1;
    
          side2 = s2;
    
          side3 = s3;
    

    }

    // My largest and shortest helper methods never got off the ground // I left them in for kicks and giggles as comments

    // private int largest(int s1, int s2, int s3) { <------ Why are these supposed to be declared as private? // // int max = Math.max(Math.max(s1, s2), s3); // and is_scalene to determine what type of
    // return max; // }

    // private int shortest(int s1, int s2, int s3) { // // all the other is_x methods below
    // int min = Math.min(Math.min(s1, s2), s3);
    // return min; // }

      public boolean is_equilateral() { // These methods will return a true or false back to main()
    
          int max = Math.max(Math.max(side1, side2),side3);  // Because I couldn't get largest/smallest
          int min = Math.min(Math.min(side1, side2), side3); // to work, I opted to use Java's Math.max/min
                                                             // methods instead.  
          boolean answer = false;     // default answer set to false                              
    
          if (max == min)     // if the largest # is also equal to the smallest # (i.e. all equal) 
                              // then true else false
          {
              answer = true;
          }
    
          else
          {
              answer = false;
          }
    
          return answer;
      }
    
    
      public boolean is_isosceles() {
    
          boolean answer = false; 
    
          if(side1 == side2 || side1 == side3 || side2 == side3)
          {
              answer = true;  // if any 2 sides are equal then true, else false
          }
    
          else
          {
              answer = false;
          }
    
          return answer;
    
      }
    
      public boolean is_scalene() {
    
          boolean answer = false;
    
          if(side1 != side2 && side2 != side3) 
          {
              answer = true;  // if all #'s are different, then true, else false
          }
    
          else 
          {
              answer = false;
          }
    
          return answer;
      }
    
      public String toString() {  // print results on same line
    
          return "" + this.side1 + " " + this.side2 + " " + this.side3 + "\n\n";
    
      }
    

    }

    // Then the test file that we use to ensure that the class is working is below

    import java.util.Scanner; public class Assignment4 {

    //=========================================================== // Create and determine properties of various triangles. //=========================================================== public static void main (String[] args) { Scanner console = new Scanner(System.in); int num1, num2, num3; String another;

    // repeat until the user enter 'n'
    do
    {
       // get the input
       System.out.println("Enter the sides of the triangle: ");
       num1 = console.nextInt();
       num2 = console.nextInt();
       num3 = console.nextInt();
    
    
       // create the Triangle
       Triangle myTriangle = new Triangle (num1, num2, num3);
    
       // print the Triangle
       System.out.println(myTriangle.toString() + " triangle:");
    
       //check the isosceles
       if (myTriangle.is_isosceles())
         System.out.println("It is isosceles");
       else
         System.out.println("It is not isosceles");
    
       //check the equilateral
       if (myTriangle.is_equilateral())
         System.out.println("It is equilateral");
       else
         System.out.println("It is not a equilateral");
    
       //check the scalene
       if (myTriangle.is_scalene())
         System.out.println("It is scalene");
       else
         System.out.println("It is not scalene");
    
       // find if the user want to repeat
       System.out.println();
       System.out.print("Check another Triangle (y/n)? ");
       another = console.next();
    
    } while (another.equalsIgnoreCase("y"));
    
    
    }  // method main
    

    } // class Assignment4

I just started learning Java and am having difficulty with understanding classes. One of my most recent assignments was to create a class called Triangle that would - given user input from the main method - take in the input to test if the hypothetical "triangle" is equilateral, isosceles, or scalene and then return that to main. I was able to get the program to work but not 100% within the guidelines of the assignment's requirements (which specified having two additional methods that calculated largest and smallest side-length). I am wondering, after the fact, if anyone can look at what I did and let me know if they can see and or explain - in beginner terms - why having those two methods would be needed if it works without them (i.e. is my program somehow at a disadvantage by nature of NOT having them) and if so, what was I doing wrong that kept throwing errors when I tried to implement those methods?

The assignment guidelines:

Your assignment is to write a class definition (not a program, there is no main method) named Triangle (saved in a file Triangle.java). Triangle has 3 instance variables: int side1, side2, side3; The class Triangle must include the following constructors and methods: (If your class does not contain any of the following methods, points will be deducted).

public Triangle (int s1, int s2, int s3) 
  • Sets up a triangle with the specified side lengths.
private int largest() 
  • Returns the length of the longest side of the triangle. This is a helper method.
private int shortest() 
  • Returns the length of the shortest side of the triangle. This is a helper method.
public boolean is_equilateral() 
  • Determines whether a triangle is equilateral. If the longest side is equal to the shortest side, then the triangle is equilateral.
public boolean is_isosceles() 
  • Determines whether a triangle is isosceles. Any (and at least) two sides must be equal.
public boolean is_scalene() 
  • Determines whether a triangle is scalene. No two sides are equal.
public String toString() 
  • Prints the sides of the triangle."

My code:

public class Triangle {

private int side1, side2, side3;   // 3 sides for user-input of triangle side length    

public Triangle (int s1, int s2, int s3){  // instance variables
    
        side1 = s1;
    
        side2 = s2;
    
        side3 = s3;
    
    
}

// My largest and shortest helper methods never got off the ground
// I left them in for kicks and giggles as comments

//      private int largest(int s1, int s2, int s3) {   <------ Why are these supposed to be declared as private?
//
//          int max = Math.max(Math.max(s1, s2), s3);                                                                   // and is_scalene to determine what type of     
//          return max;
//          }

//      private int shortest(int s1, int s2, int s3) { 
//        // all the other is_x methods below   
//          int min = Math.min(Math.min(s1, s2), s3);   
//          return min;
//          }
    
    
    public boolean is_equilateral() { // These methods will return a true or false back to main()
        
        int max = Math.max(Math.max(side1, side2),side3);  // Because I couldn't get largest/smallest
        int min = Math.min(Math.min(side1, side2), side3); // to work, I opted to use Java's Math.max/min
                                                           // methods instead.  
        boolean answer = false;     // default answer set to false                              
        
        if (max == min)     // if the largest # is also equal to the smallest # (i.e. all equal) 
                            // then true else false
        {
            answer = true;
        }
        
        else
        {
            answer = false;
        }

        return answer;
    }
    
    
    public boolean is_isosceles() {
        
        boolean answer = false; 
        
        if(side1 == side2 || side1 == side3 || side2 == side3)
        {
            answer = true;  // if any 2 sides are equal then true, else false
        }
        
        else
        {
            answer = false;
        }
        
        return answer;
        
    }
    
    public boolean is_scalene() {
        
        boolean answer = false;
        
        if(side1 != side2 && side2 != side3) 
        {
            answer = true;  // if all #'s are different, then true, else false
        }
        
        else 
        {
            answer = false;
        }
        
        return answer;
    }
    
    public String toString() {  // print results on same line
        
        return "" + this.side1 + " " + this.side2 + " " + this.side3 + "\n\n";
        
    }



}

// Then the test file that we use to ensure that the class is working is below

import java.util.Scanner;
public class Assignment4 
{

//===========================================================
// Create and determine properties of various triangles.
//===========================================================
public static void main (String[] args) 
{
  Scanner console = new Scanner(System.in);
  int num1, num2, num3;
  String another;
  
  // repeat until the user enter 'n'
  do
  {
     // get the input
     System.out.println("Enter the sides of the triangle: ");
     num1 = console.nextInt();
     num2 = console.nextInt();
     num3 = console.nextInt();
  

     // create the Triangle
     Triangle myTriangle = new Triangle (num1, num2, num3);

     // print the Triangle
     System.out.println(myTriangle.toString() + " triangle:");

     //check the isosceles
     if (myTriangle.is_isosceles())
       System.out.println("It is isosceles");
     else
       System.out.println("It is not isosceles");
    
     //check the equilateral
     if (myTriangle.is_equilateral())
       System.out.println("It is equilateral");
     else
       System.out.println("It is not a equilateral");
    
     //check the scalene
     if (myTriangle.is_scalene())
       System.out.println("It is scalene");
     else
       System.out.println("It is not scalene");

     // find if the user want to repeat
     System.out.println();
     System.out.print("Check another Triangle (y/n)? ");
     another = console.next();

  } while (another.equalsIgnoreCase("y"));


  }  // method main

}  // class Assignment4
Source Link
Loading