Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah) [email protected] http://edutek.net/cascadia/bit115 BIT 115: Introduction To Programming Quiz • https://catalysttools.was hington.edu/webq/survey /babaw/56674 BIT 115: Introduction To Programming 2 Today • Ch 7.2, 7.3.1-7.3.2, 7.3.3 – Numeric Types – Non-Numeric Types – Strings & toString() BIT 115: Introduction To Programming Next Lecture • Wednesday (6/4): – Ch 8.2 (Reference Variables) – A4 and J5 will be due BEFORE CLASS BIT 115: Introduction To Programming String Objects • This class seeks to model stuff that you can write down. • Text: Words, usually, but also numbers, punctuation, random symbols • This is done with objects of a class called java.lang.String • String is basically short for: a string of characters – example: the letter ‘a’ is a character BIT 115: Introduction To Programming 5 Java API • Included with the Java language is a library of classes that seek to model common programming things (like strings) • The public classes and methods you can use are called the Java API (Application Programming Interface) – It's got so much stuff, that it tries to group related stuff together. • So everything that belongs in the language goes under java.lang – E.g., java.lang.String BIT 115: Introduction To Programming 6 Packages in Java API • java.lang is called a package – The java.lang package is automatically available to your programs – Everything else has to be imported • To tell the Java compiler what you're going to use • You include a package using the import keyword/command – import java.applet.AudioClip; // To use just the "AudioClip" class in the java.applet package – import java.applet.*; // To use anythingBIT 115: inIntroduction the Tojava.applet Programming 7 JavaDocs of Java API • Immediate implication: – You'll need to look for documentation under java.lang.String – Use the link to the online HTML JavaDocs called “API/JavaDoc of Java 5 (JDK 1.5)” on the course website – You should be able to get to the doc .zip file version on Sun's java.sun.com site BIT 115: Introduction To Programming 8 String seeks to model an immutable (unchanging) string • java.lang.String class contains a number of methods (behaviors) that are common to all strings. • Yet each individual String has attributes which are unique to it – most importantly, the characters in it's string. BIT 115: Introduction To Programming 9 2 ways to create an instance of the String class • String s = new String("Baba"); • I like this way, because it's clear that you're actually creating a new instance (instantiating a new object) of the String class. This does the same thing: • String s = "Baba"; • Whenever you write a literal like "some text" in your code, at compilation time, the Java compiler changes this to be a reference to a String object that it creates for you • So the above twoBITlines are Tosemantically identical 115: Introduction Programming 10 4 Important Points • You can't leave out the semi-colon at the end!! ; is required • You have to first list the type (String), then the name of the variable (like s). – Java is strongly typed, meaning that it (mostly) won't convert from, say, a String to an integer. JavaScript is loosely typed, meaning that it will attempt to convert from, say, a String to an integer. • Optionally, you can immediately assign a value to the new variable (initialize it) • s is a reference variable – it doesn't actually contain the entire string inside it, but instead it is a reference to the object that actually does, that’s why it’s immutable (can’t BIT 115: Introduction To Programming 11 change) Calling methods on a String • Let's say that you want to see your name, in all caps. – The point of providing an API to a library is that it'll do work for you; you don't have to figure out all the details – It just so happens that the String class can do this for you, using the toUpperCase method – So, we'd like to send a message to the String object referred to by the reference variable s, telling the object to produce an upper-case version of itself: •s.toUpperCase(); BIT 115: Introduction To Programming 12 • However, Strings are immutable, meaning that they can't change. – Thus, the object that s refers to will be exactly the same after the method call is finished. – Instead of changing the object that s refers to, toUpperCase actually produces a brand-new String object, who's value is a copy of s's, except that the new String is in all uppercase. – If we don't do anything with it, then we'll lose it. In order to keep track of it, we'll need a reference: •String s2; •s2 = s.toUpperCase(); •System.out.println(s2); BIT 115: Introduction To Programming 13 Vocabulary review • We're using a single class (String), but we've got 2 instances of the String class (2 objects). We refer to these objects using references – the variables s, and s2. We created s2 by sending a message to s (a.k.a. "calling a method on s"). Even though both objects are of the same class, each object has different attributes – in this case, the characters(letters) that it contains – yes, upper & lower case letters are different • You can think of the String class as being a cookie-cutter for individual objects. BIT 115: Introduction To Programming 14 Composing a Message –String s2; –s2 = s.toUpperCase(); –System.out.println(s2); • Notice that we never actually use s2 again, so we can avoid using an s2 with: –System.out.println(s.toUpperCase()); • This is sometimes called composing a message the result of the first message (toUpperCase) is used as an argument to the second message. • Note that there is an implicit order here – you can't print the string until you've first changed it to uppercase, so the toUpperCase happens first. BIT 115: Introduction To Programming 15 Working with user input Strings • This will get a String from the user • TextInput in = new TextInput(); String input = ""; input = in.readLine(); • Then you can compare it to other Strings • if(input.equals(s)) • {// do something} • if(input.equalsIgnoreCase(“jim”)) • {// do something} • DON’T use == or != BIT 115: Introduction To Programming 16 ICE 17 BIT 115: Introduction To Programming 17