triangle, rectangle and TwoDShape

Thread Solved
Reply

Join Date: Jul 2007
Posts: 57
Reputation: piers is an unknown quantity at this point 
Solved Threads: 2
piers piers is offline Offline
Junior Poster in Training

triangle, rectangle and TwoDShape

 
0
  #1
Feb 26th, 2008
I am currently working on something where I have to make two shapes which extend to a class called 2d shape. I am currently working on my Triangle class which is this:

  1. lass Triangle extends TwoDShape
  2. {
  3.  
  4. String style;
  5.  
  6. Triangle(double width, double height, String style)
  7. {
  8. super(width,height);
  9.  
  10. }
  11.  
  12. private double calculateArea()
  13. {
  14. double area = (height * width/2.0);
  15.  
  16. return area;
  17. }
  18.  
  19. public String getStyle()
  20. {
  21. return style;
  22. }
  23.  
  24. public String toString()
  25. {
  26.  
  27. return "this is the area"+area + "this is the style"+style;
  28. }
  29. }

Now I am getting an error message because width and height which I am trying to use in my calculateArea method are private in the TwoDShape class which looks like this:

  1. public class TwoDShape
  2. {
  3. private double width;
  4. private double height;
  5.  
  6. /**
  7.   * The defaultConstructor for objects of class TwoDShape
  8.   */
  9. public TwoDShape()
  10. {
  11. }
  12.  
  13. /**
  14.   * AConstructor for objects of class TwoDShape
  15.   *
  16.   * @param width the width of the shape
  17.   * @param height the height of the shape
  18.   */
  19. public TwoDShape(double width, double height)
  20. {
  21. this.width = width;
  22. this.height = height;
  23. }
  24.  
  25. /**
  26.   * getter method for width
  27.   *
  28.   * @return the width
  29.   */
  30. public double getWidth()
  31. {
  32. return width;
  33. }
  34.  
  35. /**
  36.   * getter method for height
  37.   *
  38.   * @return the height
  39.   */
  40. public double getHeight()
  41. {
  42. return height;
  43. }
  44.  
  45. /**
  46.   * setter method for width
  47.   *
  48.   * @param width the new value for width
  49.   */
  50. public void setWidth(double width)
  51. {
  52. this.width = width;
  53. }
  54.  
  55. /**
  56.   * setter method for height
  57.   *
  58.   * @param height the new value of height
  59.   */
  60. public void setHeight()
  61. {
  62. this.height = height;
  63. }
  64.  
  65. /**
  66.   * produces a string representation of the class
  67.   *
  68.   * @return the String represntation of the class attributes
  69.   */
  70. public String toString ()
  71. {
  72. return "Height is " + height + " Width is " + width ;
  73. }

Now the TwoDShape class I cannot change so how do I use height and width in my calculateArea method in the triangle class when the height and the width are private variables?

This is the first section of my homework
Your first task is to write a class Triangle that extends the TwoDShape class. The class Triangle has the following attributes:

• A String holding the name of the style of the triangle, e.g. Scalene, Isosceles, Equilateral. (Note: Your program does not have to calculate the type of triangle)

It will have a constructor which takes three parameters the width, the height and the style.

It should also have the following methods that provide the following services:

• Calculates the area of the triangle (height * width / 2.0)
• Returns the style of the triangle
• Returns a string representing the values of all the attributes of the triangle

You will need to provide a driver to show your Triangle class behaves correctly.
What I am not sure of is if I need a JOptionPane to enter in the style. Any ideas on that cus I am not sure on that. I don't understand what calculating the type of triangle is.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 126
Reputation: new_2_java is an unknown quantity at this point 
Solved Threads: 6
new_2_java new_2_java is offline Offline
Junior Poster

Re: triangle, rectangle and TwoDShape

 
0
  #2
Feb 26th, 2008
In your caculateArea() you should use the getter method of your TwoDShape class to obtain the hight and width and not directly since they are private to TwoDShape. You can do something like this:
  1. private double calculateArea()
  2. {
  3. double area = (getHight() * getWidth()/2.0);
  4.  
  5. return area;
  6. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 57
Reputation: piers is an unknown quantity at this point 
Solved Threads: 2
piers piers is offline Offline
Junior Poster in Training

Re: triangle, rectangle and TwoDShape

 
0
  #3
Feb 26th, 2008
Thnx. I am not marking this thread as solved yet because I may have other questions but that means I can now do a big chunk of my coding :-) thnx
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,346
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 498
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: triangle, rectangle and TwoDShape

 
0
  #4
Feb 26th, 2008
Making those variables "protected" would also allow subclasses to access them.

(Of course, that may lead to some design traps by locking your base class into a particular implementation, but I doubt you really have to worry about that in this case )
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 57
Reputation: piers is an unknown quantity at this point 
Solved Threads: 2
piers piers is offline Offline
Junior Poster in Training

Re: triangle, rectangle and TwoDShape

 
0
  #5
Feb 26th, 2008
That TwoDShape class I can't change. That was given to me by my lecturer to do this work. the only other things If I have questions they will be on 3 things
1. the calculate area if I keep it the same code for a rectangle driver or take the /2.0 off the end of it.
2. Testing- my last homework I didn't test it correctly and I thought it worked and when my lecturer tested it with some other ways I hadnt thought of then it didnt work.
3. The different styles of triangle.

however I will go and code some more and then if I can't figure those things out then I will psot again.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 13
Reputation: shinnxennosagga is an unknown quantity at this point 
Solved Threads: 1
shinnxennosagga shinnxennosagga is offline Offline
Newbie Poster

Re: triangle, rectangle and TwoDShape

 
0
  #6
Feb 26th, 2008
If you need a Rectangle class, then you can just inherit it from TwoDShape and code it all over again, or you may inherit it from Triangle class and override method calculateArea(), but the latter is not suggested.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 57
Reputation: piers is an unknown quantity at this point 
Solved Threads: 2
piers piers is offline Offline
Junior Poster in Training

Re: triangle, rectangle and TwoDShape

 
0
  #7
Feb 26th, 2008
I have to create the rectangle class. I can't inherit it from triangle.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 57
Reputation: piers is an unknown quantity at this point 
Solved Threads: 2
piers piers is offline Offline
Junior Poster in Training

Re: triangle, rectangle and TwoDShape

 
0
  #8
Feb 28th, 2008
Hi guys I am now onto my driver class and it is causing me issues. I need it to show that it can print out the correct area and for the style for the triangle. for the rectangle I just need to print out the area. It is weird cus when I use the toString in my code it only ever displays for both rectangle and Triangle the result 0.0.

  1. class TwoDShapeDriver
  2. {
  3. public static void main (String []args)
  4. {
  5. Triangle t1 = new Triangle(44,55,"scalene");
  6.  
  7. System.out.println(t1.toString());
  8.  
  9.  
  10. Rectangle r1 = new Rectangle(22,44);
  11.  
  12. System.out.println(r1.toString());
  13. }
  14. }

This is my Triangle class
  1. class Triangle extends TwoDShape
  2. {
  3.  
  4. String style;
  5. double area;
  6. Triangle(double width, double height, String style)
  7. {
  8. super(width,height);
  9.  
  10. }
  11.  
  12. private double calculateArea()
  13. {
  14. area = (getHeight() * getWidth()/2.0);
  15.  
  16. return area;
  17. }
  18.  
  19. public String getStyle()
  20. {
  21. return style;
  22. }
  23.  
  24. public String toString()
  25. {
  26.  
  27.  
  28. return "this is the area"+area + "this is the style"+style;
  29. }
  30. }
this is my Rectangle class
  1. class Rectangle extends TwoDShape
  2. {
  3. String style;
  4. double area;
  5.  
  6. Rectangle(double width, double height)
  7. {
  8. super(width,height);
  9.  
  10. }
  11.  
  12. private double calculateArea()
  13. {
  14. double area = (getHeight() * getWidth());
  15.  
  16. return area;
  17. }
  18.  
  19. public String getStyle()
  20. {
  21. return style;
  22. }
  23.  
  24. public String toString()
  25. {
  26.  
  27. return "this is the area"+area;
  28. }
  29. }
Last edited by piers; Feb 28th, 2008 at 12:49 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 126
Reputation: new_2_java is an unknown quantity at this point 
Solved Threads: 6
new_2_java new_2_java is offline Offline
Junior Poster

Re: triangle, rectangle and TwoDShape

 
0
  #9
Feb 28th, 2008
In your calculateArea don't use a local instance, and set your class variable "area" to the newly calculated value like:
  1. area = getHight() * getWidth...
Last edited by new_2_java; Feb 28th, 2008 at 1:01 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 57
Reputation: piers is an unknown quantity at this point 
Solved Threads: 2
piers piers is offline Offline
Junior Poster in Training

Re: triangle, rectangle and TwoDShape

 
0
  #10
Feb 28th, 2008
I changed it in my rectangle class but I am still getting 0.0 when I run my driver class.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC