CS 200 - Lab 27

Java rectangles

In this lab you will practice writing Java programs.

Your task is to prompt the user to enter two int values, and to draw a rectangle of the specified size.

Example run (user input in bold red):

Height and width? 7 45
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************

Getting started

Start Eclipse.  Create a new Java project (File->New->Java Project).  Create a new package called edu.ycp.cs200.lab27.  Create a class called Rect in that package.  Create a main method in the class.

Hints

Java has for, while, and do/while loops.  Each one works just like its counterpart in C.

Use an instance of java.util.Scanner to read from the keyboard:

Scanner scanner = new Scanner(System.in);

The nextInt method reads one int value from the scanner.  Make sure you have an import statement at the top of your source file (just underneath the package declaration):

import java.util.Scanner;

Once your program can print a solid rectangle, try some more interesting rectangles, e.g.:

Outlined rectangle:

Height and width? 7 45
*********************************************
* *
* *
* *
* *
* *
*********************************************

Barn door square (height and width must be same):

Height and width? 10 10
**********
** **
* * * *
* * * *
* ** *
* ** *
* * * *
* * * *
** **
**********