it can execute a prerecorded list of instructions (a...

44

Upload: vuongtu

Post on 28-Apr-2018

214 views

Category:

Documents


2 download

TRANSCRIPT

It can execute a prerecorded list of instructions (a program).

Chapter 1 Java: an Introduction to Computer Science & Programming - Walter Savitch 2

Computer Basics

Computer system:

hardware + software

Hardware: the physical

components

Software: the

instructions that tell the

hardware what to do

Chapter 1 Java: an Introduction to Computer Science & Programming - Walter Savitch 8

Running a Program

Program

ComputerData

(input for the program)Output

Computing has been around for millennia

1835 – Charles Babbage first used punch cards to “program” his analytic engine

• Pre 1940 - Analog Computers

• 1940 – First electric digital computers

http://en.wikipedia.org/wiki/History_of_computers

Machine language

Assembly language

1945 - Plankalkül (Konrad Zuse)

1954 - FORTRAN

1958 - LISP

1958 - ALGOL

1959 - COBOL

1962 - APL

1962 - Simula

1964 - BASIC

1964 - PL/I

1970 - Pascal

1972 - C

1972 - Smalltalk

1972 - Prolog

1973 - ML

1978 - SQL

1983 - Ada

1983 - C++

1985 - Eiffel

1987 - Perl

1989 - FL (Backus)

1990 - Haskell

1990 - Python

1991 - Java

1993 - Ruby

2000 - C#

So Many Languages...

People understand

words, symbols,

and pictures.

Computers can

only execute 1’s

and 0’s

People need to

write in 1’s and

0’s

Computers

understand words.

People write

program in words

Program is

translated into

1’s and 0’s

Now the

computer can

execute the

program

Compiler

Translates

words

Get and

executable

Translate high-level language to machine language

Source code

The original program in a high level language

Object code

The translated version in machine language

Slide 1- 11

Invention of a Compiler

Grace Hopper

Admiral Grace Hopper was a “mathematician,

computer scientist, social scientist, corporate politician,

marketing whiz, systems designer,

and programmer,”

In 1953 she invented the compiler, the

intermediate program that translates English

language instructions into the language of the

target computer.

She did this, she said, because she was “lazy”

and hoped that “the programmer may return to

being a mathematician.”

The First Digital Programmers

The Evolution of Programming

1001 0110 1001 1001 0110

1001 1011 1011 1111 0000

1010 1110 1101 1010 1010

Programming in Binary

0

100

200

300

400

500

600

700

800

900

1000

Hardness Level Number of

programmers who

could program

with it

Effeciency of

creating a

program

The Evolution of Programming

int BDISK_INT

mov si,seg DGROUP ;

mov ds,si ;

mov si,offset err_write ;

jc ErrorExit ;

mov si,offset

msg_ok ;

Programming in Assembly Language

0

100

200

300

400

500

600

700

800

900

1000

Hardness Level Number of

programmers who

could program with

it

Effeciency of

creating a program

The Evolution of Programming

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h> /* sleep() */

#include <time.h>

int main(void) {

time_t start, end;

start = time(NULL);

if(start == (time_t)-1) {

fprintf(stderr, "Error: `time()'\n");

return 1;

}

printf("sleepingggzzzzzzz....5sec's\n");

sleep(5);

end = time(NULL);

printf("according to difftime(), slept for %.2f sec's\n", difftime(end, start));

return 0;

}

Programming in High-Level Language

0

100

200

300

400

500

600

700

800

900

1000

Hardness Level Number of

programmers who

could program with

it

Effeciency of

creating a program

The Evolution of Programming

/** * This program prints out all its

command-line arguments. **/

public class Echo {

public static void main(String[] args) {

int i = 0; // Initialize the loop variable

while(i < args.length) {

// Loop until we reach end of array

System.out.print(args[i] + " ");

// Print each argument out

i++;

// Increment the loop variable

}

System.out.println( );

// Terminate the line

}

}

Programming in High-Level Language with OOD

0

100

200

300

400

500

600

700

800

900

1000

Hardness Level Number of

programmers who

could program with

it

Effeciency of

creating a program

Did not want to use C because of its lack of security, garbage collection, portability, and threading.

James Gosling, Arthur Van Hoff, and Andy Bechtolsheim, or Just Another Vague Acronym

first 4 bytes (the so-called "Magic number") of any class file are, in hexadecimal, 0xCAFEBABE

Why Java?

Object Oriented – Allows one to reuse code...yeah! you do not have to write the same code twice.

Garbage Collection - When no references to an object remain, the Java garbage collector automatically deletes the object, freeing memory and preventing a memory leak...yeah! no more slow running programs that make your computer crash!

Platform Independent - programs written in the Java language must run similarly on diverse hardware...yeah! now those with Macs and Linux could also use your programs.

JAVA

6.5 million developers

Java powers more than 4.5 billion devices including:

800+ million PCs

2.1 billion mobile phones and other handheld devices (source: Ovum)

3.5 billion smart cards

Set-top boxes, printers, Web cams, games, car navigation systems, lottery terminals, medical devices, parking payment stations, and more.

SDK

A SDK is short for Software Development Kit.

A SDK it used to create a new programs.

A SDK is given out by the people who create the program language.

A SDK is updated, improved upon, and grows in size as time goes on.

JAVA SDK aka JDK

Java 1.02

250 classes

Slow

Cute name and logo, fun to use, lots of bugs, and applets are the big thing

Java 1.1

500 classes

A little faster

More capable, friendlier. Becoming very popular. Better GUI code.

Java 2

(versions 1.2 – 1.4)

2300 classes

Much faster

Can run at native speeds. Serious and powerful. Comes in 3 flavors: Micro Edition (J2ME), Standard Edition (J2SE), and Enterprise Edition (J2EE). Becomes the language of choice for new enterprise and mobile applications.

Java 5.0

(versions 1.5 -1.5.0_18)

3500 classes

More Powerful and easier to Develop.

Java 5.0 or “Tiger” added major changes to the language itself, making it easier for programmers and giving new features that were popular in other languages.

Java 7 or “Dolphin”

Planned release in 2010

improved multithreading capabilities to take advantage of multi-processors

Java 6

(Update 10-21)

5000 classes

More Support of Graphics.

Better start up time, and an attempt to improve applets by adding JavaScript and making them AJAX applications.

JAVA SDK

Download it from java.com of java.sun.com

Download the latest non-beta version

Java SDK

Contains a compiler – takes your code and makes it into an executable

Contains a number of libraries of pre-written code that you can use in your programs...Yeah!

DOES NOT contain a text editor or IDE that you can use to write your programs.

You can not see it in “All Programs” on your computer.

JAVA SDK

In order to use it/test it you must run it from a command line.

Later we will see how to call the Java SDK functions that we make on the command line via a simple IDE.

Textpad

Notepad++

• Most high-level languages need a different

compiler for each type of computer and for

each operating system.

• Most compilers are very large programs that

are expensive to produce.

Compilers

• A compiler translates a program

from a high-level language to a

low-level language the computer

can run.

• You compile a program by

running the compiler on the high-

level-language version of the

program called the source

program.

Compilers

• Compilers produce machine- or assembly-

language programs called object programs.

• Most high-level languages need a different compiler

for each type of computer and for each operating

system.

• Most compilers are very large programs that are

expensive to produce.

Compilers, cont.

Source Code

Compiler

Executable

Mac PC Other

Source Code

Compiler

Executable

Source Code

Compiler

Executable

• The Java compiler does not translate a Java

program into assembly language or machine

language for a particular computer.

• Instead, it translates a Java program into

byte-code.

– Byte-code is the machine language for a

hypothetical computer (or interpreter) called the

Java Virtual Machine.

Java Byte-Code

JVM

Source Code

Executable

Mac PC Other

Compiler

Executable

Byte Code

Executable

Mac - JVM

PC - JVM Other - JVM

• The Java compiler does not translate a Java

program into assembly language or machine

language for a particular computer.

• Instead, it translates a Java program into

byte-code.

– Byte-code is the machine language for a

hypothetical computer (or interpreter) called the

Java Virtual Machine.

Java Byte-Code

• After compiling a Java program into byte-

code, that byte-code can be used on any

computer with a byte-code interpreter and

without a need to recompile.

• Byte-code can be sent over the Internet and

used anywhere in the world.

• This makes Java suitable for Internet

applications.

Portability

Byte Code

Java Program

Java is better than other programming code

because…

it is platform independent.

the interpreter is smaller than a full compiler.

Needed

•JDK

•Text Editor

•Command Line

Steps

•Create a source file

•Compile it to a .class

•Run the Program

Chapter 1 Java: an Introduction to Computer Science & Programming - Walter Savitch 31

A Sample Java Program

public class FirstProgram

{

public static void main(String[] args)

{

System.out.println("Hello out there.");

System.out.println("Want to talk some more?");

System.out.println("Answer y for yes or n for no.");

char answerLetter;

answerLetter = SavitchIn.readLineNonwhiteChar();

if (answerLetter == 'y')

System.out.println("Nice weather we are having.");

System.out.println("Good-bye.");

System.out.println("Press enter key to end program.");

String junk;

junk = SavitchIn.readLine();

}

}

IDE

A Integrated Development Environment

Used to help in the creation, compilation, and running of your Java programs.

Different levels of IDE’s.

TextPad NetBeans

TextPad or Notepad++

How to use TextPad and Notepad++

How to Download Java, NotePad++, and TextPad so that you can create JAVA Programs on your own.

Help is Out There

There are 6.5 million users of JAVA

Many of these users need a lot of assistance

There are hundreds of forums out on the Web that have programmers that like to help.

Many beginning JAVA programmer forums

javaranch.com

java.sun.com

java.com

grammatical mistakes in a program

the grammatical rules for writing a program are very strict

The compiler catches syntax errors and prints an error message.

example: using a period where a program expects a comma

errors that are detected when

your program is running, but

not during compilation

When the computer detects an

error, it terminates the program

an prints an error message.

example: attempting to divide

by 0

errors that are not detected during compilation

or while running, but which cause the program

to produce incorrect results

example: an attempt to calculate a Fahrenheit

temperature from a Celsius temperature by

multiplying by 9/5 and adding 23 instead of 32

http://java.sun.com/nav/used/

http://javaboutique.internet.com/applications/

Chapter 1 Java: an Introduction to Computer Science & Programming - Walter Savitch 38

Good Programming Practice:Identifier Naming Conventions

Always use meaningful names, e.g. finalExamScore, instead of something like x, or

even just score.

Use only letters and digits.

Capitalize interior words in multi-word names, e.g. answerLetter.

Names of classes start with an uppercase letter.

» every program in Java is a class as well as a program.

Names of variables, objects, and methods start with a

lowercase letter.