-1

I have been instructed to "Write a class, Triangle, with one instance variable that takes two string values, (filled or not filled)".

I'm new to Java, and still haven't come across a situation where you could have two potential values for one instance variable.

How would I do this?

main method was given:

public static void main(String[] args)
{
TwoDPolygon polygons[] = new TwoDPolygon[3];
polygons[0] = new Triangle("filled", 8.5, 12.0);
polygons[1] = new Triangle("not Filled", 6.5, 7.5);
polygons[2] = new Triangle(7.0);

for (int i=0; i<polygons.length; i++)
{
System.out.println("Object is " + polygons[i].getName());
System.out.println("Triangle " + polygons[i].getStatus());
System.out.println("Area is " + polygons[i].area());
}
}
Stephanie
  • 1
  • 3
  • 9
    I can do `String var = "filled";` and I can do `var = "not filled";`. – Sotirios Delimanolis Mar 21 '15 at 17:33
  • What Sotirios Delimanolis said, then you can perform tasks based on var's value of "filled" or "not filled." It would actually make more sense to me to have a "filled" boolean here, but then again, we're not aware of the full context of your assignment. – fia928 Mar 21 '15 at 17:39
  • I was thinking of using a a boolean type as well, but I wasn't sure how to approach it. We are not exactly told what the class is supposed to do, but the code for the main method was given, as a sort of hint. I will edit my question to include the given code. – Stephanie Mar 21 '15 at 20:45

3 Answers3

1

Ok I have redesigned the code based on your updated question.

First of all, you need an abstract class called TwoDPolygon. This class is an abstract representation of all your polygons. It contains the constructors and the methods you need.

abstract class TwoDPolygon {
    protected String filled;
    protected double x;
    protected double y;
    protected TwoDPolygon(String filled, double x, double y){
        this.filled=filled;
        this.x=x;
        this.y=y;
    }
    protected TwoDPolygon(double x, double y){
        this.x=x;
        this.y=y;
    }
    protected TwoDPolygon(double y){
        this.y=y;
    }
    abstract String getName();

    abstract String getStatus();
    abstract Double area();
}

Then the next step is to create the Triangle class. You will have to extend the abstract TwoDPolygon. This is the code:

public class Triangle extends TwoDPolygon {

    //the first constructor
    public Triangle(String filled, double x, double y) {
        super(filled, x, y);

    }
    //the second one
    public Triangle(double x, double y){
        super(x,y);
    }
    //the third one
    public Triangle(double y){
        super(y);
    }

    public String getName() {

        return "Triangle";
    }

    public String getStatus() {

        return filled;
    }

    public Double area() {
        //Insert code here which calculates the area
        return 0.0;
    }
}

This is all. Every time when you instantiate a Triangle polygon it will chose the right constructor based on the parameters you supply. Now when you run your main you will have the following output:

Object is Triangle
Triangle filled
Area is 0.0
Object is Triangle
Triangle not Filled
Area is 0.0
Object is Triangle
Triangle null
Area is 0.0

Note: The area's code is not done. You will have to do that but I guess that shouldn't be a problem.

Also I have created three constructors as you said, but I don't know the parameters of the third one. I just guessed that it has only the x and y value.

I hope this is what you're looking for!! It shouldn't be that hard to adapt to your specific requirements, as I think it looks almost done.

Theo
  • 1,165
  • 9
  • 14
  • Note that `if (fill==true)` is redundand and does the same as `if (fill)` – Clashsoft Mar 21 '15 at 18:18
  • Even the the first { } can be removed as there is only one statement! ;) I have updated it.. – Theo Mar 21 '15 at 20:50
  • The boolean method makes sense. However I'm given detailed instructions and usually told which methods to use/write. I was told to include 3 constructors, but there was no mention of a method (thought it's hard to say). I have updated this question to include the main method which was given to me in the instructions. Thank you for your input. – Stephanie Mar 21 '15 at 21:15
0

It is most likely meant that it takes one argument with 2 valid values:

class Triangle {
    Triangle(String val) {
        if (!"filled".equals(val) || !"not filled".equals(val))
            throw ...;
    }
}

or enum type

public enum Type {
    FILLED,
    NOT)FILLED
}
Crazyjavahacking
  • 9,343
  • 2
  • 31
  • 40
  • Would you say this explanation still makes sense given the update I have made to the question? Either way, thanks for your input. – Stephanie Mar 21 '15 at 21:16
  • Absolutely, you will check the content of the passed string against the 2 valid values and throw an Exception if it has some other value. – Crazyjavahacking Mar 21 '15 at 22:52
0

I think what is meant is you have one boolean instance variable named isFilled. then you could have something like this:

boolean isFilled;

public triangle(String filled, int x, int y) {
    if (filled == "filled") {
        isFilled = true;
    } else if (filled == "notFilled") {
        isFilled = false;
    } else {
        //handle exception or whatever
    }
}

That way you can have one instance variable but still use a string in the constructor. I don't think this is a very practical thing to do but if that is what your assignment said then that is a good way to do it. I hope I helped!

Vincent Williams
  • 2,836
  • 3
  • 17
  • 22