# teaching machines

## CS 145 Lecture 6

### Topics Covered

• what does this do?
• facing off the computer in guess-a-number
• generating a random spelunking workout
• an acrostic editor
• calculating average cell phone payment

### What does this do?

1. public static String ?(??) {
return text + " :)";
}
2. public static ? ??(String text) {
return text.length() == 0;
}
3. public static ? ??(String text) {
int index = text.indexOf('.');
return text.substring(0, index + 1);
}

### Code

#### GeometryFun.java

package lecture;

import java.util.Scanner;

public class GeometryFun {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double value = in.nextDouble();
double area = getAreaOfCircle(value);
System.out.println("Area: " + area);

//    getAreaOfCircle("t");

System.out.println(getAreaOfAnnulus(6, 8));
}

public static double getAreaOfCircle(double radius) {
//    radius = 6.0; // this has absolutely no effect on anything at all
return area;
}

// meaningful name?
// return type?
// outside information?

double totalArea = outsideArea - insideArea;
}

public static double f(double t) {
return 1 / (1 + Math.sqrt(t));
}
//
public static double getEdgeOfCube(double diagonalLength) {
// find diagonal of bottom face
double edgeLength = diagonalLength / Math.sqrt(3);
return edgeLength;
}

}


#### GuessANumber.java

package lecture;

import java.util.Random;
import java.util.Scanner;

public class GuessANumber {
public static void main(String[] args) {
// think of a number

// computer generate a #
// see if #s same
// respond to correctness

// computer generate a #
// see if #s same
// respond to correctness

Random generator = new Random();
boolean isSame = false;

Scanner in = new Scanner(System.in);
System.out.print("Your number in [0, 5]: ");

while (isSame == false) {
int guess = generator.nextInt(6); // number between 0 and 5
System.out.println("The computer's answer (" + guess + ") is right? " + isSame);
}
}
}


#### SpelunkingFitnessDisplay.java

package lecture;

import java.util.Random;

public class SpelunkingFitnessDisplay {

public static void main(String[] args) {

printLine();
}

public static void printLine() {

Random generator = new Random();
int nDots = generator.nextInt(11);
System.out.println(nDots);

while (nDots > 0) {
System.out.print("*");
nDots = nDots - 1;
}
}
}


### TODO

• Read sections 3.1 through 3.3.

### Haiku

still my loop goes on
why? some never even start