ITP © Ron Poet Lecture 2 1 Mental Models. ITP © Ron Poet Lecture 2 2 Mental Models  A mental model is a way of making sense of something we experience.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Advertisements

ITP © Ron Poet Lecture 9 1 Exceptions. ITP © Ron Poet Lecture 9 2 The Standard Way for Object to Return Work  Object 1 gives object 2 some work.  It.
Lecture 2 Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to C Programming
Helper Methods ITP © Ron Poet Lecture 11.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
CMT Programming Software Applications
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Introduction to C Programming
Hello, world! Dissect HelloWorld.java Compile it Run it.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
ITP © Ron Poet Lecture 3 1 Comments. ITP © Ron Poet Lecture 3 2 Legibility  It is important that programs are easy to read.  It is easier to find bugs.
Input, Output, and Processing
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
ITIP © Ron Poet Lecture 12 1 Finding Out About Objects.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
ITIP © Ron Poet Lecture 14 1 Responsibilities. ITIP © Ron Poet Lecture 14 2 Two Sections of Code  If we ask two people to work together to do a job,
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Variables 1. What is a variable? Something whose value can change over time This value can change more than once over the time period (no limit!) Example:
ITP © Ron Poet Lecture 6 1 More on if. ITP © Ron Poet Lecture 6 2 Remembering Tests  We often want to remember the result of a test, so that we can use.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
ITP © Ron Poet Lecture 5 1 Branching. ITP © Ron Poet Lecture 5 2 CookTime After Midnight  We want to improve our program so that we can cook meals after.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
What is Binary Code? Computers use a special code of their own to express the digital information they process. It's called the binary code because it.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Topics Designing a Program Input, Processing, and Output
User-Written Functions
COMP 170 – Introduction to Object Oriented Programming
Introduction to Computer Science / Procedural – 67130
Variables and Primative Types
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Introduction to C++ Programming
Introduction to Primitive Data types
Chapter 3 – Introduction to C# Programming
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Topics Designing a Program Input, Processing, and Output
Building Java Programs
Topics Designing a Program Input, Processing, and Output
Introduction to Primitives
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Introduction to Primitives
In this class, we will cover:
Unit 3: Variables in Java
Just Enough Java 17-May-19.
Introduction to Primitive Data types
Presentation transcript:

ITP © Ron Poet Lecture 2 1 Mental Models

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.

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.

ITP © Ron Poet Lecture 2 4 Mental Model on an Object Oriented Program

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

ITP © Ron Poet Lecture 2 15 Example Programs As Objects

ITP © Ron Poet Lecture 2 16 Program 1 public class Ex1 { public static void main(String[] arg) { System.out.println("Hello World"); }

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.

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.

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”

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.

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

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.

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

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.

ITP © Ron Poet Lecture 2 25 Sequential Statements

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.

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.

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.

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

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

ITP © Ron Poet Lecture 2 31 Variables

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.

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.

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.

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.

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

ITP © Ron Poet Lecture 2 37 Types

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.

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.

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 , e23.

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.

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;

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.

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;

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.

ITP © Ron Poet Lecture 2 46 Expressions  An expression is a calculation that involves some operations.  An arithmetic formula, for example.

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

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

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 = * 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).

ITP © Ron Poet Lecture 2 50 Strings

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"

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;

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.

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 “” "" " is a special number";

ITP © Ron Poet Lecture 2 55 The FormatIO Package

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)

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

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

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

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

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.

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

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.

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