Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'ve been working on this for almost 4 hours nowand I just can\'t seem to get i

ID: 3612709 • Letter: I

Question

I've been working on this for almost 4 hours nowand I just can't seem to get it. Any help would beappreciated.
  1. Write recursive methods to print each of the following shapes
    1. Rectangle: In this shape thenumber of characters printed per line and the number of lines isdifferent. These shapes will be printed flush left with aspace in between characters. The calling program shouldprovide the number of lines to print, the number of characters perline, and the character to use. IE a 4 X 5 rectangle of *’swould be:
* * * * * * * * * * * * * * * * * * * * * * * * *
  1. Right Triangle: This shape can beprinted with the longest row on either the top of bottom (rightside up or upside down!) Each row in this shape will print 1 fewercharacter. A space will be printed betweencharacters. The calling program will provide the character touse and the total # number of rows: IE a right triangle containing6 rows is:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  1. Right Triangle: This shape can beprinted with the longest row on either the top of bottom (rightside up or upside down!) Each row in this shape will print 1 fewercharacter. A space will be printed betweencharacters. The calling program will provide the character touse and the total # number of rows: IE a right triangle containing6 rows is:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Explanation / Answer

Rectangle public void printRect(int lines, intchars, char c) {       if(lines==0)       {          return;       }       printChars(chars,c);       out.println();       printRect(lines-1,chars, c); } public void printChars(int chars, charc) {       if(chars==0)       {           return;       }       out.print(c+"");       printChars(chars-1,c); } The two recursive methods basically each take the place of afor loop. printRect() callsprintChars(), creating a nestedeffect.
public void printTriangle(int rows, charc) {       if(lines==0)       {          return;       }       printChars2(rows,c);       out.println();       printTriangle(rows-1,, c); } public void printChars2(int chars, charc) {       if(chars==0)       {           return;       }       out.print(c+"");       printChars(chars-1,c); } Because the triangle's width and height decrease at the samerate, only one integer for rows is needed. The printChars2() method takes that integer, whichdecreases with printTriangle() calls,and prints the appropriate number of characters. The two recursive methods basically each take the place of afor loop. printRect() callsprintChars(), creating a nestedeffect.
public void printTriangle(int rows, charc) {       if(lines==0)       {          return;       }       printChars2(rows,c);       out.println();       printTriangle(rows-1,, c); } public void printChars2(int chars, charc) {       if(chars==0)       {           return;       }       out.print(c+"");       printChars(chars-1,c); } public void printTriangle(int rows, charc) {       if(lines==0)       {          return;       }       printChars2(rows,c);       out.println();       printTriangle(rows-1,, c); } public void printChars2(int chars, charc) {       if(chars==0)       {           return;       }       out.print(c+"");       printChars(chars-1,c); } Because the triangle's width and height decrease at the samerate, only one integer for rows is needed. The printChars2() method takes that integer, whichdecreases with printTriangle() calls,and prints the appropriate number of characters.
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote