CS99 | 
 Fundamental Programming Concepts
 
Summer 2001  | 
| 
 | 
|
Lab 3: Solutions
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 : ");        
        double firstVal = in.readDouble();
       
        System.out.print("Enter a second
integer value : ");        
        double secondVal = in.readDouble();
       
        System.out.print("The sum of the
values is " + ( firstVal + secondVal) );
       
        System.out.println("; their
difference is " + ( firstVal - secondVal) + ";" );
       
        System.out.print("their product,
" + ( firstVal*secondVal) );        
        System.out.println("; and their
quotient, " + ( firstVal/secondVal) + "." );    
    } 
}
Lab3.TXT
Part A:
Enter an integer value : 31
Enter a second integer value : 12
The sum of the values is 43; their difference is 19;
their product, 372; and their quotient, 2.
Why does the quotient return 0 if firstVal is 7, and secondVal is 8? 
The quotient is zero because we are performing integer division, and the
return value must be an integer.  Since 7/8 is not an integer and is less
than 1, Java returns the next smallest integer, which is 0.
What does it return if firstVal is 8 and secondVal is 7? 
In this case, the next smallest integer is 1, because 8/7 is just a little
greater than 1.
Part B:
Enter a double value : 2.71828
Enter a second double value : 3.14159
The sum of the values is 5.85987; their difference is -0.42330999999999985;
their product, 8.539721265199999; and their quotient, 0.8652561282662601.
Part C:
The program does not compile because the computer does not know how to take
the difference, product, and quotient of Strings.
Error   : Incompatible type for -. Can't convert
java.lang.String to int.
Lab3.java line 17     System.out.println("; their
difference is " + ( firstVal - secondVal) + ";" );
Note: Depending on how you wrote your program, you may have gotten a different
error.
Part D:
Enter a String value : Banana
Enter a second String value :  Bread
The sum of the Strings is BananaBread.
class TaxiFare {
   public static void main( String[] args ) {
       TokenReader in = new TokenReader( System.in
);
       System.out.print("Enter a distance (in
miles): " );
       double distance = in.readDouble();
       double fare = 5 + 2*(Math.ceil ( 8*distance
) - 1);
       System.out.println("The taxi fare is
" + fare + " dollars." );
   }
 
}
class TimeConv {
   public static void main( String[] args ) {
       TokenReader in = new TokenReader( System.in
);
       System.out.print("Enter a time (in
seconds): " );
       int time = in.readInt();
       int hours = time/3600;
       int minutes = (time%3600)/60;
       int seconds = (time%3600)%60;
       System.out.println(time + " seconds
equals " + hours + " hours, " + minutes +
           " minutes, and
" + seconds + " seconds." );
   }
 
}