CPTR 124 Fundamentals of Programming


This lab examines Java application I/O, variables, and arithmetic.
  1. Forming a Team

    You are strongly encouraged to choose a partner from this class as a teammate. You two will work together on this assignment.

    You are not required to work with someone else; you may work alone if you insist.

  2. Familiarize yourself with the assignment

    Read over the complete lab before you begin creating your project and Java source files. Be sure you can work out the solutions with pencil and paper before you start coding. If you cannot do the arithmetic by hand, you have little hope of writing a computer program to do so.

    A good approach is to work out by yourself the mathematical stategy to solve each problem. Next, see how your partner solved each of the problems. Agree on the best strategy for each and then work together on implementing that strategy in Java. If you both feel confident in your Java programming skills, you could work separately coding each program and then get together to build a final version incorporating the best ideas from both.

  3. Ground rules

    You may use any of the techniques described in the textbook in Chapters 2 and 3. Specifically, you may not use conditional statements in any of these programs. The purpose of this lab is to better understand Java's mathematical operators. All the requirements of this lab can be met by the clever application of these mathematical operators.

  4. Housekeeping

    Create a project named Lab2. Within this new project create five classes (that is, five Java programs), one for each problem to solve. Use the following names for your classes:

    • HoursMinutesSecondsToSeconds
    • SecondsToHoursMinutesSeconds
    • Distance
    • Money
    • ExerciseComputer

    Each class should be created with a main method as you did in the previous lab.

    You probably should not create all the classes at once. Create one class, and when you have finished working on it create the next one. It's OK to not finish one and go on to another, especially if you're getting frustrated with one of them. Just create another class to work on and come back to the unfinished one later.

    You can create the classes in any order you wish, just so you eventually complete them all.

    Use a Scanner object when user input is required. Look in your textbook or review the programs we wrote in class to see how to use Scanner objects. Use nextInt() to get integer values and nextDouble() to get double-precision floating point values.

    Choose your variable names thoughtfully. The names you choose should reflect their purpose within the program. For example, don't use the name x (a popular variable name in algebra class) when the name seconds would better represent the purpose of a variable participating in a time calculation.

  5. What to do

    Write Java programs to solve each of the following problems:

    1. HoursMinutesSecondsToSeconds

      Receive three integer values from the user. These three integer values represent respectively hours, minutes, and seconds. The program should display the number of seconds represented by this time value. For example, if the user enters
      3 12 50
      

      your program should display
      11570
      

      Use only integer variables to solve this problem.

    2. SecondsToHoursMinutesSeconds

      Next, do the reverse of the above problem. Request an integer value from the user. This integer represents some number of seconds. The program should display the number of hours, minutes, and seconds that corresponds to the number of seconds the user enters. For example, if the user enters
      11570 
      

      your program should display
      3 hrs 12 min 50 sec  
      

      Again, use only integer (int) variables to solve this problem. Hint: A good way to solve this problem is to use the division (/) and modulus (%) operators in succession.

    3. Distance

      In mathematics, the distance between two points (x1,y1) and (x2, y2), is given by the formula

      Distance formula

      You should use four double precision floating point variables (doubles) to represent the coordinates. The user should type in four numbers: x1, y1, x2, y2, in that order. The distance between the two points should be printed. The Math.sqrt() method can be used to compute the square root; for example, the following statement will assign 5 to d:

      		d = Math.sqrt(25);
      		

      Exponentiation (raising to a power) can be done with another Math method, but if the exponent is a small integer like 2 in our case, it is just as easy to multiply the expression by itself.

    4. Money

      Next, request four integers from the user representing the number of quarters, dimes, nickels, and pennies. The program should display the number of dollars in the normal way currency is printed. For example, if the user enters
      3 12 5 36
      

      the program would display

      $2.56
      

      Use only integer variables and calulations to solve this problem. This means you should not use numeric literals like 0.25 within your calculations.

      Be careful! If the user enters
      0 0 1 0
      

      the program should display

      $0.05
      

      not

      $0.5
      

      Note the zero in the correct result. Hint: Your strategy here can be similar to the SecondsToHoursMinutesSeconds program. Once you have figured out the total number of cents, determine how that amount can be expressed in hundreds of cents (dollars), tens of cents (remaining dimes), and units of cents (remaining pennies). Print these computed values separately, with the dollar sign ($) and decimal point (.) appropriately placed.

    5. ExerciseComputer

      The following table lists the Calorie contents of several foods:

      FoodCalories
      Bean burrito357
      Salad w/dressing185
      Milkshake388

      Also, running or walking burns off about 100 Calories per mile. The program should request three values from the user: the number of bean burritos, salads, and shakes consumed (in that order). The program should then display the number of miles that must be run or walked to burn off the Calories represented in that food. The program should run as follows (the user types in the 3 2 1):

      How many bean burritos, bowls of salad, and milkshakes did you consume?   3 2 1
      
      You ingested 1829 Calories
      You will have to run 18.29 miles to expend that much energy
      

  6. Check out

    I will review your lab with you before you leave. You will receive a Lab Report that you must complete and return to me by the next lab. Each team should submit one Lab Report.

  7. Finish up

    Don't forget to close your project, exit Eclipse, and properly log out from your workstation.