CS99

Fundamental Programming Concepts
Summer 2001

Lab 3

 

Overview

The goals of this lab are to

  • Practice some simple input and output with TokenReader, and use some simple variable operations

  • Write a simple program to calculate a Taxi fare after being supplied with a distance from the user

  • (Bonus) Write a simple program to convert a time in seconds to hours, minutes and seconds.
     

Part I: Input & Output, Variables (Required)

Create a new project and call it Lab3.  You should know how to do this by now, but if you don't remember look at the instructions in the second lab.  Create a new text file called Lab3.java and add it to your project (Don't forget to change the Java Target to Lab3).  Now go to the Web site, click on Labs, and click on TokenReader.java.  Choose Save this file to disk, and save it in the same folder as your project.  Add TokenReader.java to the project.  You are now ready to read in input from the user.

Enter, compile, and run the following code in Lab3.java:
  
import java.io.*;
 
/**
 * CS99 Lab 3
 * Author: your name here
 * NetID: your NetID here
 * Date: today's date
 */

class Lab3 {
 
   public static void main( String[] args ) {
      TokenReader in = new TokenReader( System.in );
      System.out.print("Enter an integer value : ");
      int firstVal = in.readInt();
      System.out.print("The value is " + firstVal );
   }

}

Part A:
Modify the code so that it asks the user for a second integer value (introduce a new variable called secondVal) and prints their sum, difference, product, and dividend.  Your output should look like the following:

Enter an integer value: 7
Enter a second integer value: 8
The sum of the values is 15; their difference is -1; 
their product, 56; and their dividend, 0.

   Press Enter to continue

Why does the dividend return 0 if firstVal is 7, and secondVal is 8?  What does it return if firstVal is 8 and secondVal is 7?  Can you explain what's happening?  Copy the screen output for the values 31 and 12 for firstVal and secondVal respectively and save it in a file called Lab3.txt under part A).

Part B:
Modify the program again, so that instead of reading in integer values, the program reads in double values.  You only need to make minor changes to the program to do this.  Run the program.  Do you notice anything different about the output?  Copy the screen output for the values 2.71828 and 3.14159 for firstVal and secondVal and save that as well in file Lab3.txt under part B).

Part C:
Finally, modify the program so that instead of reading integer values it reads String values.  Does the program compile?  Copy the errors the program gives into Lab3.txt part C).  

Part D:
Fix the println statement so that the program *does* compile by deleting portions (hint: which three operations make no sense for Strings?).  When you've deleted enough so that the program compiles, run the program and copy the screen output for the values "Banana" and "Bread" for firstVal and secondVal respectively (you do not need to type the quotes at the command line!).  Copy the output into Lab3.txt part D).

Remember to put your name, ID, and date at the top of Lab3.txt.
 

Part II: Taxi Fare (Required)
A sign on a taxi reads "$5 for the first 1/8 mile or fraction thereof, and 2 dollars for each successive 1/8 mile or fraction thereof." Here is a small table that clarifies the method of charging:

Distance Charge
0.10 5
0.20 7
0.99 19
1.00 19


Write a Java program called TaxiFare.java that solicits the distance traveled in miles ( assumed to be a positive decimal ) and prints the charge. You should only need two variables, distance and fare.

Use the method Math.ceilMath.ceil( <numeric argument> ) returns the next integer larger than its argument.  So Math.ceil ( 5.1 ) returns 6; Math.ceil ( -1.8 ) returns -1.  Hint: think about the value of Math.ceil ( 8*distance ).

Test your program on the distances in the table, to see if you get the same charges.

A sample output from your program might be:

Please enter a distance (in miles):  0.1
The taxi fare is 5 dollars. 

  Press enter to continue

Part III: Time Conversion (Bonus)
Write a Java program called TimeConv.java that solicits an integer time period T in seconds and then prints its equivalent in units of hours, minutes, and seconds. For instance, if T = 10000, then T = 2*3600 + 46*60 + 40. Your program should display the fact that 10000 seconds equals 2 hours, 46 minutes, and 40 seconds.

To do this you will need to use the mathematical operations, % and /.

The % operator (see pp.70-71 in Savitch) is called the modulo operator.  It  returns the remainder when the first operand is divided by the second.  If x and y are int variables, and x = 25, y = 6, x%y returns 1. If x = 17, and y = 3, x%y returns 2.  Note that the modulo operator only works with bytes, shorts, ints, and longs; it does *not* work with doubles or floats.

The / operator is the division operator.   It works with both floating-point and integer variables. When used with integer variables, it returns the quotient when the first operand is divided by the second. So if x and y are int variables, and x = 25 and y = 6, x/y returns 4; if x = 17, and y = 3, x/y returns 5.

Consider now how we would use these operators to write our program.  

Your program only needs to use 4 int variables: time, hours, minutes, seconds, but you may introduce other variables if you think they may be needed.

If T = 5000 seconds, how many *hours* are there in that time period? Well, the number of hours is equal to the number of times 3600 goes into 5000 (because there are 3600 seconds in an hour).  That number is 5000/3600 or T/3600.  We can store that number in a variable called hours. So you should have the following snippet of code in your program:

int hours = time/3600;

If we take out the time for the hours, what are we left with?  The number of seconds for the remaining minutes and seconds.  To get the remaining seconds after the hours are taken out, we want the *remainder* from dividing T by 3600.  We know how to do that:  T%3600 returns the number of seconds left after the hours are removed.  

With this remaining time (and note that the time is in seconds), we need to find out how many minutes are in it.  Well, using the same ideas as before (only now using 60 instead of 3600) we can extract the number of minutes by using T%60, and the remaining number of seconds using T/60. Your code might have the following snippet of code

The output from the program, if time = 10000, should be exactly:

10000 seconds equals 2 hours, 46 minutes, and 40 seconds.

As a hint for printing this output, note that if m and n are int variables, with values 5 and 10 respectively, then the following println statement

System.out.println("The value of m is " + m + ", and the value of n is " + n + ". " );

prints on the screen exactly the following:

The value of m is 5, and the value of n is 10.

Part IV: Submit Your Lab

Hand in printouts of the following:

  • Lab3.java (any version)

  • Lab3.txt

  • TaxiFare.java

  • and if you do the bonus, TimeConv.java

Don't forget to put your name and net ID at the top of each file.

This lab is due at the beginning of lab on Thursday!  Hand it in to Siddarth.