Recap of OO concepts. Objects, classes, methods and more. Mairead Meagher Dr. Siobhán Drohan. Produced by:

Similar documents
Using Methods. Writing your own methods. Dr. Siobhán Drohan Mairead Meagher. Produced by: Department of Computing and Mathematics

Ticket Machine Project(s)

Some miscellaneous concepts

Object-Oriented Programming Concepts

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

Using Methods. Methods that handle events. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Conditional Events. Mouse events and Operators. Dr. Siobhán Drohan Mairead Meagher. Produced by:

Inheritance. Exploring Polymorphism. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Strings. Strings and their methods. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Using Methods. More on writing methods. Dr. Siobhán Drohan Mairead Meagher. Produced by: Department of Computing and Mathematics

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

Iteration in Programming

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

Chapter 4: Writing Classes

CS1004: Intro to CS in Java, Spring 2005

BBM 102 Introduction to Programming II Spring 2017

ECE 122. Engineering Problem Solving with Java

More Sophisticated Behaviour

An Introduction to Processing

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

Encapsulation. Mason Vail Boise State University Computer Science

CS 302 Week 9. Jim Williams

Software and Programming 1

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

In our classes, we've had

Principles of Object Oriented Programming. Lecture 4

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

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

Ch 7 Designing Java Classes & Class structure. Fields have data values define/describe an instance

Ch 7 Designing Java Classes & Class structure. Methods: constructors, getters, setters, other e.g. getfirstname(), setfirstname(), equals()

CMPS 11 Intermediate Programming Midterm 2 Review Problems

One of these "compartments" is more correctly referred to as an element of the array

Chapter 4 Defining Classes I

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

Inheritance. Improving Structure with Inheritance. Dr. Siobhán Drohan Mairead Meagher. Produced by:

Programming II (CS300)

Anatomy of a Class Encapsulation Anatomy of a Method

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

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

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

Computer Programming

ITI Introduction to Computing II

Open Closed Principle (OCP)

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

Object Oriented Programming 2015/16. Final Exam June 17, 2016

Classes and Methods: Classes

Programming II (CS300)

Simple Java Reference

Programming II (CS300)

CMPS 12A Introduction to Programming Midterm 2 Review Problems

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

Ch 7 Designing Java Classes & Class structure

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

Exception Handling. Handling bad user input. Dr. Siobhán Drohan Maireád Meagher. Produced by:

CS1083 Week 2: Arrays, ArrayList

Classes. Classes as Code Libraries. Classes as Data Structures

Objects. say something to express one's disapproval of or disagreement with something.

ITI Introduction to Computing II

CS 101 Fall 2006 Midterm 3 Name: ID:

Assignment 2 specification Inheritance, Polymorphism and Abstraction

Software and Programming 1

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

Informatik II Tutorial 6. Subho Shankar Basu

Methods and Data (Savitch, Chapter 5)

G52CPP C++ Programming Lecture 9

Defining Classes. Chap 1 introduced many concepts informally, in this chapter we will be more formal in defining

Designing Classes part 2

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

The Object Oriented Paradigm

More Sophisticated Behaviour

AP COMPUTER SCIENCE A

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

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

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

Object-Oriented Design Lecture 16 CS 3500 Fall 2010 (Pucella) Friday, Nov 12, 2010

JUnit Framework. Terminology: assertions, annotations, fixtures. Dr. Siobhán Drohan Mairead Meagher. Produced by:

Informatik II (D-ITET) Tutorial 6

(a) Write the signature (visibility, name, parameters, types) of the method(s) required

Chapter 6 Class and Method

CSCI 1301: Introduction to Computing and Programming

Software and Programming 1

Encapsulation in Java

Working with classes and objects COSC346

Object Oriented Programming

Documenting, Using, and Testing Utility Classes

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

Understanding class definitions

Recommended Group Brainstorm (NO computers during this time)

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

Today s Agenda. Quick Review

More on Abstraction in Java

Java Classes & Primitive Types

Notes on Chapter Three

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

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

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

An Introduction to Processing

CLASSES AND OBJECTS IN JAVA

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

Transcription:

Recap of OO concepts Objects, classes, methods and more. Produced by: Mairead Meagher Dr. Siobhán Drohan Department of Computing and Mathematics http://www.wit.ie/

Classes and Objects A class defines a group of related methods (functions) and fields (variables). An object is a single instance of a class i.e. an object is created from a class. Objects can be related to real-world artefacts. Many objects can be constructed from a single class definition. Each object must have a unique name within the program.

Spot Class xcoord ycoord diameter Spot Spot() Spot(float, float, float) display() getxcoord() getycoord() getdiameter() setxcoord(float) setycoord(float) setdiameter(float) tostring() xcoord ycoord outerspot innerspot 50.0 70.0 diameter 60.0 xcoord ycoord 60.0 75.0 diameter 20.0 Two objects. Each has a unique name and it s own copy (values) of the fields.

Object State There are two objects of type Spot. Each object has a unique name: innerspot outerspot Each object has a different object state: each object has it s own copy of the fields (xcoord, ycoord, diameter) in memory and has it s own data stored in these fields. outerspot xcoord 50.0 ycoord 70.0 diameter 60.0 innerspot xcoord 60.0 ycoord 75.0 diameter 20.0

Spot Class Define the class as public public class Spot private float xcoord, ycoord; private float diameter; Declare the fields in the class as private Source: Reas & Fry (2014)

Constructor(s) public class Spot private float xcoord, ycoord; private float diameter; //Default Constructor public Spot() Spot() is the default constructor that is called to build an object in memory. A constructor is a method that has the same name as the class but has no return type. //Constructor to initialise all instance fields. public Spot(float xcoord, float ycoord, float diameter) this.xcoord = xcoord; this.ycoord = ycoord; this.diameter = diameter;

Getters Accessor methods return information about the state of an object. A getter method is a specific type of accessor method and typically: contains a return statement (as the last executable statement in the method). defines a return type. does NOT change the object state. public class Spot private float xcoord, ycoord; private float diameter; //constructors // getters // public float getdiameter() return diameter; public float getxcoord() return xcoord; public float getycoord() return ycoord;

Getters visibility modifier return type method name public float getdiameter() return diameter; parameter list (empty) return statement start and end of method body (block)

Setters Mutator methods change (i.e. mutate!) an object s state. A setter method is a specific type of mutator method and typically: contains an assignment statement takes in a parameter changes the object state. public class Spot private float xcoord, ycoord; private float diameter; //constructors // getters // setters // public void setdiameter(float diameter) this.diameter = diameter; public void setxcoord(float xcoord) this.xcoord = xcoord; public void setycoord(float ycoord) this.ycoord = ycoord;

Setters return type visibility modifier method name parameter public void setdiameter(float diameter) this.diameter = diameter; field being mutated. assignment statement Value passed as a parameter

Getters/Setters For each instance field in a class, you are normally asked to write: A getter A setter

Improving the constructor with validation public Spot(float xcoord, float ycoord, float diameter) this.xcoord = xcoord; this.ycoord = ycoord; this.diameter = diameter; public Spot(float xcoord, float ycoord, float diameter) this.xcoord = xcoord; this.ycoord = ycoord; if ((diameter > 0) && (diameter < 500)) else this.diameter = diameter; this.diameter = 10; Note: in the constructor, you typically set the field to a default value if invalid data was entered.

Improving the setter public void setdiameter(float diameter) if ((diameter > 0) && (diameter < 500)) this.diameter = diameter; Note: The validation done at constructor level must be repeated at setter level for that field data integrity! However, in setter methods, you typically do not update the field s value if invalid data was entered (notice how the else part of the if is not there).

tostring() public class Spot private float xcoord, ycoord; private float diameter; //constructors //getters //setters public String tostring() return ( + xcoord +, + ycoord + ). Diameter is: + diameter;

tostring() The tostring() method returns a string version of an object. This is a useful method and you will write a tostring() method for most of your classes. When you print an object, Java automatically calls the tostring() method e.g. Spot spot = new Spot(); //both of these lines of code do the same thing System.out.println(spot); System.out.println(spot.toString());

Other Methods xcoord ycoord diameter Spot Spot() Spot(float, float, float) display() getxcoord() getycoord() getdiameter() setxcoord(float) setycoord(float) setdiameter(float) tostring() public class Spot private float xcoord, ycoord; private float diameter; //constructors //getters //setters public void display() ellipse(xcoord, ycoord, diameter, diameter);

Using the Spot Class Declaring an object sp, of type Spot. Calling the Spot(float, float, float) constructor to build the sp object in memory. Calling the display method, over the sp object. Spot sp; sp = new Spot(40,30,45); sp.display();

Questions?

Department of Computing and Mathematics http://www.wit.ie/