Question
Consider the Rows class, as introduced in the textbook. Here is a link to the Rows.java class. Now add a new method to the Rows
Consider the Rows class, as introduced in the textbook. Here is a link to the Rows.java class. Now add a new method to the Rows class called makeRectangle, which takes one parameter, the height of the rectangle, and then displays a rectangle of that height.
For example, if you make this Rows object: Rows r = new Rows('x',5); and then make this call to makeRectangle: r.makeRectangle(3); your code should print
xxxxx
xxxxx
xxxxx
Notice that if you make a different Rows object, r2, with this constructor call: Rows r2 = new Rows('$',7); then the same method call, applied to the second object: r2.makeRectangle(3); would print
$$$$$$$
$$$$$$$
$$$$$$$
Hint: for your solution, use Rows methods makeRow and newLine, wrapped in an appropriate for loop.
Rectangle API:
*public* *class* Rows{
*char* sym; *int* width;
*public* Rows(*char* s, *int* w){ sym = s; width = w; }
*public* *char* getSym(){ *return* sym; }
*public* *int* getWidth(){ *return* width; }
*final* *char* BLANK = ' '; /// a constant!/
*public* *void* makeRow(){ *for*(*int* j = 0; j < width; j++) System.out.print(sym); }
*public* *void* varyRow(*int* k){ *for*(*int* j = 0; j < k; j++) System.out.print(sym); }
*public* *void* newLine(){ System.out.println(); }
*public* *void* spacedRow(){ *for*(*int* j = 0; j < width; j++) *if* (j % 2 == 0) System.out.print(sym); *else* System.out.print(BLANK); }
*public* Rows changeSymbol(*char* newSymbol){ Rows r = *new* Rows(newSymbol, width); *return* r; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started