Tutorial 21 - Introduction to Java


Home > Tutorials > Tutorial21

This tutorial is in the Unix lab.

Question 1

If you haven't done so already, I recommend that you set the preferences in nedit as specified in an earlier tutorial.

Change directory to your Programming Environment tutorials directory.

Use nedit to type in the following java program. The file must be called HelloAgain.java because the program/class is called HelloAgain. Remember to run nedit in the background. e.g.

nedit HelloAgain.java&

Try arranging your windows so that you can clearly see the nedit window, Unix shell window and if necessary this tutorial in a web browser. This can make it easier to switch between the windows.

Type in the following program.

Save the file and stay in nedit.

Click on the title bar of the Unix shell window, to make it the current window.

Compile the program e.g.

javac HelloAgain.java

If you have any compilation errors.

Run the program e.g.

java HelloAgain

Question 2

Problem Description

Write a Java program to calculate the area and perimeter of a rectangle. The area of a rectangle is its length multiplied by its width. The perimeter is the distance around the outside of the rectangle. The logic for the problem is specified below. The program/class name is Rectangle, so the file name must be Rectangle.java. Hint: remember to convert the strings input to numbers.

Multiplication in Java uses a "*" like in Basic.

Example Run

Length ? 10
Width ? 5
Area = 50
Perimeter = 30

Pseudo Code

Rectangle()
    Input length, width
    area = length * width
    perimeter = (length + width) * 2
    Display area, perimeter
STOP

Data Dictionary

Name Data Type Description
area Integer The area of the rectangle
length Integer The length of the rectangle
perimeter Integer The length of the perimeter of the rectangle
width Integer The width of the rectangle

 


Previous Tutorial | Tutorial Index | Next Tutorial | Lecture
Last modified 11-May-2006 by Tim Whitfort.
Copyright © 2003-2006
Tim Whitfort