Consider the Rows class, as introduced in the textbook. Here is a link to the Ro
ID: 3670145 • Letter: C
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 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;
}
}
Explanation / Answer
Here we'll just call the String value val and we'll call the int width.
public void makeRectangle(int height)
{
for (int i = 0; i < height; i++)
{
makeRow();
newLine();
}
/*Here I'm just guessing what the code in makeRow() and newLine() does, since you didn't provide those methods.*/
Rows.java class:
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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.