Methods in Class for Java program -


i started learning java , having difficulty understanding classes. 1 of recent assignments create class called triangle - given user input main method - take in input test if input see if hypothetical "triangle" equilateral, isosceles, or scalene , return main. able program work not 100% within guidelines of assignment's requirements (which specified having 2 additional methods calculated largest , smallest side-length). wondering, after fact, if can @ did , let me know if can see , or explain - in beginner terms - why having 2 methods needed if works without them (i.e. program somehow @ disadvantage nature of not having them) , if so, doing wrong kept throwing errors when tried implement methods?

i'll include both assignment guidelines (for reference since not assume mind reader) , code:

"your assignment write class definition (not program, there no main method) named triangle (saved in file triangle.java). triangle has 3 instance variables: int side1, side2, side3; class triangle must include following constructors , methods: (if class not contain of following methods, points deducted).

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

- sets triangle specified side lengths.

private int largest()  

- returns length of longest side of triangle. helper method.

private int shortest()  

- returns length of shortest side of triangle. helper method.

public boolean is_equilateral()  

- determines whether triangle equilateral. if longest side equal shortest side, triangle equilateral.

public boolean is_isosceles()  

- determines whether triangle isosceles. (and @ least) 2 sides must equal.

public boolean is_scalene()  

- determines whether triangle scalene. no 2 sides equal.

public string tostring()  

- prints sides of triangle."

public class triangle {  private int side1, side2, side3;   // 3 sides user-input of triangle      side length    public triangle (int s1, int s2, int s3){  // instance variables          side1 = s1;          side2 = s2;          side3 = s3;   }  // largest , shortest helper methods never got off ground // left them in kicks , giggles comments  //      private int largest(int s1, int s2, int s3) {   <------ why these supposed declared private? // //          int max = math.max(math.max(s1, s2), s3);                                                                   // , is_scalene determine type of      //          return max; //          }  //      private int shortest(int s1, int s2, int s3) {  //        // other is_x methods below    //          int min = math.min(math.min(s1, s2), s3);    //          return min; //          }       public boolean is_equilateral() { // these methods return true or false main()          int max = math.max(math.max(side1, side2),side3);  // because couldn't largest/smallest         int min = math.min(math.min(side1, side2), side3); // work, opted use java's math.max/min                                                            // methods instead.           boolean answer = false;     // default answer set false                                        if (max == min)     // if largest # equal smallest # (i.e. equal)                              // 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 2 sides equal true, else false         }          else         {             answer = false;         }          return answer;      }      public boolean is_scalene() {          boolean answer = false;          if(side1 != side2 && side2 != side3)          {             answer = true;  // if #'s different, 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";      }    }  // test file use ensure class working below  import java.util.scanner; public class assignment4  {  //=========================================================== // create , 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 user enter 'n'     {      // input      system.out.println("enter sides of triangle: ");      num1 = console.nextint();      num2 = console.nextint();      num3 = console.nextint();        // create triangle      triangle mytriangle = new triangle (num1, num2, num3);       // print triangle      system.out.println(mytriangle.tostring() + " triangle:");       //check isosceles      if (mytriangle.is_isosceles())        system.out.println("it isosceles");      else        system.out.println("it not isosceles");       //check equilateral      if (mytriangle.is_equilateral())        system.out.println("it equilateral");      else        system.out.println("it not equilateral");       //check scalene      if (mytriangle.is_scalene())        system.out.println("it scalene");      else        system.out.println("it not scalene");       // find if user want repeat      system.out.println();      system.out.print("check triangle (y/n)? ");      = console.next();    } while (another.equalsignorecase("y"));     }  // method main  }  // class assignment4 

again - have done assignment , don't want me, really, appreciate in understanding did wrong in helper methods (commented out still within class) , why needed if program can still function without them? i'm new , classes aren't coming easy other things did. thanks!


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -