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:
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:
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 triangleYou 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.