Structured Problem Solving 2009-2010

Post on 22-Feb-2016

22 views 0 download

Tags:

description

Structured Problem Solving 2009-2010. Week 7: Java basics Stewart Blakeway blakews@hope.ac.uk 0151 291 3113. Java: The Basics. Pages 69 - 87. What we have done already. Seen what an algorithm is a set of instructions that, if carried out, will lead to a successful conclusion - PowerPoint PPT Presentation

Transcript of Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEStructured Problem Solving

2009-2010

Week 7: Java basicsStewart Blakewayblakews@hope.ac.uk0151 291 3113

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEJava: The Basics

Pages 69 - 87

2

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEWhat we have done already• Seen what an algorithm is

– a set of instructions that, if carried out, will lead to a successful conclusion

• 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

3

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEWhat we shall do today• What a computer program is• Low level programming• High level programming• The Java programming language

4

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEWhat we shall do today• What a computer program is.• Low level programming• High level programming• The Java programming language

5

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEA computer program

• .. 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

6

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEThreeBit• Machine code (non-mnemonics mode)

7

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEWhat we shall do today• What a computer program is.• Low level programming• High level programming• The Java programming language

8

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEThreeBit Instruction format

9

001 01101

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEThreeBit Instruction format

10

001 01101

Op codeWhat the instruction

is to do(add, multiply, move)

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEThreeBit Instruction format

11

001 01101

Op codeWhat the instruction

is to do(add, multiply, move)

OperandWhat the instruction

is to do it to

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEProgramming in machine code

• Almost impossible to spot errors. • Easy to mistype a 1 for a 0 and vice versa

without noticing• Boring to do• Meaningless to read

12

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEProgramming in machine code

• First generation language

13

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE1950s solution: Assembly language• Assembly language

– uses mnemonics for op codes – like ThreeBit

• LDI• LDD• STD• ADD• SUB• JMP• JEZ• STP

14

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE1950s solution: Assembly language

15

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE1950s solution: Assembly language• LDI 20

– Load the CPU accumulator register with the value 20

• 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

16

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE1950s solution : Assembly language• Other processors use different mnemonics

• mov a, 0 • moves 0 into a register labelled a

• Special program translates assembly language into machine code

• Assembler

17

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE1950s solution : Assembly language• One line of assembly code for one line of

machine code

• Assembly language is a low level language – it is very close to being in written in terms the machine understands

18

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE1950s solution : Assembly language• 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

19

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEWhat we shall do today• What a computer program is.• Low level programming• High level programming• The Java programming language

20

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE1960s solution: High level languages• Pioneers decided to:

– 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

21

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE1960s solution: High level languages

• FORTRAN, COBOL were the earliest– FORTRAN aimed at scientist and engineers– COBOL aimed at business people

• Look like Structured English

22

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEFORTRAN 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

23

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PECOBOL Example

IF HOURS_WORKED OF PAYROLL_RECORD IS GREATER THAN 40 PERFORM PAY_CALCULATION_WITH_OVERTIME ELSE PERFORM_PAY_CALCULATION_NO_OVERTIME

COMPUTE GROSS_PAY = REGULAR_PAY + OVERTIME_PAY

Business oriented language

COBOL == COmmon Business Oriented Language

24

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE1960s solution: High level languages

• 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

25

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE1960s solution: High level languages

• Compiler– Special program translates high level language

into machine code

• 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

26

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PECompiling a program

• This program is written in C– another high level language

27

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEOn this module ...

• ... 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++

28

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEMaking a Program

• 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

29

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEProgramming house style

• 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

30

while (a>b) { System.in.read(first); }

Three spaces in indentation

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEStructure of a Java program in Java Trainer

31

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEStructure of a Java program in Java Trainer

32

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PETranslating a design into Java

A := 8B := 11C := A + Bdisplay ‘Answer is ‘, C

33

The design …

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PETranslating a design into Java

• We need to create two sections– declarations– statements

34

DesignA := 8B := 11C := A + Bdisplay ‘Answer is ‘, C

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PETranslating a design into Java

• Create a data table

35

DesignA := 8B := 11C := A + Bdisplay ‘Answer is ‘, C

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PETranslating a design into Java

• Create the declarations

36

int a;int b;int c;

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEIf 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;

37

Translating a design into Java

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE• Create the statements

38

DesignA := 8B := 11C := A + Bdisplay ‘Answer is ‘, C

a = 8;b = 11;c = a + b;System.out.println(“Answer is: ” + c);

Translating a design into Java

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PERead and write/display in Java

39

Design

read(X)

display X

display ‘X is’, X

JavaSystem.in.read(x);System.out.println(x);System.out.println(“x is ”+x);

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEprint and println

40

System.out.print(“One ”);System.out.print(“Two ”);System.out.print(“Three ”);

System.out.println(“One ”);System.out.println(“Two ”);System.out.println(“Three ”);

Produces:

One Two Three

Produces:

OneTwoThree

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEPrinting text and variable values

41

double a;int b;a = 4.7;b = 8;System.out.println(“a is ” + a + “ b is “ + b);

Output:a is 4.7 b is 8

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEPrinting text and variable values

42

double a;int b;a = 4.7;b = 8;System.out.println(“a is ” + a + “ b is “ + b);

Text between quotes printed exactly as written

Output:a is 4.7 b is 8

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEPrinting text and variable values

43

double a;int b;a = 4.7;b = 8;System.out.println(“a is ” + a + “ b is “ + b);

Text outside quotes treated as variable names – print

their values

Output:a is 4.7 b is 8

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEPascal and Java

44

Pascal ProgramPROGRAM AddVat;VAR Price, VAT, Total: Integer;BEGIN Price := 20; VAT := 3; Total := Price + VAT; WriteLn(VAT, Total);END.

Java Trainer equivalentint Price;

int VAT;

int Total;

Price = 20;VAT = 3;Total = Price + VAT;System.out.println(VAT, Total);

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEPascal and Java: declarations

45

Pascal ProgramPROGRAM AddVat;VAR Price, VAT, Total: Integer;BEGIN Price := 20; VAT := 3; Total := Price + VAT; WriteLn(VAT, Total);END.

Java Trainer equivalentint Price;

int VAT;

int Total;

Price = 20;VAT = 3;Total = Price + VAT;System.out.println(VAT, Total);

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEPascal and Java: statements

46

Pascal ProgramPROGRAM AddVat;VAR Price, VAT, Total: Integer;BEGIN Price := 20; VAT := 3; Total := Price + VAT; WriteLn(VAT, Total);END.

Java Trainer equivalentint Price;

int VAT;

int Total;

Price = 20;VAT = 3;Total = Price + VAT;System.out.println(VAT, Total);

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PELet’s try a program

int age;int yearOfBirth;

System.out.print ("How old are you?");System.in.read(age);yearOfBirth = 2010 - age;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

HO

PEAnother

String name;String doing;

System.out.println ("Who are you?");System.in.read (name);System.out.println ("What are you doing");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

HO

PEArrgh! Syntax!

int a;int B;int c;

a = 9;b = 12;C = 0;

c = a;a = b;b = c

3 Syntax Errors

Can you spot them?

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEArrgh! Syntax!

double wages;double tax;

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");

3 Syntax Errors

Can you spot them?

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PEAny Questions?

• We have covered– What a computer program is.– Low level programming– High level programming– The Java programming language