CS 132: Intro to Computer Science II

Spring 1999

Assignment 1

Java Basics

Due Monday February 1

Warm Up

Copy the files in ~miller/cs132/lab1 and place them in your cs132/lab1 directory. Take a look at the file F2C.java, which is a variation of the temperature program in the text. Compile and run it.

You will note that the centigrade temperature may be displayed with many digits on the right-hand side of the decimal point. Devise a way to round the centigrade temperature to the nearest tenth and edit your program so that the centigrade temperature is displayed this way. Hint: page 31 in the text discusses how to round to the nearest integer. With some multiplication and division, you can also get a rounding to the nearest tenth. Consult the instructor if you need more help.

Part 1

Create a new program called Grades (Grades.java as the source file name) that calculates a student's grade percentage for this class. The program should prompt the user for the quiz average (entered as a double float out of 5), the lab average (entered as a double float out of 20), the test 1 and test 2 scores (each entered separately as an integer out of 50), and the final score (entered as an integer out of 100). Your program should then do the following:

Make sure your program is easy to use and clearly labels its output.

Part 2

In your directory, there is a java program called Loopy. Compile and run it. You will notice that this program takes some time to run. You can get a more precise run time by running it in the following way:

time java Loopy

The first number is the CPU runtime in seconds.

Take a look at the source code for this program. While we have not covered for loops in the course yet, it is not hard to figure out that the constant OUTERLOOP controls how many times the code inside the outer for loop is executed. Experiment with different values for OUTERLOOP and make sure you record your results.

Also in this directory is a C++ program (Loopy.cpp), which is essentially the same program as the Loopy java program. To compile this program, use the following command:

g++ -o cloopy Loopy.cpp

To run and time the program use the following command:

time ./cloopy

The C++ program also has the OUTERLOOP constant, which you can modify to run some additional experiments. Collect five data points for both the java program and the C++ program (use the same data points for both languages). Graph your results (you may do it by hand). Write a brief summary (one or two paragraphs) describing your results and what you can conclude from them (the summary must not be hand-written).

Submission

For submitting your lab assignment turn in the following hard copies:

Make sure you have documented the code as described in class.