Class Hierarchy extending triangles

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2008
Posts: 467
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 39
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is online now Online
Posting Pro in Training

Class Hierarchy extending triangles

 
0
  #1
Mar 15th, 2009
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.
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!
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,584
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 52
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster

Re: Class Hierarchy extending triangles

 
1
  #2
Mar 15th, 2009
  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.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 11
Reputation: blackcompe is an unknown quantity at this point 
Solved Threads: 4
blackcompe's Avatar
blackcompe blackcompe is offline Offline
Newbie Poster

Re: Class Hierarchy extending triangles

 
0
  #3
Mar 15th, 2009
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...
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,584
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 52
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster

Re: Class Hierarchy extending triangles

 
0
  #4
Mar 15th, 2009
Originally Posted by blackcompe View Post
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.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 467
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 39
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is online now Online
Posting Pro in Training

Re: Class Hierarchy extending triangles

 
0
  #5
Mar 16th, 2009
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!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 467
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 39
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is online now Online
Posting Pro in Training

Re: Class Hierarchy extending triangles

 
0
  #6
Mar 16th, 2009
I have rewritten my program with your suggestions but I am still getting the same error message.
  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.
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!
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,584
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 52
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster

Re: Class Hierarchy extending triangles

 
0
  #7
Mar 16th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 467
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 39
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is online now Online
Posting Pro in Training

Re: Class Hierarchy extending triangles

 
0
  #8
Mar 16th, 2009
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?:
  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.
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!
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,584
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 52
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster

Re: Class Hierarchy extending triangles

 
1
  #9
Mar 16th, 2009
Originally Posted by Grn Xtrm View Post
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.
  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.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 467
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 39
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is online now Online
Posting Pro in Training

Re: Class Hierarchy extending triangles

 
0
  #10
Mar 16th, 2009
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!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1270 | Replies: 11
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC