Presentation is loading. Please wait.

Presentation is loading. Please wait.

310417 2000 WEEK 1 1 What is a Computer Program? An algorithm that can be executed on a computer is a program. A program usually contains several algorithms.

Similar presentations


Presentation on theme: "310417 2000 WEEK 1 1 What is a Computer Program? An algorithm that can be executed on a computer is a program. A program usually contains several algorithms."— Presentation transcript:

1 310417 2000 WEEK 1 1 What is a Computer Program? An algorithm that can be executed on a computer is a program. A program usually contains several algorithms. A program is DATA plus PROCESSING. Examples the set of calculations required to work out your tax payable (algorithms for separate parts, too, e.g. gross income, deductions, Medicare) the calculation to work out whether this is a leap year

2 310417 2000 WEEK 1 2 A Program (Calculate Interest on a Bank Account) calculate interest get balance get interest rate interest = balance * interest rate balance interestRate interest 10005.050 variables

3 310417 2000 WEEK 1 3 History of Programming Languages MACHINE CODE ASSEMBLER LANGUAGES HIGH-LEVEL LANGUAGES 4GLs 5GLs ForTran, COBOL, C, C ++, LISP, Pascal, Java,... ORACLE, SEQUEL, INGRES,... artificial intelligence

4 310417 2000 WEEK 1 4 Different Programming Languages Machine code Assembler 000 100C LDA VALUE 006 300D ADA TOTAL 007 200D STA TOTAL 100 DW1 TOTAL 110 DW1 VALUE COBOL FORTRAN LISP Pascal C, C ++, Java ADD VALUE TO TOTAL TOTAL = TOTAL + VALUE (set! total (+ total value)) total := total + value total += value;

5 310417 2000 WEEK 1 5 Program Translation A program written in a high level language must be translated into machine code (or executable code ) before the computer can run it (i.e., execute the instructions). The program as written is called the source code. The translation is done by a program called a compiler or an interpreter. A compiler attempts to translate an entire program and gives a list of the problems it found (if any). This is a batch process. An interpreter translates one instruction at a time and causes it to be executed at once. This is an interactive process.

6 310417 2000 WEEK 1 6 Translation of Java Programs Java programs are designed to be able to run on any kind of computer when downloaded from the Web. With most languages, that would mean downloading source code for the program and having a compiler translate it into the machine code for your machine. The user would have to tell the machine to compile the source code before running the program. Java uses bytecodes to solve this problem.

7 310417 2000 WEEK 1 7 Java Bytecodes The Java compiler translates the source code to bytecodes, the machine code for an imaginary machine. The bytecodes are downloaded, then translated by an interpreter on the local machine to its own machine code. This may sound like no improvement, but it is easier to write a bytecode interpreter for a particular machine than a compiler for source programs. Each type of machine has its own bytecode interpreter for Java. These are downloadable from the Web, and included in web browsers.

8 310417 2000 WEEK 1 8 Executing a Java Program EditorCompiler Bytecode Interpreter Bytecode Verifier copy of bytecodes source code (Maybe) On remote computer? On local computer

9 310417 2000 WEEK 1 9 Objects We are all familiar with the idea of an object. We are surrounded by them: cars, books, people, houses, cats, etc. Objects have attributes, e.g. colour, size, age, name. Objects also have behaviour. They can do things, e.g. grow, breathe, run, stop.

10 310417 2000 WEEK 1 10 Attributes and Behaviour Car Cat AttributesBehaviour Registration number Make Model Year Colour Start Stop Turn Accelerate Flash lights Name Breed Colour Age Weight Sleep Eat Purr Climb Scratch

11 310417 2000 WEEK 1 11 Abstraction and Models The previous slide is not intended to suggest that every object will have 5 attributes and 5 behaviours. We can all think of many more attributes and behaviours of cars and cats. In a program, we choose the attributes and behaviours that are relevant to the problem we are trying to solve. This process of selecting some aspects of the objects and ignoring the irrelevant ones is called abstraction. We create a model that is a simplification of the real situation, strictly relevant to the problem to be solved.

12 310417 2000 WEEK 1 12 Simulation When we write a program, we are often simulating something that happens in real life. The models we create in our program correspond to real life objects, e.g. customers, invoices, receipts. Sometimes the simulation is more explicit, e.g. a program that models a set of traffic lights to work out the optimum times for changing them to keep the traffic flowing smoothly. The object-oriented style (or paradigm) of programming is very well suited to simulations.

13 310417 2000 WEEK 1 13 Object-Oriented Programs The object-oriented programming paradigm uses objects to model a situation. A program can be written in a non-object-oriented way, but the object-oriented style is becoming increasingly popular for three main reasons: programs are becoming more and more complex, and the OO style handles this best the OO style makes it easy to re-use existing programs or parts of them the OO style makes programs much easier to maintain.

14 310417 2000 WEEK 1 14 Classification We classify objects into groups according to their common attributes and behaviour. Let's say we want to classify objects into balls and not-balls. What are the common attributes and behaviour of something we would classify as a ball? How would you recognise a ball if you saw one?

15 310417 2000 WEEK 1 15 Classes In a computer program, we use classes to describe similar objects. A class is a description of what an object would look like if we had one. It is based on common attributes and behaviour of objects. Radio currentStation volume turnOn turnOff changeStation increaseVolume decreaseVolume

16 310417 2000 WEEK 1 16 Instances of Classes A class is a template that describes what an object would look like if we had one. We instantiate the class to create an instance, i.e. an object of that class. The object is an instance of the class. We can have many instances of the same class. Each object has the same attributes and the same behaviour. However, the values of the attributes might be different.

17 310417 2000 WEEK 1 17 Identity We can have several different objects that have the same attributes and behaviour (even the same state) but are still different objects. An object has identity. We can give it a name to distinguish it from the others (e.g. ball1, ball2).

18 310417 2000 WEEK 1 18 State of an Object The state of a particular object is the values of its attributes at any particular moment. For example, a cat might have the following state: Name: Max Breed: American Shorthair Colour: Black, brown & white Age: 3 years, 4 months, 8 days Weight: 5.1 kg Some of these attributes change constantly. Others stay the same for long periods or forever.

19 310417 2000 WEEK 1 19 A Person Class Let's imagine we need a model of a person to solve some problem. For the purposes of this problem, it is enough to let a person have just a name and an age. Person name age display getAge getName setAge setName

20 310417 2000 WEEK 1 20 An Object Hides Its Attributes name age If you want to find out the value of an attribute of an object, you have to ask the object to tell you.

21 310417 2000 WEEK 1 21 Message Passing To get an object to do something it knows how to do, you have to send it a message requesting that behaviour. name age getName "Jack" bestFriend

22 310417 2000 WEEK 1 22 Interfaces How do we know what we can ask an object to do? The object has a public interface that advertises what it can be asked to do. It also indicates how to ask for these behaviours. What the outside world sees is the object's interface. display getAge getName setName setAge

23 310417 2000 WEEK 1 23 Sending a Message to an Object There are 3 parts to a message sent to an object: the name of the object that is the receiver the action that the receiver is requested to take in parentheses, any extra information the receiver needs to know. In Java, the syntax for asking the person called bestFriend to tell you its name would be: bestFriend.getName();

24 310417 2000 WEEK 1 24 Passing Information in a Message When an object needs to know some extra information in order to do what you want, you can pass it that information with the message. If you decide that Jack is no longer your best friend but Sally now is, and want the object bestFriend to change its name to "Sally", you need to send it a message that tells it to change its name, and also what to change it to. The extra information is called arguments or parameters. bestFriend.setName("Sally");

25 310417 2000 WEEK 1 25 Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can be done with it, and how much memory needs to be put aside for it. When we create a variable in Java, we need to specify: the type of the value we want to put in there, and the name we will use for that variable.

26 310417 2000 WEEK 1 26 Type String A String is a collection of characters (e.g. "Seree" ). String name = "Seree"; A String value is always written in double quotes. You can have an empty String, shown as "". A String has an interface that includes: change itself to upper/lower case tell you how long it is (how many characters) give you the character at a specified position Seree name

27 310417 2000 WEEK 1 27 Type int An int is a whole number (e.g. 21). You can do arithmetic with an int. int age = 21; addition + subtraction - multiplication * division / modulus % age 15 int age = 15; age + 3 2 * age - 4 age / 2 age % 10

28 310417 2000 WEEK 1 28 Objectives for Week 1 By the end of this week, students should be able to: explain the difference between an object and a class, and what instantiation means describe the components of an object explain the concepts of abstraction, state, identity, message passing, parameters, interface give examples of variables of types String and int download and install BlueJ use BlueJ to create objects from existing classes, inspect their state and invoke their behaviours

29 310417 2000 WEEK 1 29 310417 Concepts from Week 1 Programming Algorithm Problem solving Software Internet World Wide Web Browser Pseudocode Data Processing High level languages Translation Compiler Interpreter Bytecodes

30 310417 2000 WEEK 1 30 310417 Concepts So Far Programming Algorithm Problem solving Software Pseudocode Data Processing High level languages Translation Object- oriented Java Class Object State Attributes Behaviour Message passing Instantiation Parameter Types Identity


Download ppt "310417 2000 WEEK 1 1 What is a Computer Program? An algorithm that can be executed on a computer is a program. A program usually contains several algorithms."

Similar presentations


Ads by Google