# teaching machines

## CS 145 Lecture 3 – Algorithms

February 1, 2012 by . Filed under cs145, lectures, spring 2012.

### Agenda

• algorithm
• pseudocode
• program this
• an early algorithm, a la Euclid
• primitives have operators
• objects have methods
• API
• String methods

### Program this

You are indecisive. So are your friends. One quiet Monday night, you all decide one night to come up with a list of restaurants, each accompanied by a probability of how often you go there. The weekend comes. Where do you go?

### Code

#### CellBill.java

package preexam1;

import java.util.Scanner;

public class CellBill {
public static void main(String[] args) {
// figure out how many people there are
// find out bill 1, add to running sum
// find out bill 2, add to running sum
// find out bill ..., add
// find out bill 8
// divide total by number of people
// print result

final int nPeople = 8;
Scanner in = new Scanner(System.in);

double runningSum = 0.0;

runningSum = runningSum + in.nextDouble();
runningSum = runningSum + in.nextDouble();
runningSum = runningSum + in.nextDouble();
runningSum = runningSum + in.nextDouble();
runningSum = runningSum + in.nextDouble();
runningSum = runningSum + in.nextDouble();
runningSum = runningSum + in.nextDouble();
runningSum = runningSum + in.nextDouble();

double average = runningSum / nPeople;
System.out.println("Average: " + average);
}
}


#### MaxSiblings.java

package preexam1;

import java.util.Scanner;

public class MaxSiblings {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);

int maxSiblings = 0;

maxSiblings = Math.max(maxSiblings, in.nextInt());
maxSiblings = Math.max(maxSiblings, in.nextInt());
maxSiblings = Math.max(maxSiblings, in.nextInt());
maxSiblings = Math.max(maxSiblings, in.nextInt());

maxSiblings = Math.max(maxSiblings, in.nextInt());
maxSiblings = Math.max(maxSiblings, in.nextInt());
maxSiblings = Math.max(maxSiblings, in.nextInt());
maxSiblings = Math.max(maxSiblings, in.nextInt());

System.out.println("Max: " + maxSiblings);
}
}


#### StringMethods.java

package preexam1;

public class StringMethods {
public static void main(String[] args) {
String email = "johnchamp@uwec.edu";

System.out.println(email.length());
System.out.println(email.charAt(0));
}
}


### Haiku

Algorithms sell.
5 easy steps to fame, wealth!
Step 1: Write self-help.