Faculty of Sciences and Social Sciences HOPE Structured Problem Solving 2009-2010 Week 7: Java basics Stewart Blakeway

Slides:



Advertisements
Similar presentations
Designing a Program & the Java Programming Language
Advertisements

Programming for Beginners
Dr. Ken Hoganson, © August 2014 Programming in R COURSE NOTES 2 Hoganson Language Translation.
Copyright © 2002 W. A. Tucker1 Chapter 1 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 8: Java: Selection and Repetition Stewart Blakeway.
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 12: Data Structures 1 Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213
The Analytical Engine Module 6 Program Translation.
1 Lecture 1  Getting ready to program  Hardware Model  Software Model  Programming Languages  The C Language  Software Engineering  Programming.
Program Flow Charting How to tackle the beginning stage a program design.
Chapter 16 Programming and Languages: Telling the Computer What to Do.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Introduction to a Programming Environment
CS 104 Introduction to Computer Science and Graphics Problems Software and Programming Language (2) Programming Languages 09/26/2008 Yang Song (Prepared.
Programming. Software is made by programmers Computers need all kinds of software, from operating systems to applications People learn how to tell the.
Types of Computer/Programming Languages Machine Language Symbolic Languages There Are Two Types Of Symbolic Languages Low level Language » Low level language.
Concept of Computer Programming November 2, 2011.
Assembly & Machine Languages
Computer Science 101 Introduction to Programming.
CS102 Introduction to Computer Programming
Introduction to Computer Programming
1 Chapter-01 Introduction to Computers and C++ Programming.
สาขาวิชาเทคโนโลยี สารสนเทศ คณะเทคโนโลยีสารสนเทศ และการสื่อสาร.
UNIVERSITI TENAGA NASIONAL “Generates Professionals” CHAPTER 4 : Part 2 INTRODUCTION TO SOFTWARE DEVELOPMENT: PROGRAMMING & LANGUAGES.
Hello World 2 What does all that mean?.
Levels of Architecture & Language CHAPTER 1 © copyright Bobby Hoggard / material may not be redistributed without permission.
AS Computing Introduction to Programming. What is a Computer Program? A list of instructions that a computer must work through, in a logical sequence,
Programming Language Rico Yu. Levels of Programming Languages 1.Low level languages 2.High level languages.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Evolution of Programming Languages Generations of PLs.
COMPUTER PROGRAMS AND LANGUAGES Chapter 4. Developing a computer program Programs are a set (series) of instructions Programmers determine The instructions.
Instructor: Tina Tian. About me Office: RLC 203A Office Hours: Wednesday 1:30 - 4:30 PM or .
Computer Science 101 Introduction to Programming.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 13: An Introduction to C++
Programming With C.
Basic of Programming Language Skill Area Computer System Computer Program Programming Language Programmer Translators.
Computer Systems Week 7: Looping and Input/Output with 3-bit Alma Whitfield.
Programming, an introduction to Pascal
1 Text Reference: Warford. 2 Computer Architecture: The design of those aspects of a computer which are visible to the programmer. Architecture Organization.
Represents different voltage levels High: 5 Volts Low: 0 Volts At this raw level a digital computer is instructed to carry out instructions.
CSC1200 INTRODUCTION TO PROGRAMMING Dr. Maureen Markel
Structured Programming (4 Credits) HNDIT Week 2 – Learning Outcomes Design an algorithmic solution for simple problem such as computation of a factorial,
Programming and Languages Dept. of Computer and Information Science IUPUI.
Computer Systems – Machine & Assembly code. Objectives Machine Code Assembly Language Op-code Operand Instruction Set.
Controlling Program Flow with Decision Structures.
ICT PROGRAMMING INTRODUCTION. WHAT & WHY PROGRAMMING Programming is a process of developing computer programs Computer program is a set of instructions.
Skill Area 311 Part B. Lecture Overview Assembly Code Assembler Format of Assembly Code Advantages Assembly Code Disadvantages Assembly Code High-Level.
Lecture #1: Introduction to Algorithms and Problem Solving Dr. Hmood Al-Dossari King Saud University Department of Computer Science 6 February 2012.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
PROGRAMMING FUNDAMENTALS INTRODUCTION TO PROGRAMMING. Computer Programming Concepts. Flowchart. Structured Programming Design. Implementation Documentation.
小型系統 心情 vs. 古典樂 心情 vs. 古典樂 浪漫求籤系統 美食導航系統 季潔亭雅鈺熒岱芸 美食導航系統 楊氏音樂模擬大會考人瑋 若維 芷萱 伽倩 楊氏音樂模擬大會考 麥當勞熱量計算系統 火星文困擾你嗎 ? 火星文困擾你嗎 ? 歌詞知多少 - 挑戰你的腦容量英琪 日馨 青雪 鈺娟.
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
Software Engineering Algorithms, Compilers, & Lifecycle.
Programming Languages Salihu Ibrahim Dasuki (PhD) CSC102 INTRODUCTION TO COMPUTER SCIENCE.
PROGRAMMING (1) LECTURE # 1 Programming and Languages: Telling the Computer What to Do.
High-level language programming paradigms. Programming languages come in many forms or 'paradigms'. Each form of language offers advantages over other.
Component 1.6.
GCSE COMPUTER SCIENCE Computers 1.5 Assembly Language.
CSCI-235 Micro-Computer Applications
Computer Programming.
Hello World 2 What does all that mean?.
Low Level Programming Languages
MARIE: An Introduction to a Simple Computer
Introduction to Computer Programming
Lecture 8 Programming Paradigm & Languages. Programming Languages The process of telling the computer what to do Also known as coding.
Tonga Institute of Higher Education IT 141: Information Systems
Tonga Institute of Higher Education IT 141: Information Systems
Presentation transcript:

Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 7: Java basics Stewart Blakeway

Faculty of Sciences and Social Sciences HOPE Java: The Basics Pages

Faculty of Sciences and Social Sciences HOPE What 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

Faculty of Sciences and Social Sciences HOPE What we shall do today What a computer program is Low level programming High level programming The Java programming language 4

Faculty of Sciences and Social Sciences HOPE What we shall do today What a computer program is. Low level programming High level programming The Java programming language 5

Faculty of Sciences and Social Sciences HOPE A 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

Faculty of Sciences and Social Sciences HOPE ThreeBit Machine code (non-mnemonics mode) 7

Faculty of Sciences and Social Sciences HOPE What we shall do today What a computer program is. Low level programming High level programming The Java programming language 8

Faculty of Sciences and Social Sciences HOPE ThreeBit Instruction format

Faculty of Sciences and Social Sciences HOPE ThreeBit Instruction format Op code What the instruction is to do (add, multiply, move)

Faculty of Sciences and Social Sciences HOPE ThreeBit Instruction format Op code What the instruction is to do (add, multiply, move) Operand What the instruction is to do it to

Faculty of Sciences and Social Sciences HOPE Programming 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

Faculty of Sciences and Social Sciences HOPE Programming in machine code First generation language 13

Faculty of Sciences and Social Sciences HOPE 1950s solution: Assembly language Assembly language – uses mnemonics for op codes – like ThreeBit LDI LDD STD ADD SUB JMP JEZ STP 14

Faculty of Sciences and Social Sciences HOPE 1950s solution: Assembly language 15

Faculty of Sciences and Social Sciences HOPE 1950s 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

Faculty of Sciences and Social Sciences HOPE 1950s 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

Faculty of Sciences and Social Sciences HOPE 1950s 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

Faculty of Sciences and Social Sciences HOPE 1950s 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

Faculty of Sciences and Social Sciences HOPE What we shall do today What a computer program is. Low level programming High level programming The Java programming language 20

Faculty of Sciences and Social Sciences HOPE 1960s 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

Faculty of Sciences and Social Sciences HOPE 1960s 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

Faculty of Sciences and Social Sciences 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 23

Faculty of Sciences and Social Sciences HOPE COBOL 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

Faculty of Sciences and Social Sciences HOPE 1960s 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

Faculty of Sciences and Social Sciences HOPE 1960s 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

Faculty of Sciences and Social Sciences HOPE Compiling a program This program is written in C – another high level language 27

Faculty of Sciences and Social Sciences HOPE On 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

Faculty of Sciences and Social Sciences HOPE Making 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

Faculty of Sciences and Social Sciences HOPE Programming 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

Faculty of Sciences and Social Sciences HOPE Structure of a Java program in Java Trainer 31

Faculty of Sciences and Social Sciences HOPE Structure of a Java program in Java Trainer 32

Faculty of Sciences and Social Sciences HOPE Translating a design into Java A := 8 B := 11 C := A + B display ‘Answer is ‘, C 33 The design …

Faculty of Sciences and Social Sciences HOPE Translating a design into Java We need to create two sections – declarations – statements 34 Design A := 8 B := 11 C := A + B display ‘Answer is ‘, C

Faculty of Sciences and Social Sciences HOPE Translating a design into Java Create a data table 35 Design A := 8 B := 11 C := A + B display ‘Answer is ‘, C

Faculty of Sciences and Social Sciences HOPE Translating a design into Java Create the declarations 36 int a; int b; int c;

Faculty of Sciences and Social Sciences HOPE 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; 37 Translating a design into Java

Faculty of Sciences and Social Sciences HOPE Create the statements 38 Design A := 8 B := 11 C := A + B display ‘Answer is ‘, C a = 8; b = 11; c = a + b; System.out.println(“Answer is: ” + c); Translating a design into Java

Faculty of Sciences and Social Sciences HOPE Read and write/display in Java 39 Design read(X) display X display ‘X is’, X Java System.in.read(x); System.out.println(x); System.out.println(“x is ”+x);

Faculty of Sciences and Social Sciences HOPE print 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: One Two Three

Faculty of Sciences and Social Sciences HOPE Printing 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

Faculty of Sciences and Social Sciences HOPE Printing 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

Faculty of Sciences and Social Sciences HOPE Printing 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

Faculty of Sciences and Social Sciences HOPE Pascal and Java 44 Pascal Program PROGRAM AddVat; VAR Price, VAT, Total: Integer; BEGIN Price := 20; VAT := 3; Total := Price + VAT; WriteLn(VAT, Total); END. Java Trainer equivalent int Price; int VAT; int Total; Price = 20; VAT = 3; Total = Price + VAT; System.out.println(VAT, Total);

Faculty of Sciences and Social Sciences HOPE Pascal and Java: declarations 45 Pascal Program PROGRAM AddVat; VAR Price, VAT, Total: Integer; BEGIN Price := 20; VAT := 3; Total := Price + VAT; WriteLn(VAT, Total); END. Java Trainer equivalent int Price; int VAT; int Total; Price = 20; VAT = 3; Total = Price + VAT; System.out.println(VAT, Total);

Faculty of Sciences and Social Sciences HOPE Pascal and Java: statements 46 Pascal Program PROGRAM AddVat; VAR Price, VAT, Total: Integer; BEGIN Price := 20; VAT := 3; Total := Price + VAT; WriteLn(VAT, Total); END. Java Trainer equivalent int Price; int VAT; int Total; Price = 20; VAT = 3; Total = Price + VAT; System.out.println(VAT, Total);

Faculty of Sciences and Social Sciences HOPE Let’s try a program int age; int yearOfBirth; System.out.print ("How old are you?"); System.in.read(age); yearOfBirth = 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) );

Faculty of Sciences and Social Sciences HOPE Another 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?");

Faculty of Sciences and Social Sciences HOPE Arrgh! 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?

Faculty of Sciences and Social Sciences HOPE Arrgh! 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?

Faculty of Sciences and Social Sciences HOPE Any Questions? We have covered – What a computer program is. – Low level programming – High level programming – The Java programming language