Object-Oriented Programming in Java

Similar documents
More on Objects and Classes

Software and Programming 1

Fundamentos de programação

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

Software and Programming 1

Loops and Expression Types

Inheritance and Subclasses

Software and Programming 1

Branching and Boolean Expressions

Introduction to Programming Using Java (98-388)

Branching and Boolean Expressions

Chapter 4 Defining Classes I

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

Software and Programming I. Classes and Arrays. Roman Kontchakov / Carsten Fuhs. Birkbeck, University of London

Arrays and Basic Algorithms

Principles of Object Oriented Programming. Lecture 4

EECS168 Exam 3 Review

Chapter 4. Defining Classes I

Input-Output and Exception Handling

Introduction Basic elements of Java

CS 455 Midterm Exam 1 Spring 2011 [Bono] Feb. 17, 2011

The Object Oriented Paradigm

MIDTERM REVIEW. midterminformation.htm

Software and Programming 1

CS 455 Midterm Exam 1 Spring 2011 [Bono] Feb. 17, 2011

Object-Oriented Design

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

ECE 122. Engineering Problem Solving with Java

Software and Programming 1

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

Object Oriented Modeling

Encapsulation in Java

Chapter 6 Introduction to Defining Classes

Software and Programming 1

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

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

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

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

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

CHAPTER 7 OBJECTS AND CLASSES

TaxiBot New attributes Variables Math! TaxiBot

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

Java Classes & Primitive Types

Java Classes & Primitive Types

Programming Language Concepts: Lecture 2

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

Selected Questions from by Nageshwara Rao

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

Java and OOP. Part 2 Classes and objects

Imperative Languages!

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

CS 101 Fall 2006 Midterm 3 Name: ID:

Object Oriented Programming in C#

Java Magistère BFA

int: integers, no fractional part double: floating-point numbers (double precision) 1, -4, 0 0.5, , 4.3E24, 1E-14

What will this print?

Chapter 6 Lab Classes and Objects

CS1083 Week 2: Arrays, ArrayList

2/9/2012. Chapter Four: Fundamental Data Types. Chapter Goals

Object Oriented Programming

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

Recitation 02/02/07 Defining Classes and Methods. Chapter 4

CPSC 233: Assignment 4 (Due March 26 at 4 PM)

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

Anatomy of a Class Encapsulation Anatomy of a Method

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

System.out.print(); Scanner.nextLine(); String.compareTo();

Programming II (CS300)

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

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Programming II (CS300)

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Dot and Scope Resolution Operator

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Collections and Maps

ENCAPSULATION AND POLYMORPHISM

Encapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods

Programming overview

Java Magistère BFA

Classes. Classes as Code Libraries. Classes as Data Structures

Object Communication

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Object Oriented Programming

CSE 214 Computer Science II Java Classes and Information Hiding

H212 Introduction to Software Systems Honors

Programming Language Concepts: Lecture 2

This exam is open book. Each question is worth 3 points.

CHAPTER 7 OBJECTS AND CLASSES

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

PASS4TEST IT 인증시험덤프전문사이트

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal

Recitation 3 Class and Objects

Java. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2)

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

public class TicketMachine Inner part omitted. public class ClassName Fields Constructors Methods

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

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

Transcription:

Software and Programming I Object-Oriented Programming in Java Roman Kontchakov / Carsten Fuhs Birkbeck, University of London

Outline Object-Oriented Programming Public Interface of a Class Instance Variables Instance Methods and Constructors Sections 8.1 8.6 (7.1 7.5 in 1/e) SP1 2018-06 1

Object-Oriented Programming Tasks are solved by collaborating objects Each object has its own set of data, together with a set of methods that act upon the data A class describes a set of objects with the same behaviour (i.e., methods) Objects are constructed with the new operator: Scanner in = new Scanner(System.in); SP1 2018-06 2

Classes and Objects Class represents a set of objects that share a common structure and a common behaviour Objects are instances of classes Booch, G.: Object Oriented Analysis and Design with Applications (2nd Edition) SP1 2018-06 Addison-Wesley, 1994 3

Example: Cash Register A cashier who rings up a sale presses a key to start the sale; then rings up each item. A display shows the amount owed as well as the total number of items purchased (a use case description) We want the following methods on a cash register object: add the price of an item get the total amount owed and the count of items purchased clear the cash register to start a new sale SP1 2018-06 4

Example: Cash Register (2) 1 /** 2 A simulated cash register 3 */ 4 public class CashRegister { 5 /* private data */ 6... 7 /* methods (public interface) */ 8 public void additem(double price) { /* */ } 9 public double gettotal() { /* */ } 10 public int getcount() { /* */ } 11 public void clear() { /* */ } 12 } SP1 2018-06 5

Using the CashRegister constructing an object (for example, in class CashRegisterTest) CashRegister register1 = new CashRegister(); invoking methods register1.additem(1.95); register1.additem(2.99); System.out.println(register1.getTotal() + " " + register1.getcount()); SP1 2018-06 6

Instance Variables An object holds instance variables that are accessed by its methods 1 public class CashRegister { 2 private int itemcount; 3 private double totalprice; 4 // the rest of the class 5 } values of the instance variables determine the state of the object SP1 2018-06 7

Instance Variables Every instance of a class has its own set of instance variables 1 // constructing objects 2 CashRegister reg1 = new CashRegister(); 3 CashRegister reg2 = new CashRegister(); 4 // invoking methods 5 reg1.additem(7.67); 6 System.out.println(reg1.getTotal() + " " + 7 reg1.getcount()); 8 reg2.additem(1.95); 9 reg2.additem(2.99); 10 System.out.println(reg2.getTotal() + " " + 11 reg2.getcount()); SP1 2018-06 8

Instance Variables Every instance of a class has its own set of instance variables 1 // constructing objects 2 CashRegister reg1 = new CashRegister(); 3 CashRegister reg2 = new CashRegister(); reg1 reg2 CashRegister itemcount = 0 totalprice = 0.0 CashRegister itemcount = 0 totalprice = 0.0 SP1 2018-06 9

Instance Variables Every instance of a class has its own set of instance variables 1 // constructing objects 2 CashRegister reg1 = new CashRegister(); 3 CashRegister reg2 = new CashRegister(); 4 // invoking methods 5 reg1.additem(7.67); CashRegister reg1 reg2 itemcount = 1 totalprice = 7.67 CashRegister itemcount = 0 totalprice = 0.0 SP1 2018-06 10

Instance Variables Every instance of a class has its own set of instance variables 1 // constructing objects 2 CashRegister reg1 = new CashRegister(); 3 CashRegister reg2 = new CashRegister(); 4 // invoking methods 5 reg1.additem(7.67); CashRegister 6 7 8 reg2.additem(1.95); 9 reg2.additem(2.99); reg1 reg2 itemcount = 1 totalprice = 7.67 CashRegister itemcount = 2 totalprice = 4.94 An object reference specifies the location of an object SP1 2018-06 11

Object: State, Behaviour & Identity NB: equal identical Booch, G.: Object Oriented Analysis and Design with Applications (2nd Edition) SP1 2018-06 Addison-Wesley, 1994 12

Access Modifiers private instance variables (and methods) can only be accessed by the methods of the same class reg1.itemcount = 0; // COMPILE-TIME ERROR (it is on the level of classes, not individual objects!) (any instance of CashRegister can change instance variables of any other instance of CashRegister provided that it has a reference to it) public instance variables and methods can be accessed by the methods of any class SP1 2018-06 13

Instance Methods public void additem (double price) {... } access modifier return type method name parameter type parameter name body signature all instances variables should be private encapsulation most methods should be public it is useful to classify the methods as accessors and mutators SP1 2018-06 14

Instance Methods: Accessors An accessor method just queries the object for some information without changing it 1 public class CashRegister { 2 //... 3 public double gettotal() { 4 return totalprice; 5 } 6 public int getcount() { 7 return itemcount; 8 } 9 } SP1 2018-06 15

Instance Methods: Mutators A mutator method changes the object on which it operates 1 public class CashRegister { 2 //... 3 public void additem(double price) { 4 itemcount++; 5 totalprice += price; 6 } 7 } NB: no return statement is needed if the return type is void SP1 2018-06 16

Constructors A constructor initialises the instance variables of an object The name of the constructor is the name of the class (and no return type, not even void) 1 public class CashRegister { 2 //... 3 public CashRegister() { 4 itemcount = 0; 5 totalprice = 0; 6 } 7 } SP1 2018-06 17

Constructors (cont.) By default, instance and class variables are initialised as follows: numbers are initialised as 0, booleans as false, and object and array references as null (special reference) what about String s;? If no constructor is provided, a constructor with no parameters is generated SP1 2018-06 18

Cash Register Class 1 public class CashRegister { 2 /* private data (instance variables) */ 3 private int itemcount; 4 private double totalprice; 5 /* methods (public interface) */ 6 public void additem(double price) 7 { itemcount++; totalprice += price; } 8 public void clear() 9 { itemcount = 0; totalprice = 0; } 10 public double gettotal() { return totalprice; } 11 public int getcount() { return itemcount; } 12 } SP1 2018-06 19

Public Interface of the CashRegister Class 1 public class CashRegister { 2 3 4 5 /* methods (public interface) */ 6 public void additem(double ) 7 { } 8 public void clear() 9 { } 10 public double gettotal() { } 11 public int getcount() { } 12 } NB: the rest are implementation details, which may be changed without affecting the users of the class SP1 2018-06 20

Testing a Class 1 public class CashRegisterTest { 2 public static void main(string[] args) { 3 CashRegister reg1 = new CashRegister(); 4 reg1.additem(2.95); 5 reg1.additem(1.99); 6 System.out.println(reg1.getCount()); 7 System.out.println((reg1.getCount() == 2) 8? "OK" : "FAIL"); 9 System.out.printf("%.2f\n", reg1.gettotal()); 10 System.out.println((reg1.getTotal() == 4.94) 11 12 } 13 }? "OK" : "FAIL"); SP1 2018-06 21

Java Compilation source compiler CashRegister.java javac source CashRegisterTest.java bytecode CashRegister.class bytecode CashRegisterTest.class Virtual Machine java public static void main(string[] args) {... } NB: statements must be inside methods! running program SP1 2018-06 22

Encapsulation Every class has a public interface: a collection of methods through which the objects of the class can be manipulated Encapsulation is the act of providing a public interface and hiding implementation details Encapsulation enables changes in the implementation without affecting users of the class SP1 2018-06 23

Encapsulation Encapsulation hides the details of the implementation of an object Booch, G.: Object Oriented Analysis and Design with Applications (2nd Edition) Addison-Wesley, 1994 SP1 2018-06 24

Encapsulation: Why? What if we want to support a method void undo() (cancels the last item)? What if we want to implement CashRegister using the fixed-point arithmetic (so that 12.92 is 1292)? Instance variables are hidden by declaring them private, but they are not hidden very well at all... SP1 2018-06 25

Take Home Messages Encapsulation enables changes in the implementation Public interface: a collection of public methods Methods: accessors and mutators Every instance of a class has its own set of instance variables All instance variables should be private A constructor initialises the instance variables SP1 2018-06 26