190 likes | 572 Views
?: operator. ex 1: max = (i > j) ? i : j; same as if-else aboveexpression returns one of 2 values i or j based on boolean condition (i > j).If condition is true, returns i else returns jex2: ovHours = (hours > 40) ? (hours - 40 ) : 0;. Java I/O ? Streams. Consistent usageInput: key
E N D
1. OOP: Java Overview ( java-2.ppt) Operators
Applications : I/O Streams, Console, Swing Dialogues
Introduction to arrays
Arrays of String objects
Javadoc
GUI applications
2. ?: operator ex 1:
max = (i > j) ? i : j;
same as if-else above
expression returns one of 2 values i or j based on boolean condition (i > j).
If condition is true, returns i else returns j
ex2:
ovHours = (hours > 40) ? (hours - 40 ) : 0;
3. Java I/O Streams Consistent usage
Input: keyboard, file, network connection
Output: monitor, file, network connection
Standard predefined streams
System.In - keyboard
System.Out monitor
Note: Gittleman text uses different technique to handle Java I/O
javax.swing package for input dialogue
Ex1: CalculatePay.java p.71
EX2: Temperature.java p. 84 (shows formatting)
4. I/O Through Objects online Java tutorial uses generic I/Othrough streams
In program must 1st create objects for I/O
To create I/O objects, Java uses predefined class library components
For efficiency, convert byte streams to buffered String objects
Well use:
stdIn (for keyboard) .. Like cin in C++
stdOut (for monitor) .. Like cout in C++
stdErr (for prompts & error msg) .. Like cerr in C++
Usage:
stdOut.println(Hello World!);
name = stdIn.readLine();
System.Err -used for prompts & error messages
System is a predefined class
5. Redefine standard I/O streams
Use BufferedReader & PrintWriter classes since they process a string of characters. (Defined in java.io package)
Redefine the standard input stream (System.in) as object stdIn
We can now use the predefined readline() method to store a string.
String name = stdIn.readline();
Redefine the standard output stream(System.out) as object stdOut
Use this code in your Java applications:
private static BufferedReader stdIn = new BufferedReader
(new InputStreamReader(System.in));
private static PrintWriter stdOut = new PrintWriter(System.out, true);
6. Demo: First I/O Program // FirstIO.java - uses standard I/O streams
import java.io.*;
public class FirstIO {
private static BufferedReader stdIn = new BufferedReader
(new InputStreamReader(System.in));
private static PrintWriter stdOut = new PrintWriter(System.out, true);
public static void main (String[] args) throws IOException {
stdOut.println ("Enter your name: ");
String name = stdIn.readLine();
stdOut.println ("Your name is: " + name);
}
}
7. Wrappers & Numeric Input // Age.java Numeric input through I/O streams
import java.io.*;
public class Age {
private static BufferedReader stdIn = new BufferedReader
(new InputStreamReader(System.in));
private static PrintWriter stdOut = new PrintWriter(System.out, true);
public static void main (String[] args) throws IOException {
int age; // local variable
stdOut.println ("Enter your age: ");
age = Integer.parseInt (stdIn.readLine());
++age;
stdOut.println ("Next year, you will be " + age);
}
}
8. I/O Buffers For efficiency, input and output data is buffered and processed when the buffer fills up
You explicitly flush the output buffer with a method called flush.
println() automatically flushes the output buffer
print() does not
9. Swing Dialogues for I/O Console application can use Swing package for
Recent Java addition
Uses a JOptionPane predefined class
Use import javax.swing.JOptionPane to import JOptionOption class
Use import javax.swing.* to import all swing classes
Uses showInputDialog & ShowMessageDialogue methods
Technique used in Gittleman Text
10. Swing Dialogues for I/O // Use Swing package for I/Oimport javax.swing.JOptionPane;public class Add { public static void main(String[] args) { int n1, n2; String input; input = JOptionPane.showInputDialog("Enter the first number"); n1 = Integer.parseInt(input); input = JOptionPane.showInputDialog("Enter the second number"); n2 = Integer.parseInt(input); JOptionPane.showMessageDialog(null,"The result is " + (n1 + n2)); }}
11. Arrays An array is an indexed collection of data of the same type
Each cell has a value and an index (index always start at zero!)
For n elements, index runs from 0 to (n-1)
Array declaration requires array type and use of bracket operator []
type varName[];
Need to allocate memory for the cells
int[] scores = new int[10];
char[] vowels = new char[5];
12. Storing Values in the Array We can assign values when we declare the array using an initializer list
int [] scores = {100, 99, 95, 100, 80};
(this declares an array of size 5)
or by assigning the values directly to the cells
final int SIZE = 100;
int[] scores = new int[SIZE];
for(int k = 0; k != SIZE; k++)
scores[k] = 10 * k;
13. Arrays of Primitives or Objects Array types can be primitives or objects (Strings for example)
float [] scores; // reference, not scalars !!
char[] vowels;
String[] names; // object types
Integer[] arr; // object type
14. Arrays in Java An array in Java is an object
It is always passed as reference, not value
it has a field named length:
int size = arr.length;
15. Static & Dynamic Arrays In general, arrays are static (size is fixed and predetermine)
Its possible to have a variable to control the size of the array
int size;
size = Integer.parseInt(stdIn.readLine());
int[] numbers = new int [size];
16. Arrays Array reference vs creating array itself
Create a ref to array, then create array object with new operator
2 step declaration
int[] a; // declares a ref to an int type object
a = new int[5]; // declares the actual array object and its size
1 step declaration: ==> int[] a = new int[5];
17. Array Code Examples Primitives data input, search, passing arrays
ArraySlots
MaxScore
ArrayInputAvg
PassArray
TryArray
String Objects
SearchTester
PhoneBookTester
RollCall @ BYU JavaHelp site
18. Javadoc Automatic documentation generator
creates an HTML document
format
/**
* @version
* @author
* @see
* @param
* @return
*
*/
See Circle javadoc example in my Code Examples Web Page
19. GUI applications Eliminate HTML page that invokes applet
Derive the new class from Frame, implement WindowListener
Constructor in new class instead of init()
Add main method to invoke the application
Set the windows width and height
See 1.1.5 AwtApplication, CustomFrame