Results 1 to 7 of 7
- 09-29-2011, 11:09 PM #1
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
Help with superclasses, subclasses and calling constructors
I have a program that has 5 classes. A superclass called GeometricObject1, a subclass called Rectangle, a subclass called Circle, a subclass called Triangle, and a sublass called TestCircleRectangleTriangle, which contains the main method. I have no problems with the Circle or Rectangle classes, only witht eh Triangle subclass. The problem is that I cannot figure out how to print the Triangle color and whether or not it is filled.
Here is the superclass GeometricObject1
Here is the triangle subclassJava Code:public class GeometricObject1 { //declare variables private String color = "white"; private boolean filled; private java.util.Date dateCreated; //Construct a default geometric object public GeometricObject1(){ dateCreated = new java.util.Date(); } //Construct a geometric object with the specified color and filled value public GeometricObject1(String color, boolean filled){ dateCreated = new java.util.Date(); this.color = color; this.filled = filled; } //return color public String getColor(){ return color; } //set new color public void setColor(String color){ this.color = color; } //return filled public boolean isFilled(boolean filled){ 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; } }
Here is the TestCircleRectangleTriangle with main methodJava Code:public class Triangle extends GeometricObject1 { //declare variables private double side1 = 1; private double side2 = 1; private double side3 = 1; //construct a default triangle public Triangle(){ } //construct a triangle with specified sides public Triangle(double side1, double side2, double side3){ this.side1 = side1; this.side2 = side2; this.side3 = side3; } //construct a triangle with specified sides, color and fill public Triangle(double side1, double side2, double side3, String color, boolean filled){ super(color, filled); this.side1 = side1; this.side2 = side2; this.side3 = side3; } //return side1 public double getSide1(){ return side1; } //return side2 public double getSide2(){ return side2; } //return side3 public double getSide3(){ return side3; } //return area public double getArea(){ double s = (side1 + side2 + side3) / 2; double area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3)); return area; } //return perimeter public double getPerimeter(){ double perimeter = side1 + side2 + side3; return perimeter; } //return a string representation of this object public String toString(){ return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3; } }
I can't put the color and fill in the toString() like I did for the rectangle and circle classes. I tried calling the color (yellow) and fill (true) by using triangle.getColor("yellow") and triangle.isFilled(true) but that doesn't work. I'm unsure what to do.Java Code:public class TestCircleRectangleTriangle { //call main method public static void main(String [] args){ //create circle object Circle circle = new Circle(1); System.out.println("A circle " + circle.toString()); System.out.println("The radius is " + circle.getRadius()); System.out.println("The area is " + + circle.getArea()); System.out.println("The diameter is " + circle.getDiameter()); //create rectangle object Rectangle rectangle = new Rectangle(2, 4); System.out.println("\nA rectangle " + rectangle.toString()); System.out.println("The area is " + rectangle.getArea()); System.out.println("perimeter is " + rectangle.getPerimeter()); //create triangle object Triangle triangle = new Triangle(1, 1.5, 1, "yellow", true); System.out.println("\nA " + triangle.toString()); System.out.println("The area is " + triangle.getArea()); System.out.println("perimeter is " + triangle.getPerimeter()); } }
-
Re: Help with superclasses, subclasses and calling constructors
- 09-30-2011, 12:06 AM #3
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
Re: Help with superclasses, subclasses and calling constructors
First I added a toString() method to the GeometricObject1 class.
Then I called them like this:Java Code:public String toString(){ return "created on " + dateCreated + "\ncolor: " + color + " and filled: " + filled;
and thisJava Code:System.out.println("\nA rectangle " + rectangle.toString());
However, per the directions I have to do it a different way for the triangle object. I thought I could call it using the triangle.getColor() and triangle.isFilled() methods from the GeometricObject1 class but it's not working.Java Code:System.out.println("A circle " + circle.toString());
-
Re: Help with superclasses, subclasses and calling constructors
Why not call the super's toString() method inside of Triangle's toString method? If you are not allowed to do this, then perhaps you should show us exactly what you are and aren't allowed to do.
- 09-30-2011, 01:22 AM #5
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
Re: Help with superclasses, subclasses and calling constructors
I must "create a triangle object with sides 1, 1.5, 1. color yellow, and filled true, and display the area, perimeter, color, and whether filled or not."
I cannot use the super's toString() method because that would display information I do not want in addition to the information I do. The triangle class itself has a toString() method, but the directions told me exactly what to put into it and it does not include the color or fill information. This is what led me to think I was supposed to call that information in some other way.
-
Re: Help with superclasses, subclasses and calling constructors
Then you will need to use the super class's public getter or accessor methods to get the information that you need within your Triangle's toString method.
- 09-30-2011, 02:51 AM #7
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
Re: Help with superclasses, subclasses and calling constructors
I figured it out. Included in this chapter is polymorphism. I was focusing on trying to obtain the color and fill by using the getter method as I would if those methods were in the same class. It wouldn't work. However, once I focused on the polymorphism section, I was able to use the those getter methods. Here is how I did it:
Although to be honest with you I am not sure why I could not use the setColor(String color) and setFilled(boolean filled) methods and then use the getColor() and isFilled(boolean filled) commands like I did the other get methods.Java Code:public class TestCircleRectangleTriangle { //call main method public static void main(String [] args){ //create circle object Circle circle = new Circle(1); System.out.println("A circle " + circle.toString()); System.out.println("The radius is " + circle.getRadius()); System.out.println("The area is " + + circle.getArea()); System.out.println("The diameter is " + circle.getDiameter()); //create rectangle object Rectangle rectangle = new Rectangle(2, 4); System.out.println("\nA rectangle " + rectangle.toString()); System.out.println("The area is " + rectangle.getArea()); System.out.println("perimeter is " + rectangle.getPerimeter()); System.out.println(); //display triangle properties displayObject(new Triangle(1, 1.5, 1, "yellow", true)); } //display geometric object properties public static void displayObject(Triangle triangle){ System.out.println("A triangle's color: " + triangle.getColor() + " and filled: " + triangle.isFilled(true) + "\nThe area is " + triangle.getArea() + "\nperimeter is " + triangle.getPerimeter()); } }
Similar Threads
-
Question about superclasses. Pros and cons of an idea.
By Dark in forum New To JavaReplies: 20Last Post: 05-17-2011, 12:31 PM -
need help with subclasses
By thorobred in forum New To JavaReplies: 6Last Post: 02-19-2011, 05:34 AM -
calling to superclass/constructors questions.
By hayden06f4i in forum New To JavaReplies: 48Last Post: 12-17-2010, 08:35 AM -
Calling subclassed methods from constructors
By arefeh in forum New To JavaReplies: 7Last Post: 01-22-2010, 12:22 AM -
Hibernate subclasses
By Ed in forum JDBCReplies: 2Last Post: 07-02-2007, 04:42 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks