Results 1 to 3 of 3
Thread: Method that displays a rectangle
- 11-17-2012, 05:21 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 3
- Rep Power
- 0
Method that displays a rectangle
First post here in the forums. Hello all! Currently in my first programming class, Java. Got some homework thats giving me some issues.
....Your task is to write a method that displays a rectangle with a given width and height. For example, if the width is 4 and the height is 2, print
[][][][]
[][][][]
Don't print anything if the width or height is larger than 25.....
This is what i have so far, i honestly lost right now. I know what i have to do. Which is First read the length, print "[]" time how big the length is. The repeat that for the total height. How exactly do i get there tho?
import java.util.Scanner;
public class DrawRects
{
/**
A method to display a rectangle of dimension <tt>height</tt>
by <tt>width</tt>. If the width or height are > 25, don't
display anything.
@param width, the width of the rectangle to be drawn
@param height, the height of the rectangle to be drawn
*/
/**
The main method reads the width and height and invokes your method.
*/
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int w = in.nextInt();
int h = in.nextInt();
displayRectangle(w, h);
}
public static void displayRectangle(int w, int h)
{
String square = "[]";
if( w > 25 && h > 25 )
{
// java will not compute if values are greater than 25
}
else
{
int value = 0;
for(int i = w; i < value; i++)
for(int n = h; n < value; n++)
System.out.print("[]");
}
}
}
- 11-17-2012, 05:50 PM #2
Member
- Join Date
- Nov 2012
- Posts
- 3
- Rep Power
- 0
Re: Method that displays a rectangle
I did figure it out. Will post so if anyone else needs.
import java.util.Scanner;
public class DrawRect {
public static void main( String args[] ) {
Scanner in = new Scanner(System.in);
int side, width;
System.out.print("Please enter a width: ");
width = in.nextInt();
System.out.print("Please enter a height: ");
side = in.nextInt();
int height = side;
displayRectangle(side, width, height);
}
public static void displayRectangle(int side, int height, int width)
{
if( side <= 25 && side > 0)
{
while(height >= 1)
{
width = 1;
while(width <= side)
{
System.out.print("[]");
width++;
while(height >=2 && height <= side-1 && width <= side-1)
{
System.out.print("[]");
width++;
}
}
--height;
System.out.println();
}
}
else
{
System.out.print("Values to big.");
}
System.out.println();
}
}
- 11-17-2012, 06:13 PM #3
Re: Method that displays a rectangle
Why do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Help me displays an image
By dinhtrieu08t4 in forum AWT / SwingReplies: 4Last Post: 10-07-2011, 04:58 PM -
button displays image
By hopelessnoob in forum AWT / SwingReplies: 2Last Post: 03-12-2011, 03:44 PM -
does rectangle contain or overlap another rectangle?
By Xycose in forum New To JavaReplies: 6Last Post: 11-30-2010, 11:29 PM -
Touch Displays
By Ciwan in forum New To JavaReplies: 4Last Post: 10-21-2010, 09:47 AM -
Wrong with Rectangle res = new Rectangle(0,0,0,0);???
By jiapei100 in forum AWT / SwingReplies: 3Last Post: 09-25-2010, 03:39 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks