package hw4;
public class HW4 {
public static void main(String[] args) {
Triangle triangle = new Triangle(1, 1.5, 1);
triangle.setColor("yellow");
triangle.setFilled(true);
System.out.println(triangle);
System.out.println("The area is " triangle.getArea());
System.out.println("The perimeter is " triangle.getPerimeter());
System.out.println("The Triangle's Color is " triangle.getColor());
System.out.println("The Triangle is " triangle.isFilled());
}
}
class GeometricObject {
public class SimpleGeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
/** Construct a default geometric object */
public SimpleGeometricObject() {
dateCreated = new java.util.Date();
}
/** Construct a geometric object with the specified color
* and filled value */
public SimpleGeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
/** Return color */
public String getColor() {
return color;
}
/** Set a new color */
public void setColor(String color) {
this.color = color;
}
/** Return filled. Since filled is boolean,
its get method is named isFilled */
public boolean isFilled() {
return filled;
}
/** Set a new filled */
public void setFilled(boolean filled) {
this.filled = filled;
}
/** Get dateCreated */
public java.util.Date getDateCreated() {
return dateCreated;
}
/** Return a string representation of this object */
public String toString() {
return "created on " dateCreated "\ncolor: " color
" and filled: " filled;
}
}
}
class Triangle extends GeometricObject {
private double side1 = 1.0;
private double side2 = 1.0;
private double side3 = 1.0;
public Triangle() {
}
public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double getSide1() {
return side1;
}
public double getSide2() {
return side2;
}
public double getSide3() {
return side3;
}
public double getArea() {
return (side1 side2 side3) / 2;
}
public double getPerimeter() {
return side1 side2 side3;
}
public String toString() {
return " Triangle: Side 1 = " side1 " Side 2 = " side2
" Side 3 = " side3;
}
}
The lines that have triangle.setColor("yellow");, triangle.setFilled(true); System.out.println("The Triangle's Color is " triangle.getColor()); and System.out.println("The Triangle is " triangle.isFilled()); aren't working and return the error method is undefined.
If I could get some insight on this that'd be great. Thank you
CodePudding user response:
So, the best way that I can think of to approach this task is as follows:
Create a new file SimpleGeometricObject.java Create a new file Triangle.java
HW4.java
package hw4;
public class HW4 {
public static void main(String[] args) {
Triangle triangle = new Triangle(1, 1.5, 1);
triangle.setColor("yellow");
triangle.setFilled(true);
System.out.println(triangle);
System.out.println("The area is " triangle.getArea());
System.out.println("The perimeter is " triangle.getPerimeter());
System.out.println("The Triangle's Color is " triangle.getColor());
System.out.println("The Triangle is " triangle.isFilled());
}
}
SimpleGeometricObject.java
package hw4;
public abstract class SimpleGeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
/**
* Construct a default geometric object
*/
public SimpleGeometricObject() {
dateCreated = new java.util.Date();
}
/**
* Construct a geometric object with the specified color
* and filled value
*/
public SimpleGeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
/**
* Return color
*/
public String getColor() {
return color;
}
/**
* Set a new color
*/
public void setColor(String color) {
this.color = color;
}
/**
* Return filled. Since filled is boolean,
* its get method is named isFilled
*/
public boolean isFilled() {
return filled;
}
/**
* Set a new filled
*/
public void setFilled(boolean filled) {
this.filled = filled;
}
/**
* Get dateCreated
*/
public java.util.Date getDateCreated() {
return dateCreated;
}
/**
* Return a string representation of this object
*/
public String toString() {
return "created on " dateCreated "\ncolor: " color
" and filled: " filled;
}
}
Triangle.java
package hw4;
public class Triangle extends SimpleGeometricObject {
private double side1 = 1.0;
private double side2 = 1.0;
private double side3 = 1.0;
public Triangle() {
}
public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double getSide1() {
return side1;
}
public double getSide2() {
return side2;
}
public double getSide3() {
return side3;
}
public double getArea() {
return (side1 side2 side3) / 2;
}
public double getPerimeter() {
return side1 side2 side3;
}
public String toString() {
return " Triangle: Side 1 = " side1 " Side 2 = " side2
" Side 3 = " side3;
}
}
I have tested this on my machine and it works as you seem to desire it to! Please write back with any further questions and I will happily continue assisting you
CodePudding user response:
This is happens because Triangle class extends GeometricObject class, and the methods you are trying to use are members of the SimpleGeometricObject class.
For fixing this issue, you should make Triangle class extends SimpleGeometricObject (you might need to declare this class as static, I'm not sure about that).
For understanding the root of your issue, you should distinguish between attributes of the class itself, and attributes of its nested classes.
If you don't know what nested classes are, read this article: Nested Classes - Oracle