CS 121 Intro to Programming:Java - Lecture 11

CS 121 – Intro to Programming:Java - Lecture 11 Announcements Ch 5 OWL assignment due Friday, 5 PM Programming assignment 4 due next Wednesday (group ...
5 downloads 1 Views 140KB Size
CS 121 – Intro to Programming:Java - Lecture 11 Announcements Ch 5 OWL assignment due Friday, 5 PM Programming assignment 4 due next Wednesday (group session with me today, 2 PM CS BLDG 151) Grading complete on prog 1, 2 Midterm up tomorrow (window: 5 Fri - noon Monday)

While loops - Java’s other important looping construct int j = 1; // control variable while (j 3){ System.out.println(n); n = n - 1; }

What about this loop? int n = 1; while (n != 10){ System.out.println(n); n = n + 2; }

Suppose you have a 300,000 word dictionary (in alphabetical order). How many comparisons does it take to find a word?

public class SplitTest{ public static void main(String[] args){ int num = 300000; int splitCount = 0; while (num > 1){ num = num/2; splitCount++; } System.out.println("split count:" + splitCount); } } ----> number of splits: 18

Wwhat does this method do (assume in SimpleDice class)? public int huh(int big){ int count = 0; int total = 0; while(total < big){ count = count + 1; total = total + throwDice(); } return count; }

Palindromes -same forwards and backwards(throw out: caps, punctuation, spaces: ablewasiereisawelba) Otto Sit on a potato pan, Otis Doc: note I dissent. A fast never prevents a fatness. I diet on cod. Live dirt up a sidetrack carted is a putrid evil A man a plan a canal, Panama! Able was I ere I saw Elba Remarkable was I ere I saw Elba, Kramer

N

o

w

I

s

r

left ->

s

N

= 0; j--){ ch = str.charAt(j); System.out.println(ch); }

this public int dayAge(){ int months = this.age; return(30*months); } public void nameChange(String name){ this.name = name; }

myKid.nameChange(“will”); Big deal

myKid

Info flow

nameChange(“will”);

myKid.dayAge(); Info flow

Looping constructions.. import java.util.Scanner; public class AnyCaps{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); String str = scan.nextLine(); boolean caps = false; for(int j = 0; j < str.length(); j++){ char c = str.charAt(j); if((c >= 'A') && (c 98.6) JOptionPane.showMessageDialog(null,"you are sick!"); else JOptionPane.showMessageDialog(null,"you are well!"); } }