An Introduc+on to Computers and Java CSC 121 Spring 2017 Howard Rosenthal
Lesson Goals Learn the basic terminology of a computer system Understand the basics of high level languages, including Java Understand algorithms Understand how numbers are represented in a computer Binary Arithmetic Two s Complement Understand how Unicode works and represents characters in a computer 2
What Is A Computer? Basic Definitions A computer is a machine that performs computations, logical operations, data manipulation, etc. according to some sequence of instructions called a program. Today pretty much all computers are electronic. Hardware and software working in tandem comprise a computer system. Can you name some examples of computers? 3
The Rapid Evolu8on of Computers Essentially the computer industry, much like television, took off after WWII transistors and integrated circuits still didn t exist in 1945 Into the 1970s most computing involved having a textual workstation connected to a close by computer once you got past the age of punch cards In the 1980 s Ken Olsen, the head of an extremely successful computer company Digital stated that he saw no need for anyone to have a computer in their home At the start of 1990 s PCs stood alone no networking to the home During the 1990 s Seinfeld episodes featured George waiting for a phone booth. In the late 1990s Best Buy still had a large CD section! Google only started in 1995! Today we are completely integrated into a computerized, networked world. We do our research, make purchases, talk on the phone, text incessantly. All because of computers. See the timelines and history of computer in the reference files Bottom line This is a great field to be in a good job and excellent pay awaits those who succeed 4
The Basic Components of A Computer (1) The four basic elements of a computer are: The Central Processing Unit The CPU Primary or Random Access Memory (RAM) Secondary or long term memory Note: some of these devices are becoming closer to RAM devices as they are built with solid state circuitry as opposed to spinning electro mechanical parts Input and output devices disks printers, etc. 5
The Basic Components of A Computer (2) 6
The Basic Components of A Computer (3) (ALU) (PLU) 7
The CPU The CPU The CPU Program Control Unit (PCU) The PCU takes the instructions, interprets them, and decides how to handle them. It determines what all the other parts of the CPU do. It is basically the Coordinator for everything that happens in the CPU. It contains the instruction Registers, and program counter Arithmetic Logic Unit (ALU) - This is where all the calculations are actually performed. The Arithmetic part is where all the operations like ADD, SUBTRACT, MULTIPLY, and DIVIDE take place. The Logic part is where numbers are compared through functions such as Equalto, Less-Than, greater-than, Not equal to. The CPU's General purpose Registers and Accumulator are also found here. The Clock synchronizes all the activities in the CPU, and allows instructions to be staged and pipelined through the CPU 8
The Memory Memory Unit - The parts that make up the Memory Unit are the read only memory (ROM) and random access memory (RAM). ROM is a special type of memory in which data is written onto a chip during manufacturing. Information stored to ROM is permanent and cannot be changed. The System ROM stores the BIOS - Basic Input/Output System, which is the set of instructions that a computer uses during the first stages of power up and initialization. Without the BIOS, the computer would not have a way to verify that the main hardware components, such as the hard drive, floppy drive, mouse, CD-ROM etc., are installed and working properly. The BIOS also includes Bootstrapping, allowing the BIOS to look for and start the loading of the operating system from the selected location. RAM, often referred to as main memory, or Primary Memory or Read-Write Memory, is a temporary form of memory that the computer uses as a work area. This type of memory is dynamic, meaning that it is constantly changing because of the activity of the CPU. When you shut off the power to the computer, RAM loses everything stored in it the memory is volatile. RAM stores program instructions and related data for the CPU to access quickly without the need for extracting the data from a much slower form of types of permanent storage, such as the Hard Disk. Generally, the closer the memory is to the CPU's ALU and CU, the faster the memory will be. As a result, it will be the most expensive, and the most limited type of memory in terms of availability and size 9
Input and Output Input - Basically where information is passed to the CPU. This is from devices like the keyboard, mouse, etc. Can also include scanners, readers, thumb drives, external sources Output - Basically where information is presented from the CPU. This is to devices like your Monitor, Printers, etc. Permanent Secondary Storage - Where the information is kept and stored persistently. Data doesn t disappear when the power is shut off. While RAM has typically been both the fastest and most expensive the gap between RAM and secondary storage is shrinking as technology advances Solid state disk (SSD) storage is becoming cheaper and more popular With fewer moving parts it is typically more reliable than spinning electromechanical disks Apple offers Fusion drives that combine the two technologies at a lower cost that a pure SSD 10
Addressing Data In A Computer Data is stored by memory addresses 64 bit machines an have 2 64 bytes of memory We often deal with the addresses, known as references, rather than with the data itself this will be a key idea as we move forward with Java and objectoriented programming The memory contents are always some sort of binary representation What the representation means depends on the type of data being represented Memory Address Memory Contents 000 00001001 (9) 001 00010001 (17) 010 01111111 (127) 011 100 101 00100100 (36) 110 111 11
How is Data Stored In A Computer Computers today store information in binary format with 0 s and 1 s that correspond to off and on this is currently the easiest way to build a digital computer. Each binary digit is called a bit. A string of 8 bits is typically called a byte. Bytes are typically organized into 32 bit words. Data types can be 1 2 4 or 8 bytes long, depending on the type (Further explained in Chapter 2) The byte is the lowest addressable unit of information in a computer. Similar to the idea that we address to homes or apartments, but not to rooms within a home or apartment 12
Basic Concepts In Computer SoJware Operating System Software that controls all aspects of the computer DOS, Windows, MAC OS, UNIX, etc. Languages and levels of abstraction Machine Language expressed in 0 s and 1 s. This is what the machine understands. A long time ago programmers coded in 0 s and 1 s. It was very hard to write complex programs. Assembly Language A low-level programming language in which a mnemonic is used to represent each of the machine language instructions for a particular computer Assembly Language Machine Language ADD 00100101 SUB 00010011 Assembler The program that translates Assembly Language into Machine Language Higher Level Languages Used to write programs in a more understandable form. Examples include Fortran, C, C++, LISP, Java Layered Architectures An approach that allows application programmers to take advantage of software written by others i.e. IO drivers, databases, user interfaces, etc. in order to make the life of the application writer easier 13
A Layered Computer Architecture 14
Basic Concepts In Computer SoJware Object Oriented Programming Original higher level languages were procedural Good for solving math problems Little relationship between the data and the operations Tended to do poorly with higher levels of abstraction Object Oriented Programming Organizes data and operations/processes together into an object. At the higher level of abstraction we have the Class which is a description of an object that specifies the types of data values that it can hold and the operations that it can perform A class is a higher form of a data type It encapsulates data and methods together Classes are instantiated into individual objects More about this starting in Chapter 3. 15
Basic Concepts In Computer SoJware Compilers and Interpreters Compilers Compilers translate source code written in a programming language into machine language. Each language has it s own compiler(s) or interpreters Compilers are not machine independent. Therefore the source code often needed to be tailored based on the capabilities of the targeted machines. And the output of the compiler was also machine specific Java has created an approach that completely separates the source code from the target machine, so the source code can be compiled independently of the machine Source code - Data type specifications and instructions written in a high-level programming language Object code - A machine language version of source code In addition to compilers some computer languages use interpreters Languages are still at a higher level Execute directly line by line from the source code Since interpretation is in real-time some static checking and reliability may be lost Slower than directly executing compiled code Python, Smalltalk, Lisp, some forms of Basic, some scripting languages, etc. 16
Original Compiler Concept Originally separate compilers were written for each machine These compilers produced machine language or assembly language code for the individual machines Assembly language code would go through a second compilation step to produce machine language code SOURCE CODE (C++) COMPUTER EXECUTES TRANSLATOR CODE (COMPILER) OBJECT CODE (MACHINE LANGUAGE VERSION OF SOURCE CODE) COMPUTER EXECUTES OBJECT CODE Windows PC C++ compiler Windows PC machine language Windows PC computer C++ Code UNIX workstation C++ compiler UNIX workstation machine language UNIX workstation computer Macintosh C++ compiler Macintosh machine language Macintosh computer Figure 1.5 High-level programming languages allow programs to be compiled on different systems 17
Java Is Machine Independent JAVA solves the problems of machine dependence. JAVA standardizes the language and the compiler, which creates bytecode a form of binary representation. The bytecode is then presented to a Java Virtual Machine (JVM) which interprets bytecode exactly the same way regardless of the machine. This is called Machine Independence. The bytecode can be thought of as the machine language of the JVM The JVM is a virtual machine in software. It has its own machine language. All Java code is compiled for the JVM However, bytecode must be interpreted in order to create machine language instructions that are executed by the physical machine Oracle typically provides the JVMs for the individual machines Source Code (.java) Java Compiler Bytecode (.class) Java Virtual Machine Machine Language Target Machine JVM takes the compiled bytecode and interprets it into the machine code 18
Syntax and Seman+cs Languages have a syntax its grammatical structure - and its semantics - which give it meaning Syntax is the study of the principles and rules for constructing sentences in natural languages. Word order, parts of speech, types of sentences, etc. are studied in syntax. Semantics is the study of meaning. It focuses on the relation between signifiers, such as words, phrases, signs and symbols, and what they stand for Elements like ambiguity, the relationship between words and sentences based on meaning are studied in language Can you write programs that are syntactically correct and yet work incorrectly? Examples I am a boy. grammatically correct and means something I boy. syntactically incorrect but does convey meaning I am a building. Syntactically correct but is meaningless. Syntax varies by language. We need to learn the syntax of the computer language since the computer is not all that adaptable (unless we write programs to teach them that adaptability). Most object-oriented languages follow a similar set of principles in implementing the programming language, but there are differences in syntax and even philosophy 19
Algorithms An algorithm is a step by step procedure for accomplishing a task or solving a problem describing The actions to be performed The order in which those actions are performed All computer programs require the use of algorithms A simple algorithm find the greatest of three numbers A complex algorithm find the shortest route between two points on a map Most programs: Read in data (input) Process the data with algorithms Generate outputs Programs that do not have any inputs are likely to generate the same answers over and over (there are exceptions when using random number generators) In this class you will be writing algorithms in the syntax of Java To get the correct answers you must implement the algorithms in a semantically meaningful way that accomplishes the goal of the algorithm 20
What Is The Binary System? In the normal decimal system each place indicates a different power of ten. For example: 13,472 = 1x10 4 + 3x10 3 + 4x10 2 +7x10 1 +2x10 0 Similarly in base 2 arithmetic each place has a value associated with it: One s place = 2 0 Two s place = 2 1 Fours place = 2 2 Eights place = 2 3 Sixteen s place = 2 4 So let s try a few examples with eight bits: 1 = 00000001 = 1x2 o 3 = 00000011 = 1x2 1 + 1x2 0 4 = 00000100 = 1x2 2 Now try a few on your own write 9 and 14 in binary format 21
Conver+ng From Decimal To Binary Successive division is used to convert from decimal to binary Example - convert 14 to binary 14/2 = 7 r0 7/2 = 3 r1 3/2 = 1 r1 ½ = 0 r 1 1110 = 14 Try using this algorithm to find the binary value for 47 22
Adding and Subtrac+ng in Binary It s the same as normal adding and subtracting in the decimal system but you only use o s and 1 s 00100110 = 38 + 00110111 = 55 01011101 = 93 01101100 = 108-00010101 = 21 01010111= 87 23
Expressing Nega+ve Numbers Java uses a method called two s complement to express negative numbers. If the leftmost bit is a one it means that you are looking a negative number We don t use the left most bit to simply indicate a negative then we d have two representations of 0 There is a three step algorithm to create a negative binary representation Step 1 write the decimal number as a positive binary number Step 2 toggle all the digits Step 3 add 1 So lets look at the 8 bit representation of -95 Step 1: 95 = 01011111 Step 2: Toggle: 10100000 Step 3: Add 1: 10100001 Note the 1 in the leftmost bit Another approach the two s complement of a number x is 2 n -x In the example above 128-95 = 33 which is what the right most 7 bits equals Some explanations use n= 8 and show that 256-95 = 161. That is the value of all 8 digits Question How large an integer do you think you can store in 8 bit integer if negatives are represented with a two s complement 2 7-1 or 01111111 = 127 The smallest is -128. Why? See the two s complement tutorial under Reference Material - Twos Complement Tutorial.pdf 24
Using Two s Complement to Subtract by Adding With two s complement you can subtract by adding a negative number Let s subtract 95 from 60 00111100 = 60 +10100001 = -95 (in two s complement) 11011101 a two s complement negative 00100010 toggle 00100011 =35 (added one) So the two s complement value was -35, exactly what we wanted it to be. This approach allows you to simplify your architecture, since subtraction is done by adding a negative number. What is the two s complement of a two s complement If the original number is x The two s complement is 2 n -x It s two s complement is 2 n -(2 n -x) = x 25
Unicode Unicode is a way of representing individual characters It actually uses two bytes per character, unlike the ASCII system which uses the 7 left most bits of a single byte, although the whole byte is used ASCII representation with extra zeroes to the left are used in Unicode Basic Unicode allows us to represent many more characters 2 16 as opposed to 27 characters in ASCII This became necessary to support many more data sets, symbols, other languages Java use Unicode for character representations There is now a Supplementary 8 byte Unicode which further extends the number of characters, but we won t worry about this Pages A-3 and A-4 of the textbook shows the ASCII character set Unicode includes ASCII characters as a subset the first 128 characters are the same You can find the complete character set at http://unicode-table.com/en/ Note: Do you understand the difference between Bytecode and Unicode? 26
Extra Class Problems Express 37 as a binary with 8 bits Add 00010011 and 00111111 Subtract 47 from 83 using 8 bit binary representations Express -43 as in 8-bit binary using two s-complement 27