JamesMadisonUniversity

Computer Science Department


CS 139 Lab:Some basic looping problems


Objectives:

  • Introduce students to using loop structures in programs.

Background:

Many computing problems and in fact one of the biggest advantages of using a computer is with the capability of repeating calculations very quickly. This lab will let you explore loop structures to solve various problems.

New Terms:

  • loop
  • loop control variable
  • off by one error
  • infinite loop

Materials:

You will write all classes in the Loop solution class from scratch or using the template program.

You will download the LoopDriver (Driver) program to use in testing your application.

Acknowledgment Lab by Nancy Harris

1 General Instructions:

Set up your programming environment for this lab. You should create a folder into which you will store both programs.

Edit a new java class called Loops.java. You do not need to build a constructor, nor will you need any global variables (attributes). Each method will be a value returning method. This class will not have a main method.

You MUST to set up your stubs first, then fill in the actual code. Recall that a stub is a compilable function that does not have any operable code yet. The driver program should work with stubs (without correct results) or your finished code. Test with the driver after you finish each method.

For each of the following problems, be sure to think through how you will do the problem on paper. Then decide which kind of loop will best solve that problem. Figure out your starting point (initialization), your decision (which will also be the ending point) and your update statement. Finally, what will the body of the loop do?

As you are testing, test under a variety of conditions. Change the constants in the driver to test the different conditions.

2 Multiplication row

Your first function, multiply, should accept an integer as a parameter and then print a list of the multiples of that number from 1 - 12 (one per line). For example, if you enter 3, the program should print the result of 3 * 1, 3 * 2, ... 3 * 12 and stop. This function does not return a value.

3 Factorial

n! is defined as the product of a series from n descending to 1. For example, 3! is 3 * 2 * 1 or 6. A special case is 0, where 0! is 1.

Your job is to create a function, factorial, that takes in one integer value and returns that value factorial. Your function should work for any positive value.

4 Sum a Range of Numbers

Write a function called sumRange that accepts two integer parameters. If the first is less than or equal to the second, return the sum of all of the numbers from the low number to the high number. If the second is less than the first, display the lowest integer value (recall the Integer class constants).

5 Multiplication table

Write a method called, multTable, which accepts an integer as a parameter and then prints a multiplication table from 1 to the number entered. For example, if you entered the number 5, you would print 1*1, 1*2, ... 1*5 on one line (separated by tabs), go to the next line and print 2*1, 2*2, ... 2*5 . You would have a total of 5 such lines in your output.

6 Another series - Optional

Write a function called sumFraction that accepts a positive integer as a parameter. The function returns a value which represents the sum of the fractions in the following example. (See book page 232, problem 5 for another example). If the parameter was 5, the return value would be 1/5 + 2/4 + 3/3 + 4/2 + 5/1. Note: you should carry out the division as double division, but the numbers used are integers.  What type of value should be returned?

 


Updated 10/24/2011 (nlh)