简体   繁体   中英

My program is only using the default values from my class GeometricObject instead of the user input

I'm currently learning about inheritance in java. I have a class GeometricObject:

public class GeometricObject {
    private String color = "white";
    private boolean filled = false;

    /**Default construct*/
    public GeometricObject() {
    }

    /**Construct a geometric object*/
    public GeometricObject(String color, boolean filled) {
        this.color = color;
        this.filled = filled;
    }

    /**Getter method for color*/
    public String getColor() {
        return color;
    }

    /**Setter method for color*/
    public void setColor(String color) {
        this.color = color;
    }

    /**Getter method for filled. Since filled is boolean,
     so, the get method name is isFilled*/
    public boolean isFilled() {
        return filled;
    }

    /**Setter method for filled*/
    public void setFilled(boolean filled) {
        this.filled = filled;
    }

}

And then I have a class Triangle that extends GemoetricObject:

import static java.lang.Math.sqrt;

public class Triangle extends GeometricObject{
    double side1 = 1.0;
    double side2 = 1.0;
    double side3 = 1.0;

    public Triangle(){
        super();
    }

    public Triangle(double side1, double side2, double side3, String color, boolean isFilled){
        super(color,isFilled);
        if (side1 <=0||side2 <= 0||side3 <= 0) {
            System.out.println("Sides must be greater than 0!");
        }

        if (side1 > (side2 + side3)||side2 > (side1 + side3)||side3 > (side1 + side2)) {
            this.side1 = side1;
            this.side2 = side2;
            this.side3 = side3;
            System.out.println("Invalid side lengths!");
        }
    }

    public double getSide1(){
        return side1;
    }

    public double getSide2(){
        return side2;
    }

    public double getSide3(){
        return side3;
    }

    public double getArea(){
        double s = (side1 + side2+ side3) / 2;
        return sqrt(s*(s - side1)*(s - side2)*(s-side3));
    }

    public double getPerimeter(){
        return side1 + side2 + side3;
    }

}

When I go to run the program, it runs fine. Perimeter and area all work. But, instead of outputting the values the user inputs, the program is outputting "The white triangle is 1.0 X 1.0 X 1.0." So it's only using the defaulted values, but I'm honestly not sure why

EDIT: Here's my main function, can't believe I forgot that:

import java.util.Scanner;
public class PA6 {

public static void main(String[] args) {

    Triangle t = new Triangle();
    Scanner input = new Scanner(System.in);

    System.out.println("Enter side 1: ");
    double side1 = input.nextDouble();
    System.out.println("Enter side 2: ");
    double side2 = input.nextDouble();
    System.out.println("Enter side 3: ");
    double side3 = input.nextDouble();
    System.out.println("Enter a color: ");
    String color = input.nextLine();
    input.nextLine();
    System.out.println("Is it filled? ");
    boolean fill = input.nextBoolean();
    input.nextLine();


    System.out.println("The " + t.getColor() + " triangle is " + t.getSide1() + " X " + t.getSide2() + " X " + t.getSide3());
    System.out.println("Area: " + t.getArea());
    System.out.println("Perimeter: " + t.getPerimeter());
    System.out.println("Filled? " + t.isFilled());
}

}

Problem is here:

Every side of triangle should be smaller than other two sum. So your > should look right. Operator || (OR) should be also changed to && (AND).

if(side1 < (side2 + side3) && side2 < (side1 + side3) && side3 < (side1 + side2)){ 
    this.side1 = side1;
    this.side2 = side2;
    this.side3 = side3;

} else {
    System.out.println("Invalid side lengths!");
}

UPD:

Use next() instead of nextLine()

String color = input.next(); 

And delete extra input.nextLine();

You use a Scanner to input data from the user, but you never pass this data to the Triangle 's constructor. Once you're done with the user input, just pass those variables as arguments to the constructor:

Triangle t = new Triangle(side1, side2, side3, color, fill);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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