| | |
Class Hierarchy extending triangles
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
Hello. I'm working on a class hierarchy that starts with an abstract class called GeometricObject. I want to use this class to create classes of different types of triangles. I want to these classes to inherit the perimeter method from the Triangle class. Here's what I have so far.
<code=java>
abstract class GeometricObject
{
//signature for perimeter method
public abstract double perimeter();
public class Triangle extends GeometricObject
{
//explicit constructor
public Triangle(double s1, double s2, double s3)
{
this.side1=s1;
this.side2=s2;
this.side3=s3;
}//terminates constructor
//perimeter method
public double perimeter()
{
return side1+side2+side3;
}//terminates perimeter method
//local data fields
private double side1;
private double side2;
private double side3;
}//terminates text of class Triangle
public class Isosceles extends Triangle
{
//constructor
public Isosceles( double EQside, double otherside)
{
super(EQside, otherside);
}//terminates constructor
//local data fields
private double EQside;
private double otherside;
}//terminates class Isosceles
public class Equilateral extends Isosceles
{
//contructor
public Equilateral(double side)
{
super(side);
}//terminates constructor
//local data fields
private double side;
}//terminates class Equilateral
}//terminates class GeometricObject
</code>
I'm recieving the following error message:
F:\Java\GeometricObject.java:35: cannot reference this before supertype constructor has been called
super(EQside, otherside);
^
F:\Java\GeometricObject.java:46: cannot reference this before supertype constructor has been called
super(side);
^
There is obviously something wrong with the 2 lines starting with super, but I have not been able to figure it out.
Thanks to everyone in advance for any help.
<code=java>
abstract class GeometricObject
{
//signature for perimeter method
public abstract double perimeter();
public class Triangle extends GeometricObject
{
//explicit constructor
public Triangle(double s1, double s2, double s3)
{
this.side1=s1;
this.side2=s2;
this.side3=s3;
}//terminates constructor
//perimeter method
public double perimeter()
{
return side1+side2+side3;
}//terminates perimeter method
//local data fields
private double side1;
private double side2;
private double side3;
}//terminates text of class Triangle
public class Isosceles extends Triangle
{
//constructor
public Isosceles( double EQside, double otherside)
{
super(EQside, otherside);
}//terminates constructor
//local data fields
private double EQside;
private double otherside;
}//terminates class Isosceles
public class Equilateral extends Isosceles
{
//contructor
public Equilateral(double side)
{
super(side);
}//terminates constructor
//local data fields
private double side;
}//terminates class Equilateral
}//terminates class GeometricObject
</code>
I'm recieving the following error message:
F:\Java\GeometricObject.java:35: cannot reference this before supertype constructor has been called
super(EQside, otherside);
^
F:\Java\GeometricObject.java:46: cannot reference this before supertype constructor has been called
super(side);
^
There is obviously something wrong with the 2 lines starting with super, but I have not been able to figure it out.
Thanks to everyone in advance for any help.
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
URL on facebook!
java Syntax (Toggle Plain Text)
public Triangle(double s1, double s2, double s3) { this.side1=s1; this.side2=s2; this.side3=s3; } public Isosceles( double EQside, double otherside) { super(EQside, otherside); //calls triangle constructor } public Equilateral(double side) { super(side); //calls isosceles constructor. }/
Last edited by jasimp; Mar 15th, 2009 at 7:59 pm.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
import java.util.ArrayList;
import java.util.List;
abstract class GeometricObject
{
public abstract String perimeter();
}
class Triangle extends GeometricObject
{
private double side1;
private double side2;
private double side3;
public Triangle(double s1, double s2, double s3)
{
this.side1=s1;
this.side2=s2;
this.side3=s3;
}
public String perimeter()
{
return "Triangle's perimeter method";
}
}
class Isosceles extends Triangle
{
public Isosceles(double s1, double s2, double s3)
{
super(s1, s2, s3);
}
public String perimeter()
{
return "Isoceles Triangle's perimeter method";
}
}
class Equilateral extends Isosceles
{
public Equilateral(double s1, double s2, double s3)
{
super(s1, s2, s3);
}
public String perimeter()
{
return "Equilateral Triangle's perimeter method";
}
}
public class Test
{
public static void main(String... args)
{
List<Triangle> list = new ArrayList<Triangle>();
list.add(new Triangle(0, 0, 0));
list.add(new Isosceles(0, 0, 0));
list.add(new Equilateral(0, 0, 0));
for(Triangle t: list)
System.out.println(t.perimeter());
}
}Triangle's perimeter method
Isoceles Triangle's perimeter method
Equilateral Triangle's perimeter method
Hope that helps.
Last edited by blackcompe; Mar 15th, 2009 at 8:04 pm.
Preparing for SCJP...
Last edited by jasimp; Mar 15th, 2009 at 8:09 pm.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Oh, right. So all of the constructor calls need to have the same number of parameters, but the parameters in the constructor definitions don't need to be the same in number, right? Thanks for your help, jasimp.
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
URL on facebook!
I have rewritten my program with your suggestions but I am still getting the same error message.
For isosceles I used three parameters in the constructor call to match the Triangle constructor. For equilateral I used two parameters in the constructor call to match the isosceles constructor. I thought this was the right way to do it but I am obviously wrong. Thanks again for any help.
Java Syntax (Toggle Plain Text)
abstract class GeometricObject { //signature for perimeter method public abstract double perimeter(); //begin class Triangle public class Triangle extends GeometricObject { //explicit constructor public Triangle(double s1, double s2, double s3) { this.side1=s1; this.side2=s2; this.side3=s3; }//terminates constructor //perimeter method public double perimeter() { return side1+side2+side3; }//terminates perimeter method //local data fields private double side1; private double side2; private double side3; }//terminates text of class Triangle //begin class Isosceles public class Isosceles extends Triangle { //constructor public Isosceles( double EQside, double otherside) { super(EQside, EQside, otherside); }//terminates constructor //local data fields private double EQside; private double otherside; }//terminates class Isosceles //begin class Equilateral public class Equilateral extends Isosceles { //constructor public Equilateral(double side) { super(side, side); }//terminates constructor //local data field private double side; }//terminates class Equilateral
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
URL on facebook!
If all that code is one Java file than that could be the problem. I separated each class GeometricShape, Triangle, Isosceles, Equilateral, into it's own file and had no trouble running a driver class creating each type of triangle.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
I know this is a beginner question but it is something that I have never had to do before. Is this the correct way to import the other classes?:
I tried without the asterisks but recieved the same error message which reads:
F:\Java\GeometricObject.java:7: package Triangle.java does not exist
import Triangle.java.*;
^
F:\Java\GeometricObject.java:8: package Isosceles.java does not exist
import Isosceles.java.*;
^
F:\Java\GeometricObject.java:9: package Equilateral.java does not exist
import Equilateral.java.*;
^
I'm doing this in my GeometricObject file. Thanks again for all your help.
Java Syntax (Toggle Plain Text)
import Triangle.java.*; import Isosceles.java.*; import Equilateral.java.*;
F:\Java\GeometricObject.java:7: package Triangle.java does not exist
import Triangle.java.*;
^
F:\Java\GeometricObject.java:8: package Isosceles.java does not exist
import Isosceles.java.*;
^
F:\Java\GeometricObject.java:9: package Equilateral.java does not exist
import Equilateral.java.*;
^
I'm doing this in my GeometricObject file. Thanks again for all your help.
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
URL on facebook!
•
•
•
•
I know this is a beginner question but it is something that I have never had to do before. Is this the correct way to import the other classes?
java Syntax (Toggle Plain Text)
public class Tester { public static void main(String[] args) { Triangle tri = new Triangle(56,56,56); System.out.println(tri.perimeter()); Isosceles iso = new Isosceles(56,56); System.out.println(iso.perimeter()); } }
Last edited by jasimp; Mar 16th, 2009 at 6:20 pm.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Good idea. Thanks so much for all your great advice.
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
URL on facebook!
![]() |
Other Threads in the Java Forum
- Previous Thread: Jasper error Cannot cast from JasperReport to String
- Next Thread: How do you write a code in java?
Views: 1270 | Replies: 11
| Thread Tools | Search this Thread |
Tag cloud for Java
911 addressbook android api append apple applet application arguments array arrays automation binary bluetooth chat class classes client code component csv database draw eclipse error event exception file fractal ftp game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me japplet java javaarraylist javaprojects jmf jni jpanel julia linked linux list loop map method methods mobile netbeans newbie number object objects oracle oriented panel print printf problem program programming project projects recursion replaydirector reporting researchinmotion return robot rotatetext scanner score screen se server set size sms socket sort sql stream string swing test threads time transfer tree ubuntu windows






