CS99

Fundamental Programming Concepts
Summer 2001

Lab 4

 

Overview

The goals of this lab are to

  • Correct a mistyped program and ensure that the program is logically correct

  •  Modify a simple if-else program to do different tasks.

Part I: Find The Errors
Create a new project called Lab4.  Download TokenReader.java from our Web site and add it to your project.  Type the following code exactly as it's written into a new Text file called Lab4a.java:

\**
* CS99 Lab 4a
* Author: your name here
 * NetID: your NetID here
 * Date: today's date
*
* Lab4a solicits a temperature (in degrees Farenheit) from the user and
* converts it degrees Celsius.
*/
PUBLIC CLASS Lab4a (
    PUBLIC void mangy ( string args ) {

TokenReader in = new TokenReader( System.in );    

System.Out.PrintToScreen("What kind of pizza would you like? "];

double farenheit = in.readInt();

*/ Convert temperature /*

int celsius = 5*fahrenheit/11 + 37;

system.out.writeOnScreen( farenheit + 'F = ' + celsius + 'C' );

}

)

Part A:
Add Lab4a.java to your project.  Change the Java target to Lab4a.  The program will not compile because it has several errors.  Modify it so that it will compile and run without errors.

Part B:
While the Java compiler may be satisfied that your program is syntactically correct at this point, it may still be the case that the program is not logically correct.  For this next part, you must change the program so that it is correct in both ways.

  Look at the header comment for the program. What is its stated purpose?  Should the program be asking the user about pizzas? What should it be asking instead?  Is the formula correct?   What data can you use to test to see if it is? If you don't know the conversion formula for Farenheit to Celsius, you can determine it by using two known temperature equivalences (0C = 32F, 100C = 212F) and doing some simple algebra (the relationship is linear). If that's too complicated, try searching on the World Wide Web using google.com.  Enter in the search box: Converting Farenheit Celsius 

  When you're satisfied that the program is completely correct, save it and print a copy.
 

Part II: A Simple if-else program

In the bonus part of the last lab we introduced the % operator (modulo).  It returns the remainder of its two operands when the second divides the first.  So 5%2 returns 1, since 5 divided by 2 has a remainder of 1.  We're going to use this operator and the if/else construct to make our first program that makes a decision. For reading, see pp. 128-133 in Savitch.  Create a new Text file called Lab4b.java and add it to your Lab4 project.  Type the following code exactly into it:

 

/**
* CS99 Lab 4b
* Author: your name here
* NetID: your NetID here
* Date: today's date
*
* Input an integer and output "even" if it is even, otherwise output odd."
*/
public class Lab4b
{
    public static void main( String[] args )
    {

TokenReader in = new TokenReader( System.in );

System.out.print( "Enter a non-negative number: " );

int num = in.readInt();  

if ( num%2 == 0 )

System.out.println( "Even" );

else

System.out.println( "Odd" );
}

}

Run the program to see what it does.  Do you see how the boolean expression num%2 == 0 works?  It returns true if num has no remainder, and false otherwise. The expression inside the parentheses in the if-else construct must be boolean, that is, true or false.  If the expression is true, the statement after the if is executed; if it's false, the statement after the else is executed.

Part A:
Modify the program so that it reads in two integers and determines and prints if the first is a multiple of the second. (Hint: use the % operator)  

Part B:
Modify the program in part A so that it determines and prints the largest of two numbers entered.
(Hint: this time, you don't want to use the % operator, but one of the others specified on p.133 in Savitch.)

Bonus:
Does your program in Part B correctly handle the case when both numbers are equal?  Modify the program in part B so that it prints if the numbers are equal as well.

 

Part III: Submit Your Lab

Hand in printouts of the following:

  • Lab4a.java (any version)

  • Lab4a.java (Part A)

  • Lab4b.java (Part B)

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.