Object Oriented Programming SCJ2153. Class and Object. Associate Prof. Dr. Norazah Yusof

Similar documents
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

Chapter 4. Defining Classes I

Notes on Chapter Three

ECOM 2324 COMPUTER PROGRAMMING II

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Programming II (CS300)

Chapter 8 Objects and Classes

Programming II (CS300)

Objects as a programming concept

OO Programming Concepts. Classes. Objects. Chapter 8 User-Defined Classes and ADTs

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

UFCE3T-15-M Object-oriented Design and Programming

ENCAPSULATION AND POLYMORPHISM

The class definition is not a program by itself. It can be used by other programs in order to create objects and use them.

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Getter and Setter Methods

Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Creating Your Own Classes

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Chapter 9 Objects and Classes. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.

Classes and Methods: Classes

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Chapter 9 Objects and Classes. OO Programming Concepts. Classes. Objects. Motivations. Objectives. CS1: Java Programming Colorado State University

Distributed Systems Recitation 1. Tamim Jabban

Distributed Systems Recitation 1. Tamim Jabban

CS1004: Intro to CS in Java, Spring 2005

Chapter 8 Objects and Classes Part 1

CS 2530 INTERMEDIATE COMPUTING

Lecture 07: Object Encapsulation & References AITI Nigeria Summer 2012 University of Lagos.

Chapter 8 Objects and Classes. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Chapter 3 Objects and Classes

Media Computation. Lecture 15.2, December 3, 2008 Steve Harrison

5. Defining Classes and Methods

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

Handout 7. Defining Classes part 1. Instance variables and instance methods.

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

Chapter 5: Classes and Objects in Depth. Introduction to methods

Chapter 9. Objects and Classes

OO Programming Concepts

Chapter 12: How to Create and Use Classes

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

Programming II (CS300)

Anatomy of a Class Encapsulation Anatomy of a Method

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

Principles of Object Oriented Programming. Lecture 4

Object-Oriented Programming (Java)

Chapter 4 Defining Classes I

CS 170, Section /3/2009 CS170, Section 000, Fall

Chapter 4: Writing Classes

Classes. Classes as Code Libraries. Classes as Data Structures

10 Creating your own Classes

Encapsulation in Java

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Chapter 6 Class and Method

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

Lecture 5: Methods CS2301

Methods and Data (Savitch, Chapter 5)

Software and Programming 1

Understanding class definitions. Looking inside classes (based on lecture slides by Barnes and Kölling)

Object-Oriented Programming in Java

Recitation 3 Class and Objects

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

CS1083 Week 2: Arrays, ArrayList

Java Professional Certificate Day 1- Bridge Session

What is Inheritance?

CLASSES AND OBJECTS IN JAVA

ECE 122. Engineering Problem Solving with Java

Learn more about our research, discover data science, and find other great resources at:

ECE 122. Engineering Problem Solving with Java

Classes and Objects. CGS 3416 Spring 2018

CS 302 Week 9. Jim Williams

Abstraction in Software Development

Ticket Machine Project(s)

This Week. Fields and Variables. W05 Example 1: Variables & Fields. More on Java classes. Constructors. Modifiers

Darshan Institute of Engineering & Technology for Diploma Studies Unit 3

Arrays Classes & Methods, Inheritance

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

CSCI 1301: Introduction to Computing and Programming Summer 2018 Lab 07 Classes and Methods

Today s Agenda. Quick Review

Final Examination Semester 3 / Year 2010

COMP 250 Winter 2011 Reading: Java background January 5, 2011

Object Oriented Relationships

Java and OOP. Part 2 Classes and objects

Overview of Eclipse Lectures. Module Road Map

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Introduction to Classes and Objects. David Greenstein Monta Vista High School

class declaration Designing Classes part 2 Getting to know classes so far Review Next: 3/18/13 Driver classes:

EECS168 Exam 3 Review

CHAPTER 7 OBJECTS AND CLASSES

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

Chapter 7. Inheritance

Software and Programming 1

5. Defining Classes and Methods

Object-Orientation. Classes Lecture 5. Classes. State and Behaviour. Instance Variables. Object vs. Classes

10 Creating your own Classes. Introduction to Programming 1 1

PIC 10A. Lecture 15: User Defined Classes

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Transcription:

Object Oriented Programming SCJ2153 Class and Object Associate Prof. Dr. Norazah Yusof

Classes Java program consists of classes. Class is a template for creating objects. Class normally consists of 3 components: 1. data members/attributes (also known as fields), 2. contructors (a special kind of method) 3. methods 2

Class Diagram Before a class is defined, it is always helpful to design using a class diagram. Class diagram is a set of standard diagram that graphically shows the object oriented system. The diagram consists of a box that is divided into 3 parts: Class name Attributes Constructors and methods 3

Structure of Class Diagram 4

Example of a Class Diagram Employee - empnum: int - empname: String + setempnum(int): void + getempnum(): int + setempname(string): void + getempname(): String 5

Class Diagram for Employee Employee - empnum: int - empname: String + setempnum(int): void + getempnum(): int + setempname(string): void + getempname(): String Class name Attributes / instance variables that define the object s state; can hold numbers, characters, strings, other objects Actions that an object of this class can take (behaviors) The symbol means private access modifier. The + symbol means public access modifier. 6

Structure of a Class Definition public class ClassName { // attributes definitions //constructor definitions public ClassName(paramList) { //method definitions public returntype methodname(paramlist) { 7

Class Definition public class Employee { private int empnum; private String empname; Employee - empnum: int - empname: String + setempnum(int): void + getempnum(): int + setempname(string): void + getempname(): String class diagram public void setempnum(int num) { empnum = num; public int getempnum() { return empnum; public void setempname(string name) { empname = name; public String getempname() { return empname; 8

Information Hiding Principle Accessor and Mutator method Information hiding using encapsulation Attributes are usually private Client application accesses them only through public interfaces Mutator method Controls data values used to set variable Accessor method Controls how value retrieved 9 9

Accessors and Mutators For the Employee class example, the accessors and mutators are: setempnum : Sets the value of the employee s number field. public void setempnum(int num) { setempname : Sets the value of the employee s name field. public void setempname(string name) getempnum : Returns the value of the employee s number field. public int getempnum() { getempname : Returns the value of the employee s name field. public String getempname() { Other names for these methods are getters and setters. 10

Declaring Object Reference Variable and Create Object To reference an object, assign the object to a reference variable. To declare a reference variable, use the syntax: ClassName objectrefvar; To create object: objectrefvar = new ClassName(); Example: Employee emp1; emp1 = Employee(); 11

Declaring/Creating Objects in a Single Step ClassName objectrefvar = new ClassName(); Assign object reference Create an object Example: Employee emp1 = new Employee(); 12

Accessing Objects After object instantiated, methods accessed using: Object s identifier Dot Data/Method call Referencing the object s data: objectrefvar.data Example: emp1.empnum Invoking the object s method: objectrefvar.methodname(arguments) Example: emp1.getempnum() 13

Constructors Special kind of methods for creating objects of a class, which play the role of initializing objects. The name of constructor must be the same as name of class. Constructors do not have a return type and they do not return a value. If a programmer does not define any constructors in a class, Java provides one default constructor i.e. a no argument constructor. 14

Constructors Constructors may take parameters. If a constructor has one parameter, it is called a one-argument constructor. If a class has more than one constructors, they must have different numbers and/or types of parameters. This is called constructor overloading Syntax: public ClassName(paramList) { 15

Class Definition with constructor public class Employee { private int empnum; private String empname; Employee - empnum: int - empname: String + Employee() + Employee (int, String) + getempnum(): int + getempname(): String class diagram public Employee() { System.out.println ( Start Employee ); public Employee(int num, String name) { empnum = num; empname = name; public int getempnum() { return empnum; public String getempname() { return empname; 16

Creating Object == invoking Constructors Constructors are invoked using the new operator when an object is created. Constructors play the role of initializing objects. Syntax: new ClassName(); Example: new Employee(); new Employee(123, Ahmad ); 17

Programming Example 18

Given the following class diagram about Travel write complete Java program for the traveller. 19

Travel.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class Travel { private String destination, name, address; private double price; public Travel(){ public Travel(String name, String destination){ this.name = name ; this.destination = destination; public String getdestination(){ return destination; 20

Travel.java (cont.) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public String getname(){ return name; public String getaddress(){ return address; public double getprice(){ return price; public void setdestination(string d){ destination = d; public void setname(string n){ name = n; 21

Travel.java (cont.) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public void setaddress(string a){ address = a; public void setprice(double p){ price = p; public void display(){ System.out.println("\nDestination = "+destination); System.out.println("Name of traveller = "+name); System.out.println("Address of traveller = " + address); System.out.printf("Price = RM %.2f \n", price); 22

TestTravel.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class TestTravel { public static void main(string[] args) { Travel ob1 = new Travel(); Travel ob2 = new Travel("Eyan","Indonesia"); ob1.setname("toh"); ob1.setdestination("italy"); ob1.setaddress("no.1 Tmn Emas,30220 Ipoh, Perak,Malaysia"); ob1.setprice(5999); ob1.display(); ob2.setaddress("n0.100 Tmn Raja,32223 Taiping, Malaysia"); ob2.setprice(3999); ob2.display(); 23