Lab 04 Solution
Version 0

 


Problem 01

 

For the following sub-problems, start with a program that solves the first problem, then expand it to solve each successive problem. Make the program working for one problem before going on to the next.

  1. Ask the user for an integer and tell them whether it is negative or non-negative.

  2. Tell them, in addition, whether the number is odd or even.

  3. Ask the user for a second integer and tell them whether it is a multiple of the first, and, if it is, how many times the first it is.

  4. Tell the user which number is larger or if they are equal.

  5. Ask the user for a third integer and tell them whether the three numbers form a pythagorean triple (that is, if the sum of the squares of the first two equals the square of the third).

Click Here To Run This Program On Its Own Page
// Program: LAB04PR01
// Author:  Joshua S. Hodas
// Date:    September 27, 1998
// Purpose: Determines some things about numbers you enter
 
import HMC.HMCSupport;
 
class LAB04PR01 {
    
    public static void main(String args[]) {
 
        int n1, n2, n3;
 
        HMCSupport.out.print("This program tells you some things"
                             + " about numbers you enter.");
        HMCSupport.out.println();
 

// Problem 1a HMCSupport.out.print("Please enter an integer: "); n1 = HMCSupport.in.nextInt(); if (n1 < 0 ) { HMCSupport.out.println("The number " + n1 + " is negative."); } else { HMCSupport.out.println("The number " + n1 + " is not negative."); }

// Problem 1b // A number is even if it's remainder, when divided by 2 // is zero (that is, if modulo 2 it is congruent to 0). if ( (n1 % 2) == 0 ) { HMCSupport.out.println("The number " + n1 + " is even."); } else { HMCSupport.out.println("The number " + n1 + " is odd."); }

// Problem 1c HMCSupport.out.print("Please enter another integer: "); n2 = HMCSupport.in.nextInt(); // A number is divisible by another if it's remainder, // when divided by the other is zero, (that is, if modulo the other // number it is congruent to 0). if ( (n1 % n2) == 0 ) { HMCSupport.out.println("The number " + n1 + " is " + (n1/n2) + " times " + n2); } else { HMCSupport.out.println("The number " + n1 + " is not divisible by " + n2); }

// Problem 1d // We could partially embed this in the last problem (since // a multiple of a number is certainly larger than the // number (if they are both positive). Here we just do them // separately. // Notice that, below, we could put the line: // // HMCSupport.out.print("The number " + n1 + // // which is shared by all the three cases below before // the if, and then just put a second println inside each case // telling the appropriate property. But this is clearer, // if less optimally efficient. if ( n1 > n2 ) { HMCSupport.out.println("The number " + n1 + " is larger than " + n2); } else if ( n1 == n2 ) { HMCSupport.out.println("The number " + n1 + " is equal to " + n2); } else { HMCSupport.out.println("The number " + n1 + " is smaller than " + n2); }

// Problem 1e HMCSupport.out.print("Please enter another integer: "); n3 = HMCSupport.in.nextInt(); if ((n1*n1 + n2*n2) == (n3*n3)) { HMCSupport.out.println("The three numbers " + n1 + ", " + n2 + ", and " + n3 + " form a pythagorean triple."); } else {

HMCSupport.out.println("The three numbers " + n1 + ", " + n2 + ", and " + n3 + " do not form a " + "pythagorean triple."); } } }


Problem 02

  Ask the user for their annual income. Tell them they are poor, comfortable, or stinking rich depending on whether their income is less than $20,000, between $20,000 and $50,000, or greater than $50,000.

Click Here To Run This Program On Its Own Page
// Program: LAB04PR02
// Author:  Joshua S. Hodas
// Date:    October 6, 1996
// Purpose: Determines if someone is rich or poor
 
import HMC.HMCSupport;
 
class LAB04PR02 {
 
  public static void main(String args[]) {
 
    int income;
 
    HMCSupport.out.print("This program tells you if you're"
                         + " rich or poor.");
    HMCSupport.out.println();
 
    HMCSupport.out.print("Please enter your annual income: ");
    income = HMCSupport.in.nextInt();
 
    if ( income < 20000 ) {
 
      HMCSupport.out.println("I am sorry to say that you're poor!");
 
    }
    else if (income <= 50000) {
 
      HMCSupport.out.println("I'd say you are comfortable (not rich).");
 
    }
    else {
 
      HMCSupport.out.println("I would say you're rich!" +
                             " Could you spare some change for a " +
                             "RAM upgrade?");
 
    }
  }
 
}


Problem 03

  Write a program which asks the user to type a number from 1 to 5 and responds with the cardinal version of the number (i.e. 1st, 2nd, 3rd, 4th, 5th) by printing the number they entered and then using an if-else-if statement to print the appropriate suffix.

Notice that the last two cases use the same suffix. Try to capture that commonality in your program by not having separate cases for those two numbers.

Print "out of range" if they enter any value other than 1 through 5.

Click Here To Run This Program On Its Own Page
// Program: LAB04PR03
// Author:  Joshua S. Hodas
// Date:    September 27, 1998
// Purpose: Print cardinal version of a number, using if-else;
 
import HMC.HMCSupport;
 
class LAB04PR03 {
    
    public static void main(String args[]) {
 
        int n;
 
        HMCSupport.out.print("This program prints the cardinal "
                             + "version of a number you enter.");
        HMCSupport.out.println();
 
        HMCSupport.out.print("Please enter a natural number: ");
        n = HMCSupport.in.nextInt();
 
        // Check to see if the number is in range:
 
        if ((n >= 1) && (n <= 5)) {
            
            // Print the bulk of the response, except the suffix
            
            HMCSupport.out.print("The cardinalization of " +
                                 n + " is " + n );
            
            
            // Select the appropriate suffix and print it:
            
            if ( n == 1 ) {
 
                HMCSupport.out.println("st");
            }
            else if ( n == 2 ) {
 
                HMCSupport.out.println("nd");
            }
            else if ( n == 3 ) {
 
                HMCSupport.out.println("rd");
            }
            else if ( n == 4 || n == 5 ) {
 
                HMCSupport.out.println("th");
            }
        }
        else {     // The number is out of range...
 
            HMCSupport.out.println("That number is out of the " +
                                   "range I can hadle (1..5).");
        }
    }
    
}


Problem 04

  Write another program that does the same thing as the last one, but which uses a switch statement instead of an if statement.

Click Here To Run This Program On Its Own Page
// Program: LAB04PR04
// Author:  Joshua S. Hodas
// Date:    September 27, 1998
// Purpose: Print cardinal version of a number, using switch
 
import HMC.HMCSupport;
 
class LAB04PR04 {
    
    public static void main(String args[]) {
 
        int n;
 
        HMCSupport.out.print("This program prints the cardinal "
                             + "version of a number you enter.");
        HMCSupport.out.println();
 
        HMCSupport.out.print("Please enter a natural number: ");
        n = HMCSupport.in.nextInt();
 
        // Check to see if the number is in range:
        // If we were not printing the bulk of the message 
        // before looking at the number, we could use the default
        // of the switch to do this.
 
        if ((n >= 1) && (n <= 5)) {
            
            // Print the bulk of the response, except the suffix
            
            HMCSupport.out.print("The cardinalization of " +
                                 n + " is " + n );
            
            
            // Select the appropriate suffix and print it:
            
            switch (n) {
 
            case 1: 
                HMCSupport.out.println("st");
                break;
 
            case 2: 
                HMCSupport.out.println("nd");
                break;
 
            case 3: 
                HMCSupport.out.println("rd");
                break;
 
            case 4:
            case 5: 
                HMCSupport.out.println("th");
                break;
 
            }
        }
        else {     // The number is out of range...
            
            HMCSupport.out.println("That number is out of the " +
                                   "range I can hadle (1..5).");
        }
    }
    
}

Last modified August 28 for Fall 99 cs5 by fleck@cs.hmc.edu


This page copyright ©1998 by Joshua S. Hodas. It was built with Frontier on a Macintosh . Last rebuilt on Sun, Sep 27, 1998 at 2:14:01 PM.
http://www.cs.hmc.edu/~hodas/courses/cs5/week_04/labsolutions.html