Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computer Science and Object-Oriented Programming

Similar presentations


Presentation on theme: "Introduction to Computer Science and Object-Oriented Programming"— Presentation transcript:

1 Introduction to Computer Science and Object-Oriented Programming
Week 6

2 Tonight Lab 2 Strings Simple Input Week 6

3 Strings Vital to solving problems
A series of 0 or more characters such as “Hello, World!” H e l o , W r d ! 1 2 3 4 5 6 7 8 9 10 11 12 Any letter, digit, punctuation mark, etc. Strings have a length (the number of characters) The characters in a String have positions starting at 0 Week 6

4 What Characters Java can represent a large number of characters
Accommodate many languages Week 6

5 The Empty String In Java, a string can have no characters!
It’s length is zero Also called the null string It is represented by the literal constant ““ (two consecutive double quotes) Turns out to be very useful Week 6

6 What’s the difference between
Question What’s the difference between ““ and “ “ Week 6

7 Noteworthy String is not a primitive type it’s a class (with methods)
But is has a symbolic operator - the + you don’t need a new to create String objects Week 6

8 String Operator What is output? String firstName = “Rob”;
String lastName = “Collins”; System.out.print(“Student’s name is: ”); System.out.println(firstName + “ ” + lastName + “: Age ” ); What is output? Week 6

9 String Methods String length Access a substring
String greeting = “Hello!”; System.out.println(“Greeting size is ” + greeting.length()); Access a substring String itemCode = “1998FXCAUS”; String stateField = itemCode.substring(6, 8); String countryField = itemCode.substring(8); Manipulating characters: String alpha = “abcdefg”; char firstChar = alpha.charAt(0); char lastChar = alpha.charAt(alpha.length() – 1); int position = alpha.indexOf(‘c’); String bigAlpha = alpha.toUpperCase(); For string positions, start counting at 0 Week 6

10 String Methods Using the substring method substring(start, pastEnd)
picks out the characters from the start position up to, but not including the character at pastEnd So substring(7,12) refers to “World” H e l o , W r d ! 1 2 3 4 5 6 7 8 9 10 11 12 Week 6

11 String Methods Using the substring method substring(start)
picks out the characters from the start position thru the end of the string So substring(7) refers to “World!” H e l o , W r d ! 1 2 3 4 5 6 7 8 9 10 11 12 Week 6

12 Question String itemCode = “1998FXCAUS”; String stateField = itemCode.substring(6, 8); String countryField = itemCode.substring(8); What values are assigned to stateField and countryField? Is the value of itemCode changed? H e l o , W r d ! 1 2 3 4 5 6 7 8 9 10 11 12 Week 6

13 String Methods Using other String methods H e l o , W r d !
String alpha = “abcdefg”; int position = alpha.indexOf(‘c’); String bigAlpha = alpha.toUpperCase(); H e l o , W r d ! 1 2 3 4 5 6 7 8 9 10 11 12 Week 6

14 Converting String to a Number
Use Integer and Double classes if a string is a proper representation of number use the parseInt, parseDouble methods String year = “2010”; String width = “22.5”; int numYear = Integer.parseInt(year); double numWidth = Double.parseDouble(width); Week 6

15 Getting Input So far we have worked with values stored them
calculated new values from exising values output them Now we learn a simple way to get input from the keyboard (the console in BlueJ) Week 6

16 The Scanner class reads input
Create a Scanner object Using System.in a a parameter You need do this only once Scanner input = new Scanner(System.in); When you want input, prompt the user to enter it System.out.print(“Enter menu choice (1-5): ”); Read in using correct method (using “input” object): int menuChoice = input.nextInt(); // reads integer // waits until user hits <Enter> key Week 6

17 The Scanner class reads input
Read a value - using the correct method int menuChoice = input.nextInt(); nextDouble reads a double nextLine reads a line (until user hits Enter) nextWord reads a word (until any white space) Week 6

18 Scanner Example p. 166 of Big Java 01: import java.util.Scanner; 02:
03: /** 04: This program simulates a transaction in which a user pays for an item 05: and receives change. 06: */ 07: public class CashRegisterSimulator 08: { 09: public static void main(String[] args) 10: { 11: Scanner in = new Scanner(System.in); 12: 13: CashRegister register = new CashRegister(); 14: 15: System.out.print("Enter price: "); 16: double price = in.nextDouble(); 17: register.recordPurchase(price); 18: 19: System.out.print("Enter dollars: "); 20: int dollars = in.nextInt(); p. 166 of Big Java Week 6

19 Scanner Example Output:
Enter price: Enter dollars: 10 Enter quarters: 2 Enter dimes: 1 Enter nickels: 0 Enter pennies: 0 Your change: is 3.05 Week 6


Download ppt "Introduction to Computer Science and Object-Oriented Programming"

Similar presentations


Ads by Google