Lab 07 Solution
Version 1

 

For each of the problems we will show just the method that solves the problem. At the end we give a program that tests all the individual methods.


Problem 01

  Write a method which prints the numbers from 1 to 50.

  // Problem 01
 
  public static void printOneToFifty() {
 
    for (int i = 1 ; i <= 50 ; i++) {
 
      HMCSupport.out.print(i + " ");
    }
 
    HMCSupport.out.println();
  }


Problem 02

  Write a method which takes an int as a parameter and prints the numbers from 1 to that value. Note this method does not get input from the user, it gets its value from the method which calls it.

  // Problem 02
 
  public static void printOneToN(int n) {
 
    for (int i = 1 ; i <= n ; i++) {
 
      HMCSupport.out.print(i + " ");
    }
 
    HMCSupport.out.println();
  }


Problem 03

  Write a method which takes an int as a parameter and returns the sum of the numbers from 1 to that value. Note that this method does not get input from the user. It also does not display anything for the user. It simply returns its result to the method which called it.

  // Problem 03
 
  public static int sumOneToN(int n) {
 
    int sum = 0;
 
    for (int i = 1 ; i <= n ; i++) {
 
      sum += i;
    }
 
    return sum;
  }


Problem 04

  Write a method which takes two int values as parameters and returns the larger of the two.

  // Problem 04
 
  // Note, the problem does not specify what to do if the values are the
  // same. This version does the most reasonable thing: returns that value.
 
  public static int Max(int n1, int n2) {
 
    int max;
 
    if (n1 <= n2) {
 
      max = n2;
    }
    else {
 
      max = n1;
    }
 
    return max;
  }


Problem 05

  Write a method which takes two int values as parameters and uses Euclid's method to compute and return their greatest common divisor.

  // Problem 05
 
  public static int gcd(int a, int b) {
 
    // Variant of Euclid's method for GCD: repeatedly replace the
    // larger of the two values with their difference
    // until they are the same. Will go to 1, if no GCD.
 
    while ( a != b ) {
 
      if ( a > b) {   // replace the larger number with
                      // the difference of the two numbers
        a = a - b;
                      // Note, since a and b are COPIES
      }               // of the actual values sent in,
      else {          // doesn't mess up the values in the
                      // method which called this one.
        b = b - a;
      }
    }
 
    return a;
  }
 
}


Unified Solution:

 

Click Here To Run This Program On Its Own Page
// Program: Methods Lab
// Author:  Joshua S. Hodas
// Date:    November 16, 1997
// Purpose: To solve the problems in lab 07 using methods
 
import HMC.HMCSupport;
 
class LAB07 {
 
  public static void main(String args[]) {
 
    int x, y;
 
    HMCSupport.out.print("Please enter two integers " +
                         "which will be used to test " +
                         "several methods: ");
    x = HMCSupport.in.nextInt();
    y = HMCSupport.in.nextInt();
 
    HMCSupport.out.println("The numbers from 1 to 50: ");
    printOneToFifty();
    HMCSupport.out.println();
 

HMCSupport.out.println("The numbers from 1 to " + x + ": "); printOneToN(x); HMCSupport.out.println();

HMCSupport.out.println("The sum of the numbers from 1 to " + x + ": " + sumOneToN(x)); HMCSupport.out.println();

HMCSupport.out.println("The larger of the numbers " + x + " and " + y + ": " + Max(x,y)); HMCSupport.out.println();

HMCSupport.out.println("The GCD of the numbers " + x + " and " + y + ": " + gcd(x,y)); HMCSupport.out.println(); }

// Problem 01 public static void printOneToFifty() { for (int i = 1 ; i <= 50 ; i++) { HMCSupport.out.print(i + " "); } HMCSupport.out.println(); } // Problem 02 public static void printOneToN(int n) { for (int i = 1 ; i <= n ; i++) { HMCSupport.out.print(i + " "); } HMCSupport.out.println(); } // Problem 03 public static int sumOneToN(int n) { int sum = 0; for (int i = 1 ; i <= n ; i++) { sum += i; } return sum; } // Problem 04 // Note, the problem does not specify what to do if the values are the // same. This version does the most reasonable thing: returns that value. public static int Max(int n1, int n2) { int max; if (n1 <= n2) { max = n2; } else { max = n1; } return max; } // Problem 05 public static int gcd(int a, int b) { // Euclid's method for GCD: repeatedly replace the // larger of the two values with their difference // until they are the same. Will go to 1, if no GCD. while ( a != b ) { if ( a > b) { // replace the larger number with // the difference of the two numbers a = a - b; // Note, since a and b are COPIES } // of the actual values sent in, else { // doesn't mess up the values in the // method which called this one. b = b - a; } } return a; } }

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 Thu, Nov 5, 1998 at 8:15:54 PM.
http://www.cs.hmc.edu/~hodas/courses/cs5/week_07/labsolutions.html