Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Creating and using Objects.

Slides:



Advertisements
Similar presentations
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
Advertisements

MiniDraw Testing COMP 102 # T1
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Creating and using Objects.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Prolog COMP 112 #
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Methods COMP.
Games and Simulations O-O Programming in Java The Walker School
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects Real and Java COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Review COMP 102 #
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Program Elements and Syntax.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
Chapter 4 Objects and Classes.
Computer Science Victoria University of Wellington Copyright: david streader, Victoria University of Wellington Simple Design COMP
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Create Objects,
©Xiaoying Gao, Peter Andreae First Java Program COMP 102 #2 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
First Java Program COMP 102 #2 2015T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
A way to pull together related data A Shape Class would contain the following data: x, y, height, width Then associate methods with that data A Shape.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Call a Method,
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
David Streader & Peter Andreae Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Event-driven Input COMP 102.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Methods COMP.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Exercise, printf,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Event-driven Input TextFields,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Classes, Objects, Fields,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 103 #
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Classes and.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Fields, Constructors.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods Calling Methods Return.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington UI methods, Graphical.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Classes and.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
© Xiaoying Gao, Peter Andreae Variables, Constants, UI methods COMP 102 #4 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Event-driven Input COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Programs with Choice Booleans,
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
OOP Basics Classes & Methods (c) IDMS/SQL News
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
© Peter Andreae Java Programs COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Comp1004: Building Better Objects II Encapsulation and Constructors.
Computer Science Victoria University of Wellington Copyright: david streader, Victoria University of Wellington Simple Design COMP
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Program Elements and Syntax.
Fields, Constructors COMP 102 # T2
Program with two classes COMP 102 # T2
Java Programming Language
Unit-1 Introduction to Java
Presentation transcript:

Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Creating and using Objects COMP 102 #8 2014

© Peter Andreae COMP102 8:2 Menu Returning values Creating objects inside the program Admin Assignment 3.

© Peter Andreae COMP102 8:3 Object oriented programming Key idea of OO programming program structured into classes of objects. each class specifies a kind of object – eg, the actions it can perform. Calling methods in OO languages like java tell an object to perform a method, passing arguments Making objects Some objects are predefined. Create objects with bluej: create object, then call methods on it. this is not standard, and not a general solution

© Peter Andreae COMP102 8:4 Objects Question: How can a program make new objects? More Questions: What is an object anyway? Why do we need them? An object is typically a collection of data with a set of actions it can perform. The objects we have made so far are a bit strange – no data; just actions. (TemperatureConverter, ReactionTimeMeasurer)

© Peter Andreae COMP102 8:5 Examples of objects Butterfly program Each butterfly is represented by an object which stores the state of the butterfly (position, wing state, direction) Butterflies have methods move(double dist) and land() CartoonFigure program Each cartoon figure is represented by an object which stores the state of the cartoon figure (image, position, direction facing, smile/frown). CartoonFigure objects have methods walk(double dist) smile()frown() lookLeft()lookRight() speak(String words) think(String words)

© Peter Andreae COMP102 8:6 Using objects If the variable bf1 and bf2 contained Butterfly objects, you could do: public void showButterflies(){ Butterfly bf1 = ????? Butterfly bf2 = ????? bf1.move(10); bf2.move(20); bf1.land(); bf2.move(20); bf1.move(5); } Problem: How do you get a Butterfly object into the variables? Nothing new here: Just standard method calls!

© Peter Andreae COMP102 8:7 Creating Objects Need to construct new objects: New kind of expression: new Butterfly bf1 = new Butterfly( …) Constructors are like method calls that return a value. have ( ) may need to pass arguments returns a value – the new object that was constructed. Constructors are NOT method calls there is no object to call a method on. must have the keyword new name must be the name of the class Calling the constructor 100, 300) Creates a new object, which is put into bf1

© Peter Andreae COMP102 8:8 Creating Objects: new Butterfly b1 = new Butterfly(100, 300); UI.setColor( new Color(255, 190, 0) ); Calling a constructor: new( a keyword) Butterfly( the type of object to construct ) ( … )(arguments: specifying information needed to construct the new object) This is an expression: it returns the object can put in variable can use in an enclosing expression or method call new〈 Class name 〉〈 arguments 〉 ()

© Peter Andreae COMP102 8:9 Reading Documentation Documentation of a class: Specifies the methods: name type of the return value (or void if no value returned) number and types of the parameters. voidmove (double dist) moves the butterfly by dist, in its current direction. Specifies the constructors: number and types of the parameters (name is always the name of the class, return type is always the class) Butterfly(double x, double y) requires the initial position of the butterfly Bluej lets you see the documentation of your classes

© Peter Andreae COMP102 8:10 Example: Butterfly Grove program public class ButterflyGrove{ /** A grove of Butterflies which fly around and land */ public void oneButterfly(){ double x = 50*Math.random(); Butterfly b1 = new Butterfly(50, 20); b1.move(5); b1.move(10); b1.move(15); b1.move(10); b1.move(11); b1.move(12); b1.move(13); b1.move(14); b1.move(15); b1.move(16); b1.move(10); b1.land(); } public void twoButterflies(){ Butterfly b1 = new Butterfly(100, 20); b1.move(5); b1.move(10); b1.move(15); double x = 400*Math.random(); Butterfly b2 = new Butterfly(x, 40); b2.move(10); b1.move(15); b2.move(10); b1.move(12); b2.move(10); b1.move(11); b1.move(7); b1.land(); b2.move(20); b2.move(25); b2.land(); }

© Peter Andreae COMP102 8:11 Example: Butterfly Grove program public void butterflyHop(){ Butterfly b1 = new Butterfly(50, 200); Butterfly b2 = new Butterfly(300, 200); this.flyAndLand(b1); this.flyAndLand(b2); this.flyAndLand(b1); this.flyAndLand(b2); this.flyAndLand(b1); this.flyAndLand(b2); } public void flyAndLand(Butterfly b){ b.move(20); b.move(30); b.move(45); b.land(); }

© Peter Andreae COMP102 8:12 Assignment CartoonStrip: Involves same idea as the ButterflyGrove, but using CartoonFigures instead: