Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Computer: Central Processing Unit (CPU) Computers, Programs, and Java • Retrieves instructions (from memory) and executes them • Built on a small silicon semiconductor chip that contains millions of transistors (tiny electric switches) for processing information. CSE114: Computer Science I Spring 2017 • Multiple parts (i.e., cores) that read and C HEN -W EI WANG execute instructions independently. 3 of 16 What is a Computer? Computer: Storing Information Bus Storage Devices Memory CPU e.g., Disk, CD, and Tape • A computer is really a series of switches: each switch can be in Communication Devices Input Devices Output Devices e.g., Modem, and NIC e.g., Keyboard, Mouse e.g., Monitor, Printer A computer includes both: • Hardware visible, physical, tangible (peripheral) devices repeatedly and efficiently executes given instructions • Software invisible, abstract, intangible task-control instructions reflects programmers’ intelligence Does the notion of stupid computer really make sense? 2 of 16 an on state or an off state. • The possible states of a switch are called a bit. • Information is stored as data by being encoded into a sequence of on-off switches. Q. # of possibilities that can be stored in n bits (n 1)? 2n Q. # of bits required to store n data items (n > 0)? dlog2 ne Data of various kinds, e.g., numbers and characters, are encoded as a series of bits. As a programmer, you need not worry about the encoding scheme. e.g., In an ASCII Table, we use 7 bits to encode 128 characters. • Basic unit of computer storage is a byte (equivalent to 8 bits). Kilobyte (KB); Megabyte (MB); Gigabyte (GB); Terabyte (TB) 4 of 16 Computer: Memory Illustrated Memory address Memory content . . . . . . 2000 01001010 Encoding for character ‘J’ 2001 01100001 Encoding for character ‘a’ 2002 01110110 Encoding for character ‘v’ 2003 01100001 Encoding for character ‘a’ 2004 00000011 Encoding for number 3 Programming Languages (1) • What computers read is difficult for humans, and vice versa. Computers are good at processing machine language (0s and 1s). Human beings are good at abstract thinking for problem solving. • Assembly language is a big step forward for humans to specify steps of primitive instructions (e.g., memory loads/stores, arithmetic operations, etc.). Say $t0, $t1, $t2, $n, $i are addresses; $n stores value N: lw mult lw mult add sw $t0, $t0, $t1, $t1, $t2, $t2, $n $t0, $t0 $n $t1, 3 $t0, $t1 $i # # # # # # fetch store fetch store store store N, store in $t0 N*N in $t0 N, store in $t1 3*N in $t1 N*N + 3*N in $t2 N*N + 3*N in $i Level of abstraction of the assembly is still too low for humans. The above is equivalent to a line of Java code: i = N*N + 3*N You will have fun with programming in assembly code in CSE220! 5 of 16 7 of 16 . . . . . Computer: Memory Programming Languages (2) • Like the CPU, memory is built on a silicon semiconductor chip. • A computer’s memory consists of an ordered sequence of • High-level programming language (e.g., Java) is even closer bytes for storing programs; data that the program works with. • A computer’s work area for executing a program Data may have to be loaded from external storage devices. Every byte in the memory has a unique address. • A computer’s memory is volatile: when the system’s power is to our natural way of thinking (i.e., closer to “writing an essay”). 1 2 3 4 5 Scanner keyboard = new Scanner(System.in); int weight = keyboard.nextInt(); int height = keyboard.nextInt(); int bmi = weight / (height * height); System.out.println("BMI (Body Mass Index) is: " + bmi); turned off, all information stored is lost. Need external storage devices for permanent storage. 6 of 16 8 of 16 Operating Systems Java Language Specification and API • When writing Java programs, you must follow the predefined • Each operating system is still a computer program. • Executing it regulates other programs by properly allocating resources of your computer (i.e., CPU and memory). • You will learn about OS in CSE306. 9 of 16 Why Java? Java enables users to develop and deploy applications on: • Internet servers • desktop computers • small hand-held devices 10 of 16 syntax; otherwise, it cannot be compiled into machine code for the computer to execute. • Java syntax is defined int he Java language specification: https://docs.oracle.com/javase/specs/. • Programming interface for Java applications is defined in the Java API: http://docs.oracle.com/javase/8/docs/api/. 11 of 16 Compile Time vs. Runtime • These terms refer to two stages of developing your program. • Compile time: Period where you type/compile Java code in the Eclipse editor. • Runtime: Period which occurs after you run/execute you Java code. For a piece of Java code to be executed, it must not contain any compile-time errors. 12 of 16 A Simple Java Program Beyond this lecture. . . public class Welcome { public static void main(String[] args) { /* Print a greeting message to the console. */ System.out.println("Welcome to Java!"); } } • main method: Entry point for the program execution • 3 different categories of errors: Compile time errors: • syntax error • type errors • Read Chapter 1 in the textbook. • Install Eclipse and try out the simple examples in Chapter 1. [e.g., writing 2 #$# 3] [e.g., writing "2" + 3] Runtime errors: • programs terminate abnormally [e.g., executing 10/0] Logical errors: • programs terminate normally but the results are wrong 13 of 16 [e.g., calculating (pi ⇤ r )(pi ⇤ r ) for the area of a circle of radius r] Developing Java Programs 14 of 16 15 of 16 Index (1) What is a Computer Computer: Central Processing Unit (CPU) Computer: Storing Information Computer: Memory Illustrated Computer: Memory Programming Languages (1) Programming Languages (2) Operating Systems Why Java? Java Language Specification and API Compile Time vs. Runtime A Simple Java Program Developing Java Programs Beyond this lecture. . . 16 of 16