Presentation is loading. Please wait.

Presentation is loading. Please wait.

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING String and Scanner Objects.

Similar presentations


Presentation on theme: "©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING String and Scanner Objects."— Presentation transcript:

1 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING String and Scanner Objects

2 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-2 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.

3 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-3 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.

4 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-4 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"; Charleston Address to the object cityName The object that contains the character string “Charleston”

5 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-5 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.javaStringDemo.java

6 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 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); }

7 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-7 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.javaStringLength.java

8 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 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."); }

9 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-9 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.javaStringMethods.java

10 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 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); }

11 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-11 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.)Scope.java

12 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 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; }

13 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-13 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.

14 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-14 Commenting Code Javadoc comments can be built into HTML documentation. See example: Comment3.javaComment3.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.

15 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 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. }

16 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-16 Commenting Code Example index.html:index.html

17 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-17 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.javaCompact.java

18 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 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.");}}

19 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-19 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.javaReadable.java

20 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 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."); }

21 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-21 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;

22 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-22 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 2- 18 in the text. See example: Payroll.javaPayroll.java

23 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 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

24 ©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();

25 ©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); }


Download ppt "©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING String and Scanner Objects."

Similar presentations


Ads by Google