Transcript Lecture 3

LCC 6310
Computation as an Expressive Medium
Lecture 3
Administrivia
• Everyone has book (or is on track to get
one)?
• Class list – if you didn’t get messages
over the weekend, give me your address
• Adjustments to syllabus
Suggestions on learning to program
• Spend a lot of time fiddling around with code
• Programming is something you have to learn by trying it
• Get help from other people
• I expect those who already know some programming to
help others
• Figure things out in groups
• Ask me questions in class
Don’t worry…
• Focus on results
• Code starts making sense through
repetition
Outline
• Finish up previous lecture
• Introduction to types
• Introduction to the Java class library
• Two classic text programs
• Programming constructs needed for first assignment
• Text input
• Control structures (e.g. if-then, while)
• Arrays
• Discuss readings on Wednesday
Reading text from the console
• Class System has a field in
• Analogous to field out
• Field in contains an InputStream
• Input stream contains a number of input methods
• The one we care about:
public int read(byte[] b) throws IOException
What’s the deal with byte[]?
• We know that byte is a primitive type
• Holds 8 bits – the numbers 0-255
• ASCII characters are numbers between 0-255
• byte[] is an array
• An array is a collection of elements with the same type
• The elements in the collection are indexed with a counter
Effect of creating a single byte
Code
Effect
Name: oneByte, Type: byte
// Single byte
byte oneByte;
// Put a value in the byte
oneByte = 3;
Name: oneByte, Type: byte
3
// Type error!
oneByte = “hello”;
Name: oneByte, Type: byte
“hello”
Can’t shove “hello” into a byte
Effect of creating an array of bytes
Code
// declare byte array
byte[] byteArray;
// initialize byte array
byteArray = new byte[5];
Effect
Name: byteArray, Type: byte[]
0
1
2
3
4
0
0
0
0
0
each element has type byte
// set first element
byteArray[0] = 3;
0
1
2
3
4
3
0
0
0
0
// set third element
byteArray[2] = 5;
0
1
2
3
4
3
0
5
0
0
Read(byte[] b) puts characters into b
• Calling System.in.read makes the
program wait until user types (and hits
the return key)
• The characters the user types are put
into the array
• Let’s try to read from the console
Code to read
public class ReadExample {
public static void main(String args[]) {
byte readBytes[] = new byte[100];
System.out.println(“Type something”);
System.in.read(readBytes);
System.out.println(readBytes);
}
}
But it won’t compile!
What’s the deal with exceptions
• Programs create exceptions to indicate errors
• Creating an exception is called throwing an exception
• When an exception is thrown you can
• Catch it (and do something about it)
• Pass it on (someone else does something about it)
• Let’s not worry about exceptions for now, just
pass them on
Code to read
import java.io.*;
public class ReadExample {
public static void main(String args[])
throws IOException {
byte readBytes[] = new byte[100];
System.out.println("Type something");
System.in.read(readBytes);
System.out.println(readBytes);
}
}
• Now we can compile and run…
• But why is it printing out garbage?
We’re printing out a reference
• An array consists of a reference pointing at
the cells of the array
• System.out.println(readBytes) is printing
out the reference, not the cells
readBytes
We’re printing out this
0
1
2
3
4
5
6
‘h’
‘e’
‘l’
‘l’
‘o’
CR
0
…
97
98
99
0
0
0
We want to print out this
We really want a String
• We don’t want to worry about all these
individual characters
• And we have a println method for printing
Strings
• Let’s look in documentation for String
• We want a constructor that takes an array of bytes and
creates a String
Code to read
import java.io.*;
public class ReadExample {
public static void main(String args[])
throws IOException {
byte readBytes[] = new byte[100];
System.out.println("Type something");
System.in.read(readBytes);
String readString = new String(readBytes);
System.out.println(readString);
}
}
• Now we’re seeing what we type…
• But why are there a bunch of extra characters?
We’re printing out all of readBytes
• readBytes is longer than what we’ve typed
• We’re printing out the characters we’ve read,
plus a bunch of default values
• If we type hello, we have…
readBytes
0
1
2
3
4
5
6
‘h’
‘e’
‘l’
‘l’
‘o’
CR
0
…
97
98
99
0
0
0
Printing out just what we’ve read
• System.in.read(bytes[] b) returns
the number of characters read
• Look in the documentation
• If we can get the number of characters
read, we should be able to print just
this number of characters
Code to read
import java.io.*;
public class ReadExample {
public static void main(String args[])
throws IOException {
int numberOfBytes;
byte readBytes[] = new byte[100];
System.out.println("Type something");
numberOfBytes = System.in.read(readBytes);
String readString = new String(readBytes, 0, numberOfBytes);
System.out.println(readString);
}
}
Ahh, it’s doing what we want!!!
Control flow
• By default Java executes the lines of a method one
after the other
• Sequential control flow
• Unconditional – doesn’t matter what happens in the world
• Often we want which steps are executed to depend on
what else has happened
• That is, we want conditional control flow
• This is necessary in order to make anything that is interactive
Computation step-by-step
• Within a method, code is executed one
line after another
public class Example {
public static void main(String args[]) {
int x;
x = 1;
System.out.println(x);
x = x + 1;
System.out.println(x);
}
}
If
• If statements introduce conditional
branches
If (<boolean expression>)
<do this code>
• Boolean expressions have one of two
values: true or false
Some boolean expressions
anInteger = = 1
true if variable anInteger is equal to 1
x > 20 true if variable x is greater than 20
1 = = 2
true if 1 is equal to 2, it’s not so this is false
! is the not operator – reverses true and false so,
! 1 = = 2 is true!
This is not a boolean expression
String someString = new String("fun");
But these are – they use the someString defined above
someString.equals("fun") true
someString.equals("painful") false
Using if with our String reader
import java.io.*;
public class ReadExample {
public static void main(String args[])
throws IOException {
int numberOfBytes;
byte readBytes[] = new byte[100];
System.out.println("Type something");
numberOfBytes = System.in.read(readBytes);
String readString = new String(readBytes, 0, numberOfBytes);
// look at indexOf() in the String documentation
// using indexOf() so we don't have to worry about the return
// character.
if (readString.indexOf("hi there") != -1)
System.out.println("nice to meet you");
if (readString.indexOf("shut up") != -1)
System.out.println("you’re rude");
}
}
What it prints depends on what you type
Do-While
• Do While provides looping
• There are other looping expressions – we’ll look at those
later
• Sometimes you want the same code to execute
multiple times
do <some code>
while (<boolean expression is true>);
Using do-while with our String reader
import java.io.*;
public class ReadExample {
public static void main(String args[])
throws IOException {
int numberOfBytes;
byte readBytes[] = new byte[100];
String readString;
do {
System.out.println("Type something");
numberOfBytes = System.in.read(readBytes);
readString = new String(readBytes, 0, numberOfBytes);
System.out.println(readString);
} while ( readString.indexOf("quit") == -1);
}
}
It keeps on repeating what you type until you type “quit”