David Streader & Peter Andreae Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

1 CSCE 1030 Computer Science 1 Introduction to Object Oriented Programming.
MiniDraw Testing COMP 102 # T1
Copyright © Texas Education Agency, Computer Programming Class Basics.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Creating and using Objects.
Computer Science and Engineering College of Engineering The Ohio State University Classes and Objects: Members, Visibility The credit for these slides.
Written by: Dr. JJ Shepherd
Animation Mrs. C. Furman. Animation  We can animate our crab by switching the image between two pictures.  crab.png and crab2.png.
Lecture 2: Object Oriented Programming I
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Interface COMP T1.
Java Syntax Primitive data types Operators Control statements.
Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects Real and Java COMP.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
CSC3170 Introduction to Database Systems
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Arrays and ArrayLists.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays with meaningful indices.
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.
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington2012 More Collections: Queues,
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
Agenda Object Oriented Programming Reading: Chapter 14.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Exercise, printf,
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Copyright Curt Hill Variables What are they? Why do we need them?
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Classes, Objects, Fields,
Variables and Functions Chapter Variables Named storage location in computer’s memory Programs may need to store data when running Types of data.
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 Designing with Classes and.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Fields, Constructors.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
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 2D arrays COMP 102 # T1.
More on Objects Mehdi Einali Advanced Programming in Java 1.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Peyman Dodangeh Sharif University of Technology Spring 2014.
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 Designing with Classes and.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Programs with Choice Booleans,
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Creating and using Objects.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
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.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Objects Real and Java COMP T1 3
Software Development Java Classes and Methods
Yanal Alahmad Java Workshop Yanal Alahmad
Advanced Programming in Java
CS 302 Week 11 Jim Williams, PhD.
Advanced Programming in Java
Assignment 7 User Defined Classes Part 2
Presentation transcript:

David Streader & Peter Andreae Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects Real and Java COMP T1 Week 3

COMP 112 lect 8: 2 © D.Streader Menu Explanation Assignment 1 Challenging! Some tricky computation & Learning Java Assignment 2: FloorCleaner, Tracer, Dodgem Cars Questions about what you are not clear on? Objects

COMP 112 lect 8: 3 © D.Streader Objects The world contains many objects for example: Student objects (Physical objects) Car objects(Physical objects) Course objects (Abstract objects) 1.The students are of a different type or class to the courses. 2.Many objects can be of the same type/class. 3.Different classes of object can do different things 4.Different objects can be in different states or have different properties.

COMP 112 lect 8: 4 © D.Streader Java Objects A Class defines a type of object What kind of data the objects contain What actions the objects can do / can be done on the objects A Class is a collection of: Private fields to hold data numbers, Strings, booleans, …or other objects Public methods Each object has its own fields that define its state/information The methods defined for a class can be applied to an object of that class and can access/change the state of the object.

COMP 112 lect 8: 5 © D.Streader Student Record System If implementing in Java, might have: 1.A Student class a Student object to represent each student. Fields for information about each student Methods to act on students get () …. 2.A Course class a Course object to represent each course Fields for information about each course Methods to act on courses: printCourseList() addStudent(Student s) ….

COMP 112 lect 8: 6 © D.Streader Objects in Java File MyObj.java should define the class MyObj MyObj class defines Fields for MyObj objects (hold data) Constructor(s) to specify how to create a new MyObj object. Name of constructor = name of Class Methods called on MyObj objects May have static fields: belong to class as whole, not a field for each object May have main method, executed when Foo run from command line

COMP 112 lect 8: 7 © D.Streader Student Class public class Student { // fields private static long nextID = 0; // static field – belongs to class private long ID; private String name; private String = “unknown”; // constructor public Student(String name){ this.ID = nextID; nextID++; this.name = name; } // methods ……. public void set (String e) { this. = e; } public String get (String e) { return this. ; } Student-85 ID: name:

COMP 112 lect 8: 8 © D.Streader More on Student Class Can have multiple constructors Same name, Distinguished by the parameters: // constructors public Student(String name){ this.ID = nextID; nextID++; this.name = name; } public Student(String nm, long id, String e ){ ID = id; name = nm; = e; } Student s1 = new Student(“Jamie Booker”); Student s2 = new Student(“Lindsay Meyer”, ,

COMP 112 lect 8: 9 © D.Streader Car class public class Car{ // fields private String color; public Car(String col){ this.color = col; } public void draw(double x, double y) { if (color.equals(“red”)) { UI.setColor(Color.red);} else if (color.equals(“blue”)) { UI.setColor(Color.blue);} UI.fillRect(x, y, 30, 15); } public boolean equals(Object other) { …. }

COMP 112 lect 8: 10 © D.Streader Primitives and Objects int x = 1; x: 1 x names a piec e of memory in which the value 1 is stored Car z = new Car(“red”); z: Car-21 Car-21 col: “red” z names a piec e of memory in which a reference to a car object is stored

COMP 112 lect 8: 11 © D.Streader Assignment of object values Car c1 = new Car(“red”); Car c2 = new Car(“red”); Car c3 = new Car(“blue”); Car c4 = c3; c1: Car-21 Car-21 col: “red” c2: Car-24 Car-24 col: “red” c3: Car-25 Car-25 col: “blue” c4: Car-

COMP 112 lect 8: 12 © D.Streader Comparing object values Car c1 = new Car(“red”);c1 == c2 ? Car c2 = new Car(“red”);c1.equals(c2) ? Car c3 = new Car(“blue”);c3 == c4 ? Car c4 = c3;c3.equals(c4) ? c1: Car-21 Car-21 col: “red” c2: Car-24 Car-24 col: “red” c3: Car-25 Car-25 col: “blue” c4: Car-

COMP 112 lect 8: 13 © D.Streader Passing and modifying values int age = 35; this.change1(age); UI.println(“age: ” + age); public void change1(int v){ v = v + 10; } Student s = new Student(“Roy”); this.change2(s); UI.println(“ ” + s.get ()); public void change2(Student st){ }

COMP 112 lect 8: 14 © D.Streader Passing Objects A variable of Object type is a reference (or “pointer”). The value passed when an Object is an argument is the pointer. two is changed

COMP 112 lect 8: 15 © D.Streader Car class: equals method (1) public class Car{ private String color; public Car(String col){ this.color = col; } public void draw(double x, double y) { if (color.equals(“red”)) { UI.setColor(Color.red);} else if (color.equals(“blue”)) { UI.setColor(Color.blue);} UI.fillRect(x, y, 30, 15); } public boolean equals(Car other) { return (this.color.equals(other.color)); } What if other object isn’t a car?

COMP 112 lect 8: 16 © D.Streader Equals You can call equals on any Object The default operation of equals is to do == public boolean equals(Object other){  in Object class return (this == other); } Many Object types have their own equals: String will check that all the characters in the strings are the same: public boolean equals(String other){  in String class if (this.length() != other.length()) { return false; } for (int i = 0; i < this.length(); i++){ if (this.charAt(i) != other.charAt(i)) { return false; } } return true; }

COMP 112 lect 8: 17 © D.Streader Car class: equals method (1) In your class: need to define equals, if it shouldn’t mean == public class Car{ private Color color; private double x, y; : public boolean equals(Car other) { return (this.color.equals(other.color)); } public boolean equals(Object other) {  better! if (this == other) return true; if (! (other instanceof Car) ) return false; Car otherCar = (Car) other; return (this.color.equals(otherCar.color)); } }