1 Computer Systems -- Introduction Chapter 1 focuses on: the structure of a Java application ...
-
Upload
elvin-booth -
Category
Documents
-
view
216 -
download
1
Transcript of 1 Computer Systems -- Introduction Chapter 1 focuses on: the structure of a Java application ...
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
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
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
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
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
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
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
8
Interpreter
A software tool which read a program and interpret(excute) it
Basic, Prolog, ML, ….
inputinput outputoutputinterpreterinterpreter
programprogram
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
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
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
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
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
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
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
16
Lincoln2.java
class Lincoln2 { public static void main (String[] args)
{ System.out.println ("Whatever you are, be a good one."); } }
17
Lincoln3.java
class Lincoln3 { public static void main (String [] args ) { System.out.println ( "Whatever you are, be a good one." ) ;} }
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 */
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
20
Reserved Words
Some identifiers, called reserved words, have predefined meanings.
abstractbooleanbreakbytebyvaluecasecastcatchcharclassconstcontinue
defaultdodoubleelseextendsfalsefinalfinallyfloatforfuturegeneric
gotoifimplementsimportinnerinstanceofintinterfacelongnativenewnull
operatorouterpackageprivateprotectedpublicrestreturnshortstaticsuperswitch
synchronizedthisthrowthrowstransienttruetryvarvoidvolatilewhile
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."
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
23
Java Translation and Execution
Java sourcecode
Machinecode
Javabytecode
Javainterpreter
Bytecodecompiler
Javacompiler
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
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
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
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
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
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
30
Execution of Java Applets
Java sourcecode
Java sourcecode
Javabytecode
Javabytecode
Javacompiler
Javacompiler
Javainterpreter
Javainterpreter
Web browserWeb browser
local computer
remotecomputerremote
computer