| | |
triangle, rectangle and TwoDShape
Thread Solved |
•
•
Join Date: Jul 2007
Posts: 57
Reputation:
Solved Threads: 2
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:
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:
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
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.
Java Syntax (Toggle Plain Text)
lass Triangle extends TwoDShape { String style; Triangle(double width, double height, String style) { super(width,height); } private double calculateArea() { double area = (height * width/2.0); return area; } public String getStyle() { return style; } public String toString() { return "this is the area"+area + "this is the style"+style; } }
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:
Java Syntax (Toggle Plain Text)
public class TwoDShape { private double width; private double height; /** * The defaultConstructor for objects of class TwoDShape */ public TwoDShape() { } /** * AConstructor for objects of class TwoDShape * * @param width the width of the shape * @param height the height of the shape */ public TwoDShape(double width, double height) { this.width = width; this.height = height; } /** * getter method for width * * @return the width */ public double getWidth() { return width; } /** * getter method for height * * @return the height */ public double getHeight() { return height; } /** * setter method for width * * @param width the new value for width */ public void setWidth(double width) { this.width = width; } /** * setter method for height * * @param height the new value of height */ public void setHeight() { this.height = height; } /** * produces a string representation of the class * * @return the String represntation of the class attributes */ public String toString () { return "Height is " + height + " Width is " + width ; }
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.
•
•
Join Date: Apr 2007
Posts: 126
Reputation:
Solved Threads: 6
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:
Java Syntax (Toggle Plain Text)
private double calculateArea() { double area = (getHight() * getWidth()/2.0); return area; }
•
•
Join Date: Jul 2007
Posts: 57
Reputation:
Solved Threads: 2
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.
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.
•
•
Join Date: Jul 2007
Posts: 57
Reputation:
Solved Threads: 2
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.
This is my Triangle class
this is my Rectangle class
Java Syntax (Toggle Plain Text)
class TwoDShapeDriver { public static void main (String []args) { Triangle t1 = new Triangle(44,55,"scalene"); System.out.println(t1.toString()); Rectangle r1 = new Rectangle(22,44); System.out.println(r1.toString()); } }
This is my Triangle class
Java Syntax (Toggle Plain Text)
class Triangle extends TwoDShape { String style; double area; Triangle(double width, double height, String style) { super(width,height); } private double calculateArea() { area = (getHeight() * getWidth()/2.0); return area; } public String getStyle() { return style; } public String toString() { return "this is the area"+area + "this is the style"+style; } }
Java Syntax (Toggle Plain Text)
class Rectangle extends TwoDShape { String style; double area; Rectangle(double width, double height) { super(width,height); } private double calculateArea() { double area = (getHeight() * getWidth()); return area; } public String getStyle() { return style; } public String toString() { return "this is the area"+area; } }
Last edited by piers; Feb 28th, 2008 at 12:49 pm.
•
•
Join Date: Apr 2007
Posts: 126
Reputation:
Solved Threads: 6
In your calculateArea don't use a local instance, and set your class variable "area" to the newly calculated value like:
Java Syntax (Toggle Plain Text)
area = getHight() * getWidth...
Last edited by new_2_java; Feb 28th, 2008 at 1:01 pm.
![]() |
Other Threads in the Java Forum
- Previous Thread: How to get User Name using LDAP when my application URL hitted by user in intranet
- Next Thread: exceptions
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card character class client code collision component crashcourse css csv database eclipse ee error fractal free game gis givemetehcodez graphics gui html ide image integer integration j2me japplet java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linux list loan machine map method methods migrate mobile netbeans newbie objects oriented output panel phone physics problem program programming project projects radio recursion replaydirector reporting researchinmotion scanner se server service set sms software sort sql string swing test textfield threads transfer tree trolltech ubuntu utility windows






) 