What is a Computer Program? For a computer to be able to do anything (multiply, play a song, run a...

Post on 26-Mar-2015

223 views 1 download

Tags:

Transcript of What is a Computer Program? For a computer to be able to do anything (multiply, play a song, run a...

What is a Computer Program?

For a computer to be able to do anything (multiply, play a song, run a word processor), it must be given the instructions to do so.

A program is a set of instructions written by humans for computers to perform tasks.

The instructions are written in programming languages such as C, C++, Java, etc.

Programming Languages

Machine Language Assembly Language High-Level Language

Machine language is a set of primitive instructions built into every computer. The instructions are in the form of binary code, so you have to enter binary codes for various instructions. Program with native machine language is a tedious process. Moreover the programs are highly difficult to read and modify. For example, to add two numbers, you might write an instruction in binary like this:

 

1101101010011010

Programming Languages

Machine Language Assembly Language High-Level Language

Assembly languages were developed to make programming easy. Since the computer cannot understand assembly language, however, a program called assembler is used to convert assembly language programs into machine code. For example, to add two numbers, you might write an instruction in assembly code like this:

ADDF3 R1, R2, R3

… ADDF3 R1, R2, R3 …

Assembly Source File

Assembler

… 1101101010011010 …

Machine Code File

Programming Languages

Machine Language Assembly Language High-Level Language

The high-level languages are English-like and easy to learn and program. For example, the following is a high-level language statement that computes the area of a circle with radius 5:

area = 5 * 5 * 3.1415;

Popular High-Level Languages

COBOL (COmmon Business Oriented Language)

FORTRAN (FORmula TRANslation)

BASIC (Beginner All-purpose Symbolic Instructional Code)

Pascal (named for Blaise Pascal)

Ada (named for Ada Lovelace)

C (whose developer designed B first)

Visual Basic (Basic-like visual language developed by Microsoft)

Delphi (Pascal-like visual language developed by Borland)

C++ (an object-oriented language, based on C)

Java (We use it in this class)

Compiling Programs

Computers do not understand the languages (C++, Java, etc) that programs are written in.

Programs must first be compiled (converted) into machine code that the computer can run.

A compiler is a program that translates a programming language into machine code.

Running Programs

All programs follow a simple format:

Input Execution Output

Inputs can be from users, files, or other computer programs

Outputs can take on many forms: numbers, text, graphics, sound, or commands to other programs

Multiple Compilers

Because different operating systems (Windows, Macs, Unix) require different machine code, you must compile most programming languages separately for each platform.

program

compiler

compiler

compiler

WinMAC

Unix

Java is a little different.

Java compiler produces bytecode not machine code.

Bytecode can be run on any computer with the Java interpreter installed.

Java Program

compiler

Java Bytecode

Win

MAC

Unix

Interpreter

Interpreter

Interpreter

Java Interpreter

Advantages and Disadvantages of Java

Advantages:

Java is platform independent. Once it's compiled, you can run the bytecode on any machine with a Java interpreter. You do not have to recompile for each platform.

Java is safe. Certain common programming bugs and dangerous operations are prevented by the language and compiler.

Java standardizes many useful operations like managing network connections and providing graphical user interfaces.

Disadvantages:

Running bytecode through the interpreter is not as fast as running machine code, which is specific to that platform.

Because it is platform independent, it is difficult to use platform specific features (e.g., Windows taskbar, quick launch) in Java.

Java interpreter must be installed on the computer in order to run Java programs.

Java’s Compiler + Interpreter

Editor

Hello.java

Compiler

Hello.class

Interpreter

Hello,World!

Interpreter

Java Virtual Machine

Must Run on Any Architecture

Programin Java

JavaCompiler

JavaBytecode

Java Virtual Machine

“WRITE ONCE, RUN ANYWHERE!”pretty

portable

Why Bytecodes?

Platform-independent

Load from the Internet faster than source code

Interpreter is faster and smaller than it would be for Java source

Source code is not revealed to end users

Interpreter performs additional security checks, screens out malicious code

Types of Programs

Console applications GUI applications

Applets

Java on the Web: Java Applets

Clients download applets via Web browser

Browser runs applet in a Java Virtual Machine (JVM)

Interactive web, security, and client consistency

Slow to download, inconsistent VMs (besides, flash won this war)

Applet

ClientServer

Java on the Web: J2EE

Thin clients (minimize download)

Java all “server side”

ClientServer

JSPs

Servlets

EJB

JDBC

Console Applications

C:\javamethods\Ch02> path=%PATH%;C:\Program Files\Java\jdk 1.5.0_07\binC:\javamethods\Ch02> javac Greetings2.javaC:\javamethods\Ch02> java Greetings2Enter your first name: JosephineEnter your last name: JaworskiHello, Josephine JaworskiPress any key to continue...

Simple text dialog:

prompt input, prompt input ... result

GUI Applications

Menus

Buttons

Clickable panel

Slider

OOP —Object-Oriented Programming

An OOP program models a world of active objects.

An object may have its own “memory,” which may contain other objects.

An object has a set of methods that can process messages of certain types.

OOP (cont’d)

A method can change the object’s state, send messages to other objects, and create new objects.

An object belongs to a particular class, and the functionality of each object is determined by its class.

A programmer creates an OOP application by defining classes.

Inheritance

A programmer can define hierarchies of classes

More general classes are closer to the top

Person

Child Adult

Baby Toddler Teen

OOP Benefits

Facilitates team development

Easier to reuse software components and write reusable software

Easier GUI (Graphical User Interface) and multimedia programming