Download Fig 3-1.jpg

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Assignment and Interactive Input
•
•
•
•
•
•
Java Shorthand Statements
Mathematical Methods
Conversion Methods
Interactive Keyboard Input
Interactive Dialog Input
Final Qualifier
©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
©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
©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
©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)
©2004 Brooks/Cole
Using and Passing Data to
a Math Class Method
Figure 3.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)
©2004 Brooks/Cole
Figure 3.8:
Conversions Using a Wrapper
Class Method
©2004 Brooks/Cole
Wrapper Class Conversion Routines
• All Start with desired class
• Method name of all is toString()
• parseInt(string)
• toString(x)
– Integer.parseInt("1234")
• parseLong(string)
– Long.parseLong("12345678")
• parseFloat(string)
– Float.parseFloat("12.34")
• parseDouble(string)
– Double.parseDouble("12.34")
– Interger.toString(123)
• toString(x)
– Long.toString(123455)
• toString(x)
– Float.toString(12.34)
• toString(x)
– Double.toString(12.34)
©2004 Brooks/Cole
System.in Is Used to Enter Data;
System.out Is Used to Display Data
Figure 3.9:
©2004 Brooks/Cole
Getting Input from the Keyboard
• InputStream
read() 1 keystroke at a time
• System.in.read();
• InputStreamReader
used to convert from
integer to string
• BufferedReader
readLine() 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)
©2004 Brooks/Cole
Figure 3.10:
Generating the EOF Value
©2004 Brooks/Cole
Figure 3.11:
The Required
Processing Using
System.in.read()
©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
©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.
©2004 Brooks/Cole
Figure 3.12:
A String Consisting of Three Tokens
©2004 Brooks/Cole
Figure 3.13:
Parsing Tokens
from a String
©2004 Brooks/Cole
Figure 3.14:
A NumberFormatException
Notification
©2004 Brooks/Cole
Figure 3.15:
A Sample showInputDialog() Dialog
©2004 Brooks/Cole
Figure 3.16:
The First Dialog After Data Are Entered
©2004 Brooks/Cole
Figure 3.17:
The Second Dialog After Data
Are Entered
©2004 Brooks/Cole
Figure 3.18:
A Sample Output Produced by
Program 3.13
©2004 Brooks/Cole
Figure 3.19:
The Input Dialog Created by
Program 3.14
©2004 Brooks/Cole
Figure 3.20:
A Sample Output Produced by
Program 3.14
©2004 Brooks/Cole
Result of Catching the
NumberFormatException Exception
Figure 3.21:
©2004 Brooks/Cole
Result of Catching the
NullPointerException Exception
Figure 3.22:
©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
©2004 Brooks/Cole
Next week
• Chapter 4 Relational Operators and
Decision Statements
• Beginning of repetition logic
• Assignment 2 available - due 4/4
©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.
©2004 Brooks/Cole