©Silberschatz, Korth and Sudarshan1.1 Chapter 1: Introduction to Computers and Java Objects Background information  hardware  software  computer languages.

Slides:



Advertisements
Similar presentations
ICS103 Programming in C Lecture 1: Overview of Computers & Programming
Advertisements

Lecture 1: Overview of Computers & Programming
Copyright © 2002 W. A. Tucker1 Chapter 1 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CSCE 145: Algorithmic Design I Chapter 1 Intro to Computers and Java Muhammad Nazmus Sakib.
Introduction to Computers and Java Module 1. Objectives overview computer hardware and software introduce program design and object-oriented programming.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
Introduction to Computers and Java Objects
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition by Tony Gaddis, Judy Walters,
ICS 102 Computer Programming University of Hail College of Computer Science & Engineering Computer Science and Software Engineering Department.
Introduction Algorithms and Programming. Computer Programming  A form of problem solving  Or, more accurately, a way to solve problems  What we will.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 1 Introduction.
1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know.
COMP Computer Basics Yi Hong May 13, 2015.
1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know.
CS102 Introduction to Computer Programming
Computer Programming-1 CSC 111 Chapter 1 : Introduction.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 1 Introduction to Computers and Programming.
Why Program? Computer – programmable machine designed to follow instructions Program – instructions in computer memory to make it do something Programmer.
Chapter Introduction to Computers and Programming 1.
Introduction to Programming Dr Masitah Ghazali Programming Techniques I SCJ1013.
CSC 125 Introduction to C++ Programming Chapter 1 Introduction to Computers and Programming.
Lecturer: Dr. AJ Bieszczad Chapter 1 COMP 150: Introduction to Object-Oriented Programming 1-1 l Background information »important regardless of programming.
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 1 l Background information »important regardless of programming.
COMP 110 Computer Basics Luv Kohli August 25, 2008 MWF 2-2:50 pm Sitterson
COMP 110 Spring Announcements Computers in class on Friday: Lab Office Hours: Monday 12-2 New students see me after class Administrative Changes.
COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014.
An intro to programming. The purpose of writing a program is to solve a problem or take advantage of an opportunity Consists of multiple steps:  Understanding.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Chapter 1: Introduction to Computers and Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
1 Computing Software. Programming Style Programs that are not documented internally, while they may do what is requested, can be difficult to understand.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1 Introduction to Computers Lect 1 Won’t tell you much you don’t know. (Misleading– the course gets more conceptual as we create webpages.) Will go into.
What is a Computer? An, electrical machine, that can be programmed to accept data (input), process it into useful information (output) and store it away.
1 Introduction to Java Brief history of Java Sample Java Program Compiling & Executing Reading: => Section 1.1.
Computer Parts. Two Basic Parts Hardware & Software.
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Java Byte Code l The Java compiler generates Java Byte Code. (Most.
Object Oriented Programming Computer Engineering Department JAVA Programming Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Introduction to Computer Systems and the Java Programming Language.
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 1 l Background information »important regardless of programming.
Chapter 1 Section 1.1 Introduction to Java Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
©Silberschatz, Korth and Sudarshan1.1 Chapter 1: Introduction to Computers and Java Objects Background information  hardware  software  computer languages.
Introduction to Computers and Java Module 1. Objectives overview computer hardware and software introduce program design and object-oriented programming.
Mrs. Ulshafer August, 2013 Java Programming Chapter 1.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
Beginning Snapshots Chapter 0. C++ An Introduction to Computing, 3rd ed. 2 Objectives Give an overview of computer science Show its breadth Provide context.
Chapter 1 Computers, Compilers, & Unix. Overview u Computer hardware u Unix u Computer Languages u Compilers.
Chapter 1 : Overview of Computer and Programming By Suraya Alias
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
A.Abhari CPS1251 Topic 1: Introduction to Computers Computer Hardware Computer components Connecting Computers Computer Software Operating System (OS)
©Silberschatz, Korth and Sudarshan1.1 Chapter 1: Introduction to Computers and Java Objects Background information  hardware  software  computer languages.
Computer and Programming. Computer Basics: Outline Hardware and Memory Programs Programming Languages and Compilers.
Lesson 1 1 LESSON 1 l Background information l Introduction to Java Introduction and a Taste of Java.
Brief Version of Starting Out with C++ Chapter 1 Introduction to Computers and Programming.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 1 l Background information »important regardless of programming.
Programming Logic and Design Seventh Edition Chapter 1 An Overview of Computers and Programming.
©Silberschatz, Korth and Sudarshan1.1 Chapter 1: Introduction to Computers and Java Objects Background information  hardware  software  computer languages.
Introduction to Computers and Programming
Chapter 1: Introduction to Computers and Programming
Programming Language Hierarchy, Phases of a Java Program
Lecture 1: Introduction to JAVA
Introduction to Computers and Java
Computers: Hardware and Software
Introduction to Java Brief history of Java Sample Java Program
ICS103 Programming in C 1: Overview of Computers And Programming
Programming Logic and Design Eighth Edition
Presentation transcript:

©Silberschatz, Korth and Sudarshan1.1 Chapter 1: Introduction to Computers and Java Objects Background information  hardware  software  computer languages  compiling, interpreting and assembling  object-oriented design and development  types of errors Introduction to Java

©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 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 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.22 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.23 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.24 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.25 Design Principles of OOP Three of the Main design principles of Object-Oriented Programming (OOP):  Encapsulation  Polymorphism  Inheritance

©Silberschatz, Korth and Sudarshan1.26 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.27 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.28 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.29 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.30 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.31 Example of an Algorithm Algorithm that determines the total cost of a list of items: 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) Output the final sum.

©Silberschatz, Korth and Sudarshan1.32 Program Design Process Design, then code (not code, then design) Design process 1. define the problem clearly 2. design objects your program needs 3. develop algorithms for the methods of objects 4. describe the algorithms, usually in pseudocode Writing/Coding 1. write the code 2. test the code 3. fix any errors and retest

©Silberschatz, Korth and Sudarshan1.33 Testing and Debugging Even with careful programming, your code could still contain errors and must be thoroughly tested. Bug—a mistake in a program Debugging—fixing mistakes in a program

©Silberschatz, Korth and Sudarshan1.34 Types of Errors Syntax Run-Time Logic

©Silberschatz, Korth and Sudarshan1.35 Syntax Syntax: the set of grammar rules for a programming language. The compiler checks your program to make sure it follows the grammar/syntax Violating the syntax => error

©Silberschatz, Korth and Sudarshan1.36 Syntax Errors caught by compiler (“compiler-time error”) automatically found, usually the easiest to fix cannot run program until all syntax errors are fixed error message may be misleading Example: Misspelling a command, for example “rtrn” instead of “return”

©Silberschatz, Korth and Sudarshan1.37 Run-Time Errors An execution error (during run-time) The program cannot continue to run Not always so easy to fix Error message may or may not be helpful Not detected by the compiler. Example: Division by zero - if your program attempts to divide by zero it automatically terminates and prints an error message.

©Silberschatz, Korth and Sudarshan1.38 Logic Errors Just because it compiles and runs without getting an error message does not mean the program is correct! An error in the design (the algorithm) or its implementation  Program compiles without errors  no run-time error messages  but incorrect action or data occurs during execution Generally the most difficult to find and fix Need to be alert and test thoroughly  think about test cases and predict results before executing the code

©Silberschatz, Korth and Sudarshan1.39 Logic Error Examples Algorithm Error:  circleArea = radius * radius; (pi * radius * radius) Implementation Error:  typed in wrong symbol in source code - sum = a - b; (should be sum = a + b; )

©Silberschatz, Korth and Sudarshan1.40 Finally! Now, a taste of Java! History 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 (it is object-oriented) 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.41 Applets vs. Java Applications Applets  Java programs intended to be downloaded via the WWW and run immediately  “little applications”  run in a web browser Applications  Java programs intended to be installed then run  often larger applications Slightly different programming for each

©Silberschatz, Korth and Sudarshan1.42 import java.util.*; 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); } A Sample Java Program

©Silberschatz, Korth and Sudarshan1.43 Explanation of Code... Code to begin the program (to be explained later): 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.

©Silberschatz, Korth and Sudarshan1.44 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.out is an object  println is a method that it carries out  double-quoted text inside the parentheses is an argument to the method  general syntax: Object_Name.Method_Name(Arguments)

©Silberschatz, Korth and Sudarshan1.45 … Explanation of Code... Code to create two variables named n1, n2 to contain two whole numbers (integer): int n1, n2; They store the user’s response.

©Silberschatz, Korth and Sudarshan1.46 … Explanation of Code... Creating an object called keyboard of the Scanner class: Scanner keyboard = new Scanner(System.in); System.in is the keyboard, but the Scanner class has easier methods to use.

©Silberschatz, Korth and Sudarshan1.47 … 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.48 … 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.49 Compiling and Running a Java Program 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.50 Summary Part 1 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.51 Summary Part 2 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)