Download presentation
Presentation is loading. Please wait.
Published byStuart Lane Modified over 9 years ago
1
ITP © Ron Poet Lecture 2 1 Mental Models
2
ITP © Ron Poet Lecture 2 2 Mental Models A mental model is a way of making sense of something we experience. It can be used to make predictions when we have to "guess" at what to do. The mental model need not correspond with "reality", provided the predictions work.
3
ITP © Ron Poet Lecture 2 3 Mental Model of a University Lecturers are dedicated to help the students do the best they can while maintaining standards. Predictions If we get stuck we can always ask a lecturer for help. If we start an assignment too late through laziness then we will not get an extension.
4
ITP © Ron Poet Lecture 2 4 Mental Model on an Object Oriented Program
5
ITP © Ron Poet Lecture 2 5 Objects are "Workers" A mental model of an object oriented style of program is: A program is like an office The objects in the program are like workers in the office. They are "electronic" workers who can be created and destroyed in the blink of an eye. They always do exactly what they are told.
6
ITP © Ron Poet Lecture 2 6 Specialised Workers Objects are specialised. They know about just one aspect of the program. They can do a limited range of tasks. These tasks should be related to their specialisation if the program is designed well. The tasks are called methods.
7
ITP © Ron Poet Lecture 2 7 Objects are Good at Delegating An object may delegate parts of its task to other objects. If we are a customer, giving the object a task, we don't care how he does it. The object is like a receptionist for a sub- department in our big office. We give them a job, and some time later they give us the results.
8
ITP © Ron Poet Lecture 2 8 Inside the Sub-Department If we are nosy we can follow the receptionist into the department. We will see several other "workers" scurrying around making sure our task was done. Some of the work will be delegated to other sub-department, who can delegate the work as well … In most cases we don't need to know this.
9
ITP © Ron Poet Lecture 2 9 Engineers In some cases we are engineers responsible for designing these sub-departments and making sure they work. In this case we need to be nosy. But we don't need to be engineers for every part of the program. This is how multi-person projects are built. We are customers for parts of the code, engineers for others.
10
ITP © Ron Poet Lecture 2 10 Unique, Omnipresent Workers Some workers are unique and always available. We can give them work whenever we want And from anywhere in our program. We should not have too many such objects. They influence all parts of our code. It gets difficult to remember them all.
11
ITP © Ron Poet Lecture 2 11 And The Clones Other workers can be cloned, so that we can have as many copies of them as we want. They each have different personal details But they have the same methods. We must create them when we need them. If we ignore them then Java will quietly remove them when they are no longer needed. We don’t need to remember to remove them.
12
ITP © Ron Poet Lecture 2 12 Only One Objects Is At Work In many programs, Only one object is working at any one time. The other object are either Waiting for another object to return work. Waiting to be given some work. The program is started of by a main object.
13
ITP © Ron Poet Lecture 2 13 Multiple Thread Some programs can use threads. Each thread has just one object working. But the program has many objects working simultaneously. One for each thread. Threads are covered in Advanced Programming.
14
ITP © Ron Poet Lecture 2 14 The main Object In Java every program must have at least one object. This is the main object. It starts off the computation. It is a unique, omnipresent object. We must define what it does ourselves.
15
ITP © Ron Poet Lecture 2 15 Example Programs As Objects
16
ITP © Ron Poet Lecture 2 16 Program 1 public class Ex1 { public static void main(String[] arg) { System.out.println("Hello World"); }
17
ITP © Ron Poet Lecture 2 17 main Object Ex1 All main classes are defined in the same way: public class { public static void main(String[] arg) { } The various special words will be explained as the course progresses.
18
ITP © Ron Poet Lecture 2 18 System.out Object This is unique, omnipresent object supplied by Java. Its area of responsibility is outputing message to a text window. It has many different methods, all associated with output. println outputs text, followed by a newline.
19
ITP © Ron Poet Lecture 2 19 Method Call We give an object some work by using a method. The form is object. method name ( info ) ; In this example. Object is System.out Method name is println Info is “Hello World”
20
ITP © Ron Poet Lecture 2 20 Program 1 As People We define what Mr. main has to do. He wants to say “Hello World” He knows that Ms System.out does this. He gives her the println task. He waits until she has finished The he finishes as well, his work is done. He could have used her angry twin sister Ms System.err, who says everything in red.
21
ITP © Ron Poet Lecture 2 21 The Console Family Mr main also knows that the Console family will say what he wants in a prettier window. There is no omnipresent Console object so he must create a member of the family first. He must choose their name and thinks con sounds good. He could choose any name. He creates the Console object Console con = new Console();
22
ITP © Ron Poet Lecture 2 22 The Console Family Con also understands the println task. con.println("Hello World"); Hello World is printed in a different place. The instruction (println) and info (“Hello World”) are the same But the workers (con and System.out) are different.
23
ITP © Ron Poet Lecture 2 23 The FileOut Family Now Mr main decides that he wants his words to be stored permanently in a file. The FileOut family will do this for him. fout is such a nice name! He must tell fout the name of the file. FileOut fout = new FileOut("greeting"); The FileOut family also does println. fout.println("Hi Ron");
24
ITP © Ron Poet Lecture 2 24 The FileIn Family Members of FileIn read from a file. This is more complicated. We need to learn some new things before returning to FileIn.
25
ITP © Ron Poet Lecture 2 25 Sequential Statements
26
ITP © Ron Poet Lecture 2 26 Sequential Processing Each bit of processing consists of a sequence of statements. The statements are executed one after the other. One statement must finish before the next one starts.
27
ITP © Ron Poet Lecture 2 27 A Simple Statement A statement is a single activity. There are many different types of statements. They each end with a semi-colon. Our simple programs have had up to 6 statements. A statement can be spread over several lines if that makes it easier to read.
28
ITP © Ron Poet Lecture 2 28 Compound Statement A compound statement is a sequence of statements. They can be either simple or compound. A compound statement starts with { and ends with }. They group statements in more complex structures. Every method contains its instructions in a compound statement.
29
ITP © Ron Poet Lecture 2 29 Example Program Our first example program contained a compound statement, the method body. Which contained one simple statement. public class Ex1 { public static void main(String[] arg) { System.out.println("Hello World"); }
30
ITP © Ron Poet Lecture 2 30 FileIn Program The FileIn program is slightly more complex. { Console con = new Console(); FileIn fin = new FileIn(); String word = ""; word = fin.readWord(); con.println(word); }
31
ITP © Ron Poet Lecture 2 31 Variables
32
ITP © Ron Poet Lecture 2 32 Mental Model of Computer Memory Computer programs store temporary values in their memory. We need to picture how this works. We can think of a piece of computer memory as a sticky box. Initially this box is empty. But we can place a single value in it.
33
ITP © Ron Poet Lecture 2 33 Copying Values The sticky part means that we cannot take the value out of the box again. It can never become empty once we give it a value. We can make as many copies of the value stored in the box as we want to.
34
ITP © Ron Poet Lecture 2 34 Changing Values The only way we can change the value in the box is by putting a new value in it. The old value is lost permanently. We can now take copies of this new value. A box can only store one value at a time.
35
ITP © Ron Poet Lecture 2 35 Variables These computer memories or boxes are called variables. We must give them names, called identifiers. Identifiers must start with a letter. Followed by letters and/or numbers. By convention Java variable names start with a lower case letter. If they are a sequence of words, then the start of each word except the first is capitalised.
36
ITP © Ron Poet Lecture 2 36 Meaningful Identifiers We can choose the names in our program so that it is easier for humans to read. Computers don't care what the names are. Don't make them too long. circleArea con fout x2, y2, z2
37
ITP © Ron Poet Lecture 2 37 Types
38
ITP © Ron Poet Lecture 2 38 Different Types of Information Our computer program can store different types of information in each of its boxes. Words Numbers Objects Words have type String Numbers can either be: Whole numbers or integers, type int Real numbers (with a decimal point), type double.
39
ITP © Ron Poet Lecture 2 39 Different Types of Information The boxes can also store objects, with different type, such as Console, FileOut etc. We can create our own types of objects. See later in the course.
40
ITP © Ron Poet Lecture 2 40 Literals A literal is an actual value written in the text of the program. Different types have different forms of literals. A String literal must be inside "". "Hello World" An int literal is a whole number, eg 42. A double literal has a decimal point and/or exponential notation, eg 3.14159, 6.0221415e23.
41
ITP © Ron Poet Lecture 2 41 Variable Definitions We have different boxes for different types. A String type box can only store text. We must tell the computer about every box we are going to use before we use it. This is a variable definition. It must include both a type and a name.
42
ITP © Ron Poet Lecture 2 42 Variable Definitions (2) Variable definitions are all statements. Terminated by ; They can occur anywhere inside a compound statement. Examples String firstName, lastName; inti, j, k; double x, y, z;
43
ITP © Ron Poet Lecture 2 43 Initialisation We can choose to give each box an initial value when we define it. Otherwise Java will start it off with a zero numerical value or empty text. Other programming languages may create a variable containing junk.
44
ITP © Ron Poet Lecture 2 44 Initialisation (2) It is safer to initialise variables when they are created. To prevent accidentally using a junk value. Use = in the variable definition. Examples Console con = new Console(); String word = ""; inti=0, j=0, k=0;
45
ITP © Ron Poet Lecture 2 45 Operations Each type has a set of operations that work with that type. Numbers ( int, double ) have the usual arithmetic: +, -, *, / Strings have join operations, called concatenation.
46
ITP © Ron Poet Lecture 2 46 Expressions An expression is a calculation that involves some operations. An arithmetic formula, for example.
47
ITP © Ron Poet Lecture 2 47 Values Values are results of calculations. A copy of the values stored in a variable 'box'. The result of arithmetic. A literal. Values can be given to methods as information println("Hello World") Or stored in memory (variables).
48
ITP © Ron Poet Lecture 2 48 Assignment Assignment changes the value stored in a variable. The old value is no longer there. The assignment operator is = This can be confusing. It does not mean 'is equal to' but copy right to left. Get the right mental model! String greeting; greeting = "Hi Ron";
49
ITP © Ron Poet Lecture 2 49 Examples Let us define a variable. double radius = 3.0; Calculate the area and store the value in a variable. double area = 3.14159 * radius * radius; Copy the value stored in the radius box (twice), Multiply by a literal, using the * operator. Produce an arithmetic expression. Store the result in a variable (assignment).
50
ITP © Ron Poet Lecture 2 50 Strings
51
ITP © Ron Poet Lecture 2 51 String Type A String is a sequence of characters. A character is a letter, a digit, punctuation, spaces, newline, tabs and other special characters. An empty String literal is "". A newline in a String is \n. "line1\nline2"
52
ITP © Ron Poet Lecture 2 52 Concatenation Concatenation means joining Strings. We use the + operator. It will join any two String values. String firstName = "Ron", lastName = "Poet"; String fullName = firstName + " " + lastName;
53
ITP © Ron Poet Lecture 2 53 Conversions from Numbers A number can be converted to a String using concatenation. Provided it is not the first item. If the number is a double then it will be converted with a lot of decimal digits. Not under user control. But see later.
54
ITP © Ron Poet Lecture 2 54 Examples If the value 3 is stored in x and 7 in y, then the string "x = " + x + " y = " + y; Will become x = 3 y = 7 There is a trick if we want to create a string where the first part is a number. We make the first part the empty string “” "" + 42 + " is a special number";
55
ITP © Ron Poet Lecture 2 55 The FormatIO Package
56
ITP © Ron Poet Lecture 2 56 Formatted Input and Output The FormatIO package provides a number of classes (families in our mental model) to provide convenient input and output. Java is not very good in this area. FormatIO was initially written for Java 1.0. Rewritten for Java 1.1 Not yet rewritten for Java 5.0 (hence warnings)
57
ITP © Ron Poet Lecture 2 57 Output Methods All classes in FormatIO that support output support the same methods. They just output to different places. Just consider two for now. print(String) // output the String value println(String) // print followed by newline print("Hi Ron"); print(firstName + " " + lastName);
58
ITP © Ron Poet Lecture 2 58 Input Methods All FormatIO classes that support input support the same methods. They just input from different places. Just consider two for now. readWord() // reads up to next space or newline readLine() // reads next line
59
ITP © Ron Poet Lecture 2 59 Input Methods They both return a String value, which must be used somehow. Stored in a variable String word = con.readWord(); passed to another method System.out.println(con.readWord()); part of an expression. String name = con.readWord() + " " + con.readWord();
60
ITP © Ron Poet Lecture 2 60 Console Class This supports both input and output. It can be created with a default size and name, or the name and size can be provided. The size is the number of rows of letters followed by number of columns. con1 = new Console(); con2 = new Console("Rons console"); con3 = new Console(10, 40); con4 = new Console("small", 5, 20);
61
ITP © Ron Poet Lecture 2 61 Console Class (2) You can have as many console objects as you wish. It is normal to only have 1. The save button saves the output. You are prompted for a file name. The close button closes that window. The quit button closes the program.
62
ITP © Ron Poet Lecture 2 62 FileOut, FileIn They write to a file or read from a file. You can provide a filename when you create them. FileOut fout = new FileOut("greeting"); If you don't provide a file name then you will get a file chooser window. FileOut fout = new FileOut();
63
ITP © Ron Poet Lecture 2 63 StringIn If we have a String variable that contains text we can treat it as a source of input. We can read the text one word at a time. We create a StringIn variable and then use the method readWord Or any other FormatIO input method.
64
ITP © Ron Poet Lecture 2 64 StringIn Example String fullName = "Ron Poet"; StringIn sin = new StringIn(fullName); String firstName = sin.readWord(); String lastName = sin.readWord();
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.