Objects Real and Java COMP T1 3

Slides:



Advertisements
Similar presentations
Classes And Objects II. Recall the LightSwitch Class class LightSwitch { boolean on = true; boolean isOn() { return on; } void switch() { on = !on; }
Advertisements

Written by: Dr. JJ Shepherd
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Animation Mrs. C. Furman. Animation  We can animate our crab by switching the image between two pictures.  crab.png and crab2.png.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects Real and Java COMP.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Programming Languages and Paradigms Object-Oriented Programming.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
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.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
David Streader & Peter Andreae Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Methods: A Deeper Look. Template for Class Definition public class { } A.Import Statement B.Class Comments C.Class Name D.Data members E.Methods (inc.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
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.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Classes and Objects Introduced
JAVA MULTIPLE CHOICE QUESTION.
Building Java Programs
Java Primer 1: Types, Classes and Operators
Intro To Classes Review
Constructors CMSC 202.
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
CS 302 Week 11 Jim Williams, PhD.
More Object Oriented Programming
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Java Programming with BlueJ
Building Java Programs Chapter 2
An Introduction to Java – Part II
Creating Objects in a Few Simple Steps
Building Java Programs
The Building Blocks Classes: Java class library, over 1,800 classes:
Building Java Programs
Building Java Programs
CHAPTER 6 GENERAL-PURPOSE METHODS
Building Java Programs
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Session 2: Introduction to Object Oriented Programming
Building Java Programs
Java Programming with BlueJ Objectives
Java Programming Language
CSC 111 Exam 3 Review Fall 2005.
Building Java Programs
Introduction to Object-Oriented Programming
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
References Revisted (Ch 5)
Classes and Objects Object Creation
Announcements Lab 5 due Wednesday at noon.
CMSC 202 Constructors Version 9/10.
Building Java Programs
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Objects Real and Java COMP 112 2017T1 3

Real Objects The world contains many real objects for example: Student objects Course objects The world has different types or classes of things. Different classes of object can do different things Different objects can be in different states. Many objects can be of the same class. A student is not the same as a course Students go to labs courses do not. Each student has a unique ID. There is more that one student.

Java Objects A Class defines what an object can do A Class is a collection of: private data (fields) can be integers…. or an Object public methods Each object has its own fields that define its state The methods defined for a class can be applied to an object of that class and can change the state of the object. Both fields and methods live on an object But static fields are are shared by all objects and static methods can be executed from the class ! Each student object must have a unique ID! Code a Student Class that enforces this.

Objects in Java Foo.java defines the class Foo Define Foo related things in Foo.java Fields (hold data) Methods (can be executed) Main method executed when Foo executed Constructor methods are used to build objects Have same name as Class Have no return type Used to construct Object of the Class Best understood by examples.

Example Program Ex1.java ----- Meeting Notes (23/02/15 16:07) ----- 1. Each object has differnt fields but same type of fields 2. Class name Ex1 3. Constructor definition 4. Constructor execution 5. Method definition 6. Method calling - on the object

AddCounter.java ----- Meeting Notes (23/02/15 16:11) ----- Note: Two objects each with different name and value.

BlueJ - Classes and Objects Right click (on class) New AddCounter (to make an object) Classes

BlueJ Creating Objects

BlueJ In BlueJ Right click on object to call method Object BlueJ allows any method to be executed. Helpful for testing ----- Meeting Notes (9/03/15 08:23) ----- Two kinds of things 1. Classes on which you may executs static methods 2. Objects on which you may execute (non-static) methods Object

Primitives and Objects int x = 1; x names a piece of memory in which the value 1 is stored X 1 1 Car z = new Car(2); z Car21 Car21 x 2 The value in z is the reference to the Object.

Method calling Primitive parameters are values. The value in an int is passed But the reference to Object Param is passed Pass a value Pass a reference ----- Meeting Notes (8/03/14 18:40) ----- Information flows into a method with value passing Information flows both ways with referance passing setx on p Pass value getx on p

Overview of what is to come Object are: Of Reference type Compared by myOb.equal(anotherOb) but you write the “.equal” methods! myOb = anotherOb assignment (makes references the same) myOb == anotherOb test (is same reference) Parameter passing Automatic Boxing and type conversion BlueJ debugging Use .equal not == Until you understand

Passing Objects A variable of Object type is a pointer. The value passed when an Object is a parameter is the pointer. state change ----- Meeting Notes (8/03/14 18:49) ----- inx by value p by reference Although method is run on Object mp the Object mq is changed ----- Meeting Notes (24/02/15 08:00) ----- Recall information flows BOTH ways with referance parameters ----- Meeting Notes (8/03/15 16:50) ----- *** is one OR two changed by one.oMethod(two,30) two is changed

Reference type Car z = new Car(2); Car y = new Car(4); z Car21 Lv 2 y Car23

Reference type Oby21 lv 2 Obj z = new Oby(2); Obj y = new Oby(1); Obj x = new Oby(1); z Oby21 Oby25 lv 1 y Oby25 x==y false x.equals(y) true z==y false ----- Meeting Notes (8/03/15 16:50) ----- What is == and what is .equals ?? ----- Meeting Notes (9/03/15 08:23) ----- Remember what x,y,z point to NOW execute x=y. What happens? Oby29 lv 1 x Oby29 You have to write the .equals()method What does y = z; do to the data above?

Reference type After y=z; Oby21 z Oby21 Oby25 y Oby21 Oby29 x Oby29 x==y false x.equals(y) false z==y true

First attempt at the equals method .equal() method in the ObjMeth class ----- Meeting Notes (8/03/14 18:06) ----- Compiler Objects Later you find out that Run time errors can occur! this.x is x in this object What happens when a different kind of Object is passed as a parameter?

The equals Method Accepts any Object ----- Meeting Notes (8/03/14 18:08) ----- This equals method is USED by System when Hashing ----- Meeting Notes (9/03/14 11:41) ----- Explain instanceof Accepts any Object Checks the object is of the correct type

Method calling Methods can be distinguished by: return type, a name and a list of parameter types More than one method with same name can be defined. Two Methods can be defined with same name but different return types (or) parameter types Hence both equals(Object x) and equals(ObjMeth x) can be defined

Parameter passing with Objects Oby z = new Oby(2); o.meth(z); … } void meth(Oby p){ p.setx(4) …. z Oby21 Oby21 x 2 p Oby21 ----- Meeting Notes (24/02/15 08:09) ----- 1. Build new Object z 2. p is set to point to the same object as z 3. seting the x value in p CHANGES z

Parameter passing Obj z = new Oby(2); o.meth(z); … void meth(Obj p){ p.setx(4) z Oby21 Oby21 x 4 p Oby21 both p and z have been changed

Boxing in java Integer to int int to Integer Integer to int to String Converting a primitive type into an Object is Boxing Can be performed manually or by the compiler Automatic type conversion helps you code Integer to int int to Integer Integer to int to String

Debugging write display statements in the code run the program One of the simplest (oldest) ways to test/debug software is: write display statements in the code run the program Inspect the output IDEs come with some built in features that can help

BlueJ Debug Window Method call Sequence Variables Step Into Call Step Over Call

Inspecting Variables Double click on a variable.