CS 145 Lecture 18 – Hello, While
Agenda
- what ?s
- mercy fortnight
- repeating yourself
- flashcards
- acrostic
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:
- You correctly solve one of the problems below from Project Euler.
- You fix your homework submission, commit, and push.
- 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.
- 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