Presentation is loading. Please wait.

Presentation is loading. Please wait.

©2004 Brooks/Cole Assignment and Interactive Input Java Shorthand Statements Mathematical Methods Conversion Methods Interactive Keyboard Input Interactive.

Similar presentations


Presentation on theme: "©2004 Brooks/Cole Assignment and Interactive Input Java Shorthand Statements Mathematical Methods Conversion Methods Interactive Keyboard Input Interactive."— Presentation transcript:

1 ©2004 Brooks/Cole Assignment and Interactive Input Java Shorthand Statements Mathematical Methods Conversion Methods Interactive Keyboard Input Interactive Dialog Input Final Qualifier

2 ©2004 Brooks/Cole Java Shorthand Accumlator type –totalInvoice = totalInvoice + itemPrice; totalInvoice += itemPrice; –Can use +=, -=, *=, /=, %= If same variable name will be on both side of assignment statement

3 ©2004 Brooks/Cole Java Shorthand Counters Special case when counter is incremented or decremented by 1 count = count + 1; –Can be replaced with Count++; –Can be used in assignment statements k = ++count; Means increment count first and then move the value to k

4 ©2004 Brooks/Cole More Java Counter Shorthand count = count - 1 –Count-- Prefix and postfix –Important when used in assignment statements k = ++n –n = n + 1 –k = n k = n++ –k = n –n = n + 1

5 ©2004 Brooks/Cole Mathmatical Methods Math.abs(x) Math.pow(x1,x2) Math.sqrt(x) Math.ceil(x) Math.floor(x) Math.min(x,y) Math.max(x,y) Math.round(x) Math.random(x)

6 ©2004 Brooks/Cole Figure 3.7: Using and Passing Data to a Math Class Method

7 ©2004 Brooks/Cole Mixed mode arithmetic and Casts { int a = 10; float b = 20.0f; double answer; int answer2; answer = a / b; // OK since double holds answer2 = a / b;// Generates an error answer2 = (int) (a / b); //OK since (int)

8 ©2004 Brooks/Cole Figure 3.8: Conversions Using a Wrapper Class Method

9 ©2004 Brooks/Cole Wrapper Class Conversion Routines All Start with desired class parseInt(string) –Integer.parseInt("1234") parseLong(string) –Long.parseLong("12345678") parseFloat(string) –Float.parseFloat("12.34") parseDouble(string) –Double.parseDouble("12.34") Method name of all is toString() toString(x) –Interger.toString(123) toString(x) –Long.toString(123455) toString(x) –Float.toString(12.34) toString(x) –Double.toString(12.34)

10 ©2004 Brooks/Cole Figure 3.9: System.in Is Used to Enter Data; System.out Is Used to Display Data

11 ©2004 Brooks/Cole Getting Input from the Keyboard InputStreamread()1 keystroke at a time System.in.read(); InputStreamReader used to convert from integer to string BufferedReaderreadLine() Retruns the characters typed at the keyboard as a string br.readline(); The read() method not much use until we learn more. EOF value needed (Ctrl Z)

12 ©2004 Brooks/Cole Figure 3.10: Generating the EOF Value

13 ©2004 Brooks/Cole Figure 3.11: The Required Processing Using System.in.read()

14 ©2004 Brooks/Cole Required Statements for using Keyboard import java.io.*; InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); throws java.io.IOException –after the main method line

15 ©2004 Brooks/Cole Using Keyboard input To get input from keyboard, define at least 1 string variable –string s1; Display a prompt message to the end user –System.out.print("prompt message here"); s1 = br.readLine() –If the Fact is a string fact you can now use it –If the fact is a numeric fact you must now convert it using the proper Wrapper Class.

16 ©2004 Brooks/Cole Figure 3.12: A String Consisting of Three Tokens

17 ©2004 Brooks/Cole Figure 3.13: Parsing Tokens from a String

18 ©2004 Brooks/Cole Figure 3.14: A NumberFormatException Notification

19 ©2004 Brooks/Cole Figure 3.15: A Sample showInputDialog() Dialog

20 ©2004 Brooks/Cole Figure 3.16: The First Dialog After Data Are Entered

21 ©2004 Brooks/Cole Figure 3.17: The Second Dialog After Data Are Entered

22 ©2004 Brooks/Cole Figure 3.18: A Sample Output Produced by Program 3.13

23 ©2004 Brooks/Cole Figure 3.19: The Input Dialog Created by Program 3.14

24 ©2004 Brooks/Cole Figure 3.20: A Sample Output Produced by Program 3.14

25 ©2004 Brooks/Cole Figure 3.21: Result of Catching the NumberFormatException Exception

26 ©2004 Brooks/Cole Figure 3.22: Result of Catching the NullPointerException Exception

27 ©2004 Brooks/Cole Final Qualifier Variable means changeable but sometimes values we want to use in our programs are really constants. –pi - 3.1416 –Months per year - 12 –And many other and we do not want the program to change we add the 'final' qualifier final double SALESTAX = 0.0825;// convention says // 'final' type constants should be type all uppercase

28 ©2004 Brooks/Cole Next week Chapter 4 Relational Operators and Decision Statements Beginning of repetition logic Assignment 2 available - due 4/4

29 ©2004 Brooks/Cole Placement of Constant definitions Up to now, we have defined everything within the main method of our program. It does restrict the useage of the value to the method it is found in.


Download ppt "©2004 Brooks/Cole Assignment and Interactive Input Java Shorthand Statements Mathematical Methods Conversion Methods Interactive Keyboard Input Interactive."

Similar presentations


Ads by Google