teaching machines

CS 145 Lecture 18 – Hello, While

October 13, 2014 by . Filed under cs145, fall 2014, lectures.

Agenda

Mercy Fortnight

If you did not get credit for homeworks 1, 2, or 3, you have an opportunity to rectify this during the next two weeks. I will regrade a homework submission under the following conditions:

  1. You correctly solve one of the problems below from Project Euler.
  2. You fix your homework submission, commit, and push.
  3. You email me one message per regrade request with the following: the number of the homework you have resubmitted, your code that solves the Project Euler problem, and a screenshot of the Project Euler confirmation page.
  4. You do all this before October 24.

Choose from among these three problems:

Program This

Write a method times that accepts a character and a count. Return a String containing a sequence of the character repeated count times.

Code

Flashcards.java

package lecture1013;

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

public class Flashcards {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    Random g = new Random();
    int nCorrect = 0;
//    boolean isPerfectSoFar = true;

    while (nCorrect < 10) {
      int a = g.nextInt(10);
      int b = g.nextInt(10);
      System.out.print(a + " * " + b + " = ");
      int guess = in.nextInt();
      if (a * b == guess) {
        ++nCorrect;
//      } else {
        // you are wrong if we get here
//        isPerfectSoFar = false;
      }
    }

    System.out.println(nCorrect);
  }
}

Acrostic.java

package lecture1013;

import java.util.Scanner;

public class Acrostic {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String theme = in.nextLine();
    
    int i = 0;
    while (i < theme.length()) {
      // show character i
      // prompt for word
      char c = theme.charAt(i);
      System.out.print(c);
      in.nextLine();
      ++i;
    }
  }
}

Haiku

Prince Charming’s routine
while (!footFits) find next foot
He found his false love