Skewed Associative Caches with Victim Caches
Download
Report
Transcript Skewed Associative Caches with Victim Caches
Java Input and Output
Java Input
Input is any information needed by your
program to complete its execution
So far we have been using InputBox for this
Examples of other Input:
Direct keyboard input
Mouse
Network
Disk
Microphone
…
Java Output
Output is any information that the program
must convey to the user
So far we have been using OutputBox and
System.out.println()
Examples of other Output:
printer
Network
Disk
Speakers
…
Java I/O
We have been using the javabook2 package
because its easier
Its easier because it eliminates a lot of errors:
It is also easier because it is simple
Ex. – NumberFormatException occurs if user enters
letters when a number is expected
getInteger() checks for this
Getting numbers, Strings, etc. is straightforward
These actions become much more complex without
javabook
Standard java classes are not as simple
because they offer added flexibility. Flexibility
comes at the cost of increasing complexity.
Java Console Input
Remember Console window is the
black window that is automatically
launched when you run your program
We haven’t used this to get input so far
There are three parts to getting
console input:
Prompt the user
Get this input
Convert the input
Java Console Input
Prompt the user
The user must be
told to enter
information
The user will only
know what type of
information to enter
if you tell them
Java Console Input
Get the input:
Several standard classes are helpful
java.io.InputStream – Stores information about the
connection between an input device and your program
java.io.InputStreamReader – used to translate data bytes
received from InputStream objects into a stream of
characters
Java.io.BufferedReader – used to buffer (store) input
received from an InputStreamReader object (stores input
as strings).
Java Console Input
None of these classes has a
method as simple as getString
BufferedReader has readLine()
which returns a string
So let’s make a BufferedReader:
Look at the constructor online
We need a Reader object
Java Console Input
InputStreamReader extends
the Reader class
Extends means:
Robin extends bird
Robin is a bird,
bird is not necessarily a robin
If you need a bird, a robin will do
We will use an InputStreamReader for our
reader (Because we want an InputStream)
Look at constructor for InputStreamReader
We need an InputStream object
Java Console Input
The System class in the java.lang package
(which is automatically imported)
automatically creates an InputStream object
for us called in
We can access it with System.in
This InputStream is connected to the
keyboard
We can use it to create our
InputStreamReader object, which we can
use to create our BufferedReader object
Java Console Input
Put it all together:
1. Use System.in to create an
InputStreamReader object
2. Use the InputStreamReader object to
create a BufferedReader object
3. Display a prompt to the user for the
desired data
4. Use the BufferedReader object to read a
line of text from the user
5. Convert/use the input received
Java Console Input
// 1. Use System.in to create an InputStreamReader object
InputStreamReader isr = new InputStreamReader(System.in);
// 2. Use the InputStreamReader object to create a
//
BufferedReader object
BufferedReader stdin = new BufferedReader(isr);
// 3. Display a prompt to the user for the desired data
System.out.print(“Johnny 5 needs more input:”);
// 4. Use the BufferedReader object to read a line of text from the user
String input = stdin.readLine();
// 5. Convert/use the input received
System.out.println(“You typed: “ + input);
Java Console Input
Questions??
Java Console Input
// 1. Use System.in to create an InputStreamReader object
InputStreamReader isr = new InputStreamReader(System.in);
// 2. Use the InputStreamReader object to create a
//
BufferedReader object
BufferedReader stdin = new BufferedReader(isr);
// 3. Display a prompt to the user for the desired data
System.out.print(“Johnny 5 needs more input:”);
// 4. Use the BufferedReader object to read a line of text from the user
String input = stdin.readLine();
// 5. Convert/use the input received
System.out.println(“You typed: “ + input);
Java Console Input
Don’t forget to import java.io.*;
Shorter version:
BufferedReader stdin = new BufferedReader(
new InputStreamReader (System.in) );
Java Console Input
Converting input is
sometimes necessary:
String input = stdin.readLine();
// user enters “123”
int number = Integer.parseInt(input);
This can cause Exceptions
Look at the Integer class online
Java Console Input
Questoins??
Java Console Output
We have used System.out.println()
as an alternative to OutbutBox
and Message box of javabook2
This console output displays a
String of characters
System.out is an instance of the
PrintStream class
Java Console Output
A Stream object is used to store information
needed to connect a computer program to
an input or output device
Just like a Reader object adds functionality
to an InputStream, a Printer object adds
functionality to an OutputStream
Console output is easy in java because
printer methods (print and println) can
handle many types of input
Java Console Output
int x = 3;
char a = ‘a’;
boolean r = true;
String phrase = “Cat’s meow”;
System.out.println(x);
System.out.println(a);
System.out.println(r);
System.out.println(phrase);
java.lang.System
java.lang.System automatically
creates three streams for your
program:
System.in - InputStream
System.out - PrintStream
System.err - PrintStream
Java Console Output
Questions????
Printing objects
In java, you can print anything:
Wanderer w = new Wanderer(“Gil”,Color.green);
System.out.println(w)
This prints:
Wanderer@13fac
This is a memory location (it is not
very useful)
Printing objects
In java, all objects have a toString( ) method
It is inherited from the Object object
You can override it by writing your own
toString( ) for your class
public String toString( ){
String coords = “(“ + myLoc.getX() + “,” +
myLoc.getY() + “)”;
return myName + “ is at “ + coords;
}
Printing objects
Now,
Wanderer w = new Wanderer(“Gil”,Color.green);
System.out.println(w)
Will print:
Gil is at (11,9)
This is Useful!
It can help you display output
easily and debug your program