Lesson 13 – Common Loop Algorithms

Big Java 6

When you come in

  1. Connect to the Internet
  2. Log in to Piazza
  3. Start Bluej
  4. Navigate to laughton.com/obrien/sjsu/cs46a/CRT_lessons/ and open this lesson

Questions

Concept - Loop algorithms

There are certain sets of code containing loops that we use over and over (algorithms)

 

Concept - Finding sum and average

That is what you did last time with Lesson 12 Q6.

Activity - Find average

Record your participation in Piazza clicker question Lesson13 Q1

Write a program to get the height of students from the user as a double and find the average height of all the students in a group.

You can use input_practice.zip from last lesson to save time

  1. I got it, no real problem
  2. I got it, but it was hard
  3. I couldn't do it in the time allowed
  4. I didn't know where to start

 

Concept - Finding the maximum

Let's visually find the tallest in a group of students.

 

Activity - Find the shortest

Record your participation in Piazza clicker question Lesson13 Q2

On a piece of paper or in a text editor, write simple pseudocode to find the shortest student in a set of students. Just write the algorithm. Don't worry about input or output

 

Activity - Translate pseudocode to Java code

Record your participation in Piazza clicker question Lesson13 Q3

Using this pseudocode for finding the minimum, write a program to get a set of heights from the user. Terminate when a non-number is entered. Then print the minimum height (the shortest). You can use input_practice.zip.

Declare a variable to hold the minimum
Prompt the user to enter a height
minimum = nextDouble()
Prompt the user to enter a height or q to quit
While another double has been entered
  read a value for height
  if the height is less than the current minimum
      Set the value of minimum to  height
   Prompt again
Print the minimum
  1. I got it, no real problem
  2. I got it, but it was hard
  3. I couldn't do it in the time allowed
  4. I didn't know where to start

Concept - Problems with this implementation

 

Concept - Counting Matches

Let's visually count the number of students 5.5 feet tall or taller.

 

Activity - Count matches

Record your participation in Piazza clicker question Lesson13 Q4

Write the pseudocode of an algorithm to determine how many students are 5.5 feet tall or taller.

 

Activity - Count matches

Record your participation in Piazza clicker question Lesson13 Q5

Write a program to determine how many students are 5.5 feet tall or taller. Starter code is in count_matches_project. It does the input for you. You need to add the counting matches algorithm code.

  1. I got it, no real problem
  2. I got it, but it was hard
  3. I couldn't do it in the time allowed
  4. I didn't know where to start

 

Activity - Infinite loop

Record your participation in Piazza clicker question Lesson13 Q6

How would you write a loop that will not terminate. That means the condition will alwas be true.

Concept: finding the first match

Find the position of the first space in a string using a loop

Activity - Finding the first match

Record your participation in Piazza clicker question Lesson13 Q7

Write a program to ask the user for a string (use nextLine()). Then find and print the position of the first @. Here is another link to input_practice.zip We are practicing loops so use a loop.

  1. I got it, no real problem
  2. I got it, but it was hard
  3. I couldn't do it in the time allowed
  4. I didn't know where to start