©Silberschatz, Korth and Sudarshan1.1 Chapter 1: Introduction to Computers and Java Objects Background information hardware software computer languages compiling, interpreting and assembling Introduction to Java Object-Oriented Development Testing and Types of Errors
©Silberschatz, Korth and Sudarshan1.2 Computer Basics Computer system: hardware + software Hardware - the physical components Software - the instructions that tell the hardware what to do
©Silberschatz, Korth and Sudarshan1.3 Common Hardware Components Input device(s) mouse, keyboard, monitor, etc. Output device(s) video display, printer, etc. Memory (main & auxiliary) Processor (CPU) Input Devices (such as mouse and keyboard) Output Devices (such as video display or printer) Processor (CPU) Central Processing Unit Interprets and executes the instructions Memory main & auxiliary holds data and instructions
©Silberschatz, Korth and Sudarshan1.4 Classification of Memory At a high-level there are two types of memory: Volatile – contents are lost when power is turned off: Main memory Cache memory Fastest and most expensive form of memory, per byte Non-Volatile – contents are maintained when power is turned off: Hard drive (internal or external) CD, DVD Floppy disk Tape (still used extensively) Slowest and cheapest form of memory, per byte
©Silberschatz, Korth and Sudarshan1.5 Classification of Memory, cont. The books breakdown: Main: working area temporarily stores programs and data during program execution Also known as Random Access Memory (RAM) and also known as “primary memory” Auxiliary: permanent (more or less) saves program and results includes floppy & hard disk drives, CDs, tape, etc. also known as “secondary memory”
©Silberschatz, Korth and Sudarshan1.6 Memory Organization Bit = one binary digit, either 0 or 1 Nibble = 4 bits Byte = 8 bits Word = machine dependant, typically 4 bytes Larger groupings: (number of bytes) nameapproximationexact Kilobyte (KB)2^10 10^3 Megabyte (MB)2^20 10^6 Gigabyte (GB)2^30 10^9 Terabytes (TB)2^40 10^12 Petabyte (PB)2^50 10^15 Exabyte (EB)2^60 10^18 Zetabyte (ZB)2^70 10^21 Yottabyte (YB)2^80 10^24
©Silberschatz, Korth and Sudarshan1.7 Main Memory Organization Main memory (RAM) is byte addressable: Consists of a list of locations, each containing one byte of data. Each location has an associated “number,” which is commonly referred to as the “address” of the location. The number of bytes per data item may vary (from one item to another, and from one computer system to another).
©Silberschatz, Korth and Sudarshan1.8 Auxiliary Memory Organization (file systems for users) Note: “directory” = “folder”
©Silberschatz, Korth and Sudarshan1.9 Running (Executing) a Program Program Computer Data (input for the program) Output A (computer) program is a set of instructions for a computer to follow, or rather, execute. The term application is sometimes used to informally refer to a computer program (we will use the term more formally later).
©Silberschatz, Korth and Sudarshan1.10 Many Types of Programs System Software - Part of a computers “infrastructure,” and necessary for the system to operate: Operating Systems - DOS, Microsoft Windows, MacOS, Linux, UNIX, etc. Database Systems – Oracle, IBM DB2, SQL Server, Access Networking Software Web Servers Application Servers User Applications - Not required for the system to operate: Games Office Applications – Word, Powerpoint, Excel Web Browsers Text Editors – textedit, vi, emacs
©Silberschatz, Korth and Sudarshan1.11 Various Types of User Interfaces Command-Line: User types in commands one line at a time DOS (Start -> run -> cmd) Unix xterm GUI (Graphical User Interface) Windows, menus, buttons, sliders, etc. MacOS, Windows Sometimes also called “event-driven” interfaces Application Program Interface (API) Allows one program to communication, interact or “interface” with another ODBC, JDBC, Swing, AWT
©Silberschatz, Korth and Sudarshan1.12 Programming Language Hierarchy Hardware Machine Language Assembly Language High-Level Language (HLL)
©Silberschatz, Korth and Sudarshan1.13 The highs and lows of programming languages... High-Level Language (HLL) closest to natural language words, numbers, and math symbols multi-line statements/commands not directly understood by hardware “portable” (hardware independent) Java, C, C++, COBOL, FORTRAN, BASIC, Lisp, Ada, etc. A program in a HLL is frequently referred to as: a source program source code source file source Machine Language (lowest level) least natural language for humans most natural language for hardware just 0s and 1s directly understood by hardware not portable (hardware dependent) A program in machine language is frequently referred to as: an object program object code executable program executable code executable
©Silberschatz, Korth and Sudarshan1.14 Assembly Language (middle level) Assembly Language: a more or less human readable version of machine language words, abbreviations, letters and numbers replace 0s and 1s single-line statements/commands easily translated from human readable to machine executable code like machine code, not portable (hardware dependent)
©Silberschatz, Korth and Sudarshan1.15 Getting from Source to Machine Code “Compiling a program” - Translating a program in a high-level language to a machine code program. “Compiler” - A program that compiles programs, i.e., translates high-level language programs to machine code. “Assembling” - Translating a program in assemble language to a machine code program. “Assembler” - A program that assembles, i.e., translates assembly code programs to machine code. Compilers and assemblers need to know the specific target hardware
©Silberschatz, Korth and Sudarshan1.16 Compilers vs. Assemblers vs. Interpreters Compilers and Assemblers: translation is a separate user step from execution translation is “off-line,” i.e. not at run time entire program is translated before execution Interpreters: (another way to translate source to object code) translation is not a separate user step from execution translation is “on-line,” i.e. at run time translation and execution occur “line at a time” Compiler, Assembler, or Interpreter Source Code Object Code
©Silberschatz, Korth and Sudarshan1.17 Java Program Translation Executing a java program involves both compilation and interpretation. Java Program Translation & Execution: Step #1: A java source program is compiled; this produces a program in “Byte Code.” Similar to assembly code, but hardware independent. Step #2: An interpreter, called the Java Virtual Machine (JVM) translates the byte code program to hardware-specific machine code, and executes it (in an interpretive manner).
©Silberschatz, Korth and Sudarshan1.18 Java Program Translation Java Program Data for Java Program Java Compiler Byte-Code Program Byte-Code Interpreter Machine-Language Instructions Computer Execution of Machine-Language Instructions Output of Java Program
©Silberschatz, Korth and Sudarshan1.19 Why Use Byte Code? Question: Why not compile directly to machine code, rather than byte code? Disadvantages of Byte Code: requires both compiler and interpreter slower program execution Advantages of Byte Code: portability very important same program can run on computers of different types (useful with the Internet) a JVM (interpreter) for new types of computers can be made quickly and inexpensively, whereas a compiler cannot; only one compiler is needed.
©Silberschatz, Korth and Sudarshan1.20 Java Program Data for Java Program Java Compiler Byte-Code Program Byte-Code Interpreter Machine-Language Instructions Computer Execution of Machine-Language Instructions Output of Java Program Class Loader (i.e., Linker) Previously Compiled Helper Programs Java Program Translation Including Linker
©Silberschatz, Korth and Sudarshan1.21 A Sip of Java! History Invented in James Gosling, Sun Microsystems, Inc. Originally a language for programming home appliances. Later (1994) used for World Wide Web applications byte code can be downloaded and run without compiling it. Eventually used as a general-purpose programming language. Why the name “Java”? Not sure - it may just be a name that came during a coffee break and it had not been copyrighted, yet.
©Silberschatz, Korth and Sudarshan1.22 Applets vs. Java Applications Applets: Java programs intended to be downloaded via the WWW and run immediately “little applications” Typically embedded in a web-page and run in a web browser Applications: Java programs intended to be installed then run Often larger, more complex applications Applets and applications are programmed slightly differently.
©Silberschatz, Korth and Sudarshan1.23 import java.util.Scanner; public class FirstProgram { public static void main(String[] args) { System.out.println("Hello out there."); System.out.println(“I will add two numbers for you"); System.out.println(“Enter two whole numbers on a line:"); int n1, n2; Scanner keyboard = new Scanner(System.in); n1 = keyboard.nextInt(); n2 = keyboard.nextInt(); System.out.println(“The sum of those two numbers is:”); System.out.println(n1 + n2); }
©Silberschatz, Korth and Sudarshan1.24 Explanation of Code... Code to begin the program (to be explained later): import java.util.Scanner; public class FirstProgram { public static void main(String[ ] args) { Java applications all have similar code at the beginning The name of the class differs from one program to another. The name of the class is also the name of the file.
©Silberschatz, Korth and Sudarshan1.25 Explanation of Code... Display text strings to the screen: System.out.println("Hello out there."); System.out.println(“I will add two numbers for you."); System.out.println(“Enter two whole numbers on a line."); Note the “dot” operator System is a class out an object println is a method that outputs something double-quoted text inside the parentheses is an argument to the method general syntax: Object_Name.Method_Name(Arguments)
©Silberschatz, Korth and Sudarshan1.26 … Explanation of Code... Code to create two variables named n1, n2 for storing two whole numbers (integer): int n1, n2; These are called “variable declarations.” In this program they are used to store the user’s response.
©Silberschatz, Korth and Sudarshan1.27 … Explanation of Code... Creating an object called keyboard of the Scanner class: Scanner keyboard = new Scanner(System.in); System.in refers to the keyboard The Scanner class provides the program with access to keyboard input.
©Silberschatz, Korth and Sudarshan1.28 … Explanation of Code... Read two integers typed in from the keyboard and store them in the variables n1 and n2 : n1 = keyboard.nextInt(); n2 = keyboard.nextInt();
©Silberschatz, Korth and Sudarshan1.29 … Explanation of Code Printing the sum to the console: System.out.println(“The sum of those two numbers is:"); System.out.println(n1 + n2);
©Silberschatz, Korth and Sudarshan1.30 Compiling and Running a Java Program Type the program into a file: FirstProgram.java Compile: javac.java Run (and link): java must have a main method BlueJ has two similar steps by mouse clicking (discussed in the labs).
©Silberschatz, Korth and Sudarshan1.31 The Object-Oriented (OO) Paradigm Some terminology: Object-oriented programming Object-oriented (programming) language Object-oriented design Object-oriented database etc. What does the term “object-oriented” mean? The OO paradigm is a philosophy that has had, and continues to have, an impact on all aspects of software design and implementation. Software can be designed an implemented in a variety of ways, and the OO paradigm is one; you will learn others over the next few years. Currently, the OO approach is the most widely used.
©Silberschatz, Korth and Sudarshan1.32 Object-Oriented Programming: OOP What is the basic idea behind the OO paradigm? The OO paradigm is based on the idea that all aspects of software – its design, implementation, internal structure, as well as the supporting tools and language – should be based on the real-world objects the software is associated with. Example - An OO software system for air traffic control would contain internal data items that correspond directly to: aircraft airports passengers runways etc.
©Silberschatz, Korth and Sudarshan1.33 Object-Oriented Programming: OOP More terminology: object - usually a person, place or thing (a noun), not necessarily physical attribute - a property, characteristic or data associated with an object method - an action associated with an object (a verb), sometimes called behavior class - a category of similar objects Objects have both attributes and methods Objects of the same class have the same data elements and methods Objects are sometimes said to send and receive messages to invoke actions A java program consists of a collection of classes, objects and methods.
©Silberschatz, Korth and Sudarshan1.34 Example of an Object Class Class “Automobile:” Data Items: manufacturer’s name model name year made color number of doors size of engine etc. Methods: start engine turn engine off accelerate decelerate engage cruise control display error code adjust fuel mixture etc.
©Silberschatz, Korth and Sudarshan1.35 Design Principles of OOP Three of the Main design principles of Object-Oriented Programming (OOP): Encapsulation Polymorphism Inheritance
©Silberschatz, Korth and Sudarshan1.36 Encapsulation A piece of software can frequently be used without knowing the details of how it works. Relatively small, well-defined and closely related “chunks” of software can be packaged together (i.e., encapsulated) for use by other larger “chunks” of software. Analogy: In order to drive a car (generally): You don’t need to know: how many cylinders the engine has whether the breaks are disk breaks or drum breaks You do need to know: Where the controls are and how to use them What type of fuel
©Silberschatz, Korth and Sudarshan1.37 Encapsulation A better analogy: The transmission manufacturer: doesn’t need to know: the size of the cylinders in the engine the size of the oil pan for the engine does need to know: specifications of the connections to the engine range of torque and acceleration of the engine One more analogy: the waiter vs. the cook The book also calls this information hiding
©Silberschatz, Korth and Sudarshan1.38 Polymorphism Polymorphism—the same word or phrase can be mean different things in different contexts Analogy: in English, bank can mean: side of a river or a place to put money Determining the correct meaning requires context, i.e., you have to see it in a sentence. In Java, two or more methods could be called “output.” Which specific method is being invoked, and what it does, depends on the context of the method call.
©Silberschatz, Korth and Sudarshan1.39 An Inheritance Hierarchy Vehicle AutomobileMotorcycleBus SedanSports CarSchool BusLuxury Bus Inheritance—a way of organizing classes. Classes with attributes (and methods) in common can be grouped so that their common attributes are only defined once. What properties does each vehicle inherit from the types of vehicles above it in the diagram?
©Silberschatz, Korth and Sudarshan1.40 Algorithms An algorithm is a set of instructions (steps) for solving a problem: each step must be clear and precise each step must require finite resources Inputs and outputs must be specified precisely the algorithm must be complete Analogous to a recipe. May be in a number of different formats: natural language (such as English) a diagram, such as a flow chart a specific programming language pseudocode – a mix of natural and programming languages
©Silberschatz, Korth and Sudarshan1.41 Example of an Algorithm Algorithm to find the total cost of a list of items (modified from the book): Input: A list of item prices. Output: The total cost of all the items. 1) Record (on a blackboard or piece of paper) an initial sum of 0. 2) Do the following for each item on the list: a) Add the cost of the item to the sum. b) Replace the previously recorded value by this new sum. 3) Report the final recorded sum as the answer.
©Silberschatz, Korth and Sudarshan1.42 Program Design Process Design THEN code THEN test (not code, then design) Design process 1. define the problem clearly, precisely; understand the inputs and outputs 2. describe the algorithms for solving the problem, usually in pseudocode 3. Design objects, methods and classes your program needs 4. re-evaluate your solution, and iteratively improve it, eliminating errors and inefficiencies whenever they are identified. Writing/Coding 1. write the code 2. compile the code 3. read the code (“structured walkthrough”) 4. test the code 5. fix any errors, and repeat
©Silberschatz, Korth and Sudarshan1.43 Testing and Debugging Even with careful programming, your code will contain errors and must be thoroughly tested. Bug - a mistake in a program Debugging - fixing mistakes in a program
©Silberschatz, Korth and Sudarshan1.44 Types of Errors Generally, there are three types of programming errors: syntax run-time logic
©Silberschatz, Korth and Sudarshan1.45 Syntax The syntax of a programming language is the set of grammar rules for that programming language. A syntax error is a grammatical mistake in a program. misspelling a command, e.g., “rtrn” instead of “return” missing variable declaration missing punctuation using things inappropriately, e.g., adding an integer and a character Syntax errors: are caught by compiler, hence the phrase “compiler-time error” relatively easy to fix prevent the program from executing frequently result in misleading error messages
©Silberschatz, Korth and Sudarshan1.46 Run-Time Errors An error that is detected when you program run (or executed) is called a run-time error*. dividing a number by 0 accessing memory that was de-allocated indexing out of bounds in an array reading input from a file that is not open Run-time errors: terminate a programs normal execution occasionally detected by the compiler occur intermittently frequently result in misleading, incomplete or non-intelligible messages frequently difficult to fix *Note: what is a run-time error in one language might be a logic- error in another
©Silberschatz, Korth and Sudarshan1.47 Logic Errors Just because it compiles and runs without getting an error message does not mean the program is correct! An error that causes a program to produce incorrect results is a logic error. circleArea = radius * radius; // pi * radius * radius sum = a - b; // should be sum = a + b; Logic-time errors: not detected by the compiler produce no run-time error messages Cause the program to take incorrect action taken, or produce incorrect results/output during program execution occur intermittently frequently difficult to fix
©Silberschatz, Korth and Sudarshan1.48 Run-time and Logic Errors Run-time errors and logic-errors can be INCREDIBLY damaging due to their intermittent and after-the-fact nature. For that reason, it is imperative that you: test extensively, on a variety of different test-cases perform structured walk-throughs
©Silberschatz, Korth and Sudarshan1.49 Run-Time and Logic Errors 1. Mariner Bugs Out (1962) Cost: $18.5 million Disaster: The Mariner 1 rocket with a space probe headed for Venus diverted from its intended flight path shortly after launch. Mission Control destroyed the rocket 293 seconds after liftoff. Cause: A programmer incorrectly transcribed a handwritten formula into computer code, missing a single superscript bar. Without the smoothing function indicated by the bar, the software treated normal variations of velocity as if they were serious, causing faulty corrections that sent the rocket off course.
©Silberschatz, Korth and Sudarshan1.50 Run-Time and Logic Errors 2. CIA Gives the Soviets Gas (1982) Cost: Millions of dollars, significant damage to Soviet economy Disaster: Control software went haywire and produced intense pressure in the Trans-Siberian gas pipeline, resulting in the largest man-made, non-nuclear explosion in Earth's history. Cause: CIA operatives allegedly planted a bug in a Canadian computer system purchased by the Soviets to control their gas pipelines. The purchase was part of a strategic Soviet plan to steal or covertly obtain sensitive U.S. technology. When the CIA discovered the purchase, they sabotaged the software so that it would pass Soviet inspection but fail in operation.
©Silberschatz, Korth and Sudarshan1.51 Run-Time and Logic Errors 3. World War III… Almost (1983) Cost: Nearly all of humanity Disaster: The Soviet early warning system falsely indicated the United States had launched five ballistic missiles. Fortunately the Soviet duty officer had a "funny feeling in my gut" and reasoned if the U.S. was really attacking they would launch more than five missiles, so he reported the apparent attack as a false alarm. Cause: A bug in the Soviet software failed to filter out false missile detections caused by sunlight reflecting off cloud-tops.
©Silberschatz, Korth and Sudarshan1.52 Run-time and Logic Errors 4. Medical Machine Kills (1985) Cost: Three people dead, three people critically injured Disaster: Canada's Therac-25 radiation therapy machine malfunctioned and delivered lethal radiation doses to patients. Cause: Because of a subtle bug called a race condition, a technician could accidentally configure Therac-25 so the electron beam would fire in high-power mode without the proper patient shielding.race condition
©Silberschatz, Korth and Sudarshan1.53 Summary A computer’s main memory holds both the program that is currently running and its data. Main memory is a series of numbered locations, each one containing a single byte. Auxiliary memory is for more or less permanent storage. A compiler is a program that translates a high-level language, like java, into a lower level format (“byte-code” for java). Actual translation of Java byte-code to the hardware’s specific machine code occurs at run time (it is interpreted).
©Silberschatz, Korth and Sudarshan1.54 Summary, Cont. An algorithm is a set of instructions for solving a problem (it must be complete and precise). An object is something that has both data and actions (methods) associated with it. A class defines a type of object; all objects of the same class have the same methods. Three OOP design principles are encapsulation, polymorphism, and inheritance. In a java program, a method invocation has the general form Object_Name.Method_Name(Arguments)