|
Communication between humans and computers are facilitated by special input and output devices
|
|
I will discuss the communication methodology using the key board and terminal as examples (see below)
|
I will now illustrate the communication methodology using the key board and terminal as examples
|
|
|
Explanation:
|
I like to give you an evidence that the key board transmits ASCII codes to the computer
Your Java programs would contain these lines:
import java.util.Scanner; // Import class from Java library ... Scanner in = new Scanner( System.in ); // Create Scanner object using the System.in object !! |
The System.in object in Java is associated with the key board.
The reason that you use System.in to create a Scanner object is so that the Scanner object will read from the key board
(You may have learned to use Scanner on an File object to read a data file.
Just than you know that: whatever object you use to create the Scanner object, it will read input from that object.)
The Scanner object contains methods to convert ASCII representation to 2's complement representation.
So what we do now is show you Java input without using Scanner:
|
// File: /home/cs255001/demo/atoi/JavaInput_1.java // // Demo reading input WITHOUT using Scanner utility class !!!! // public class JavaInput_1 { public static void main(String[] args) throws Exception { byte[] b = new byte[100]; int count; while ( true ) { System.out.print("Enter input: "); count = System.in.read(b); // Read from key board System.out.println("# characters read = " + count); for (int i = 0; i < count; i++) System.out.println(b[i]); System.out.println("\n"); } } } |
The method call System.in.read(b) will read an input line (until you hit <RETURN> key) and store it in the byte[] array b,
The method System.in.read(b) returns the number of characters in the input.
cheung@aruba> java JavaInput_1 Enter input: 12 # characters read = 3 49 50 10 Enter input: 123 # characters read = 4 49 50 51 10 |
When you enter 12<ENTER>, the input values are: 49 50 10 because:
49 is the ASCII code for the character '1' 50 is the ASCII code for the character '2' 10 is the ASCII code for the character NEWLINE (ENTER) |
(This fact is easier to explain with a live demo (where I point out to you what to look for and where to look at) and I will demo this in class - so if you don't come to class, you will miss out on some stuff even most of the material are in webpages)
In other words: when we want to print text messages to a terminal, the output context is always ASCII code
Try doing this experiment:
|
Note: what is actually happening is the following:
|
How the computer "communicate" 01001000 ('H') to a human:
|
use
|
Text information Representation in ASCII code (binary) --------------------+------------------------------------------------ H 01001000 He 01001000 01100101 Hello 01001000 01100101 01101100 01101100 01101111 |
But also:
Text information Representation in ASCII code (binary) --------------------+------------------------------------------------ 1 00110001 12 00110001 00110010 123 00110001 00110010 00110011 |
Try it for yourself:
|