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
String and Scanner Objects CSC 110 – INTRO TO COMPUTING - PROGRAMMING ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. The String Class • Java has no primitive data type that holds a series of characters. • The String class from the Java standard library is used for this purpose. • In order to be useful, the a variable must be created to reference a String object. String number; • Notice the S in String is upper case. • By convention, class names should always begin with an upper case character. ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-2 Primitive vs. Reference Variables • Primitive variables actually contain the value that they have been assigned. number = 25; • The value 25 will be stored in the memory location associated with the variable number. • Objects are not stored in variables, however. Objects are referenced by variables. ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-3 Primitive vs. Reference Variables • When a variable references an object, it contains the memory address of the object’s location. • Then it is said that the variable references the object. String cityName = "Charleston"; The object that contains the character string “Charleston” cityName Address to the object ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Charleston 2-4 String Objects • A variable can be assigned a String literal. String value = "Hello"; • Strings are the only objects that can be created in this way. • A variable can be created using the new keyword. String value = new String("Hello"); • This is the method that all other objects must use when they are created. See example: StringDemo.java ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-5 StringDemo.java // A simple program demonstrating String objects. public class StringDemo { public static void main(String[] args) { String greeting = "Good morning, "; String name = "Herman"; System.out.println(greeting + name); } } ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. The String Methods • Since String is a class, objects that are instances of it have methods. • One of those methods is the length method. stringSize = value.length(); • This statement runs the length method on the object pointed to by the value variable. See example: StringLength.java ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-7 StringLength.java // This program demonstrates the String class's length method. public class StringLength { public static void main(String[] args) { String name = "Herman"; int stringSize; stringSize = name.length(); System.out.println(name + " has " + stringSize + " characters."); } } ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. String Methods • The String class contains many methods that help with the manipulation of String objects. • String objects are immutable, meaning that they cannot be changed. • Many of the methods of a String object can create new versions of the object. See example: StringMethods.java ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-9 StringMethods.java // This program demonstrates a few of the String methods. public class StringMethods { public static void main(String[] args) { String message = "Java is Great Fun!"; String upper = message.toUpperCase(); String lower = message.toLowerCase(); char letter = message.charAt(2); int stringSize = message.length(); System.out.println(message); System.out.println(upper); System.out.println(lower); System.out.println(letter); System.out.println(stringSize); } } ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Scope • Scope refers to the part of a program that has access to a variable’s contents. • Variables declared inside a method (like the main method) are called local variables. • Local variables’ scope begins at the declaration of the variable and ends at the end of the method in which it was declared. See example: Scope.java (This program contains an intentional error.) ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-11 Scope.java // This program can't find its variable. public class Scope { public static void main(String[] args) { System.out.println(value); // ERROR! int value = 100; } } ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Commenting Code • Java provides three methods for commenting code. Comment Style Description // Single line comment. Anything after the // on the line will be ignored by the compiler. /* … */ Block comment. Everything beginning with /* and ending with the first */ will be ignored by the compiler. This comment type cannot be nested. /** … */ Javadoc comment. This is a special version of the previous block comment that allows comments to be documented by the javadoc utility program. Everything beginning with the /** and ending with the first */ will be ignored by the compiler. This comment type cannot be nested. ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-13 Commenting Code • Javadoc comments can be built into HTML documentation. • See example: Comment3.java • To create the documentation: – Run the javadoc program with the source file as an argument – Ex: javadoc Comment3.java • The javadoc program will create index.html and several other documentation files in the same directory as the input file. ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-14 Comment3.java /** This class creates a program that calculates company payroll. */ public class Comment3 { /** The main method is the program's starting point. */ public static void main(String[] args) { double payRate; // Holds the hourly pay rate double hours; // Hours holds the hours worked int employeeNumber; // Holds the employee number // The Remainder of This Program is Omitted. } } ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Commenting Code • Example index.html: ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-16 Programming Style • Although Java has a strict syntax, whitespace characters are ignored by the compiler. • The Java whitespace characters are: – – – – – space tab newline carriage return form feed See example: Compact.java ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-17 Compact.java public class Compact {public static void main(String[] args){int shares=220; double averagePrice=14.67; System.out.println( "There were "+shares+" shares sold at $"+averagePrice+ " per share.");}} ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Indentation • Programs should use proper indentation. • Each block of code should be indented a few spaces from its surrounding block. • Two to four spaces are sufficient. • Tab characters should be avoided. – Tabs can vary in size between applications and devices. – Most programming text editors allow the user to replace the tab with spaces. See example: Readable.java ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-19 Readable.java /** This example is much more readable than Compact.java. */ public class Readable { public static void main(String[] args) { int shares = 220; double averagePrice = 14.67; System.out.println("There were " + shares + " shares sold at $" + averagePrice + " per share."); } } ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. The Scanner Class • To read input from the keyboard we can use the Scanner class. • The Scanner class is defined in java.util, so we will use the following statement at the top of our programs: import java.util.Scanner; ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-21 The Scanner Class • Scanner objects work with System.in • To create a Scanner object: Scanner keyboard = new Scanner (System.in); • Scanner class methods are listed in Table 218 in the text. • See example: Payroll.java ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-22 Payroll.java import java.util.Scanner; // Needed for the Scanner class /** This program demonstrates the Scanner class. */ public class Payroll { public static void main(String[] args) { String name; // To hold a name int hours; // Hours worked double payRate; // Hourly pay rate double grossPay; // Gross pay ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Payroll.java (cont) // Create a Scanner object to read input. Scanner keyboard = new Scanner(System.in); // Get the user's name. System.out.print("What is your name? "); name = keyboard.nextLine(); // Get the number of hours worked this week. System.out.print("How many hours did you work this week? "); hours = keyboard.nextInt(); // Get the user's hourly pay rate. System.out.print("What is your hourly pay rate? "); payRate = keyboard.nextDouble(); ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Payroll.java (cont) // Calculate the gross pay. grossPay = hours * payRate; // Display the resulting information. System.out.println("Hello, " + name); System.out.println("Your gross pay is $" + grossPay); } } ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.