Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to rotate an asterisk trapezoid in java?
    text
    copied!<p>In a school assignment we have to draw a trapezoid with asterisk in java. The constructor is like </p> <p><code>Trapezoid(int tWidth, int bWidth, char signA, char signB, int margin)</code></p> <p>tWidth: is the top width, bWidth: bottom width, signA: foreground sign, signB: background sign and margin: the margin (left and right) of the bWidth.</p> <p>my <code>main()</code> class looks like this:</p> <pre><code>Trapezoid t = new Trapezoid(3, 9, '*', '-', 2); t.printTrapezoid(); </code></pre> <p>then it should look like this:</p> <pre><code>-----***----- ----*****---- ---*******--- --*********-- </code></pre> <p>the <code>printTrapezoid()</code> method looks like this:</p> <pre><code>int width = 2* margin + bWidth; int length = (width - tWidth) / 2; while (tWidth &lt;= bWidth) { printChar(signB, length); printChar(signA, tWidth); printChar(signB, length); System.out.println(); tWidth++; } </code></pre> <p>The printChar() method looks like:</p> <pre><code>printChar(char signB, int length) { for (int i = 0; i &lt; length; i++) { System.out.print(signB); } } </code></pre> <p>So. This works fine for me. But i think there are some other and better solutions for that. The next exercise is to rotate this trapezoid just with <code>t.rotate()</code> in the <code>main()</code> class. So that it looks like:</p> <pre><code>--*********-- ---*******--- ----*****---- -----***----- </code></pre> <p>I have no idea how to do it. Can anybody give me a hint? Please, I don't want the solution here. Just a hint how i can solve it. Maybe it is better to refactor the draw method? I don't know...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload