Presentation is loading. Please wait.

Presentation is loading. Please wait.

Announcements & Review

Similar presentations


Presentation on theme: "Announcements & Review"— Presentation transcript:

1 Announcements & Review
Last Time: int, boolean some operations boolean logic if-then-else! Announcements Friday I’m away, Katie Coons will teach Lab 1 Due Thursday 10pm Use turnin 1 file per pair Lecture 6: String Objects and Input

2 Lecture 6: String Objects and Input
Today Clairfy float verses double Finish if-then-else example More basic program elements Objects versus values Strings & their operators Input Using the Scanner class Lecture 6: String Objects and Input

3 Lecture 6: String Objects and Input
Values versus objects Numbers Have values but they do not have behaviors Objects Have attributes and behaviors System.out - References an OutputStream Attribute: monitor Behaviors: printing System.in: References an InputStream Attribute: keyboard Behaviors: reading Java treats object variables differently. Although an object variable is the symbolic name for a memory location being used by the program, the memory location for an object variable does not store a value of that object type. Instead the value in the memory location tells the program where to find a value (object) of that type. We say that an object variable references or points to an object of the object type. Thus, a String variable references a memory location that holds a value of type String. Similarly, a BufferedReader variable references a memory location holding a BufferedReader value. Lecture 6: String Objects and Input

4 Lecture 6: String Objects and Input
Consider String a = "excellence"; String b = a; What is the representation? Lecture 6: String Objects and Input

5 Lecture 6: String Objects and Input
Examples Consider String a = "excellence"; String b = a; What is the representation? a "excellence" b Lecture 6: String Objects and Input

6 Uninitialized versus null
Consider String dayOfWeek; Scanner inStream; What is the representation? Lecture 6: String Objects and Input

7 Uninitialized versus null
Consider String dayOfWeek; Scanner inStream; What is the representation? dayOfWeek - inStream - Lecture 6: String Objects and Input

8 Uninitialized versus null
Consider String fontName = null; Scanner fileStream = null; What is the representation? Lecture 6: String Objects and Input

9 Uninitialized versus null
Consider String fontName = null; Scanner fileStream = null; What is the representation? fontName null fileStream null Lecture 6: String Objects and Input

10 Lecture 6: String Objects and Input
Assignment Consider String word1 = "luminous"; String word2 = "graceful"; -> word1 = word2; Representation at -> word1 "luminous" word2 "graceful" Lecture 6: String Objects and Input

11 Lecture 6: String Objects and Input
Assignment Consider String word1 = "luminous"; String word2 = "graceful"; word1 = word2; -> After assignment word1 word2 "graceful" Lecture 6: String Objects and Input

12 String representation
Consider String alphabet = "abcdefghijklmnopqrstuvwxyz"; Standard shorthand representation Truer representation alphabet "abcdefghijklmnopqrstuvwxyz" Another useful String member method is charAt(). This method expects a single index as its parameter and returns the value of the character at that position in its String. While such behavior seems relatively natural given the method name and its parameter, what is not natural is that in Java, as in several other programming languages, the first character of a String is located at index 0. alphabet a b c d e f g h i j k l m n o p q r s t u v w y z Lecture 6: String Objects and Input

13 Lecture 6: String Objects and Input
Some String Methods // a string objects String alphabet = "abcdefghijklmnopqrstuvwxyz"; // operations on string objects char c1 = alphabet.charAt(9); int position = alphabet.indexOf(“e”); String ab = alphabet.substring(0, 2); String moreLetters = ab.concat(alphabet); boolean sameStr = moreLetters.equals(ab); …. lots more … see pgs in Cohoon & Davidson or Javadoc (google it!) Lecture 6: String Objects and Input

14 Lecture 6: String Objects and Input
Example String Method String alphabet = "abcdefghijklmnopqrstuvwxyz"; char c1 = alphabet.charAt(9); char c2 = alphabet.charAt(2); int i1 = alphabet.indexOf(“de”); What are the values of c1, c2, and c3? Why? Lecture 6: String Objects and Input

15 Lecture 6: String Objects and Input
BlueJ Examples Lecture 6: String Objects and Input

16 Reading Input Features
// Scanner is an “extra” Java class, so we // need to tell the compiler where to find it import java.util.* // allocates an object of class Scanner Scanner stdin = new Scanner(System.in); String name = stdin.next(); int age = stdin.nextInt(); double commuteDistance = stdin.nextDouble(); boolean parent = stdin.nextBoolean(); // peeking to make sure the format is right // we will use these in Lecture 8 boolean isString = stdin.hasNext(); boolean isInt = stdin.hasNextInt(); boolean isDouble = stdin.hasNextDouble(); boolean isBoolean = stdin.hasNextBoolean(); Lecture 6: String Objects and Input

17 Lecture 6: String Objects and Input
Reading Input Example // Scanner is an “extra” Java class, so we // need to tell the compiler where to find it import java.util.* // allocates an object of class Scanner Scanner stdin = new Scanner(System.in); System.out.print("Enter a word: "); String word = stdin.next(); int wordLength = word.length(); System.out.println("Word " + word + " has length ” + wordLength + "."); Lecture 6: String Objects and Input

18 Lecture 6: String Objects and Input
BlueJ Examples Lecture 6: String Objects and Input

19 Lecture 6: String Objects and Input
More Questions? Lecture 6: String Objects and Input


Download ppt "Announcements & Review"

Similar presentations


Ads by Google