Computer Science 202
Introduction to Programming

Fall 2012, The College of Saint Rose

Rectangle BlueJ Project

Click here to download a BlueJ project for Rectangle.


Download file: Rectangle.vls


Rectangle Source Code

The Java source code for Rectangle is below. Click on a file name to download it.


Rectangle.java

/*
 * Example Rectangle
 * 
 * Compute the area and perimeter of a rectangle
 *
 * Jim Teresco, The College of Saint Rose, CSC 202, Fall 2012
 *
 * $Id: Rectangle.java 1892 2012-09-06 04:08:44Z terescoj $
 */

import java.util.Scanner;

public class Rectangle {

    public static void main(String[] args) {
        
        // we need a Scanner to get our rectangle's dimensions from the keyboard
        Scanner keyboard = new Scanner(System.in);
        
        // retrieve the width and height
        System.out.print("What is your rectangle's width? ");
        
        // we want to read a number, not a String, so we use a different method
        // of the Scanner to do this.
        int width = keyboard.nextInt();
        
        System.out.print("What is your rectangle's height? ");
        
        int height = keyboard.nextInt();
        
        // now, compute our two results and store them in two
        // more variables
        int area = width * height;
        int perimeter = 2 * width + 2 * height;
        
        // report the result
        System.out.println("Rectangle has area of " + area +
            " and perimeter of " + perimeter + ".");
    }
}