Instance Method Review - CIS 1068 Program Design and Abstraction

Slides:



Advertisements
Similar presentations
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Advertisements

Overloading methods review When is the return statement required? What do the following method headers tell us? public static int max (int a, int b)
CS180 Chapter 4 2/1/08. Announcements Project 3 is out –2 weeks due to the exam Exam –Thursday, February 7, 8:30 – 9:30 pm, WTHR 104 –Covers chapters.
1 More on Classes Overview l Overloading l The this keyword l The toString method l The equals method.
OOPDA Review. Terminology class-A template defining state and behavior class-A template defining state and behavior object-An instance of a class object-An.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Intro to CS – Honors I Classes and Methods GEORGIOS PORTOKALIDIS
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Constructors and Encapsulation reading: self-checks: #10-17.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
CSC 142 Computer Science II Zhen Jiang West Chester University
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
CSC 142 Computer Science II Zhen Jiang West Chester University
Software development For large and complex software use divide and conquer rule. Software design methods: Structured design Object-Oriented design.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Sorting - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 13/14/2016.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Re-Intro to Object Oriented Programming
Object - CIS 1068 Program Design and Abstraction
Object Oriented Programming
Objects as a programming concept
Examples of Classes & Objects
Creating Your OwnClasses
CSC240 Computer Science III
CSC240 Computer Science III
Lecture 8-3: Encapsulation, this
Defining Classes and Methods
An Introduction to Java – Part II
Building Java Programs
Classes & Objects: Examples
Building Java Programs
Encapsulation and Constructors
CSC240 Computer Science III
Building Java Programs
More on Classes and Objects
Building Java Programs
© A+ Computer Science - OOP © A+ Computer Science -
CSE 214 – Computer Science I More on Linked Lists
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
Building Java Programs
Building Java Programs
JAVA An Introduction to Java OOP Basics
Instance Method – CSC142 Computer Science II
CIS 199 Final Review.
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Chapter 9 Objects and Classes
Building Java Programs
Classes and objects Readings: 8.1.
Building Java Programs
Building Java Programs
OO Programming Concepts
Building Java Programs
Lecture 8-2: Object Behavior (Methods) and Constructors
Building Java Programs
CIS 110: Introduction to Computer Programming
Presentation transcript:

Instance Method Review - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University 1050 Wachman Hall, Main Campus Email: zhen.jiang@temple.edu 11/12/2018

Table of Contents Introduction Declaration and call Access and scope 11/12/2018

Introduction Why? Why loop Why array Why method Why object (the host of instance method)? 11/12/2018

Every thing under the control inside of object (which must be declared in the class, as a general template to cover all possible changes/statuses) 11/12/2018

state: String name, breed int age Dog class state: String name, breed int age behavior: writeOutput ( ) getAgeInHumanYears ( )‏ Dog object balto state: “Balto” (name) 8 (age) “Siberian Husky” (breed) behavior: getAgeInHumanYears ( ) writeOutput ( ) Dog object scooby state: “Balto” (name) 8 (age) “Siberian Husky” (breed) behavior: getAgeInHumanYears ( ) writeOutput ( )

Declaration and Call No more static How to use instance method m()? public class A { public void m() { … } 11/12/2018

public class A { public void m() { … } public void m2() { m(); public class B { public void m() { … } public void m3() { m() A a; //a.m is not supported A b = new A (); b.m(); 11/12/2018

public class A { public void m() { … } public static void m2() { A a = new A(); a.m(); 11/12/2018

For comparison public class A { public static void m() { … } public void m2() { m(); public class B { public void m() { … } public void m3() { m() A.m(); 11/12/2018

For comparison public class A { public void m1(){ System.out.println("A's m1"); m2(); } public static void m2(){ System.out.println("A's static m2"); } } public class B { public void m1(){ A a = new A(); a.m1(); A.m2(); } public static void m2(){ System.out.println("B's static m2"); B b2 = new B(); b2.m1(); } public static void main(String args[]){ A a = new A(); B b = new B(); b.m1(); B.m2(); } } A's m1 A's static m2 B's static m2 11/12/2018

Constructor Name No return Not static Related to the use of new Overloading 11/12/2018

Mutator Accessor toString Argument and parameter Type (compatible & overloading) Primitive type and reference type (array & object) Accessor Return toString 11/12/2018

Constructor public class A { public A() { … } public A(int x) { public class B { public void m3() { A a = new A(); A b = new A(2); } 11/12/2018

Mutator and accessor public class A { public A() { … } public void a() { public void A() { public class B { public void m3() { A a = new A(); a.a(); a.A(); // but very confused! } 11/12/2018

Note: Every class has a toString method. When an object is printed or concatenated with a String, Java calls the object's toString method. System.out.println("p1 is " + p1); is equivalent to: System.out.println("p1 is " + p1.toString()); Note: Every class has a toString method. 11/12/2018

toString public class A { private int x = 2; public A() { … } public String toString () { // no parameter! return “a” + x; public class B { public void m3() { A a = new A(); System.out.println(a); } 11/12/2018

Access and Scope Attribute (property, data field, etc) Parameter What is “this” Parameter 11/12/2018

Attribute what is this public class A { private int x; public A(int y) { x = y; } public void m(int x) { // int x; this.x = x; public String toString(){ return x+””; public class B { public void m3() { A a = new A(2); a.m(3); System.out.println(a); } 11/12/2018

Parameter public class A { public int x; public A(int y) { x = y; } public void n() { // another method // other than m public void m(A a) { // x and a.x // n(); and a.n(); public class B { public void m3() { A a = new A(2); A b = new A(3); a.m(b); } 11/12/2018

Parameter public class A { private int x; public A(int y) { x = y; } public void n() { // another method // other than m public int getX(){ return x; public void m(A a) { // x and a.getx() // n(); and a.n(); public class B { public void m3() { A a = new A(2); A b = new A(3); a.m(b); } 11/12/2018

ArrayOperation. java vs. ArrayOperations. java http://www. cis. temple 11/12/2018