Download presentation
Presentation is loading. Please wait.
1
LCC 6310 Computation as an Expressive Medium
Lecture 3
2
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
3
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
4
Don’t worry… Focus on results
Code starts making sense through repetition
5
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
6
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
7
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
8
Effect of creating a single byte
Code Effect Name: oneByte, Type: byte // Single byte byte oneByte; // Put a value in the byte oneByte = 3; // Type error! oneByte = “hello”; Name: oneByte, Type: byte 3 Name: oneByte, Type: byte “hello” Can’t shove “hello” into a byte
9
Effect of creating an array of bytes
Code Effect Name: byteArray, Type: byte[] // declare byte array byte[] byteArray; // initialize byte array byteArray = new byte[5]; // set first element byteArray[0] = 3; // set third element byteArray[2] = 5; 1 2 3 4 each element has type byte 1 2 3 4 3 1 2 3 4 3 5
10
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
11
Code to read But it won’t compile! 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!
12
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
13
Code to read Now we can compile and run…
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?
14
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 We’re printing out this readBytes 1 2 3 4 5 6 97 98 99 … ‘h’ ‘e’ ‘l’ ‘l’ ‘o’ CR We want to print out this
15
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
16
Code to read Now we’re seeing what we type…
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?
17
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 1 2 3 4 5 6 97 98 99 … ‘h’ ‘e’ ‘l’ ‘l’ ‘o’ CR
18
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
19
Code to read Ahh, it’s doing what we want!!! 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!!!
20
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
21
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; }
22
If If statements introduce conditional branches
If (<boolean expression>) <do this code> Boolean expressions have one of two values: true or false
23
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
24
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
25
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>);
26
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”
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.