25 Jan 2011 CMPT166 Dr. Sean Ho Trinity Western University

Slides:



Advertisements
Similar presentations
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Advertisements

Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Written by: Dr. JJ Shepherd
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
Road Map Introduction to object oriented programming. Classes
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
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.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Static Class Members Wrapper Classes Autoboxing Unboxing.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
Writing Classes (Chapter 4)
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
More Object Concepts Chapter 4.  Our class is made up of several students and each student has a name and test grades  How do we assign the variables.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
CSC 142 Computer Science II Zhen Jiang West Chester University
CSE 501N Fall ‘09 04: Introduction to Objects 08 September 2009 Nick Leidenfrost.
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 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Chapter 5 Introduction to Defining Classes
1 Chapter 3 – Object-Based Programming 2 Initializing Class Objects: Constructors Class constructor is a specical method to initialise instance variables.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Recitation 8 User Defined Classes Part 2. Class vs. Instance methods Compare the Math and String class methods that we have used: – Math.pow(2,3); – str.charAt(4);
Written by: Dr. JJ Shepherd
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Topic: Classes and Objects
Outline Creating Objects The String Class Packages Formatting Output
Examples of Classes & Objects
Java Primer 1: Types, Classes and Operators
More About Objects and Methods
University of Central Florida COP 3330 Object Oriented Programming
Implementing Classes Yonglei Tao.
Road Map Introduction to object oriented programming. Classes
CMSC 202 Static Methods.
Defining Classes II.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Classes & Objects: Examples
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Classes and Objects 5th Lecture
Java Classes and Objects 3rd Lecture
JAVA Constructors.
Object-Oriented Programming
Chapter 9 Objects and Classes
Chapter 6.
Chap 2. Identifiers, Keywords, and Types
OO Programming Concepts
Creating and Using Classes
Chapter 7 Objects and Classes
Presentation transcript:

25 Jan 2011 CMPT166 Dr. Sean Ho Trinity Western University Object References 25 Jan 2011 CMPT166 Dr. Sean Ho Trinity Western University

CMPT166: static, references, JUnit Outline for today Keyword static Scope References and constructors Wrapper classes for primitive types Date and DateFormat Unit testing with JUnit CMPT166: static, references, JUnit 25 Jan 2011

CMPT166: static, references, JUnit static keyword public static void main( String args[] ) { static keyword: class attribute Shared by all instances of this class vs. instance attribute: separate for each object Exists before class is instantiated Invoke class methods as: ClassName.method() Running a class vs. instantiating a class: Run a class from JRE: java MyClass No instances made, just MyClass.main() invoked Instantiating: new MyClass() Constructor is run, main() is not run CMPT166: static, references, JUnit 25 Jan 2011

CMPT166: static, references, JUnit static import import static java.lang.Math.*; Import all static members of a class Brings static variables/methods into current namespace: sqrt( 36.0 ); instead of Math.sqrt( 36.0 ); log( E ); instead of Math.log( Math.E ); Can also bring in one particular member: import static java.lang.Math.sqrt; CMPT166: static, references, JUnit 25 Jan 2011

CMPT166: static, references, JUnit Scope vs. duration The duration (lifetime) of an identifier is the runtime period when it exists in memory Automatic duration Local variables disappear when block finishes Static duration As long as the object/module/program exists The scope of an identifier is the lexical extent where it can be referenced Block scope Class scope CMPT166: static, references, JUnit 25 Jan 2011

CMPT166: static, references, JUnit Scope example public class ScopeExample { int numApples = 0; // class scope public void listApples() { int counter = 0; // block scope } numApples is an instance variable with class scope: accessible to all methods of this class counter is a local variable with block scope: not accessible outside the listApples() method CMPT166: static, references, JUnit 25 Jan 2011

References and copy construct. Straight assignment of objects merely makes an alias (reference): Student joe = new Student(“Joe Smith”); Student jane = joe; // alias How to implement deep copy? Copy constructor Overload constructor to accept another object of the same type: public Student(String name) { … } public Student(Student other) {// copy constr. name = other.name; Using the copy constructor: Student jane = new Student(joe); CMPT166: static, references, JUnit 25 Jan 2011

Overloaded constructors In summary, any well-designed class that stores data (attributes) ought to have: Private (or protected) attributes Public set/get methods as appropriate Several overloaded constructors: Using args to initialize attributes With fewer or no args (using default values) With a single object of same type (copy constructor) Other public methods for desired functionality CMPT166: static, references, JUnit 25 Jan 2011

CMPT166: static, references, JUnit Null reference To create an object, first declare it: Student joe; Then create a new instance: joe = new Student(“Joe Smith”); joe.getName(); Before an object is assigned, it has value null When accepting objects as function parameters, check to ensure they are not null references: e.g., in the copy constructor: public void Student(Student other) { if (other != null) { ... CMPT166: static, references, JUnit 25 Jan 2011

Initializing object attributes Set default values for attributes in constructor: public class Student { String name; Date birthdate; public Student() { name = “Joe”; birthdate = new Date(); } Or initialize in declaration (only for non-objects): String name = “Joe”; birthdate = new Date(); } CMPT166: static, references, JUnit 25 Jan 2011

CMPT166: static, references, JUnit Wrapper classes Java is OO: “everything is an object” What about primitive types: int, char, etc.? Wrapper classes: Integer, Character, Double, … Auto-boxing/unboxing: Integer numApples = 15; int numA = numApples; Static methods to convert to/from Strings: int numA = Integer.parseInt(“12.58”); Double.toString(12.58); Can define .toString() for any class (Py: __str__) CMPT166: static, references, JUnit 25 Jan 2011

CMPT166: static, references, JUnit Date Get the current date and time: import java.util.Date; Date now = new Date(); Stores number of milliseconds since midnight 1Jan1970 UTC (the “epoch”) Format it in current timezone for display: import java.text.SimpleDateFormat; DateFormat fmt = new SimpleDateFormat( “yyyy/MM/dd HH:mm:ss”); fmt.format( now ); CMPT166: static, references, JUnit 25 Jan 2011

CMPT166: static, references, JUnit DateFormat The date is universal, same across the globe How it is formatted depends on local timezone SimpleDateFormat creates a DateFormat formatter object that can convert between the Date (universal) and a string (localized) Date → String: fmt.format( date ); String→Date: fmt.parse(“27 Jan 2010 15:00”) More info: see JavaSE documentation: Date, DateFormat CMPT166: static, references, JUnit 25 Jan 2011

CMPT166: static, references, JUnit Class design: testbed Main class (Student): attribs, methods, constr. public class Student { String name; short ID; public Student() {…} Testbed class (StudentTest): main() and other methods create instances of Student and call methods: public class StudentTest { public static void main( String args[] ) { Student s1 = new Student(); s1.setName(“Joe Smith”); CMPT166: static, references, JUnit 25 Jan 2011

Unit testing with JUnit4 Create a separate class to hold your testcases import org.junit.Test; import static org.junit.Assert.*; Each test case is a method: declare with @Test Create some objects from your class Call some methods on your objects Make assertions: assertEquals( a, b ); Run the test cases: In Eclipse: New → JUnit Test Case, and Run org.junit.runner.JUnitCore.runClasses( TestClass1.class ); CMPT166: static, references, JUnit 25 Jan 2011