Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 1 l Background information »important regardless of programming.

Similar presentations


Presentation on theme: "Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 1 l Background information »important regardless of programming."— Presentation transcript:

1 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 1 l Background information »important regardless of programming language l Introduction to Java Introduction to Computers and Java Objects

2 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 2 Computer Basics l Computer system: hardware + software l Hardware: the physical components l Software: the instructions that tell the hardware what to do

3 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 3 Common Hardware Components l Processor (CPU) »Central Processing Unit »Interprets and executes the instructions l Memory »main & auxiliary »holds data and instructions l I nput device(s) »mouse, keyboard, etc. l O utput device(s) »video display, printer, etc. l CPU and memory are physically housed together Standard Hardware Organization Memory (main & auxiliary) Processor (CPU) Input Devices (such as mouse and keyboard) Output Devices (such as video display or printer)

4 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 4 Physical Organization l Keyboard l Monitor l Chassis »CPU »memory »disk drives »I/O connectors »etc.

5 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 5 Two Kinds of Memory l Main »working area »temporarily stores program and data (while program is executing) l Auxiliary »permanent (more or less) »saves program and results »includes floppy & hard disk drives, CDs, tape, etc.

6 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 6 Main Memory Organization l Bit = one binary digit »Binary digit can have only one of two values, 0 or 1 l Byte = 8 bits l “Byte Addressable” »Main memory is a list of numbered locations that contain one byte of data in each location l Number of bytes per data item may vary

7 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 7 Auxiliary Memory Organization [file systems for users]

8 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 8 Running a Program Program Computer Data (input for the program) Output Program—a set of instructions for a computer to follow

9 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 9 Many Types of Programs l User-created applications l Existing applications »word-processor/editor »web browser »compiler or assembler »etc. l Operating System »DOS, Microsoft Windows, MacOS, Linux, UNIX, etc.

10 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 10 Various Types of User Interfaces l Command-line »type in key words and letters »DOS and UNIX l Menu »parts of DOS and Windows l GUI (Graphical User Interface) »click on icon »also called “event-driven” »MacOS, Windows

11 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 11 Programming Language Hierarchy

12 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 12 The highs and lows of programming languages... High-Level Language (HLL) »closest to natural language »words, numbers, and math symbols »not directly understood by hardware »“portable” source code (hardware independent) »Java, C, C++, COBOL, FORTRAN, BASIC, Lisp, Ada, etc. 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)

13 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 13 Assembly Language (middle level) l a more or less human readable version of machine language l words, abbreviations, letters and numbers replace 0s and 1s l easily translated from human readable to machine executable code l like machine code, not portable (hardware dependent)

14 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 14 Getting from Source to Machine Code l “Compiling a program” translating from a high-level language source code to machine (object, or executable) code. l “Compiler” a program that translates HLL source code to machine (object, or executable) code. l “Assembly” translating from assemble language source code to machine (object, or executable) code. l “Assembler” a program that translates assembly source code to machine (object, or executable) code. l Compilers need to know the specific target hardware

15 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 15 Compilers vs. Assemblers vs. Interpreters l Compilers and Assemblers »translation is a separate user step »translation is “off-line,” i.e. not at run time l Interpreters - another way to translate source to object code »interpretation (from source to object code) is not a separate user step »translation is “on-line,” i.e. at run time Compiler, Assembler, or Interpreter Source Code Object Code

16 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 16 Java Program Translation l Both Compilation and Interpretation l Intermediate Code: “Byte Code” »similar to assembly code, but hardware independent l Interpreter translates from generic byte code to hardware- specific machine code 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 Java Virtual Machine

17 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 17 Java Byte Code l generated by Java compiler »Instead of generating machine language as most compilers do, the Java compiler generates byte code. l translated to machine language of various kinds of computers l executed by Java interpreter l invisible to programmer »You don't have to know anything about how byte code works to write a Java program.

18 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 18 Why Use Byte Code? Disadvantages: l requires both compiler and interpreter l slower program execution Advantages: l portability »very important »same program can run on computers of different types (useful with the Internet) »Java compiler for new types of computers can be made quickly

19 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 Java Virtual Machine Linker Previously Compiled Helper Programs Java Program Translation Including Linker

20 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 20 Object-Oriented Programming: OOP l A design and programming technique l Some terminology: »object - usually a person, place or thing (a noun) »method - an action performed by an object (a verb) »type or class - a category of similar objects (such as automobiles) l Objects have both data and methods l Objects of the same class have the same data elements and methods l Objects send and receive messages to invoke actions

21 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 21 Example of an Object Class Data Items: »manufacturer’s name »model name »year made »color »number of doors »size of engine »etc. Methods: »Define data items (specify manufacturer’s name, model, year, etc.) »Change a data item (color, engine, etc.) »Display data items »Calculate cost »etc. Class: Automobile

22 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 22 Why OOP? l Save development time (and cost) by reusing code »once a class is created, it can be used in other applications l Easier debugging »classes can be tested independently »reused objects have already been tested

23 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 23 Design Principles of OOP Three main design principles of Object- Oriented Programming (OOP): l Encapsulation l Polymorphism l Inheritance

24 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 24 Encapsulation l Design software »can be easily used »without knowing the details of how it works. l Also known as information hiding An analogy: l When you drive a car, you don’t have know »the details of how many cylinders the engine has or »how the gasoline and air are mixed and ignited. l only have to know how to use the controls.

25 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 25 Reusable Components Advantages of using reusable components: l saves time and money l components that have been used before »often better tested and more reliable than new software Make your classes reusable: l encapsulation l general classes have a better chance of being reused than ad hoc classes

26 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 26 Polymorphism l Polymorphism—the same word or phrase can be mean different things in different contexts l Analogy: in English, bank can mean: »side of a river or »a place to put money In Java, two or more classes could each have a method called output Each output method would do the “right thing” for the class that it was in. E.g. »display a number (Integer class) »display an image (Photo class)

27 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 27 Inheritance l Inheritance—a way of organizing classes l Term comes from inheritance of traits like eye color, hair color, and so on. l Classes with attributes in common can be grouped so that their common attributes are only defined once.

28 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 28 An Inheritance Hierarchy Vehicle AutomobileMotorcycleBus SedanSports CarSchool BusLuxury Bus What properties does each vehicle inherit from the types of vehicles above it in the diagram?

29 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 29 Algorithms l Algorithm - a set of instructions (steps) for solving a problem. »must be precise »must be complete l May be in a number of different formats »natural language (such as English) »a specific programming language »a diagram, such as a flow chart »pseudocode - a mix of natural and programming languages

30 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 30 Example of an Algorithm Algorithm that determines the total cost of a list of items: 1. Write the number 0 on the blackboard. 2. Do the following for each item on the list: --Add the cost of the item to the number on the blackboard. --Replace the old number on the board by this sum. 3. Announce that the answer is the number written on the board

31 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 31 Program Design Process l Design, then code (not code, then design) l 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 l Writing/Coding 1.write the code 2.test the code 3.fix any errors and retest

32 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 32 Testing and Debugging l Even with careful programming, your code could still contain errors and must be thoroughly tested. l Bug—a mistake in a program l Debugging—fixing mistakes in a program

33 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 33 Types of Errors l Syntax l Run-Time l Logic

34 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 34 Syntax l Syntax: the set of grammar rules for a programming language. l The compiler checks your program to make sure it follows the grammar/syntax l Violating the syntax => error

35 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 35 Syntax Errors l caught by compiler (“compiler-time error”) l automatically found, usually the easiest to fix l cannot run program until all syntax errors are fixed l error message may be misleading Example: Misspelling a command, for example “rtrn” instead of “return”

36 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 36 Run-Time Errors l An execution error (during run-time) l The program cannot continue to run l Not always so easy to fix l Error message may or may not be helpful l 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.

37 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 37 Logic Errors Just because it compiles and runs without getting an error message does not mean the program is correct! l 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 l Generally the most difficult to find and fix l Need to be alert and test thoroughly »think about test cases and predict results before executing the code

38 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 38 Logic Error Examples l Algorithm Error: »circleArea = radius * radius; (pi * radius * radius) l Implementation Error: »typed in wrong symbol in source code - sum = a - b; (should be sum = a + b; )

39 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 39 Finally! Now, a taste of Java! History l 1991 - James Gosling, Sun Microsystems, Inc. l originally a language for programming home appliances l later (1994) used for World Wide Web applications »byte code can be downloaded and run without compiling it l eventually used as a general-purpose programming language (it is object-oriented) l 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.

40 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 40 Applets vs. Java Applications l Applets »Java programs intended to be downloaded via the WWW and run immediately »“little applications” »run in a web browser l Applications »Java programs intended to be installed then run »often larger applications l Slightly different programming for each

41 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 41 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

42 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 42 Explanation of Code... l Code to begin the program (to be explained later): public class FirstProgram { public static void main(String[ ] args) { l Java applications all have similar code at the beginning »The name of the class differs from one program to another.

43 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 43 Explanation of Code... l 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)

44 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 44 … Explanation of Code... Code to create two variables named n1, n2 to contain two whole numbers (integer): int n1, n2; l They store the user’s response.

45 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 45 … 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.

46 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 46 … 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();

47 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 47 … Explanation of Code l Printing the sum to the console: System.out.println(“The sum of those two numbers is:"); System.out.println(n1 + n2);

48 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 48 Compiling and Running a Java Program l Compile »javac.java l Run (and link) »java » must have a main method l BlueJ has two similar steps by mouse clicking (discussed in the labs).

49 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 49 Summary Part 1 l A computer’s main memory holds both the program that is currently running and its data. l Main memory is a series of numbered locations, each one containing a single byte. l Auxiliary memory is for more or less permanent storage. l A compiler is a program that translates a high-level language, like java, into a lower level format (“byte- code” for java). l Actual translation of Java byte-code to the hardware’s specific machine code occurs at run time (it is interpreted).

50 Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 50 Summary Part 2 l An algorithm is a set of instructions for solving a problem (it must be complete and precise). l An object is something that has both data and actions (methods) associated with it. l A class defines a type of object; all objects of the same class have the same methods. l 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)


Download ppt "Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 1 l Background information »important regardless of programming."

Similar presentations


Ads by Google