943,754 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2668
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 15th, 2009
0

Class Hierarchy extending triangles

Expand Post »
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.
Reputation Points: 100
Solved Threads: 48
Posting Pro in Training
Grn Xtrm is offline Offline
495 posts
since Nov 2008
Mar 15th, 2009
1

Re: Class Hierarchy extending triangles

java Syntax (Toggle Plain Text)
  1.  
  2. public Triangle(double s1, double s2, double s3)
  3. {
  4. this.side1=s1;
  5. this.side2=s2;
  6. this.side3=s3;
  7. }
  8.  
  9.  
  10.  
  11. public Isosceles( double EQside, double otherside)
  12. {
  13. super(EQside, otherside); //calls triangle constructor
  14. }
  15.  
  16.  
  17. public Equilateral(double side)
  18. {
  19. super(side); //calls isosceles constructor.
  20. }/
Look at all your constructors. You have one triangle constructor, and it takes three parameters. Then in the Isosceles constructor you call super() with two parameters. You made the same mistake in the Equilateral constructor.
Last edited by jasimp; Mar 15th, 2009 at 7:59 pm.
Featured Poster
Reputation Points: 533
Solved Threads: 53
Senior Poster
jasimp is offline Offline
3,593 posts
since Aug 2007
Mar 15th, 2009
0

Re: Class Hierarchy extending triangles

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.
Reputation Points: 8
Solved Threads: 4
Newbie Poster
blackcompe is offline Offline
11 posts
since Mar 2009
Mar 15th, 2009
0

Re: Class Hierarchy extending triangles

Click to Expand / Collapse  Quote originally posted by blackcompe ...
Hope that helps.
It doesn't help because you just threw code at the OP, didn't offer any explanation to what you changed and didn't even show what you fixed. You should point the OP in the right direction instead of thrusting code at them.
Last edited by jasimp; Mar 15th, 2009 at 8:09 pm.
Featured Poster
Reputation Points: 533
Solved Threads: 53
Senior Poster
jasimp is offline Offline
3,593 posts
since Aug 2007
Mar 16th, 2009
0

Re: Class Hierarchy extending triangles

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.
Reputation Points: 100
Solved Threads: 48
Posting Pro in Training
Grn Xtrm is offline Offline
495 posts
since Nov 2008
Mar 16th, 2009
0

Re: Class Hierarchy extending triangles

I have rewritten my program with your suggestions but I am still getting the same error message.
Java Syntax (Toggle Plain Text)
  1. abstract class GeometricObject
  2. {
  3. //signature for perimeter method
  4. public abstract double perimeter();
  5. //begin class Triangle
  6. public class Triangle extends GeometricObject
  7. {
  8. //explicit constructor
  9. public Triangle(double s1, double s2, double s3)
  10. {
  11. this.side1=s1;
  12. this.side2=s2;
  13. this.side3=s3;
  14. }//terminates constructor
  15. //perimeter method
  16. public double perimeter()
  17. {
  18. return side1+side2+side3;
  19. }//terminates perimeter method
  20. //local data fields
  21. private double side1;
  22. private double side2;
  23. private double side3;
  24. }//terminates text of class Triangle
  25. //begin class Isosceles
  26. public class Isosceles extends Triangle
  27. {
  28. //constructor
  29. public Isosceles( double EQside, double otherside)
  30. {
  31. super(EQside, EQside, otherside);
  32. }//terminates constructor
  33. //local data fields
  34. private double EQside;
  35. private double otherside;
  36. }//terminates class Isosceles
  37. //begin class Equilateral
  38. public class Equilateral extends Isosceles
  39. {
  40. //constructor
  41. public Equilateral(double side)
  42. {
  43. super(side, side);
  44. }//terminates constructor
  45. //local data field
  46. private double side;
  47. }//terminates class Equilateral
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.
Reputation Points: 100
Solved Threads: 48
Posting Pro in Training
Grn Xtrm is offline Offline
495 posts
since Nov 2008
Mar 16th, 2009
0

Re: Class Hierarchy extending triangles

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.
Featured Poster
Reputation Points: 533
Solved Threads: 53
Senior Poster
jasimp is offline Offline
3,593 posts
since Aug 2007
Mar 16th, 2009
0

Re: Class Hierarchy extending triangles

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)
  1. import Triangle.java.*;
  2. import Isosceles.java.*;
  3. import Equilateral.java.*;
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.
Reputation Points: 100
Solved Threads: 48
Posting Pro in Training
Grn Xtrm is offline Offline
495 posts
since Nov 2008
Mar 16th, 2009
1

Re: Class Hierarchy extending triangles

Click to Expand / Collapse  Quote originally posted by Grn Xtrm ...
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?
Why would you do this in your GeometricObject class? Just create a driver class, here's the one I made.
java Syntax (Toggle Plain Text)
  1. public class Tester {
  2. public static void main(String[] args) {
  3. Triangle tri = new Triangle(56,56,56);
  4. System.out.println(tri.perimeter());
  5. Isosceles iso = new Isosceles(56,56);
  6. System.out.println(iso.perimeter());
  7.  
  8. }
  9.  
  10. }
Creating a driver class just tests code from other classes you have built. Your GeometricObject class is an abstract class and has no need to import the other classes because it never uses them. I'm pretty sure that if you keep your Java files in the same folder, say "/Java Code/Geometric" when you compile them the compiler will find all the classes it needs.
Last edited by jasimp; Mar 16th, 2009 at 6:20 pm.
Featured Poster
Reputation Points: 533
Solved Threads: 53
Senior Poster
jasimp is offline Offline
3,593 posts
since Aug 2007
Mar 16th, 2009
0

Re: Class Hierarchy extending triangles

Good idea. Thanks so much for all your great advice.
Reputation Points: 100
Solved Threads: 48
Posting Pro in Training
Grn Xtrm is offline Offline
495 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Jasper error Cannot cast from JasperReport to String
Next Thread in Java Forum Timeline: How do you write a code in java?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC