Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Object Oriented Concepts 3 Stewart Blakeway FML 213

Slides:



Advertisements
Similar presentations
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Advertisements

Faculty of Sciences and Social Sciences HOPE Structured Problem Solving An Introduction Stewart Blakeway
Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
Introduction to Programming Lecture 39. Copy Constructor.
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 5: Steps in Problem Solving Stewart Blakeway FML 213
Object Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.
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 6: Problem Solving Exercises Stewart Blakeway FML.
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Object Oriented Concepts 2 Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 7: Java basics 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 PHP – Working with Input Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Examination Revision Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE Object Oriented Programming Pie Eater Revisited Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE Object Oriented Programming Pie Eater Stewart Blakeway FML
Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213
Looking inside classes Choices Week 4. Class bodies contain fields, constructors and methods. Fields store values that determine an object’s state. Constructors.
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving An Introduction Stewart Blakeway
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
Fall 2007ACS-1903 BlueJ Ron McFadyen Lab 1: Using BlueJ at UofW The purpose of this lab is to give you some experience with BlueJ. Over the course of the.
More sophisticated behavior Using library classes to implement some more advanced functionality 4.0.
OOPS Object-Oriented Programming in Smalltalk. Practical Sessions 1-5 These sessions introduce you to the Smalltalk Environment, and use the familiar.
Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.
Lecture 4. Review (Attributes and Methods) Class will contain –a set of attributes and methods public class Turtle { ///// Field (Attributes) ///// …
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
Object interaction Creating cooperating objects. 28/10/2004Lecture 3: Object Interaction2 Main concepts to be covered Abstraction Modularization Class.
Object Oriented Software Development
CSC 142 C 1 CSC 142 Object based programming in Java [Reading: chapter 4]
Lecture 1 Introduction to Software Construction
Introduction to Programming with Java. Overview What are the tools we are using – What is Java? This is the language that you use to write your program.
Java Classes Using Java Classes Introduction to UML.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Introduction to Java. 2 Textbook David J. Barnes & Michael Kölling Objects First with Java A Practical Introduction using BlueJ Fourth edition, Pearson.
Object Oriented Programming … and other things you need to program in java.
OBJECTS AND CLASSES CITS1001. Concepts for this lecture class; object; instance method; parameter; signature data type multiple instances; state method.
1 COS 260 DAY 2 Tony Gauvin. 2 Agenda Questions? Class roll call Blackboard Web Resources Objects and classes 1 st Mini quiz on chap1 terms and concepts.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Applications Development
Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical.
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 3: Algorithms Stewart Blakeway FML 213
Chapter 3 Introduction to Classes and Objects Definitions Examples.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Looking inside classes Conditional Statements Week 4.
CS2 Module 26 Category: OO Concepts Topic: Interfaces Objectives –Interfaces.
Objects and Classes Start on Slide 30 for day 2 Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Much of.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Java Enums Why have Choice.Yes and Choice.No?. Old Way  Enumerated types consist of a set of named values  Example: Font has BOLD, ITALIC rather than.
Testing in OO Environment The reasons for testing is not any different for any of the design and implementation methodologies, including OO methodology.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Fundamental of Java Programming
CompSci 230 Software Construction
Java Programming with BlueJ
Board: Objects, Arrays and Pedagogy
An Introduction to Java – Part II
CS18000: Problem Solving and Object-Oriented Programming
Java Lesson 36 Mr. Kalmes.
Object Oriented Programming Review
Life is Full of Alternatives
Object based programming in Java
Object based programming in Java
Java Programming with BlueJ Objectives
Introduction to Programming
Introduction to Programming
Lecture 1 Introduction to Software Construction
Day 11 The Last Week!.
Presentation transcript:

Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Object Oriented Concepts 3 Stewart Blakeway FML 213

Faculty of Sciences and Social Sciences HOPE What we did in OO Lecture 2 Introduction to BlueJ Creating objects in BlueJ 2

Faculty of Sciences and Social Sciences HOPE What we did in OO Lecture 2 Sending messages to those objects – square1.makeVisible() Messages with integer and String parameters – square1.moveHorizontal(50) – square1.changeColor(“blue”) 3

Faculty of Sciences and Social Sciences HOPE What we did in OO Lecture 2 Modifying the code for a message to draw a picture of a house 4

Faculty of Sciences and Social Sciences HOPE What we shall do today Reminder of Java programming constructs Adding animation to picture drawing Adding new messages to the protocol of a Java class Visualising messages passing between objects 5

Faculty of Sciences and Social Sciences HOPE Java Programming Constructs Sequence Selection Repetition 6

Faculty of Sciences and Social Sciences HOPE Sequence pie1.turnLeft(); pie1.walk(); 7

Faculty of Sciences and Social Sciences HOPE Selection Design if row is less than 4 then begin walk end Java if (row < 4 ) { pie1.walk(); } 8 ‘Decisions, decisions!'

Faculty of Sciences and Social Sciences HOPE Selection Design if row is less than 4 then begin walk end else begin turn left end Java if (row < 4 ) { pie1.walk(); } else { pie1.turnLeft(); } 9 ‘Decisions, decisions!'

Faculty of Sciences and Social Sciences HOPE Repetition - while Design i := 0 while i is less than 5 begin walk turn left i := i+1 end Java i = 0; while (i < 5 ) { pie1.walk(); pie1.turnLeft(); i++; } 10

Faculty of Sciences and Social Sciences HOPE Repetition - for Design for 100 times begin turn left end Java for (int i=0; i < 100; i++) { pie1.turnLeft(); } 11 Declare integer i, set to 0 OK in full Java like BlueJ, but not in Java Trainer As long as i is less than 100 Increment i by 1 for each pass

Faculty of Sciences and Social Sciences HOPE Alternative Java layouts if (row < 4 ) { pie1.walk(); } if (row < 4 ){ pie1.walk(); } 12

Faculty of Sciences and Social Sciences HOPE Alternative Java layouts if (row < 4 ) { pie1.walk(); } else { pie1.turnLeft(); } if (row < 4 ){ pie1.walk(); } else { pie1.turnLeft(); } 13

Faculty of Sciences and Social Sciences HOPE Alternative Java layouts i = 0; while (i < 5 ) { pie1.walk(); pie1.turnLeft(); i++; } i = 0; while (i < 5 ) { pie1.walk(); pie1.turnLeft(); i++; } 14

Faculty of Sciences and Social Sciences HOPE Alternative Java layouts for (int i=0; i < 100; i++ ) { pie1.turnLeft(); } for (int i=0; i < 100; i++ ) { pie1.turnLeft(); } 15

Faculty of Sciences and Social Sciences HOPE Conditions in Java Pseudo CodeJavaDescription A < B A is less than B A > B A is greater than B A = B A == B A equals B A <> B A != B A does not equal B A >= B A is greater than or equal to B A <= B A is less than or equal to B 16

Faculty of Sciences and Social Sciences HOPE BlueJ Project hopeball: Adding animation 17 public class Picture { private Circle ball; public void draw() { int i; ball = new Circle(); ball.changeColor("blue"); ball.moveVertical(50); ball.moveHorizontal(70); ball.changeSize(50); ball.makeVisible(); for (i=1; i<100; i++) { ball.moveHorizontal(1); }

Faculty of Sciences and Social Sciences HOPE Slowing it down a little for (int i=0; i < 1000; i++ ) { // Do something useless that doesn’t // get optimised out by the compiler } 18

Faculty of Sciences and Social Sciences HOPE Slowing it down a little for (int i=0; i < 1000; i++ ) { System.out.print(“Hello”); } 19

Faculty of Sciences and Social Sciences HOPE BlueJ Project hopeball: Adding animation 20 public class Picture { private Circle ball; public void draw() { int i; ball = new Circle(); ball.changeColor("blue"); ball.moveVertical(50); ball.moveHorizontal(70); ball.changeSize(50); ball.makeVisible(); } public void moveLeft() { ball.moveHorizontal(1); } public void moveLeft(int dist) { ball.moveHorizontal(dist); } Creating a new message

Faculty of Sciences and Social Sciences HOPE Pie Eater revisited Programming PieEater 21

Faculty of Sciences and Social Sciences HOPE 22 Project named javapieeater Pie Eater revisited

Faculty of Sciences and Social Sciences HOPE 23 Creating a PieEater object using a constructor Create a PieEater object with the constructor PieEater(String title, String colourName)

Faculty of Sciences and Social Sciences HOPE Constructor parameters Remember – we need double quotes around the String values 24

Faculty of Sciences and Social Sciences HOPE Pie1 Object 25 PieEater attributes

Faculty of Sciences and Social Sciences HOPE Objects can have other objects as attributes Just like our Picture objects included Circle, Triangle etc 26 Picture attributes

Faculty of Sciences and Social Sciences HOPE Objects can have other objects as attributes PieBoss class includes PieEater object as an attribute 27 PieBoss attribute

Faculty of Sciences and Social Sciences HOPE The full PieBoss class 28 public class PieBoss { private PieEater pie1; public void demo1() { pie1 = new PieEater("pie1", "red"); pie1.walk(); } Naming the classDeclaring the attribute Defining a method

Faculty of Sciences and Social Sciences HOPE The full PieBoss class 29 public class PieBoss { private PieEater pie1; public void demo1() { pie1 = new PieEater("pie1", "red"); pie1.walk(); } Creating a new PieEater object called pie1

Faculty of Sciences and Social Sciences HOPE Adding a method to walk a number of steps 30 public class PieBoss { private PieEater pie1; public void demo1() { pie1 = new PieEater("pie1", "red"); this.steps(5); } public void steps(int n) { int i; for( i=0; i<n; i++) { pie1.walk(); }

Faculty of Sciences and Social Sciences HOPE Adding a method to walk a number of steps 31 public class PieBoss { private PieEater pie1; public void demo1() { pie1 = new PieEater("pie1", "red"); this.steps(5); } public void steps(int n) { int i; for( i=0; i<n; i++) { pie1.walk(); } Using this to send a message to itself

Faculty of Sciences and Social Sciences HOPE Sequence diagram Visualising the flow of messages between objects 32 time

Faculty of Sciences and Social Sciences HOPE Sequence diagram Vertical lines represent objects User treated as an object sending messages 33 time

Faculty of Sciences and Social Sciences HOPE Sequence diagram User sends PieBoss message demo1() 34 time

Faculty of Sciences and Social Sciences HOPE Sequence diagram PieBoss responds by creating a new PieEater object 35 time

Faculty of Sciences and Social Sciences HOPE Sequence diagram... then sending itself the message steps(5) time

Faculty of Sciences and Social Sciences HOPE Sequence diagram... which in turn sends the PieEater the message walk(). 37 time

Faculty of Sciences and Social Sciences HOPE What we have covered Reminder of Java programming constructs Adding animation to picture drawing Adding new messages to the protocol of a Java class Visualising messages passing between objects

Faculty of Sciences and Social Sciences HOPE Any Questions