CS 171 & 171L Introduction to Computer Science I & Lab Fall 2017 

Lab 4: Loops

Goals and Objectives

The main goal this lab is for you to get a more practice with Java's loop control structures, in particular, for loops and nested loops. You will work more with loops to gain a better appreciation for their power and capabilities.

Task

You will create a simple class which contains methods that compute some simple computations.

Background

Iteration is the ability to perform a computation many times. These computations can be nearly identical, often with a slight difference in the coding. Recognizing and using patterns is a key element in computer science. Identifying the patterns present in these computations will allow you to easily perform the required computations.

Loops are everywhere! Armed with the ability to use the selection and iteration statements, you can write almost any program.

Examples:

  • Tables of data
  • Missile trajectories - early use of computers
  • fetch-decode-execute cycle inside a CPU
  • Process control of an OS
  • Payroll Office
  • Computer Animation

Introductions

Introduce yourself to your lab partner(s). Tell your lab partner(s) about your favorite things to do in Albion.

Download the lab

Download the source file Lab4.java. It contains the stubs of several static methods. Save it directly to your folder with your other lab files and open it in jGRASP.

Getting Started

In this lab, you will practice writing loops. You should start off with s1 driving and s2 navigating. Complete the methods in Lab4.java. You should change driver/navigator roles after each method is completed.

Use a for loop to solve each of these problems. You may not use while statement. The main method in Lab4.java contains 3 test cases for each method you need to complete. For each method, you should verify some of the test cases and consider adding others you feel might help test your program.

There are several independent methods in this lab. These are listed in roughly easiest to hardest, but if you get stuck on a part, you should consider trying the next step. You will write static methods as follows.

  1. Write a method which has a single integer parameter (say n) and and returns an integer which the sum of the integers from 1 through n using a for loop. Outputs the result in a main method and verify it is correct by also displaying the value of n(n+1)/2. FYI, the sum of the integers from 1 to 100 is 5050.
    1 + 2 + 3 + . . . + i + . . . + n = sum
  2. Add a method that takes a single int parameter (say N) that computes the factorial of N using a single for loop and then returns the result. Recall that the factorial of an integer N (written N!) is given by the product of the first N integers (1 x 2 x 3 x ... x N). Demonstrate your method is correct by computing multiple values in you main method.
    1 x 2 x 3 x . . . x i x . . . x n = n!
  3. Add a method that takes a single int parameter (say N) that computes 2n using a single for loop and returns the result. Recall that 20=1 and 2n = (2 * 2 * 2 * ... * 2) where there are n 2s in the expression.
    2 x 2 x 2 x . . . x 2 x . . . x 2 = 2n
    n 2's  
  4. Add to the above program to compute and output the sum of the first n squares (1*1 + 2*2 + 3*3 + ... + n*n) using a single for loop.
    12 + 22 + 32 + . . . + i2 + . . . + n2 = sum
  5. Add to the above program to compute the sum of the first n odd numbers (1 + 3 + 5 + ... + 2*n-1). Hint: Consider the values of 2*i-1 for i=1, 2, and 3. Do this using a single for loop.
    1 + 3 + 5 + . . . + 2 i - 1 + . . . + 2 n - 1 = sum
  6. Add to the above program to compute the sum of the first n powers of 2. Use a single for loop and your previous method to compute 2n-1. How does this value compare to the value of 2n+1-1?
    20 + 21 + 22 + 23 + . . . + 2i + . . . + 2n = sum
  7. Compute Pi using the expression
    Pi = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + ...
  8. Write a function called term that takes a double parameter x and an int parameter k and returns a double that represents
    (xk)/k! = (x/1)(x/2)(x/3)...(x/k).
    Note that for large values of k, computing k! will result in overflow, so that computing individual terms is a better way to perform this computation. Remember that the computer is only a model of our number system!
  9. Another definition of the sine function is given by the following infinite sum of terms:
    sin(x) = x - x3/3! + x5/5! - x7/7! + ...
    Use as many terms as you can in your computation. Compute sin(x) in this way and compare it to the value of sin(x) returned from the system using Math.sin(x) for values of x between 0 and Pi. Use the term function in the previous step.
  10. Another definition of the cosine function is given by the following infinite sum of terms:
    cos(x) = 1 - x2/2! + x4/4! - x6/6! + ...
    Use as many terms as you can in your computation. Compute cos(x) in this way and compare it to the value of sin(x) returned from the system using Math.sin(x) for values of x between 0 and Pi. Use the term function in the previous step.
  11. Add to the above program to compute the nth Fibonacci number. Fibonacci numbers, Fi are defined in the following way. F1 = 1, F2 = 1, and Fi = Fi-1+Fi-2, when i > 2. The Fibonacci numbers are 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, and so on. Do this using a single for loop.
    F1=1 
    F2=1 
    F3=2=1+1
    F4=3=2+1
    F5=5=3+2
         
    F6=8=5+3
         
    F7=13=8+5
    . . .
    Fi=  Fi-1+Fi-2
    . . .
    This is trickier than the previous steps because you will need to remember not just the current sum, but also the previous sum. Hint: use additional variables.
  12. A zoologist orders a shipment of animals. The shipment contains spiders, beetles, mice, and canaries. The shipment contained a total of N legs. How many of each animal is present? There are many solutions, but a computer program can easily generate and count all solutions. Add to the above program code which determines and counts all integer solutions to the general problem of N legs by using the equation
    8s + 6b + 4m + 2c = N
    where s, b, m, and c are between 0 and N. You will need to nest four loops corresponding to s, b, m, and c. You will also need an if-else statement. Only output the total number of solutions and when finished. Note you may want to try small values of N.

Have Fun!

Final Steps

When done, email you lab partner and instructor your lab (.java file).


Copyright © 2017, David A. Reimann. All rights reserved.