1 Computer Systems -- Introduction Chapter 1 focuses on: the structure of a Java application ...

30
1 Computer Systems -- Introduction Chapter 1 focuses on: the structure of a Java application basic program elements preparing and executing a program basic object-oriented programming concepts helpful support for writing software Java applets

Transcript of 1 Computer Systems -- Introduction Chapter 1 focuses on: the structure of a Java application ...

Page 1: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

1

Computer Systems -- Introduction

Chapter 1 focuses on:

the structure of a Java application

basic program elements

preparing and executing a program

basic object-oriented programming concepts

helpful support for writing software

Java applets

Page 2: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

2

Focus of the Course

Object-Oriented Software Development

problem solving program design and implementation object-oriented concepts

• objects• classes• interfaces• inheritance• polymorphism

graphics and Graphical User Interfaces the Java programming language

Page 3: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

3

Programming Languages

A programming language specifies the words and symbols that we can use to write a program

There are four basic programming language levels:

• machine language• assembly language• high-level language• fourth-generation language

Page 4: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

4

Machine Languages

Each CPU has its own specific machine language

machine-dependent code

Pentium processor has its own machine language SPARC process has its own machine language

hard to read, write, understand

1101 0000 0000 0111

1011 1111 1110 1000

1101 0010 0000 0111

Page 5: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

5

Assembly language

Symbolic code for machine language

Machine-dependent Easier to read, write, and understand than machine langua

ge

ld [%fp-20], %o0

ld [%fp-24], %o1

add %o0, %o1, %o0 Still, assembly programming is a difficult job

Assembler

assemblycode

assemblycode

machinecode

machinecodeassemblerassembler

Page 6: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

6

High-level Languages

Machine-independent language

Fortran, COBOL, C, C++, Java, … easy to program

a + b

Program execution

compilation(translation) interpretation mixture of the two

Page 7: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

7

Compiler

A program must be translated into machine language

a software tool which translate source code into target code

sourcecode

sourcecode

targetcodetargetcodecompilercompiler

Fortran

C, C++

Assembly code

Machine code

Page 8: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

8

Interpreter

A software tool which read a program and interpret(excute) it

Basic, Prolog, ML, ….

inputinput outputoutputinterpreterinterpreter

programprogram

Page 9: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

9

The Java Programming Language

Java was created by Sun Microsystems, Inc.

It was introduced in 1995 and has become quite popular

It is an object-oriented language

It is an Internet programming language

Page 10: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

10

Problem Solving

The purpose of writing a program is to solve a problem

The general steps in problem solving are:

Understand the problem

Dissect the problem into manageable pieces

Design a solution

Consider alternatives to the solution and refine it

Implement the solution

Test the solution and fix any problems that exist

Page 11: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

11

Java Program Structure

See Lincoln.java

A program is made up of one or more classes

A class contains one or more methods

A method contains program statements

A Java application always executes the main method

Page 12: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

12

Java Program Structure

public class MyProgram

{

}

// comments about the class

class headerclass header

class bodyclass body

Comments can be added almost anywhereComments can be added almost anywhere

Page 13: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

13

Java Program Structure

public class MyProgram

{

}

public static void main (String[] args)

{

}

// comments about the class

// comments about the method

method headermethod headermethod bodymethod body

Page 14: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

14

Lincoln.java

class Lincoln {

public static void main (String[] args) {

System.out.println ("Whatever you are, be a good one.");

} // method main

} // class Lincoln

Page 15: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

15

White Space

Spaces, blank lines, and tabs to separate words and symbols in a program

Extra white space is ignored

A valid Java program can be formatted in many different ways

See Lincoln2.java and Lincoln3.java

Programs should be formatted to enhance readability, using consistent indentation

Page 16: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

16

Lincoln2.java

class Lincoln2 { public static void main (String[] args)

{ System.out.println ("Whatever you are, be a good one."); } }

Page 17: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

17

Lincoln3.java

class Lincoln3 { public static void main (String [] args ) { System.out.println ( "Whatever you are, be a good one." ) ;} }

Page 18: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

18

Comments

Comments in a program are also called inline documentation

They should explain the purpose of the program and describe processing steps

Java comments can take two forms:

// comment runs to the end of the line

/* comment runs to terminating

symbol, even across line breaks */

Page 19: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

19

Identifiers

The words a programmer uses in a program

Most identifiers have no predefined meaning

An identifier can be made up of

letters, digits, _, and $

they cannot begin with a digit

Java is case sensitive,

Total and total are different identifiers

Page 20: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

20

Reserved Words

Some identifiers, called reserved words, have predefined meanings.

abstractbooleanbreakbytebyvaluecasecastcatchcharclassconstcontinue

defaultdodoubleelseextendsfalsefinalfinallyfloatforfuturegeneric

gotoifimplementsimportinnerinstanceofintinterfacelongnativenewnull

operatorouterpackageprivateprotectedpublicrestreturnshortstaticsuperswitch

synchronizedthisthrowthrowstransienttruetryvarvoidvolatilewhile

Page 21: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

21

Literals

A literal is an explicit data value used in a program

Integer literals:

25 69 -4288

Floating point literals:

3.14159 42.075 -0.5

String literals:

"The result is: "

"To thine own self be true."

Page 22: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

22

Java Translation and Execution

The Java compiler translates Java source code into a special representation called bytecode

Java bytecode a virtual machine code is not the machine language for any traditional CPU

Interpreter interpret Java bytecode

Java compiler is not tied to any particular machine Java is considered to be architecture-neutral

Page 23: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

23

Java Translation and Execution

Java sourcecode

Machinecode

Javabytecode

Javainterpreter

Bytecodecompiler

Javacompiler

Page 24: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

24

Java Translation and Execution

Compiling a Java program

> javac Lincoln.java

This creates a file called Lincoln.class

Interpret the class file

> java Lincoln

The .java extension is used at compile time, but the .class extension is not used with the interpreter

Page 25: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

25

Three types of program errors

Compile-time errors The compiler will find problems with syntax and

semantics If compile-time errors exist, an executable version

of the program is not created

Run-time errors A problem can occur during program execution, divide by zero

Logical errors A program may run, but produce incorrect results

Page 26: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

26

Command Line Arguments

The main method accepts extra information on the command line when a program is executed

> java Name_Tag John

Each extra value is called command line argument

In Java, command line arguments are always read as a list of character strings

Page 27: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

27

Java Applets

A Java applet

a Java program that is intended to be sent across a network and executed using a Web browser

A Java application is a stand alone program

Applications have a main method, but applets do not

Applets are derived from the java.applet.Applet class

Page 28: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

28

Java Applets

See No_Parking.java

Links to applets can be embedded in HTML documents

Appletviewer in JDK

appletviewer xxx.html

Web browser

Netscape navigator Internet explorer

Page 29: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

29

No_Parking.java

import java.applet.Applet;

import java.awt.*;

public class No_Parking extends Applet {

public void paint (Graphics page) {

page.drawString ("Parking", 50, 50);

page.drawOval (45, 24, 43, 43);

page.drawLine (82, 30, 51, 61);

} // method paint

} // class No_Parking

Page 30: 1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.

30

Execution of Java Applets

Java sourcecode

Java sourcecode

Javabytecode

Javabytecode

Javacompiler

Javacompiler

Javainterpreter

Javainterpreter

Web browserWeb browser

local computer

remotecomputerremote

computer