Transcript Document

HOPE
Structured Problem Solving
2009-2010
Week 7: Java basics
Stewart Blakeway
[email protected]
0151 291 3113
www.hope.ac.uk
Faculty of Sciences and Social Sciences
Java: The Basics
HOPE
Pages 69 - 87
www.hope.ac.uk
Faculty of Sciences and Social Sciences
2
What we have done already
• Seen what an algorithm is
– a set of instructions that, if carried out, will lead
to a successful conclusion
HOPE
• Learned how to represent algorithms in
– Structured English
– Flow charts
• Used variables to remember
• Applied the top down, stepwise refinement
approach to creating algorithms
• Looked at problems more oriented towards
being solved on a computer – stacks,
queues, functions, procedures
www.hope.ac.uk
Faculty of Sciences and Social Sciences
3
What we shall do today
HOPE
•
•
•
•
What a computer program is
Low level programming
High level programming
The Java programming language
www.hope.ac.uk
Faculty of Sciences and Social Sciences
4
What we shall do today
HOPE
•
•
•
•
What a computer program is.
Low level programming
High level programming
The Java programming language
www.hope.ac.uk
Faculty of Sciences and Social Sciences
5
A computer program
•
•
•
•
HOPE
.. is a set of instructions
Stored on disc
Patterns of 0s and 1s
Copied into main memory
when required
• Executed by CPU fetching
and executing the
instructions from main
memory
www.hope.ac.uk
Faculty of Sciences and Social Sciences
6
ThreeBit
HOPE
• Machine code (non-mnemonics mode)
www.hope.ac.uk
Faculty of Sciences and Social Sciences
7
What we shall do today
HOPE
•
•
•
•
What a computer program is.
Low level programming
High level programming
The Java programming language
www.hope.ac.uk
Faculty of Sciences and Social Sciences
8
ThreeBit Instruction format
HOPE
001 01101
www.hope.ac.uk
Faculty of Sciences and Social Sciences
9
ThreeBit Instruction format
HOPE
001 01101
Op code
What the instruction
is to do
(add, multiply, move)
www.hope.ac.uk
Faculty of Sciences and Social Sciences
10
ThreeBit Instruction format
HOPE
001 01101
Op code
What the instruction
is to do
(add, multiply, move)
www.hope.ac.uk
Operand
What the instruction
is to do it to
Faculty of Sciences and Social Sciences
11
Programming in machine code
HOPE
• Almost impossible to spot errors.
• Easy to mistype a 1 for a 0 and vice versa
without noticing
• Boring to do
• Meaningless to read
www.hope.ac.uk
Faculty of Sciences and Social Sciences
12
Programming in machine code
HOPE
• First generation language
www.hope.ac.uk
Faculty of Sciences and Social Sciences
13
1950s solution: Assembly language
HOPE
• Assembly language
– uses mnemonics for op codes
– like ThreeBit
•
•
•
•
•
•
•
•
LDI
LDD
STD
ADD
SUB
JMP
JEZ
STP
www.hope.ac.uk
Faculty of Sciences and Social Sciences
14
HOPE
1950s solution: Assembly language
www.hope.ac.uk
Faculty of Sciences and Social Sciences
15
1950s solution: Assembly language
• LDI 20
– Load the CPU accumulator register with the value
20
HOPE
• STD 30
– Copy the CPU accumulator register contents to
memory location 30
• ADD 10
– Add the contents of memory location 10 to the
CPU accumulator register
www.hope.ac.uk
Faculty of Sciences and Social Sciences
16
1950s solution : Assembly language
• Other processors use different mnemonics
• mov a, 0
• moves 0 into a register labelled a
HOPE
• Special program translates assembly
language into machine code
• Assembler
www.hope.ac.uk
Faculty of Sciences and Social Sciences
17
1950s solution : Assembly language
• One line of assembly code for one line of
machine code
HOPE
• Assembly language is a low level language –
it is very close to being in written in terms the
machine understands
www.hope.ac.uk
Faculty of Sciences and Social Sciences
18
1950s solution : Assembly language
HOPE
• Easier to write but still difficult to get right
• Many hundreds of instructions to do simple
things like input and output
• Can only run on one type of machine
• Not easy to tell what an assembler program
is intended to do by reading it
www.hope.ac.uk
Faculty of Sciences and Social Sciences
19
What we shall do today
HOPE
•
•
•
•
What a computer program is.
Low level programming
High level programming
The Java programming language
www.hope.ac.uk
Faculty of Sciences and Social Sciences
20
1960s solution: High level
languages
• Pioneers decided to:
HOPE
– enable scientist, engineer and business programmers to
write programs in a language that looked familiar to them
– avoid needing to know the internal structure of the
computer
– high level programming language
• Invented compiler programs that would translate
high level programs into machine code
automatically
– the compiler can generate machine code from the high
level language
• E.g. GOTO becomes JMP op code
– the compiler knows the internal structure of the computer
www.hope.ac.uk
Faculty of Sciences and Social Sciences
21
1960s solution: High level
languages
• FORTRAN, COBOL were the earliest
– FORTRAN aimed at scientist and engineers
– COBOL aimed at business people
HOPE
• Look like Structured English
www.hope.ac.uk
Faculty of Sciences and Social Sciences
22
HOPE
FORTRAN Example
READ *, A, B, C
X1 = (-B + SQRT(B*B - 4*A*C)) / (2*A)
X2 = (-B - SQRT(B*B - 4*A*C)) / (2*A)
Science and Engineering Oriented Language
FORmula TRANslation
www.hope.ac.uk
Faculty of Sciences and Social Sciences
23
COBOL Example
IF HOURS_WORKED OF PAYROLL_RECORD IS GREATER THAN 40
PERFORM PAY_CALCULATION_WITH_OVERTIME
ELSE
PERFORM_PAY_CALCULATION_NO_OVERTIME
HOPE
COMPUTE GROSS_PAY = REGULAR_PAY + OVERTIME_PAY
Business oriented language
COBOL == COmmon Business Oriented Language
www.hope.ac.uk
Faculty of Sciences and Social Sciences
24
1960s solution: High level
languages
HOPE
• Key features of a high level language
– Written in a language the user can understand
– Program looks like an algorithm so easier to write
– Does not depend on knowledge of internal
workings of the computer
– Can be run in different types of machines
www.hope.ac.uk
Faculty of Sciences and Social Sciences
25
1960s solution: High level
languages
• Compiler
– Special program translates high level language
into machine code
HOPE
• Lots of different languages
– C, C++, Java, Python, PHP, Basic, Visual Basic,
C#, Pascal, Fortran, COBOL, Lisp, Prolog
– which one to use ?
– discussion on page 71
www.hope.ac.uk
Faculty of Sciences and Social Sciences
26
HOPE
Compiling a program
• This program is written in C
– another high level language
www.hope.ac.uk
Faculty of Sciences and Social Sciences
27
On this module ...
HOPE
• ... we shall use the high level programming
language called Java
• But what we do with Java can be done
with many other languages e.g. Pascal, C,
C++
www.hope.ac.uk
Faculty of Sciences and Social Sciences
28
Making a Program
HOPE
• The steps to be followed in creating a
successful program include:
– writing down a design for the program in
Structured English
– translating the design into the chosen high level
language: Java in your case
www.hope.ac.uk
Faculty of Sciences and Social Sciences
29
Programming house style
HOPE
• Using a house style looks more professional
• Follow the Liverpool Hope house style
– See Appendix B, page 106
– variable names and method names in lower case
– Three spaces for each indentation
Three spaces
in indentation
while (a>b)
{
System.in.read(first);
}
www.hope.ac.uk
Faculty of Sciences and Social Sciences
30
HOPE
Structure of a Java program in
Java Trainer
www.hope.ac.uk
Faculty of Sciences and Social Sciences
31
HOPE
Structure of a Java program in
Java Trainer
www.hope.ac.uk
Faculty of Sciences and Social Sciences
32
HOPE
Translating a design into Java
The design …
A := 8
B := 11
C := A + B
display ‘Answer is ‘, C
www.hope.ac.uk
Faculty of Sciences and Social Sciences
33
Translating a design into Java
• We need to create two sections
HOPE
– declarations
– statements
Design
A := 8
B := 11
C := A + B
display ‘Answer is ‘, C
www.hope.ac.uk
Faculty of Sciences and Social Sciences
34
Translating a design into Java
• Create a data table
HOPE
Design
A := 8
B := 11
C := A + B
display ‘Answer is ‘, C
www.hope.ac.uk
Faculty of Sciences and Social Sciences
35
Translating a design into Java
HOPE
• Create the
declarations
int a;
int b;
int c;
www.hope.ac.uk
Faculty of Sciences and Social Sciences
36
HOPE
Translating a design into Java
If we want to store numbers
with a decimal fractional part
e.g. money values, then we
must declare the variable as a
real number having double
precision:
double x;
www.hope.ac.uk
Faculty of Sciences and Social Sciences
37
Translating a design into Java
HOPE
Design
A := 8
B := 11
C := A + B
display ‘Answer is ‘, C
• Create the statements
a = 8;
b = 11;
c = a + b;
System.out.println(“Answer is:
www.hope.ac.uk
” + c);
Faculty of Sciences and Social Sciences
38
Read and write/display in Java
Design
Java
System.in.read(x);
display X
System.out.println(x);
display ‘X is’, X
System.out.println(“x is
HOPE
read(X)
www.hope.ac.uk
Faculty of Sciences and Social Sciences
”+x);
39
print and println
System.out.print(“One ”);
System.out.print(“Two ”);
Produces:
One Two Three
HOPE
System.out.print(“Three ”);
System.out.println(“One ”);
Produces:
System.out.println(“Two ”);
One
System.out.println(“Three ”);
Two
Three
www.hope.ac.uk
Faculty of Sciences and Social Sciences
40
Printing text and variable values
double a;
HOPE
int b;
a = 4.7;
Output:
a is 4.7 b is 8
b = 8;
System.out.println(“a is ” + a + “ b is “ + b);
www.hope.ac.uk
Faculty of Sciences and Social Sciences
41
Printing text and variable values
double a;
HOPE
int b;
a = 4.7;
Output:
a is 4.7 b is 8
b = 8;
System.out.println(“a is ” + a + “ b is “ + b);
Text between quotes
printed exactly as written
www.hope.ac.uk
Faculty of Sciences and Social Sciences
42
Printing text and variable values
double a;
HOPE
int b;
a = 4.7;
Output:
a is 4.7 b is 8
b = 8;
System.out.println(“a is ” + a + “ b is “ + b);
Text outside quotes treated
as variable names – print
their values
www.hope.ac.uk
Faculty of Sciences and Social Sciences
43
Pascal and Java
Pascal Program
Java Trainer equivalent
HOPE
PROGRAM AddVat;
VAR Price, VAT, Total: Integer;
BEGIN
Price := 20;
VAT := 3;
Total := Price + VAT;
WriteLn(VAT, Total);
END.
www.hope.ac.uk
int Price;
int VAT;
int Total;
Price = 20;
VAT = 3;
Total = Price + VAT;
System.out.println(VAT, Total);
Faculty of Sciences and Social Sciences
44
Pascal and Java: declarations
Pascal Program
Java Trainer equivalent
HOPE
PROGRAM AddVat;
VAR Price, VAT, Total: Integer;
BEGIN
Price := 20;
VAT := 3;
Total := Price + VAT;
WriteLn(VAT, Total);
END.
www.hope.ac.uk
int Price;
int VAT;
int Total;
Price = 20;
VAT = 3;
Total = Price + VAT;
System.out.println(VAT, Total);
Faculty of Sciences and Social Sciences
45
Pascal and Java: statements
Pascal Program
Java Trainer equivalent
HOPE
PROGRAM AddVat;
VAR Price, VAT, Total: Integer;
BEGIN
Price := 20;
VAT := 3;
Total := Price + VAT;
WriteLn(VAT, Total);
END.
www.hope.ac.uk
int Price;
int VAT;
int Total;
Price = 20;
VAT = 3;
Total = Price + VAT;
System.out.println(VAT, Total);
Faculty of Sciences and Social Sciences
46
Let’s try a program
int age;
int yearOfBirth;
System.out.print ("How old are you?");
System.in.read(age);
yearOfBirth = 2010 - age;
HOPE
System.out.println ("Ah, you must of been born in " + yearOfBirth);
int age;
System.out.print ("How old are you?");
System.in.read(age);
System.out.println ("Ah, you must of been born in " + (2010-age) );
www.hope.ac.uk
Faculty of Sciences and Social Sciences
Another
String name;
String doing;
System.out.println ("Who are you?");
System.in.read (name);
System.out.println ("What are you doing");
HOPE
System.in.read (doing);
System.out.println ("Hello " + name + " why are you " + doing + " when
you could be programming?");
www.hope.ac.uk
Faculty of Sciences and Social Sciences
Arrgh! Syntax!
int a;
int B;
int c;
HOPE
a = 9;
b = 12;
3 Syntax Errors
Can you spot them?
C = 0;
c = a;
a = b;
b = c
www.hope.ac.uk
Faculty of Sciences and Social Sciences
3 Syntax Errors
Arrgh! Syntax!
Can you spot them?
double wages;
double tax;
HOPE
System.out.println ("How much did you get paid
this week?");
System.In.read(wages);
tax = wage * 0.20;
System.out.println ("You owe £" + tax " in
taxes");
www.hope.ac.uk
Faculty of Sciences and Social Sciences
Any Questions?
HOPE
• We have covered
–
–
–
–
What a computer program is.
Low level programming
High level programming
The Java programming language
www.hope.ac.uk
Faculty of Sciences and Social Sciences