csci s-1 section 7. coming soon problem set three, part b – tuesday, july 14, 17:00 est problem...
Post on 19-Dec-2015
215 views
TRANSCRIPT
CSCI S-1Section 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?
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.
While Loops, Example 1
int i = 0;while (i < 10) {
i += 2;System.out.println(i);
}
Answer:246810
While Loops, Example 2
int i = 81;while (i % 3 == 0) {
i /= 3;System.out.println(i);
}
Answer:27931
While Loops, Example 3
int i = 81;while (i % 3 == 0) {
System.out.println(i);i /= 3;
}
Answer:812793
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();
}
While Loops, Example 5
//reconstruct this as a while loopFor (int i = 2; i < 10; i++) {
System.out.println("Hello from " + i + "!")}
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++;
}
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) {
}}
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();
}}
}
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
Do-While// prompt user (repeatedly) to enter 1 or 2
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);
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?
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?
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
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{
}
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);}
}}
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?
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')
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?
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?
String Exercises//use one Java statement to print string s, one char at a time
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’
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));
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