011012 011012 - 4 months ago 8
Java Question

Printing a rectangle of asterisks in Java

The following is the source code to print a rectangle entirely composed of asterisks (*), with a test class included as well.

import javax.swing.JOptionPane ;

/**
* A class to print block rectangles.
*/
class RectanglePrinter
{
// instance var's
int height ; // height of rectangle (i.e. number of segments)
int width ; // width of each segment(i.e. number of "*"s printed)

/**
* Create a RectanglePrinter object.
* @param height height of rectangle (i.e., number of lines to print)
* @param width width of rectangle (i.e., number of '*'s per line
*/
public RectanglePrinter(int height, int width) // constructor
{
this.height = height ;
this.width = width ;
}

/**
* Prints one line of a rectangle, by printing exactly "width" asterisks
*/
public void printSegment()
{
// write the body of this method here


}

/**
* Prints a rectangle exactly "height" lines in height. Each line is
* printed via a call to method printSegment
*/
public void printRectangle()
{
System.out.println("Printing a " + height + " x " + width + " rectangle:") ;
// write the body of this method here


}

} // end of class rectanglePrinter definition

public class RectanglePrinterTest
{
public static void main (String [] args)
{
String input = JOptionPane.showInputDialog
("What is the height of the rectangle?") ;
int height = Integer.parseInt(input) ;

input = JOptionPane.showInputDialog
("What is the width of the rectangle?") ;

int width = Integer.parseInt(input) ;

RectanglePrinter r = new RectanglePrinter(height, width) ;

System.out.println() ;
r.printRectangle() ;
System.out.println() ;
}
}


In the segments where it says to fill out the method body, I was instructed to use for loops to print out the asterisks. I think I have a basic idea of how to do the printSegment() method:

for (int w = 1; w <= width; w++)
{
System.out.println("*");
}


But from there, I am unsure of what to do within the printRectangle method. Judging from the comments in the code, I think I'm supposed to write a for loop in the printRectangle method that calls the printSegment method, except I don't think you can call a void method within the body of another method. Any help on this would be greatly appreciated.

Update:

I attempted to use the following code within the body of printRectangle()

for (int h = 1; h <= height; h++)
{
printSegment();
}


After running the code and inputting the height and width, I received the following output:

Printing a 6 x 7 rectangle:
*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*


Now I can print out the asterisks at least, so I just need to know how to modify the code so the output is a rectangular block of asterisks.

Update #2.

I figured out the solution. Here's the code that got me the result I was looking for.

import javax.swing.JOptionPane ;

/**
* A class to print block rectangles.
*/
class RectanglePrinter
{
// instance var's
int height ; // height of rectangle (i.e. number of segments)
int width ; // width of each segment(i.e. number of "*"s printed)

/**
* Create a RectanglePrinter object.
* @param height height of rectangle (i.e., number of lines to print)
* @param width width of rectangle (i.e., number of '*'s per line
*/
public RectanglePrinter(int height, int width) // constructor
{
this.height = height ;
this.width = width ;
}

/**
* Prints one line of a rectangle, by printing exactly "width" asterisks
*/
public void printSegment()
{
// write the body of this method here
for (int w = 1; w <= width; w++)
{
for (int h = 1; h <= height; h++)
{
System.out.print("*");
}
System.out.println();
}


}

/**
* Prints a rectangle exactly "height" lines in height. Each line is
* printed via a call to method printSegment
*/
public void printRectangle()
{
System.out.println("Printing a " + height + " x " + width + " rectangle:") ;
// write the body of this method here
printSegment();


}

} // end of class rectanglePrinter definition

public class RectanglePrinterTest
{
public static void main (String [] args)
{
String input = JOptionPane.showInputDialog
("What is the height of the rectangle?") ;
int height = Integer.parseInt(input) ;

input = JOptionPane.showInputDialog
("What is the width of the rectangle?") ;

int width = Integer.parseInt(input) ;

RectanglePrinter r = new RectanglePrinter(height, width) ;

System.out.println() ;
r.printRectangle() ;
System.out.println() ;
}
}


The differences made: I added a nested for loop in the body of the printSegment() method, and instead of using
System.out.println("*");
, used
System.out.print("*")
in the innermost loop, and
System.out.println()
in the outer loop, which resulted in the following output for the input of 5 x 7:

Printing a 5 x 7 rectangle:
*****
*****
*****
*****
*****
*****
*****

Answer

It does indeed sound like you are supposed to make a loop in printRectangle where you call printSegment for height amount times.

To call your printSegment method inside printRectangle simply write printSegment(); to call it.

Since its in the same class this is all you have to do to call it. If it were called from another class you can check how RectanglePrinterTest main method calls printRectangle using r.printRectangle(); where r is an instance of the RectanglePrinter class.

As its a void method it wont return anything so you don't need to do anything else. :)

So to sum it up you are on the right track! Keep going and just try what you think feels right and you will surely figure it out.