Java Reference
In-Depth Information
public class
Keyboard {
Bus bus;
public
Keyboard(Bus bus) {
this
.bus
#
bus;
}
BufferedReader lineReader
#
new
BufferedReader(
new
InputStreamReader( System.in ) );
void
execute() {
if
(bus.command.equals(Bus.INPUT)) {
try
{
System.out.print("> ");
bus.data
#
lineReader.readLine();
}
catch
(IOException ioe) {
ioe.printStackTrace();
bus.data
#
"";
}
bus.command
#
Bus.ACK;
}
}
}
The simplest class is
Display
. Its
execute()
method waits for the
OUTPUT
command, then prints the string lying on the data bus to the default output
and acknowledges the command.
public class
Display {
Bus bus;
public
Display(Bus bus) {
this
.bus
#
bus;
}
void
execute() {
if
(bus.command.equals(Bus.OUTPUT)) {
System.out.println("\n> "
!
bus.data);
bus.command
#
Bus.ACK;
}
}
}
The
HardDisk
class is similar to the
Keyboard
class in that it is based on a
BufferedReader
. In addition to a command to read a line from the reader, it
recognizes two other commands: one initializes the reader to read from a
file, the other closes the file. The
execute()
method recognizes the commands
FOPEN
,
FCLOSE
and
FREAD
. At each operation it checks for exceptions; if
an exception is caught it prints an error message and acknowledges the
command to avoid deadlocks.
public class
HardDisk {
Bus bus;