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

## Lab 4: Loops## Goals and ObjectivesThe 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. ## TaskYou will create a simple class which contains methods that compute some simple computations.
## BackgroundIteration 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
## IntroductionsIntroduce yourself to your lab partner(s). Tell your lab partner(s) about your favorite things to do in Albion. ## Download the labDownload 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
Use a 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. - 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 - 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! - Add a method that takes a single int parameter (say N) that computes 2
^{n}using a single for loop and returns the result. Recall that 2^{0}=1 and 2^{n}= (2 * 2 * 2 * ... * 2) where there are n 2s in the expression.2 x 2 x 2 x . . . x 2 x . . . x 2 = 2 ^{n}n 2's - 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.
1 ^{2}+ 2 ^{2}+ 3 ^{2}+ . . . + i ^{2}+ . . . + n ^{2}= sum - 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 - 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 2
^{n}-1. How does this value compare to the value of 2^{n+1}-1?2 ^{0}+ 2 ^{1}+ 2 ^{2}+ 2 ^{3}+ . . . + 2 ^{i}+ . . . + 2 ^{n}= sum - Compute Pi using the expression
Pi = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + ... - Write a function called term that takes a double parameter x and an int parameter k and returns a double that represents
(x ^{k})/k! = (x/1)(x/2)(x/3)...(x/k). - Another definition of the sine function is given by the following infinite sum of terms:
sin(x) = x - x ^{3}/3! + x^{5}/5! - x^{7}/7! + ... - Another definition of the cosine function is given by the following infinite sum of terms:
cos(x) = 1 - x ^{2}/2! + x^{4}/4! - x^{6}/6! + ... - Add to the above program to compute the nth Fibonacci number.
Fibonacci numbers, F
_{i}are defined in the following way. F_{1}= 1, F_{2}= 1, and F_{i}= F_{i-1}+F_{i-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.F _{1}= 1 F _{2}= 1 F _{3}= 2 = 1 + 1 F _{4}= 3 = 2 + 1 F _{5}= 5 = 3 + 2 F _{6}= 8 = 5 + 3 F _{7}= 13 = 8 + 5 . . . F _{i}= F _{i-1}+ F _{i-2}. . . - 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 StepsWhen done, email you lab partner and instructor your lab (.java file). Copyright © 2017, David A. Reimann. All rights reserved. |