Quote
6. Write a class named RightTriangle that extends Shape. Add two int
fields for the base and height of the triangle, and add a constructor
with two int parameters that are used to initialize these fields. Use
encapsulation to ensure that the value of base and height are
between 1 and 20.
7. In your RightTriangle class, override the draw() method in Shape.
Using nested for loops and asterisks, print out the triangle similarly
to the way you printed out the Rectangle. For example, if the base is
8 and the height is 4, the output should look similar to:
*
***
******
********
fields for the base and height of the triangle, and add a constructor
with two int parameters that are used to initialize these fields. Use
encapsulation to ensure that the value of base and height are
between 1 and 20.
7. In your RightTriangle class, override the draw() method in Shape.
Using nested for loops and asterisks, print out the triangle similarly
to the way you printed out the Rectangle. For example, if the base is
8 and the height is 4, the output should look similar to:
*
***
******
********
I wrote a code that, according to me atleast, should be working 100%. But it doesn't. I get the program to draw me a Triangle but it doesnt work out exactly as in the project request.
No use in me posting the entire class, so I'm just going to post the method that gets invoked that must "draw" the triangle.
public void draw()
{
int chan = (int)this.base/this.height;
for( int a = 1; a < this.height; a++ )
{
double sum = Math.round((this.base/(this.base-chan)) * a);
for( int b = 0; b < sum; b++ )
{
System.out.print("*");
}
System.out.print("\n");
}
for( int c = 0; c < this.base; c++ )
{
System.out.print("*");
}
System.out.print("\n");
}


Sign In
Create Account


Back to top









