DOC PREVIEW
GT LCC 6310 - LCC 6310 Lecture 3

This preview shows page 1-2-3-24-25-26 out of 26 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

LCC 6310 Computation as an Expressive MediumAdministriviaSuggestions on learning to programDon’t worry…OutlineReading text from the consoleWhat’s the deal with byte[]?Effect of creating a single byteEffect of creating an array of bytesRead(byte[] b) puts characters into bCode to readWhat’s the deal with exceptionsSlide 13We’re printing out a referenceWe really want a StringSlide 16We’re printing out all of readBytesPrinting out just what we’ve readSlide 19Control flowComputation step-by-stepIfSome boolean expressionsUsing if with our String readerDo-WhileUsing do-while with our String readerLCC 6310Computation as an Expressive MediumLCC 6310Computation as an Expressive MediumLecture 3Lecture 3AdministriviaAdministrivia•Everyone has book (or is on track to Everyone has book (or is on track to get one)?get one)?•Class list – if you didn’t get messages Class list – if you didn’t get messages over the weekend, give me your over the weekend, give me your addressaddress•Adjustments to syllabusAdjustments to syllabusSuggestions on learning to programSuggestions on learning to program•Spend a lot of time fiddling around with codeSpend a lot of time fiddling around with code•Programming is something you have to learn by trying it•Get help from other peopleGet help from other people•I expect those who already know some programming to help others•Figure things out in groups•Ask me questions in classAsk me questions in classDon’t worry…Don’t worry…•Focus on resultsFocus on results•Code starts making sense through Code starts making sense through repetitionrepetitionOutlineOutline•Finish up previous lectureFinish up previous lecture•Introduction to types• Introduction to the Java class library• Two classic text programs•Programming constructs needed for first assignmentProgramming constructs needed for first assignment•Text input •Control structures (e.g. if-then, while)•Arrays•Discuss readings on Wednesday Discuss readings on WednesdayReading text from the consoleReading text from the console•Class Class SystemSystem has a field has a field inin•Analogous to field out•Field Field inin contains an contains an InputStreamInputStream•Input stream contains a number of input methods•The one we care about:The one we care about:public int read(byte[] b) throws IOExceptionWhat’s the deal with byte[]?What’s the deal with byte[]?•We know that byte is a primitive typeWe know that byte is a primitive type•Holds 8 bits – the numbers 0-255•ASCII characters are numbers between 0-255•byte[]byte[] is an is an arrayarray•An array is a collection of elements with the same type•The elements in the collection are indexed with a counterEffect of creating a single byteEffect of creating a single byte// Single bytebyte oneByte;// Put a value in the byteoneByte = 3;// Type error!oneByte = “hello”;Code EffectName: oneByte, Type: byte3Name: oneByte, Type: byteName: oneByte, Type: byte“hello”Can’t shove “hello” into a byteEffect of creating an array of bytesEffect of creating an array of bytes// declare byte arraybyte[] byteArray; // initialize byte arraybyteArray = new byte[5]; // set first elementbyteArray[0] = 3; // set third elementbyteArray[2] = 5; Code EffectName: byteArray, Type: byte[]0 1 2 3 4each element has type byte0 0 0 0 00 1 2 3 43 0 0 0 00 1 2 3 43 0 5 0 0Read(byte[] b) puts characters into bRead(byte[] b) puts characters into b•Calling Calling System.in.readSystem.in.read makes the makes the program wait until user types (and program wait until user types (and hits the return key)hits the return key)•The characters the user types are The characters the user types are put into the arrayput into the array•Let’s try to read from the consoleLet’s try to read from the consoleCode to readCode to readpublic 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 exceptionsWhat’s the deal with exceptions•Programs create exceptions to indicate errorsPrograms create exceptions to indicate errors•Creating an exception is called throwing an exception•When an exception is thrown you canWhen 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 Let’s not worry about exceptions for now, just pass them onpass them onimport 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);}}Code to readCode to read• Now we can compile and run…Now we can compile and run… • But why is it printing out garbage?But why is it printing out garbage?We’re printing out a referenceWe’re printing out a reference•An array consists of a reference pointing An array consists of a reference pointing at the cells of the arrayat the cells of the array•System.out.println(readBytes)System.out.println(readBytes) is is printing out the reference, not the cellsprinting out the reference, not the cells0 1 2 3 4‘h’ ‘e’ ‘l’ ‘l’ ‘o’5 6 97 98 99CR 0 0 0 0…readBytesWe’re printing out thisWe want to print out thisWe really want a StringWe really want a String•We don’t want to worry about all these We don’t want to worry about all these individual charactersindividual characters•And we have a And we have a printlnprintln method for printing method for printing StringsStrings•Let’s look in documentation for Let’s look in documentation for StringString•We want a constructor that takes an array of bytes and creates a Stringimport 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);}}Code to readCode to read• Now we’re seeing what we type…Now we’re seeing what we type… • But why are there a bunch of extra characters?But why are there a bunch of extra characters?We’re printing out all of readBytesWe’re printing out all of readBytes•readBytes is longer than what we’ve


View Full Document

GT LCC 6310 - LCC 6310 Lecture 3

Documents in this Course
Load more
Download LCC 6310 Lecture 3
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view LCC 6310 Lecture 3 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view LCC 6310 Lecture 3 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?