Transcript Slide 1

APCS-AB: Java
Control Structures
October 17, 2005
week7
1
If Statements (Review)
if ( << conditional >> ) {
<< body >>
}
else if ( << conditional >> ) {
<< body >>
}
else {
<< body >>
}
• The << conditional >> can be any true or false conditional




week7
A simple boolean like (true)
A check for equality like (x == 5)
A greater than or equal to like (x >= 1)
A combination of the above with &&(and) , ||(or), or another conditional
(( x==5 && y == 2) || (z > 42))
2
If/Else
• Remember, the brackets are technically optional
 BUT only if you want to execute ONE statement after the if or else
statement
if (amount == 0)
System.out.println(“okay”);
else
System.out.println(“nonzero amount”);
____________________________________________
if(amount == 0)
amount = 500;
System.out.println(“amount equal to 0”);
else
amount = 200;
System.out.println(“amount was not equal to 0”);
• In this bottom example, both print statements will print, regardless of
the value of amount
week7
3
Looping
• The if/else code structure lets us change the flow
•
of the program, depending on certain conditions
Looping always us to easily repeat an action,
until a condition has been met
 What situations can you imagine in which this would
be really helpful?
• There are two kinds of loops in Java
 While they are technically interchangeable, each is
syntactically geared to a specific kind of situation
week7
4
While loop
• While loops logically follow the pattern of “while
something is true then perform the following set
of actions”
 This is useful in a situation in which you don’t know
how many times you need to do something, but you
know what the end result needs to be
• The syntax is simple:
while ( << conditional >> ) {
<< body >>
}
week7
5
Example
boolean keepLooping = true;
while (keepLooping){
printMenu();
int choice = getUserInput();
if(choice == 0){ // 0 is the “exit” choice
keepLooping = false;
}
else{
System.out.println(“Good choice”);
// do other stuff
}
}
System.out.println(“Thanks for playing”);
week7
6
For Loops
• We use for loops when we want to do a set of
•
statements a predetermined number of times
The syntax for a for loop is:
for ( <starting value>; <conditional>; <update statement>)
{
<< body >>
}
for (int x = 0; x < 10; x++) {
System.out.println(“x is: “ + x);
}
• The conditional is the same as it is in a while loop
• The update statement is optional, but usually is used to
increment or change the looping variable
week7
7
Class Exercise
• How would we write a method that would
print the numbers between 1 and 100, but
only in increments of 10?
week7
8
APCS-AB: Java
Control Structures
October 20, 2005
week7
9
Switch Statement
int someValue;
// someValue gets a value
switch ( someValue ) {
case 0:
//do something
break;
case 1:
//do something
break;
default:
//do something
}
char someValue;
// someValue gets a character
switch ( someValue ) {
case ‘A’:
//do something
break;
case ‘B’:
//do something
break;
default:
//do something
}
• The someValue needs to be an int or a char
 If no case value is matched, then the optional default case is executed -but it’s a good idea to always have the default case even if you don’t
expect to use it
week7
10
Schedule
• Today: Work on finishing loop lab in class
• Homework: Mini-project first, loop lab if there is
•
•
•
•
•
time
Friday - quiz postponed; String Manipulation
lecture
Monday: Work Day
Tuesday: Programming Quiz (One problem to
solve, replaces Friday Quiz)
Wednesday: Work Day/ Review
Thursday: Cumulative Java Quiz (Written)
week7
11
APCS-AB: Java
Java API & Strings
October 21, 2005
week7
12
Checkpoint
• Loop Lab
 How many of the tasks have you completed?
• Graphics Mini-Project
 Due today, extensions (one free late, or 10%
each day late) count weekend days, so get it
to me over the weekend if you can
week7
13
Java API
• API = application programming interface
• In Java, it is the list of all the classes available,
•
with details about the constructors, methods, and
usually a description of how to use the class
I had you download the full API to your
computers at home, there is also a scaled down
version that only has the methods and classes
that are used for the APCS test
 That is available online at:
http://www.cs.duke.edu/csed/ap/subset/doc/
week7
14
Why this is Cool
• There is so much code in Java that is
already written for you - you just have to
 Know that it is out there
 Figure out how to use it
• The API gives a standard way to look at
classes and methods so that any Java
programmer can understand how to use a
class without having to see the code
week7
15
String Class (APCS subset)
week7
16
Strings are immutable
• Once a string is created, it cannot change
• So string methods always return new
strings -- that way you can just change the
pointer
String name = “Jane”;
X
“Jane”
String name
name = name + “ Dow”;
week7
“Jane Dow”
17
Other String Methods
(Java API)
• In addition to what the AP people think you need to know,
there are some other cool String methods








boolean equalsIgnoreCase(String str)
String replace (char oldChar, char newChar)
boolean endsWith (String suffix)
boolean startsWith (String prefix)
String toUpperCase()
String toLowerCase()
String concat(String str)
String trim() //takes off white space from front & back
• Note: to make a char:
char ch = ‘A’;
week7
18
Java Packages
• All Java classes are grouped into libraries (or packages)
 String is part of the java.lang package, which is pre-loaded
when you are programming in Java
 We’ve already seen one other library, the java.util library,
where Scanner is
• Some of the other standard Java Libraries:




week7
java.applet
java.awt
java.io
java.lang
java.util
java.math
java.net
javax.swing
19
Using Packages
• Everything in java.lang is available for use
 So it’s as if somebody already did:
import java.lang.*;
• To use other packages, we need to import either
the specific class or the entire package (just like
we did for Scanner class)
 To import a class we use the whole package name:
import java.util.Scanner; import java.io.File;
 To import an entire library we use the asterisk:
import java.util.*; import java.io.*;
week7
20
String Project/Schedule
• Codebreaker due Thursday
week7
21