Example: Points in 2D Coordinate

Similar documents
Object-Oriented Programming Lecture 2: Classes and Objects

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

CS1002: COMPUTER SCIENCE OO MODELLING & DESIGN: WEEK 5

Description of Class Mutation Mutation Operators for Java

Java Classes. GEEN163 Introduction to Computer Programming

Basic Object-Oriented Programming in Java

D06 PROGRAMMING with JAVA. Ch3 Implementing Classes

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Glossary of Object Oriented Terms

More on Objects and Classes

History OOP languages Year Language 1967 Simula Smalltalk

Introduction to Object-Oriented Programming

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Java Interview Questions and Answers

Object Oriented Programming Design Basics CMSC 202

Principles of Software Construction: Objects, Design, and Concurrency. Course Introduction. toad. toad Fall School of Computer Science

Part I. Multiple Choice Questions (2 points each):

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

An Introduction To UML Class Diagrams Classes

Fundamentals of Java Programming

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Java (12 Weeks) Introduction to Java Programming Language

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Object-Oriented Programming in Java

Java How to Program, 5/e Test Item File 1 of 5

Java is an Object-Oriented Language. As a language that has the Object Oriented feature, Java supports the following fundamental concepts:

Java CPD (I) Frans Coenen Department of Computer Science

Chapter 5 Aspect Oriented Programming

JAVA - METHODS. Method definition consists of a method header and a method body. The same is shown below:

Stack vs. Heap. Introduction to Programming. How the Stack Works. Primitive vs. Reference Types. Stack

Programming Languages Featherweight Java David Walker

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

JAVA - OBJECT & CLASSES

13 Classes & Objects with Constructors/Destructors

CSE 1020 Introduction to Computer Science I A sample nal exam

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Moving from CS 61A Scheme to CS 61B Java

C++ INTERVIEW QUESTIONS

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Yosemite National Park, California. CSE 114 Computer Science I Inheritance

The Interface Concept

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

AP Computer Science Java Subset

Object Oriented Design

Inheritance, overloading and overriding

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Coding Standard for Java

LAB4 Making Classes and Objects

JavaScript. JavaScript: fundamentals, concepts, object model. Document Object Model. The Web Page. The window object (1/2) The document object

Advanced Data Structures

Object Oriented Software Design II

How to create/avoid memory leak in Java and.net? Venkat Subramaniam

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

CMSC 132: Object-Oriented Programming II. Design Patterns I. Department of Computer Science University of Maryland, College Park

Introduction: Abstract Data Types and Java Review

Lecture J - Exceptions

CMSC 202H. ArrayList, Multidimensional Arrays

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Web development... the server side (of the force)

Realizing Enterprise Integration Patterns in WebSphere

Install Java Development Kit (JDK) 1.8

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

What is Java? Applications and Applets: Result of Sun s efforts to remedy bad software engineering practices

Short Introduction to the Concepts of Programming in Java Overview over the most important constructs

The C Programming Language course syllabus associate level

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

CS193j, Stanford Handout #10 OOP 3

Java Application Developer Certificate Program Competencies

Chapter 4 OOPS WITH C++ Sahaj Computer Solutions

D06 PROGRAMMING with JAVA

Lecture 5: Java Fundamentals III

To prevent our programs to inherit multiple copies of a base class we can declare this base class as a virtual base class.

11 November

C++ Overloading, Constructors, Assignment operator

Computer Programming I

Constructor, Destructor, Accessibility and Virtual Functions

CIS 190: C/C++ Programming. Polymorphism

UML for C# Modeling Basics

Introduction to Programming

El Dorado Union High School District Educational Services

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Java Software Structures

Unit Testing and JUnit

CS 111 Classes I 1. Software Organization View to this point:

public static void main(string[] args) { System.out.println("hello, world"); } }

CS-XXX: Graduate Programming Languages. Lecture 25 Multiple Inheritance and Interfaces. Dan Grossman 2012

3D Animation of Java Program Execution for Teaching Object Oriented Concepts

Programming Methods & Java Examples

Computer Science 483/580 Concurrent Programming Midterm Exam February 23, 2009

Specialized Programme on Web Application Development using Open Source Tools

Automatic generation of fully-executable code from the Domain tier of UML diagrams

Writing Self-testing Java Classes with SelfTest

1 bool operator==(complex a, Complex b) { 2 return a.real()==b.real() 3 && a.imag()==b.imag(); 4 } 1 bool Complex::operator==(Complex b) {

MODULE 17 POLYMORPHISM

Transcription:

Example: Points in 2D Coordinate 1 class Point { 2 double x, y; // fields: data member 3 } 1 public class PointDemo { 2 public static void main(string[] args) { 3 // now create a new instance of Point 4 Point p1 = new Point(); 5 p1.x = 1; 6 p1.y = 2; 7 System.out.printf("(%d, %d)\n", p1.x, p1.y); 8 9 // create another instance of Point 10 Point p2 = new Point(); 11 p2.x = 3; 12 p2.y = 4; 13 System.out.printf("(%d, %d)\n", p2.x, p2.y); 14 } 15 } Zheng-Liang Lu Java Programming 233 / 257

Class Definition First, give a class name with the first letter capitalized, by convention. The class body, surrounded by balanced braces {}, contains data members (fields) and function members (methods) for objects. We may need an entry point to run the program if the class is used to run the whole program. Zheng-Liang Lu Java Programming 234 / 257

Data Members The fields are the states of the object. May have an access modifier, say public and private. public: accessible from all classes private: accessible only within its own class Access modifiers realize encapsulation! In other word, you can decide if these fields are accessible! In OO practice, all the fields should be declared private. Zheng-Liang Lu Java Programming 235 / 257

Function Members As said, the fields are hidden by encapsulation. So we may need accessors and mutators if necessary. Accessors: return the state of the object Mutators: set the state of the object For example, getx() and gety() are accessors, and setpoint(double, double) is a mutator in the class Point. Zheng-Liang Lu Java Programming 236 / 257

Example: Point (Encapsulated) 1 class Point { 2 private double x; 3 private double y; 4 5 double getx() { return x; } 6 double gety() { return y; } 7 8 void setx(double a) { x = a; } 9 void sety(double a) { y = a; } 10 void setpoint(double a, double b) { 11 x = a; 12 y = b; 13 } 14 } Zheng-Liang Lu Java Programming 237 / 257

Unified Modeling Language 1 Unified Modeling Language (UML) is a tool for specifying, visualizing, constructing, and documenting the artifacts of software systems, as well as for business modeling and other non-software systems. Free software: http://staruml.io/ (available for all platforms) 1 See http://www.tutorialspoint.com/uml/ and http://www.mitchellsoftwareengineering.com/introtouml.pdf. Zheng-Liang Lu Java Programming 238 / 257

Example: Class Diagram for Point Modifiers are placed before the fields and the methods: + for public for private Zheng-Liang Lu Java Programming 239 / 257

Constructors A constructor is called by the new operator. Constructors are like special methods except that they use the name of the class and have no return type. Note that the constructors are not the part of the instances. As with methods, the constructors can be overloaded. If you don t define an explicit constructor, Java assumes a default constructor for your class. Moreover, adding any explicit constructor disables the default constructor. Zheng-Liang Lu Java Programming 240 / 257

Parameterized Constructors You can provide specific information to the parameterized constructor as the object is instantiated. For example, 1 class Point { 2... 3 4 Point() {} // restore a default constructor; 5 6 // parameterized constructor 7 Point(double a, double b) { 8 x = a; 9 y = b; 10 } 11... 12 } Zheng-Liang Lu Java Programming 241 / 257

Example: Point (Revisited) 1 public class PointDemo { 2 public static void main(string[] args) { 3 Point p1 = new Point(1, 2); 4 System.out.printf("(%f, %f)", p1.getx(), p1.gety()); 5 // output (1, 2) 6 } 7 } Zheng-Liang Lu Java Programming 242 / 257

Self-reference You can refer to any (instance) member of the current object within methods and constructors by using this. The most common reason for using the this keyword is because a field is shadowed by method parameters. You can also use this to call another constructor in the same class by invoking this(). Zheng-Liang Lu Java Programming 243 / 257

Example: Point (Revisited) 1 class Point { 2... 3 Point(int x, int y) { 4 this.x = x; 5 this.y = y; 6 } 7... 8 } Note that the this operator is referenced to instance members only, but not members declared static. Zheng-Liang Lu Java Programming 244 / 257

Instance Members and Static Members You may notice that all the members are declared w/o static. These members belong to some specific object. They are called instance members. This implies that these instance members are available only when the object is created. Those declared w/ static are static members, aka class members. Zheng-Liang Lu Java Programming 245 / 257

Zheng-Liang Lu Java Programming 246 / 257

Static Members Static members mean that there is only one copy of the static members, no matter how many objects of the class are created. The static members belong to the class, and are shared between the instance objects. They are ready once the class is loaded. They can be invoked directly by the class name without creating an instance. For example, Math.random(). Zheng-Liang Lu Java Programming 247 / 257

A static method can access other static members. However, static methods cannot access to instance members directly. (Why?) For example, 1... 2 double getdistancefrom(point p) { 3 return Math.sqrt(Math.pow(this.x p.x, 2) + Math.pow( this.y p.y, 2)); 4 } 5 6 static double distancebetween(point p1, Point p2) { 7 return Math.sqrt(Math.pow(p1.x p2.x, 2) + Math.pow(p1. y p2.y, 2)); 8 } 9... Zheng-Liang Lu Java Programming 248 / 257

Example: Count of Points 1 class Point { 2... 3 private static int cnt = 0; 4 5 Point() { 6 cnt++; 7 } 8 9 Point(int x, int y) { 10 this(); // calling the constructor with no input argument; should be placed in the first line in the constructor 11 this.x = x; 12 this.y = y; 13 } 14 15... 16 } Zheng-Liang Lu Java Programming 249 / 257

Exercise: Singleton 2 In some situations, you may create the only instance of the class. 1 class mysingleton { 2 3 // Will be ready as soon as the class is loaded. 4 private static mysingleton Instance = new mysingleton(); 5 6 // Do now allow to invoke the constructor by other classes. 7 private mysingleton() {} 8 9 // Only way to obtain the singleton from the outside world. 10 public static mysingleton getsingleton() { 11 return Instance; 12 } 13 } 2 See any textbook for design patterns. Zheng-Liang Lu Java Programming 250 / 257

Garbage Collection (GC) 3 Java handles deallocation automatically. Automatic garbage collection is the process of looking at the heap memory, identifying whether or not the objects are in use, and deleting the unreferenced objects. An object is said to be unreferenced if the object is no longer referenced by any part of your program. Simply assign null to the reference-type variable to make the object referenced by this variable unreferenced. So the memory used by these objects can be reclaimed. 3 http://www.oracle.com/webfolder/technetwork/tutorials/obe/ java/gc01/index.html Zheng-Liang Lu Java Programming 251 / 257

finalize() Method The method finalize() conducts a specific task that will be executed as soon as the object is about to be reclaimed by GC. The finalize() method can be only invoked prior to GC. In practice, it must not rely on the finalize() method for normal operations. (Why?) Zheng-Liang Lu Java Programming 252 / 257

Example 1 public class finalizedemo { 2 static int numofpointkilled = 0; 3 4 public void finalize() { 5 numofpointkilled++; 6 } 7 8 public static void main(string[] args) { 9 double n = 1e7; 10 for (int i = 1; i <= n; i++) 11 new finalizedemo(); 12 System.out.println(numOfPointKilled); 13 } 14 } You may try different number for instance creation. The number of the objects reclaimed by GC is not deterministic. Zheng-Liang Lu Java Programming 253 / 257

HAS-A Relationship Association is a relationship where all objects have their own lifecycle and there is no owner. For example, teacher student Aggregation is a specialized form of association where all objects have their own lifecycle, but there is ownership and child objects can not belong to another parent object. For example, knight sword Composition is a specialized form of aggregation and we can call this as a death relationship. For example, house room Zheng-Liang Lu Java Programming 254 / 257

Example: Lines on 2D Cartesian Coordinate +2: two Point objects contained in a Line object. Zheng-Liang Lu Java Programming 255 / 257

More Examples More geometric objects, say Circle, Triangle, and Polygon. Complex number (a + bi) equipped with + and so on. Book with Authors. Lecturer and Students in the classroom. Zoo with many creatures, say Dog, Cat, and Bird. Channels played on TV. Zheng-Liang Lu Java Programming 256 / 257

More Relationships Between Classes Inheritance: passing down states and behaviors from the parents to their children Interfaces: grouping the methods, which belongs to some classes, as an interface to the outside world Packages: grouping related types, providing access protection and name space management Zheng-Liang Lu Java Programming 257 / 257