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

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:

private double calculateArea()
{
         double area = (getHight() * getWidth()/2.0);

         return area;
}

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

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 :) )

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.

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.

I have to create the rectangle class. I can't inherit it from triangle.

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.

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

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;
	}
}

this is my Rectangle class

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; 
	}
}

In your calculateArea don't use a local instance, and set your class variable "area" to the newly calculated value like:

area = getHight() * getWidth...

I changed it in my rectangle class but I am still getting 0.0 when I run my driver class.

I changed it in my rectangle class but I am still getting 0.0 when I run my driver class.

Do you ever call the calculateAre of Triangle and Rectangle? you should call calculateArea to actually calculate and assing new value to area e.g.

class TwoDShapeDriver
{
     public static void main (String []args)
     {
          Triangle t1 = new Triangle(44,55,"scalene");  
          t1.calculateArea();
          System.out.println(t1.toString());

	
          Rectangle r1 = new Rectangle(22,44);
          r1.calculateArea();
          System.out.println(r1.toString());
     }
}
commented: thnx 4 ur help +1

thnx m8. I have solved it. Your spot on. I wasnt using my methods properly. once I have handed the homework in I will post my solution if ur interested. Until then this is a solved thread and I think if I am able 2 u can have a rep point for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.