csci s-1 section 7. coming soon problem set three, part b – tuesday, july 14, 17:00 est problem...

28
CSCI S-1 Section 7

Post on 19-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST

CSCI S-1Section 7

Page 2: CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST

Coming Soon

• Problem Set Three, Part B– Tuesday, July 14, 17:00 EST

• Problem Set Four (72 + 5/15 points)– Friday, July 17, 17:00 EST

Page 3: CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST

LoopsWhen do you want to use a For Loop? What are the alternatives?

Page 4: CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST

While LoopsWhen do you want to use a For Loop? What are the alternatives?

while (condition){

// loop code goes here;}

where condition is some expression that evaluates to a boolean. At the start of every iteration of a while-loop, Java will evaluate the expression. If it's true, the loop will execute; if not, the loop will stop, and program execution will go on to the next line after the loop.

Page 5: CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST

While Loops, Example 1

int i = 0;while (i < 10) {

i += 2;System.out.println(i);

}

Answer:246810

Page 6: CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST

While Loops, Example 2

int i = 81;while (i % 3 == 0) {

i /= 3;System.out.println(i);

}

Answer:27931

Page 7: CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST

While Loops, Example 3

int i = 81;while (i % 3 == 0) {

System.out.println(i);i /= 3;

}

Answer:812793

Page 8: CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST

While Loops, Example 4

//when does this loop terminate?

Scanner console = new Scanner(System.in);

int i = console.nextInt();while (i > 0 || i < 10) {

System.out.println(i);i= console.nextInt();

}

Page 9: CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST

While Loops, Example 5

//reconstruct this as a while loopFor (int i = 2; i < 10; i++) {

System.out.println("Hello from " + i + "!")}

Page 10: CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST

While Loops, Example 5

//reconstruct this as a while loopFor (int i = 2; i < 10; i++) {

System.out.println("Hello from " + i + "!")}

int i = 2;while (i < 10) {

System.out.println("Hello from " + i + "!");i++;

}

Page 11: CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST

While Loops, Example 6

//Read int temperatures from the keyboard until there’s an increase//Inside the loop we read #s and compare with the prior #//Loop stops when the current # is greater than the prior #

Class NoIncreases {public static void main (String[] args) {

}}

Page 12: CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST

While Loops, Example 6import java.util.*;Class NoIncreases {

public static void main (String[] args) {Scanner kbd = new Scanner(System.in);System.out.println("Enter a number: ");int currentNumber = kbd.nextInt();int previousNumber = currentNumber; // so loop will startwhile (currentNumber <= previousNumber) {

previousNumber = currentNumber;System.out.println("Enter a number: ");currentNumber = kbd.nextInt();

}}

}

Page 13: CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST

Do-While// do loops always execute at least once; while loops may never execute// do loops can be helpful for obtaining/initializing variables

do {

// loop code goes here;}while (condition) ; // note the semi-colon

Page 14: CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST

Do-While// prompt user (repeatedly) to enter 1 or 2

Page 15: CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST

Do-While// prompt user (repeatedly) to enter 1 or 2import java.util.*;

…Scanner keyboard = new Scanner(System.in);int i;do{

System.out.print("Please enter 1 or 2: ");i = keyboard.nextInt();

} while (i != 1 && i != 2);

Page 16: CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST

Characters and ASCII// roman letters and numbers are encoded sequentially// 0-9 is 48-57 ; A-Z is 65-90; a-z is 97-122

//typecast a char into an intchar ch = 'A';int i = (int) ch;System.out.println(i); // what does this print?

Page 17: CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST

Characters and ASCII// roman letters and numbers are encoded sequentially// 0-9 is 48-57 ; A-Z is 65-90; a-z is 97-122

//typecast a char into an intchar ch = 'A';int i = (int) ch;System.out.println(i); // prints 65

//typecast an int into a charint i = 90;char ch = (char) i;System.out.println(ch); // what does this print?

Page 18: CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST

Characters and ASCII// roman letters and numbers are encoded sequentially// 0-9 is 48-57 ; A-Z is 65-90; a-z is 97-122

//typecast a char into an intchar ch = 'A';int i = (int) ch;System.out.println(i); // prints 65

//typecast an int into a charint i = 90;char ch = (char) i;System.out.println(ch); // prints Z

Page 19: CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST

Characters and ASCII//print out an encoding table of all capital letters// 0-9 is 48-57 ; A-Z is 65-90; a-z is 97-122 class Ascii{

}

Page 20: CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST

Characters and ASCII//print out an encoding table of all capital letters // 0-9 is 48-57 ; A-Z is 65-90; a-z is 97-122

class Ascii{

public static void main(String[] args){

for (int i = 65; i <= 90; i++){

System.out.println(i + "\t" + (char) i);}

}}

Page 21: CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST

Characters and ASCII//Given some char ch. How could we determine if ch is a capital vowel?

//How could we determine if ch is between the letters `b' and `y', inclusive?

Page 22: CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST

Characters and ASCII//Given some char ch. How could we determine if ch is a capital vowel?ch == `A' || ch == `E' || ch == `I' || ch == `O' || ch == `U’

//How could we determine if ch is between the letters `b' and `y', inclusive? (ch >= `b' && ch <= `y')

Page 23: CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST

Character StringsString s= new String(); //make an empty stringString s= “Hello, World!”; //assign a literal to a string

int len= s.length(); //what is len?

Page 24: CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST

Character StringsString s= new String(); //make an empty stringString s= “Hello, World!”; //assign a literal to a string

int len= s.length(); //len is 13

//indexing is zero-basedchar charAt(pos); //returns char at pos in some strings.charAt(4) == s.charAt(8); //true or false?s.charAt(0) <= s.charAt(7); //true or false?

Page 25: CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST

String Exercises//use one Java statement to print string s, one char at a time

Page 26: CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST

String Exercises//use one Java statement to print string s, one char at a timefor (int i = 0; i < s.length(); i++)

System.out.print(s.charAt(i));

//now print it backwards

//now print only the lowercase letters between ‘g’ and ‘q’

Page 27: CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST

String Exercises//use one Java statement to print string s, one char at a timefor (int i = 0; i < s.length(); i++)

System.out.println(s.charAt(i));

//now print it backwardsfor (int i = s.length() - 1; i >= 0; i--)

System.out.print(s.charAt(i));

//now print only the lowercase letters between ‘g’ and ‘q’For (int i = 0; i < s.length(); i++)

if (s.charAt(i) >= ‘g’ && s.charAt(i) <= ‘q’)System.out.print(s.charAt(i));

Page 28: CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST

More String Methodssubstring()

s.substring(7) “World!” //substring from pos 7 to ends.substring(0,5) “Hello” //substring betw pos 0 and 4, inc

s.substring(7).charAt(0) ‘W’ //why?

toUpperCase(), toLowerCase() //return UC/LC string

s.substring(0,5).toUpperCase() //what’s the difference?s.toUpperCase().substring(0,5)

indexOf() //charAt in reverse -- s.indexOf(‘W’) 7 //first instance of char ‘W’ in ss.indexOf(“World”) 7 //first instance of String “World” in s

equals() // == doesn’t work with Stringss.equals(s2) //compares Strings s and s2