0

Im working on a project for class and we have to create a Triangle class that will hold the lengths of each side of the triangle. I created a default constructor that gives each side of the triangle a random length no more than 6. Were asked to create a parameterized constructor that allows the caller to determine the upper bound for the randomized side length, if the supplied upper bound is invalid, this constructor should default to the range used in the default constructor.

Next step is we have to create another parameterized constructor that allows the caller to directly specify the three side length & if any of the three lengths is invalid, all lengths should be randomly generated using the range used in the default constructor.

Im not sure how to get this. This is what i have so far...

import java.util.Random;

public class Triangle {
    int side1;
    int side2;
    int side3;

   //Default Constructor
    public Triangle() {
        Random rng = new Random();
        side1 = rng.nextInt(6)+1;
        side2 = rng.nextInt(6)+1;
        side3 = rng.nextInt(6)+1;
    }

   //toString method
    public String toString() {
        return new String (" " + side1+ " x " +side2+ " x " +side3);
    }

    //Parameterized Constructor
    Triangle (int side11, int side22, int side33) {
        side1 = side11;
        side2 = side22;
        side3 = side33;
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Jay Khan
  • 5
  • 3
  • 1
    Hi @Jay Khan, do you want to make Triangle randomly? For the purpose to make a triangle, your current code cannot fit for ```triangle inequality```. For example (1,1,3) is not a triangle. – Milo Chen Oct 27 '21 at 01:51
  • Yes, each side has to be given a random value no more than 6 – Jay Khan Oct 27 '21 at 03:59
  • Hi @Jay Khan, You think any three random slides can be a triangle. But it's wrong. For example, your code may choose some three sides like 1 1 4. And it is impossible to be a triangle by these 3 sides. So, do you want to create 3 random numbers or create a random triangle? – Milo Chen Oct 27 '21 at 04:25
  • 1
    This was the instruction given... • A default constructor that randomly determines the values for all three instance variables, ensuring that no side is longer than 6 units – Jay Khan Oct 27 '21 at 04:49

2 Answers2

0

I'd move the code in the no parameter constructor out to a method that receives that max bound, then use something like the below:

public Triangle() {
    initSides(6);
}

public Triangle(int max) {
    this(); // call the no parameter constructor
    if (max > 0) {
        initSides(max);
    }
}

public Triangle (int side1, int side2, int side3) {
    this(); // call the no parameter constructor
    if ((side1 > 0) && (side2 > 0) && (side3 > 0)) {
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
    }
}

private void initSides(int max) {
    Random rng = new Random();
    side1 = rng.nextInt(max)+1;
    side2 = rng.nextInt(max)+1;
    side3 = rng.nextInt(max)+1;
}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • Maybe I'm misreading things, but your code appears to be at risk for creating invalid triangles, ones where the sum of 2 sides is smaller than the 3rd side. This matters most for your `initSides(...)` method, the one that you created to help solve the OP's problem. – Hovercraft Full Of Eels Oct 27 '21 at 01:33
  • @HovercraftFullOfEels Correct. I'm not checking to see if it's a valid triangle. The author can put in more code checks for that. All I checked for was that the values are not negative as the author really didn't define what "invalid" meant in their case. – Idle_Mind Oct 27 '21 at 01:57
  • I need a default constructor handy, the instructions we were given are as follows... A default constructor that randomly determines the values for all three instance variables, ensuring that no side is longer than 6 units • A toString() method that returns a reference to a String like "6 x 2 x 4" – Jay Khan Oct 27 '21 at 03:58
  • Then the parameterized constructor – Jay Khan Oct 27 '21 at 03:59
  • This, `public Triangle()`, is a PARAMETERLESS constructor. It is NOT a default constructor. A default constructor is one that the compiler adds for you when you have NO constructors explicitly added to your class. If you have no constructors explicitly written, then the "default" constructor (the one added for you at compile time) is called when you use `new Triangle()`. All classes must have at least one constructor, otherwise you can't create an instance of it. This is why the DEFAULT constructor is added for you when you don't explicitly write one into your class. PARAMETERLESS != DEFAULT. – Idle_Mind Oct 27 '21 at 12:42
  • Seems to me what I've written satisfies the given requirements. Did this work for you? Were you able to add the toString() override? Pretty easy, just look up examples on the internet... – Idle_Mind Oct 27 '21 at 12:43
  • No, im getting an error that "constructor call must be first statement in costructor" – Jay Khan Oct 27 '21 at 13:31
  • Ah...sorry. The `this()` has be at the top. Been awhile since I used that feature. Will edit it. – Idle_Mind Oct 27 '21 at 13:37
  • Also, would you happen to know how i can find out if the triangle has a right angle? – Jay Khan Oct 27 '21 at 15:20
  • This is the instructions...• An isRight() method that returns a value representing whether the triangle has a right angle – Jay Khan Oct 27 '21 at 15:21
  • You'd have to do some comparisons to find the longest side, then simply use the Pythagorean theorem to test if you have a right triangle or not: https://www.varsitytutors.com/hotmath/hotmath_help/topics/converse-of-pythagorean-theorem – Idle_Mind Oct 27 '21 at 18:00
  • I had a question, how does the parameterized constructor that you sent me determine the upper bound for the randomized values? – Jay Khan Oct 27 '21 at 18:15
  • This constructor, `Triangle(int max)`, passes the value in "max" to `initSides()`. The parameterless constructor is passing a hard-coded 6. In `initSides()`, it's using that max value in the formula `rng.nextInt(max)+1`. – Idle_Mind Oct 27 '21 at 18:21
  • Thank you so much for all your help! Everything works great, if i post my final code here, would you mind looking it over one last time? I'd appreciate it! – Jay Khan Oct 27 '21 at 19:34
  • I will post my code as an answer under my question... – Jay Khan Oct 27 '21 at 19:35
0
package Proj3;

/**
 * Title: Triangle data class
 * Description: 
 * @author Jahangir Khan N00769290
 * 
 */

import java.util.Random;

public class Triangle {

    int side1;
    int side2;
    int side3;

    public Triangle() {
        initSides(6);
    }


    private void initSides(int max) {
        Random rng = new Random();
        side1 = rng.nextInt(max)+1;
        side2 = rng.nextInt(max)+1;
        side3 = rng.nextInt(max)+1;
    }


    //toString method
    public String toString() {
        return new String (" " + side1+ " x " +side2+ " x " +side3);
    }

    //Parameterized constructor
    public Triangle(int max) {
        this(); 
        if (max > 0) {
            initSides(max);
        }
    }


    //Parameterized constructor
    public Triangle (int side1, int side2, int side3) {
        this(); // call the no parameter constructor
        if ((side1 > 0) && (side2 > 0) && (side3 > 0)) {
            this.side1 = side1;
            this.side2 = side2;
            this.side3 = side3;
        }
    }

    //isEquilateral method
    public boolean isEquilateral() {
        if (side1 == side2 && side1 == side3)
        {
            return true;
        }
        else 
        {
            return false;
        }
    }

    public boolean isRight() {
        int a1 = side1*side1;
        int b1 = side2*side2;
        int c1 = side3*side3;
        if(c1== a1+b1 || b1==a1+c1 || a1==b1+c1){
            return true;
        }
        else {
            return false;
        }

    }

}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
Jay Khan
  • 5
  • 3