Download CIS 1068 Administrative Stuff Last Time type casting someVar

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Administrative Stuff
I
Midterm 1
I
I
CIS 1068
I
I
I
I
Quizzes
I
February 14, 2017
Last Time
Thursday, February 23
In Walk Auditorium
Could cover anything up to and including Feb 16
Closed book, closed notes
We’ll talk more about it in class soon
I
Quiz 2. This week in lab
No quiz next week
I
Assignment 3. Game due
I
Assignment 4. Nest egg
I
Lecture vids. Still trying
type casting
someVar = (newType)otherVar ;
I
typecasting
I
some practice with Strings
I
for loop
example
int x;
double d=2.5;
x=(int)d;
type casting
for loop review
for (
initial cond
;
someVar = (newType)otherVar ;
test ; update ) {
loop
body
example
}
int x;
double d=2.5;
x=(int)d;
but remember . . .
not all casts are legal, e.g., can’t do Scanner → int
for loop review
for loop review
for (
initial cond
;
test ; update ) {
loop
body
for (
I
}
I
initial cond – only done the first time
test
I
I
I
any expression that boils down to true or false
continue to execute loop body as long as test is true
update – done every time we finish instance of loop body
;
test ; update ) {
loop
body
}
parts
initial cond
for loop review
Don’t Forget About Scope
1
...
2
3
4
for (
initial cond
;
test ; update ) {
for (int i=0; i<NUM_ITEMS; i++) {
int highScores=0;
5
x=myRandom.nextInt(100)+1;
if (x > CUTOFF) {
highScores++;
}
6
loop
body
7
8
9
}
10
}
11
12
is it possible . . .
I
that the loop body is never executed?
I
that the loop never ends?
What does this do?
13
System.out.println("There were " + highScores +
" readings above " + CUTOFF);
scope error
5
7
8
9
includes i
public static final int NUM_TO_READ=5;
public static void main(String args[]) {
int sum=0;
Scanner in = new Scanner(System.in);
7
8
9
for (int i=0; i<NUM_TO_READ; i++) {
System.out.print("Please enter an integer> ");
int x=in.nextInt();
System.out.println("You entered " + x);
if (x%2==0) {
sum+=x;
}
}
System.out.println("The total is " + sum);
10
for (int i=0; i<NUM_TO_READ; i++) {
System.out.print("Please enter an integer> ");
int x=in.nextInt();
System.out.println("You entered " + x);
sum+=x;
}
System.out.println("The total is " + sum);
10
11
12
13
14
15
16
}
17
18
I
5
6
public static void main(String args[]) {
int sum=0;
Scanner in = new Scanner(System.in);
6
anything declared within loop local to the loop
What does this do?
4
public static final int NUM_TO_READ=5;
4
I
}
11
12
13
14
15
16
17
18
}
19
20
}
What does this do?
public static final int NUM_TO_READ=5;
4
5
public static void main(String args[]) {
int sum=0, numEvens=0;
Scanner in = new Scanner(System.in);
6
7
8
Stupid Casting Tricks
What is?
I
(int)’A’
9
for (int i=0; i<NUM_TO_READ; i++) {
System.out.print("Please enter an integer> ");
int x=in.nextInt();
System.out.println("You entered " + x);
if (x%2==0) {
sum+=x;
numEvens++;
}
}
System.out.println("You entered " + numEvens +
" even integers that total " + sum);
10
11
12
13
14
15
16
17
18
19
20
}
21
22
}
Stupid Casting Tricks
What is?
I
(int)’A’ answer: 65
Stupid Casting Tricks
What is?
I
(int)’A’ answer: 65
I
(char)67
Stupid Casting Tricks
What is?
Stupid Casting Tricks
What is?
I
(int)’A’ answer: 65
I
(int)’A’ answer: 65
I
(char)67 answer: ’C’
I
(char)67 answer: ’C’
Can we do this?
char c = ’A’;
c++;
Stupid Casting Tricks
What is?
I
(int)’A’ answer: 65
I
(char)67 answer: ’C’
Stupid Casting Tricks
What is?
I
(int)’A’ answer: 65
I
(char)67 answer: ’C’
Can we do this?
Can we do this?
char c = ’A’;
c++;
char c = ’A’;
c++;
What about this?
What about this?
for (char c=’a’; c<=’z’; c++) {
System.out.print(c);
}
System.out.println();
for (char c=’a’; c<=’z’; c++) {
System.out.print(c);
}
System.out.println();
prints abcdefghijklmnopqrstuvwxyz
Printing Every Character in a String
Printing Every Character in a String
Java
Java
output
String s = "spring break";
String s = "spring break";
for (int i=0; i<s.length(); i++) {
System.out.println(s.charAt(i));
}
for (int i=0; i<s.length(); i++) {
System.out.println(s.charAt(i));
}
s
p
r
i
n
g
b
r
e
a
k
Same in Reverse
Java
output
String s = "spring break";
k
a
e
r
b
for (int i=s.length()-1; i>=0; i--) {
System.out.println(s.charAt(i));
}
g
n
i
r
p
s