Presentation is loading. Please wait.

Presentation is loading. Please wait.

BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)

Similar presentations


Presentation on theme: "BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)"— Presentation transcript:

1 BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah) bkweusijana@cascadia.edu http://edutek.net/cascadia/bit115

2 BIT 115: Introduction To Programming 2 Quiz https://catalysttools.was hington.edu/webq/survey /babaw/56674 https://catalysttools.was hington.edu/webq/survey /babaw/56674 2

3 BIT 115: Introduction To Programming Today Ch 7.2, 7.3.1-7.3.2, 7.3.3 –Numeric Types –Non-Numeric Types –Strings & toString()

4 BIT 115: Introduction To Programming Next Lecture Wednesday (6/4): –Ch 8.2 (Reference Variables) –A4 and J5 will be due BEFORE CLASS

5 BIT 115: Introduction To Programming 5 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

6 BIT 115: Introduction To Programming 6 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 6

7 BIT 115: Introduction To Programming 7 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 anything in the java.applet package 7

8 BIT 115: Introduction To Programming 8 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 websiteAPI/JavaDoc of Java 5 (JDK 1.5) –You should be able to get to the doc.zip file version on Sun's java.sun.com site 8

9 BIT 115: Introduction To Programming 9 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. 9

10 BIT 115: Introduction To Programming 10 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 two lines are semantically identical 10

11 BIT 115: Introduction To Programming 11 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 change) –You can give it new values: s = “Dr.”; 11

12 BIT 115: Introduction To Programming 12 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(); 12

13 BIT 115: Introduction To Programming 13 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); 13

14 BIT 115: Introduction To Programming 14 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. 14

15 BIT 115: Introduction To Programming 15 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. 15

16 BIT 115: Introduction To Programming 16 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 != 16

17 BIT 115: Introduction To Programming 17 ICE 17 17


Download ppt "BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)"

Similar presentations


Ads by Google