Download presentation
Presentation is loading. Please wait.
Published byYenny Budiono Modified over 6 years ago
1
CSS 161 Fundamentals of Computing Introduction to Computers & Java
Joe McCarthy CSS 161: Fundamentals of Computing
2
CSS 161: Fundamentals of Computing
Outline Updates / reminder Recap: Intro to Java Today: Chapter 1: Assignments, expressions, data types Chapter 2: Console Input & Output (I/O) Next time: Lab 2 (UW1-120) CSS 161: Fundamentals of Computing
3
CSS 161: Fundamentals of Computing
Updates / Reminder UW1-120 Mondays (“lectures”) & Fridays (labs) Wednesdays: here (UW2-031) Modified office hours MW 10:15-10:45 [no change] MW 7:45-8:15 (UW1-220) TTh 7:15-7:45pm [no change] & by appointment Lab 1 Due Friday, Oct 7, 11am Assignment 1 Due Monday, Oct 10, 11am CSS 161: Fundamentals of Computing
4
CSS 161: Fundamentals of Computing
Recap: Intro to Java Class Method main() Identifier Reserved word Variable Declaration Assignment Operator System.out.println() Output: = 30 CSS 161: Fundamentals of Computing
5
CSS 161: Fundamentals of Computing
6
Assignments & Expressions
Syntax: variable = expression Semantics: Evaluate expression, assign value to variable Expression: Constant (1, 1.23, 3.45e6, ‘a’, “css161”, true, false) final static int COURSENUMBER = 161; Variable (num2) expression operator expression Operators: *, /, % +, - … CSS 161: Fundamentals of Computing
7
Operators & Precedence
Can use parentheses to change precedence 1 + 2 * 3 (1 + 2) * 3 CSS 161: Fundamentals of Computing
8
CSS 161: Fundamentals of Computing
9
Shorthand assignments
Example: Equivalent To: count += 2; count = count + 2; sum -= discount; sum = sum – discount; bonus *= 2; bonus = bonus * 2; time /= rushFactor; time = time / rushFactor; change %= 100; change = change % 100; amount *= count1 + count2; amount = amount * (count1 + count2); CSS 161: Fundamentals of Computing
10
Increment & Decrement Operators
Syntax: variable++ | ++variable | variable-- | --variable Semantics: Set variable to next / previous value Examples int num1 = 1; char char1 = ‘B’; num1++; char1--; Precedence: increment/decrement before/after use 2*(num1++) 2 2*(++num1) 4 CSS 161: Fundamentals of Computing
11
Assignment Compatibility
byteshortintlongfloatdouble char CSS 161: Fundamentals of Computing
12
Type casting & coercion
Syntax: variable = (type) expression Semantics Convert expression to type, assign value to variable Only allowed if expression type is compatible with variable type Type coercion: Automatic type casting: int float double Examples short num3 = (short) ‘A’; int num4 = (int) 1.9; double num5 = 123; // coercion int num6 = 1.23; // error CSS 161: Fundamentals of Computing
13
CSS 161: Fundamentals of Computing
Console Input & Output System.out A Java object associated with the console (screen) print(), println() Methods associated with the System.out object Syntax: System.out.print(expression) System.out.println([expression]) Semantics: Evaluate expression, output its value println: expression is optional (hence the square brackets []) output a carriage return / line feed (newline) at end of line CSS 161: Fundamentals of Computing
14
Formatted output: printf
Syntax: System.out.printf(format[,expression]*) Semantics: Use format string to output expression(s) Examples: System.out.printf(“Hello”); System.out.printf(“Hello%n”); System.out.printf(“%5.1f”, 1.25); System.out.printf(“%-5.1f”, 1.25); System.out.printf(“$.2f”, 1.2); CSS 161: Fundamentals of Computing
15
Format string specifiers
CSS 161: Fundamentals of Computing
16
Console input: Scanner class
Not built-in, have to import import java.util.Scanner; Next: create an instance of the class Syntax: classname variable = new classname([args]) Semantics: Create an instance of classname, initialize it with args, assign instance to variable Example: Scanner keyboard = new Scanner(System.in); Create an instance of the Scanner class (in java.util package) Initialize it to accept input from the system console (System.in) Assign it to keyboard (NB: keyboard not a reserved word) CSS 161: Fundamentals of Computing
17
Console Input Using the Scanner Class
The method nextInt reads one int value typed in at the keyboard and assigns it to a variable: int numberOfPods = keyboard.nextInt(); The method nextDouble reads one double value typed in at the keyboard and assigns it to a variable: double d1 = keyboard.nextDouble(); Multiple inputs must be separated by whitespace and read by multiple invocations of the appropriate method Whitespace is any string of characters, such as blank spaces, tabs, and line breaks that print out as white space Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
18
Console Input Using the Scanner Class
The method next reads one string of non-whitespace characters delimited by whitespace characters such as blanks or the beginning or end of a line Given the code String word1 = keyboard.next(); String word2 = keyboard.next(); and the input line jelly beans The value of word1 would be jelly, and the value of word2 would be beans Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
19
Console Input Using the Scanner Class
The method nextLine reads an entire line of keyboard input The code, String line = keyboard.nextLine(); reads in an entire line and places the string that is read into the variable line The end of an input line is indicated by the escape sequence '\n' This is the character input when the Enter key is pressed On the screen it is indicated by the ending of one line and the beginning of the next line When nextLine reads a line of text, it reads the '\n' character, so the next reading of input begins on the next line However, the '\n' does not become part of the string value returned (e.g., the string named by the variable line above does not end with the '\n' character) Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
20
Keyboard Input Demonstration (Part 1 of 2)
Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
21
Keyboard Input Demonstration (Part 2 of 2)
Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
22
Another Keyboard Input Demonstration (Part 1 of 3)
Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
23
Another Keyboard Input Demonstration (Part 2 of 3)
Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
24
Another Keyboard Input Demonstration (Part 3 of 3)
Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
25
Pitfall: Dealing with the Line Terminator, '\n'
The method nextLine of the class Scanner reads the remainder of a line of text starting wherever the last keyboard reading left off This can cause problems when combining it with different methods for reading from the keyboard such as nextInt Given the code, Scanner keyboard = new Scanner(System.in); int n = keyboard.nextInt(); String s1 = keyboard.nextLine(); String s2 = keyboard.nextLine(); and the input, 2 Heads are better than 1 head. what are the values of n, s1, and s2? Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
26
Pitfall: Dealing with the Line Terminator, '\n'
Given the code and input on the previous slide n will be equal to "2", s1 will be equal to "", and s2 will be equal to "heads are better than" If the following results were desired instead n equal to "2", s1 equal to "heads are better than", and s2 equal to "1 head" then an extra invocation of nextLine would be needed to get rid of the end of line character ('\n') Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
27
Methods in the Class Scanner (Part 1 of 3)
Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
28
Methods in the Class Scanner (Part 2 of 3)
Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
29
Methods in the Class Scanner (Part 3 of 3)
Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
30
Java Documentation for Scanner
CSS 161: Fundamentals of Computing
31
Programming Tip: Prompt for Input
A program should always prompt the user when he or she needs to input some data: System.out.println( "Enter the number of pods followed by"); "the number of peas in a pod:"); Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
32
Programming Tip: Echo Input
Always echo all input that a program receives from the keyboard In this way a user can check that he or she has entered the input correctly Even though the input is automatically displayed as the user enters it, echoing the input may expose subtle errors (such as entering the letter "O" instead of a zero) Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
33
Self-Service Checkout Line (Part 1 of 2)
Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
34
Self-Service Checkout Line (Part 2 of 2)
Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
35
The Empty String A string can have any number of characters, including zero characters "" is the empty string When a program executes the nextLine method to read a line of text, and the user types nothing on the line but presses the Enter key, then the nextLine Method reads the empty string Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
36
Other Input Delimiters
The delimiters that separate keyboard input can be changed when using the Scanner class For example, the following code could be used to create a Scanner object and change the delimiter from whitespace to "##" Scanner keyboard2 = new Scanner(System.in); Keyboard2.useDelimiter("##"); After invocation of the useDelimiter method, "##" and not whitespace will be the only input delimiter for the input object keyboard2 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
37
Changing the Input Delimiter (Part 1 of 3)
Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
38
Changing the Input Delimiter (Part 2 of 3)
Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
39
Changing the Input Delimiter (Part 3 of 3)
Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
40
Next Time Lab 2 (UW1-120) Don’t forget: Lab 1 due Friday, 11am
Assignment 1 due Monday, 11am
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.