The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Generics and the ArrayList Class
Chapter 4 Defining Classes I Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 41 Defining Classes and Methods Chapter 4.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Chapter 4 Defining Classes I Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Learning Objectives  Parameters  Call-by-value  Call-by-reference  Mixed parameter-lists  Overloading and Default Arguments  Examples, Rules  Testing.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS102--Object Oriented Programming Lecture 3: – Defining classes (10 questions) – The StringTokenizer class – The Math class Copyright © 2008 Xiaoyan Li.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Classes, Objects, and Methods
CMSC 202 Inheritance II. Version 10/102 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Advanced topic - variable.
SWE 510: Object Oriented Programming in Java1 Defining Classes 1 This slide set was compiled from the Absolute Java textbook and the instructor’s own class.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part C Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined.
CMSC 202 Inheritance II. Version 10/092 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
Slides prepared by Rose Williams, Binghamton University Chapter 4 Defining Classes I.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Chapter 4 Defining Classes I Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Java-02 Basic Concepts Review concepts and examine how java handles them.
Static Methods Slides 2 to 15 only Static Methods A static method is one that can be used without a calling object A static method still belongs.
Chapter 4 Defining Classes I Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
The need for Programming Languages
Arrays 2/4 By Pius Nyaanga.
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.
Classes, Objects, and Methods
Defining Classes and Methods
CSE 1030: Implementing Non-Static Features
Implementing Non-Static Features
Arrays .
Defining Classes and Methods
Java Classes and Objects 4th Lecture
Chapter 4 Defining Classes I
Defining Classes and Methods
Defining Classes and Methods
Classes, Objects and Methods
Chapter 4 Constructors Section 4.4
CMSC 202 Inheritance II.
Classes and Objects Object Creation
CMSC 202 Constructors Version 9/10.
Presentation transcript:

The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been fully tested and debugged

Pitfall: Use of the Terms "Parameter" and "Argument" Do not be surprised to find that people often use the terms parameter and argument interchangeably When you see these terms, you may have to determine their exact meaning from context

The this Parameter All instance variables are understood to have. in front of them If an explicit name for the calling object is needed, the keyword this can be used –myInstanceVariable always means and is always interchangeable with this.myInstanceVariable

The this Parameter this must be used if a parameter or other local variable with the same name is used in the method –Otherwise, all instances of the variable name will be interpreted as local int someVariable = this.someVariable

The this Parameter The this parameter is a kind of hidden parameter Even though it does not appear on the parameter list of a method, it is still a parameter When a method is invoked, the calling object is automatically plugged in for this

A Constructor Has a this Parameter Like any ordinary method, every constructor has a this parameter The this parameter can be used explicitly, but is more often understood to be there than written down The first action taken by a constructor is to automatically create an object with instance variables Then within the definition of a constructor, the this parameter refers to the object created by the constructor

Methods That Return a Boolean Value An invocation of a method that returns a value of type boolean returns either true or false Therefore, it is common practice to use an invocation of such a method to control statements and loops where a Boolean expression is expected –if-else statements, while loops, etc.

The methods equals and toString Java expects certain methods, such as equals and toString, to be in all, or almost all, classes The purpose of equals, a boolean valued method, is to compare two objects of the class to see if they satisfy the notion of "being equal" –Note: You cannot use == to compare objects public boolean equals(ClassName objectName) The purpose of the toString method is to return a String value that represents the data in the object public String toString()

Encapsulation

Preconditions and Postconditions The precondition of a method states what is assumed to be true when the method is called The postcondition of a method states what will be true after the method is executed, as long as the precondition holds It is a good practice to always think in terms of preconditions and postconditions when designing a method, and when writing the method comment

Variables in Memory

References Every variable is implemented as a location in computer memory When the variable is a primitive type, the value of the variable is stored in the memory location assigned to the variable –Each primitive type always require the same amount of memory to store its values

References When the variable is a class type, only the memory address (or reference) where its object is located is stored in the memory location assigned to the variable –The object named by the variable is stored in some other location in memory –Like primitives, the value of a class variable is a fixed size –Unlike primitives, the value of a class variable is a memory address or reference –The object, whose address is stored in the variable, can be of any size

References Two reference variables can contain the same reference, and therefore name the same object –The assignment operator sets the reference (memory address) of one class type variable equal to that of another –Any change to the object named by one of theses variables will produce a change to the object named by the other variable, since they are the same object variable2 = variable1;

Class Type Variables Store a Reference (Part 1 of 2)

Class Type Variables Store a Reference (Part 2 of 2)

Class Parameters All parameters in Java are call-by-value parameters –A parameter is a local variable that is set equal to the value of its argument –Therefore, any change to the value of the parameter cannot change the value of its argument Class type parameters appear to behave differently from primitive type parameters –They appear to behave in a way similar to parameters in languages that have the call-by- reference parameter passing mechanism

Class Parameters The value plugged into a class type parameter is a reference (memory address) –Therefore, the parameter becomes another name for the argument –Any change made to the object named by the parameter (i.e., changes made to the values of its instance variables) will be made to the object named by the argument, because they are the same object –Note that, because it still is a call-by-value parameter, any change made to the class type parameter itself (i.e., its address) will not change its argument (the reference or memory address)

Parameters of a Class Type

Memory Picture for Display 5.14 (Part 1 of 3)

Memory Picture for Display 5.14 (Part 2 of 3)

Memory Picture for Display 5.14 (Part 3 of 3)

Differences Between Primitive and Class-Type Parameters A method cannot change the value of a variable of a primitive type that is an argument to the method In contrast, a method can change the values of the instance variables of a class type that is an argument to the method

Pitfall: Use of = and == with Variables of a Class Type Used with variables of a class type, the assignment operator ( = ) produces two variables that name the same object –This is very different from how it behaves with primitive type variables The test for equality ( == ) also behaves differently for class type variables –The == operator only checks that two class type variables have the same memory address –Unlike the equals method, it does not check that their instance variables have the same values –Two objects in two different locations whose instance variables have exactly the same values would still test as being "not equal"