CS180 Spring 2011 Lab2 Assignment

Lab goals

In this lab you will practice with some errors that you may have when compiling and running a Java program. To this end, you will receive the source code of a Java program containing some syntactical errors, as well as logical errors. Your task will be to find out these errors and to correct them. Do not be scared! Your lab TA will help you in this task.

Setup

Login with your UNIX account. Then create a directory named lab02 under your home directory. To do that, issue the following commands:

$ cd

$ mkdir lab02

Download the program DeerPopulationCalculator in the lab02 directory.

Assignment description

In the first lab, you learned how to write a simple Java program, how to save it on a file, how to compile and run it. You know that any programming language has its own syntax. The language syntax prescribes how correct language statements must be formed. You already saw the syntax of some Java statements such as the for and the if statement. When writing a Java program you can make several common syntax mistakes. If the Java program is not syntactically correct, the Java compiler will raise errors. Hence it is important to get familiar with some of these common syntactical errors and with the error messages that the Java compiler may produce for each error.

A Java program can also contain logical errors. For example, if your program code contains a for loop, you may expect that the loops is executed, say 100 times, but when you run your program, you discover that the for loop is executed, say just 50 times. Then you have to figure out what you did wrong!

The Java program that we will use in this lab id the one that was presented in the last week recitation.

Read the description of the problem that the Java program is intended to solve in the Rec01 file.

Launch Dr. Java, open the program DeerPopulationCalculator, and compile it. You will see that there are many syntax errors. You will have to correct them, and then run the program. When you run the program the first time, OOPs!!, you’ll see that it does not stop and it continues to print “Population for year = xxx”, where xxx is an ever increasing number! (By the way, to stop the execution of your program, you can click the Reset button in the Dr. Java menu bar.) So, you have to find out what logical error is in the program, since we expected it to produce only 300 lines of output!

TODO #1

Find and correct the 4 syntax errors of the provided program. Note that a single line may contain more than one syntax error!

TODO #2

Find and correct the 5 logical errors.

 

Grading Rubric

NOTE: syntax errors in the same line accounts for 1 error.

TODO

Points

Syntax error#1 found&corrected

10

Syntax error#2 found&corrected

10

Syntax error#3 found&corrected

20

Syntax error#4 found&corrected

10

Logical error #1

10

Logical error #2

10

Logical error #3

10

Logical error #4

10

Logical error #5

10

TOT POINTS

100

 

Turnin Instructions

To turn in first remove all the .class files from your Lab02 folder. Then issue the following commands:

% pwd

You should see this as result: /u/u91/yourlogin/CS180/lab02

% rm *.class

Next change your current folder to your CS180 folder and run the turnin command.

% cd   ../

% turnin -v -c cs180=XXX -p lab02 lab02

 

*Where XXX is your section number (ask your lab TA).*