简体   繁体   English

创建参数化构造函数以确定随机边长的上限

[英]Creating a parameterized constructor to determine upper bound for randomized side lengths

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.我正在研究一个班级项目,我们必须创建一个 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.我创建了一个默认构造函数,它为三角形的每条边提供不超过 6 的随机长度。被要求创建一个参数化构造函数,允许调用者确定随机边长的上限,如果提供的上限无效,此构造函数应默认为默认构造函数中使用的范围。

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;
    }
}

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) {
    if (max > 0) {
        initSides(max);
    }
    else {
        this(); // call the no parameter constructor
    }
}

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

private void initSides(int max) {
    Random rng = new Random();
    side1 = rng.nextInt(max)+1;
    side2 = rng.nextInt(max)+1;
    side3 = rng.nextInt(max)+1;
}
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;
        }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM