Results 1 to 7 of 7
  1. #1
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default 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

    Java 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 triangle subclass

    Java 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;
      }
    }
    Here is the TestCircleRectangleTriangle with main method

    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());		
      }
    }
    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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with superclasses, subclasses and calling constructors

    Quote Originally Posted by jim01 View Post
    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.
    How did you do it with Circle and Rectangle?

  3. #3
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default Re: Help with superclasses, subclasses and calling constructors

    Quote Originally Posted by Fubarable View Post
    How did you do it with Circle and Rectangle?
    First I added a toString() method to the GeometricObject1 class.

    Java Code:
    public String toString(){
      return "created on " + dateCreated + "\ncolor: " + color + " and filled: " + filled;
    Then I called them like this:

    Java Code:
    System.out.println("\nA rectangle " + rectangle.toString());
    and this

    Java Code:
    System.out.println("A circle " + circle.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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  5. #5
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default Re: Help with superclasses, subclasses and calling constructors

    Quote Originally Posted by Fubarable View Post
    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.
    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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  7. #7
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default Re: Help with superclasses, subclasses and calling constructors

    Quote Originally Posted by Fubarable View Post
    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.
    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:

    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());
      }
    }
    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.

Similar Threads

  1. Replies: 20
    Last Post: 05-17-2011, 01:31 PM
  2. need help with subclasses
    By thorobred in forum New To Java
    Replies: 6
    Last Post: 02-19-2011, 06:34 AM
  3. calling to superclass/constructors questions.
    By hayden06f4i in forum New To Java
    Replies: 48
    Last Post: 12-17-2010, 09:35 AM
  4. Calling subclassed methods from constructors
    By arefeh in forum New To Java
    Replies: 7
    Last Post: 01-22-2010, 01:22 AM
  5. Hibernate subclasses
    By Ed in forum JDBC
    Replies: 2
    Last Post: 07-02-2007, 05:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •