CSCI 241 Labs: Lab 3

CSCI 241 Labs: Lab 3
Write, Test, Write Some More ...


There are 6 checkpoints () in this lab, including the cleanup checkpoint. You and your partner should work together using just one of your accounts. So that one partner doesn't dominate the computer, CHANGE WHO IS IN CHARGE OF TYPING AT THE COMPUTER AFTER EACH CHECKPOINT! If you need help with any exercise, raise your hand.

Getting Started

Copy the directory Lab03 from /home/student/Classes/Cs241/Labs to your account. If you have forgotten how to do this, refer to the steps in Lab01.

Change directories into Lab03 and start BlueJ by entering bluej&.

Translating Mathematical Formulas

When you studied geometry you learned how to calculate the area of different geometric shapes. Your instructor found several at this website:

Area formulas (found at: www.math.com/tables/geometry/areas.htm)

These are not written in Java (yet). It takes a little thought to "translate" them, since you can't just copy them directly. One can think of a mathematical formula as a simple algorithm. It contains all the information you need to write a program to calculate it.

Open the project called Calculations and open the editor for the AreaFormulas class. This program looks similar to one of the early versions of the ComputeArea class shown in your textbook. In the comments, it describes what kind of steps need to be coded. By the time you are finished, it should ask for the necessary input values, do the proper calculations for each type of geometric figure's area, and display the results.

Remember, if you need to take the square root of a number, such as 18.2, you have to write
double answer = Math.sqrt(18.2);

Once written, you will also need to test your program. Work with your partner to think of some sample values to try. If you wish to try them in a calculator, you can reach one from the left side of the upper panel on your lab account desktop. Choose Applications-Accessories-Calculator to use it.

Use the comments you see to guide you while you write the code. Write the related code directly below the comment that describes it.

1 Demonstrate your AreaFormulas program with your test data. Be prepared to answer the following questions:
  1. What kind of errors occurred as you were working? What did you do to fix them?
  2. Are there values that will not work in this program?
  3. How many variables did you need? Could this program have been written with a different number?
Close the AreaFormulas class.

Increment(++) and Decrement(--) Operators

A couple of operators you will probably use quite frequently (once we learn about loops) are the ++ (sometimes called "auto-increment") and -- (sometimes called "auto-decrement") operators. They can be quite tricky! The key to using them is to isolate the operator in a single statement. However, it is also important to realize what can happen when they are NOT used in isolation.

Still within the Calculations project, open the IncrDecr class in the editor. This class has some simple and some sample trick statements using ++ and --.

Compile and run the program as it looks now. What do you see printed? Is the effect of the pre- and post- versions the same or different? For this class, we are looking for side effects. Remember, a side effect occurs when more than one variable changes its value in a single assignment statement.

You must have noticed that the trick statements are only in comment form. To see what happens when they run, work with your partner to do the following:

  1. Declare and initialize all the necessary variables, as indicated in the comments.
  2. For each of the trick statements, add System.out.println() statements before and after the statement that print the values of ALL variables affected by the trick statement.
    For example: where you see the line
    int e = ++a;
    you should uncomment it, and print the value of a before and after the line, and print the value of e after the line, too.
  3. See if you can figure out why you got the answers that you did!

2 Demonstrate your IncrDecr program. Be prepared to answer the following questions:
  1. How did the variable e get its value? What were the side effects?
  2. How did the variable f get its value? What were the side effects?
  3. (Trickiest!!!) How did the variable g get its value? Describe the side effects.
Close the IncrDecr class, and the Calculations project.

Time Conversions

Remember to switch which partner is at the keyboard.

Open the project called ConvertFromSeconds. This project contains a class named SecondsConvert which is supposed to take a total number of seconds and break it into minutes and seconds. But - a lot of the code is missing!

Review the comments to see how the steps are organized. Work with your partner to fill in the missing code. Use Divide (/) and Modulus (%) appropriately as you separate the minutes and seconds. Think about 3 test cases you can try to check your work.

3 Show us that the SecondsConvert program works. Be prepared to answer the following questions:

  1. Why does it make sense to use modulus (%) to find the seconds?
  2. Why don't we get fractions of minutes when we do the minutes calculation?
  3. minutes, seconds and totalSeconds are all declared to be int. Why? Would the program give correct results if they were declared to be double?
  4. Demonstrate your 3 test cases and describe how you know the results are correct.

Converting to Total Seconds

Switch which partner is working at the keyboard before you continue.

Create a new class named CountSeconds. In this class, write a main method to work backwards from the previous program. In other words, you'll combine days, hours, minutes and seconds and sum up total seconds. Note: The conversion is going in the opposite direction of the previous program. You will need to ask the user for four different input numbers: days, hours, minutes and seconds. To make the task a bit more interesting, allow the user to enter seconds with a decimal point and display an answer that contains total seconds (including a decimal point).

4 Show us that your program runs. Be prepared to answer the following questions:

  1. Does your program care if minutes are larger than 60? If hours are larger than 24?
  2. How many digits maximum does your output display?

Program From Scratch

As usual, time to switch keyboard control with your partner for this exercise.

Create a new class named DivideMod which will do the following:

  1. Use standard input to ask the user for two integers: a number and a divisor.
  2. Calculate the integer result of the number divided by divisor.
  3. Calculate the integer result of the remainder when you divide the number by the divisor.
  4. Print both the original numbers and the 2 results to the console in this format using 2 lines with the given text (sample data shown below - try other numbers to see those results as well):
        Original number:    27    Divisor:            12
        number / divisor:    2    number mod divisor:  3
    

5 Show us that your program runs correctly, displaying the test values above plus 2 other tests. Be prepared to answer the following questions:

  1. Is modulus more useful for ints or doubles?
  2. What happens to the spacing when you use larger numbers? Why?
Close project ConvertFromSeconds.

After the Lab

Return to Firefox, log onto Canvas and get your next programming assignment, #3. Don't forget to log out of Canvas and close Firefox before you log out.

6 Show us that you have logged out, cleaned up, pushed in your chairs and turned off your monitor for this last checkpoint.

End of Lab