This assignment is really to learn about inheritance. I have a main that
uses this program via inheritance, but I can't figure out how to make it
print the asterisks in the form of a triangle instead of a square. Can
someone help me?
Thanks
Jo
public class Triangle extends Shape
{
private int side;
public Triangle()
{
side = 0;
}
public Triangle(int sideLength, int offSet)
{
super(offSet);
side = sideLength;
}
public String Draw()
{
String out = "";
for (int i = 1; i <= side; i++)
{
out += super.Draw();
for (int j = 1; j <= side; j++ )
{
out += "* ";
}
out += "\n";
}
return out;
}
public String Display()
{
String out = "Triangle: side = " + side + " ";
out += super.Display();
return out;
}
}
KC Wong - 08 Dec 2004 00:55 GMT
> This assignment is really to learn about inheritance. I have a main that
> uses this program via inheritance, but I can't figure out how to make it
> print the asterisks in the form of a triangle instead of a square. Can
> someone help me?
Draw a triangle with asterisk on paper.
1 *
2 ***
3 *****
Then look at it line by line - observe the relationship between the line
numbers and the number of asterisks.
Next observe not only the asterisks, but also the space in front of
asterisks in each line too.