Lab 5 In-Class: Exploring Data Representation & Conditional (if) Statements

Lab Objectives

  • Experience the problem of overflow; gain an understanding of why it happens and how to recognize it.
  • Gain more experience with casting and widening data conversions.
  • Gain experience in using if statements.

Log onto the Linux system and create a lab5 subdirectory of your cs120/labs directory for today's work. As usual, you will need to have three windows open: an xterm, Emacs, and Firefox.

What is your age in seconds?

The file Age.java contains the skeleton of a Java application that will take as input a person's age in years, months, and days and then compute and print the age in seconds.
  1. Complete the program as follows:

    Test your program. For example, a person who is 18 years, 3 months, and 21 days old has lived 577,238,400 seconds (under the assumptions of the program). A person who is 21 years, 0 months, and 0 days has lived 662,256,000 seconds.

  2. How many seconds has a person who is 68 years, 6 months, and 12 days old lived? Run your program to find out.

    You should have gotten a strange answer to the last question! Why did your program produce such an answer? Hint: Java uses a 32-bit two's complement representation for type int. So do the following to see what's going on:

  3. The above answer was clearly wrong. However, the answer can be wrong without it being so obvious. Run the program again with an age of 1000 years, 1 month, and 1 day. Why is this wrong?

  4. Fix your program as follows:

    Test the program again and make sure it works. A person who has lived 68 years, 6 months, and 12 days is 2,161,036,800 seconds old. Also try 99 years, 9 months, and 9 days and make sure the answer makes sense.

 

A Base Conversion Program

In class we learned an algorithm for converting a base 10 number to another base by repeatedly dividing by the base. Each time a division is performed the remainder and quotient are saved. At each step, the number used in the division is the quotient from the preceding step. The algorithm stops when the quotient is 0. For example to convert the base 10 number 1878 to base 8 you would do the following:
                            Quotient      Remainder
    1878 divided by 8 -->     234             6
     234 divided by 8 -->      29             2
      29 divided by 8 -->       3             5
       3 divided by 8 -->       0             3

The number in the new base is the sequence of remainders in reverse order (the last one computed goes first; the first one goes last). In this example, the base 8 answer is 3526 (that is 187810 = 35268). In this exercise you will use this algorithm to write a program that converts a base 10 number to a 4-digit number in another base (you don't know enough programming yet to be able to convert any size number). The base 10 number and the new base (2 - 9) will be input to the program. The start of the program is in the file BaseConvert.java. Open the file in Firefox, save it to your lab5 subdirectory, then open it in Emacs. Modify the program one step at a time as follows:
  1. The program will only work correctly for base 10 numbers that fit in 4 digits in the new base. We know that in base 2 the maximum unsigned integer that will fit in 4 bits is 11112 which equals 15 in base 10 (or 24 - 1). In base 8, the maximum number is 77778 which equals 4095 in base 10 (or 84 - 1). In general, the maximum unsigned base 10 number that fits in 4 base b digits is b4 - 1. Add an assignment statement to the program to compute this value for the base that is input and assign it to the variable maxNumber. Add a statement that prints out the result (appropriately labeled). Run the program to make sure it is correct so far.

  2. Now it is time to add the code to do the conversion. The comments in the program guide you through the calculations -- place the appropriate Java statements after each comment. Note that after each digit is calculated you concatentate it to the variable baseBNum. This variable is a String variable that is initially an empty string (see the instantiation already in the code). You are "building" the string up one digit at a time. Remember that the digits need to go in the reverse order they are calculated (the first calculated is at the end, the last is at the beginning). There is already a statement to print the answer (the baseBNum variable).

  3. Run your program. Test it using the following values: Enter 2 for the base and 13 for the base 10 number -- the program should print 1101 as the base 2 value; enter 8 for the base and 1878 for the number -- the program should print 3526 for the base 8 value; enter 3 for the base and 50 for the number -- the program should print 1212.

 

Adding ifs to the Base Conversion Program

Go back to your base conversion program.
  1. Remember that this program only computes 4 digits in the new base so if the user enters a number that is too large to fit in 4 digits the answer produced by the program is incorrect (it is incomplete -- it only gives the rightmost 4 digits). Unfortunately a lot of people believe the computer so we need to be sure the computer doesn't print out incorrect information. So do the following:
    1. Replace the statement that prints the answer with an if ... else ... If the number is too large to fit in 4 digits in the new base print a message saying so (remember you already calculated the maximum number that will fit); otherwise, print the answer.
    2. To test the program, use the following input: 4 for the base and 375 for the base 10 number (the program should say 375 is too big to fit in a 4-digit base 4 number); 7 for the base and 5341 for the number (again, too big); 7 for the base and 537 for the base 10 number (the program should print 1365).

  2. Currently the program only works for bases 2 - 9. Our next goal is to expand it to work for bases 2 - 16. The main problem is that we must convert the remainders larger than 9 to the appropriate letter (10 to "A", 11 to "B", ...). Of course the letter must be represented as a char or a String. Modify the program as follows:
    1. After computing the remainder for the units place, we need to replace the statement that concatenates the remainder to the baseBNum variable with an if ... else ... to concatentate the appropriate string representation for the remainder. Complete the following if ... else ... . The first blank should be the condition for concatenating the remainder as it is (that is the integer remainder is the character added to the string) Also include the final else to indicate an error. (NOTE: The comparison operator for equality is ==)
      
      
               if ( _______________________________________________ )
           
      
                   // Concatenate the remainder with baseBNum as before
      
                   _______________________________________________________
      
               else if (remainder == 10)
      
                   // Concatenate "A" with baseBNum
      
                   ______________________________________________________
      
               else if .....
      
                   ....
      
               else 
      
                 baseBNum = "Error";
      
      
    2. Make the appropriate modifications to find the other place values (you can do some copying of code!).
    3. Run the program to make sure it works. Test the numbers you have already tried plus try 16 for the base and 15101 for the number. You should get 3AFD for the answer.

  3. Modify the original prompt so it asks for a base in the range 2 - 16.

  4. Modify the first if you added (the one that checks to see if the base 10 number fits in 4 digits in the new base before printing the answer) so that it first checks to make sure the base is in the correct range. The if should do the following (you must supply the correct conditions and code):
            if the base is NOT in the range 2 - 16
              print an error message telling the user the base is incorrect
            else if the base 10 number doesn't fit in 4 digits
              print an error message telling the user
            else
              print the number in the new base
    
  5. Test your program!! Try values such as 18, 1, 17, -2 for the base.

Final Exercise: Activities at Lake Lazydays

As activity directory at Lake Lazydays Resort, it is your job to suggest appropriate activities to guests based on the weather. You recommend that when the temperature is greater than 95 or less than 20 people should visit the resort shops. Otherwise you recommend the following activities:

  temp >= 80:         swimming
  60 <= temp < 80:    tennis
  40 <= temp < 60:    golf   
  20 <= temp < 40:    skiing 

The file LazyDays.java contains the outline of a program that prints recommended acativities based on a user entered temperature. The conditionals of the cascading if statements need to be filled in according to the above table. Thoroughly test your program.

Submission