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

Similar documents
Today s Agenda. Quick Review

Understanding class definitions

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

Ticket Machine Project(s)

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

UNDERSTANDING CLASS DEFINITIONS CITS1001

CSC 222: Object-Oriented Programming. Fall 2015

CSCI 161 Introduction to Computer Science

CSC 222: Object-Oriented Programming. Fall 2017

If too much money is inserted the machine takes it all - no refund. If there isn't enough money inserted, it still prints out the ticket.

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

Lab: PiggyBank. Defining objects & classes

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

CS 302: Introduction to Programming in Java. Lecture 15

Chapter 4 Defining Classes I

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

Anatomy of a Class Encapsulation Anatomy of a Method

Introduction to Programming Using Java (98-388)

CS 302 Week 9. Jim Williams

MID-SEMESTER TEST 2014

Principles of Object Oriented Programming. Lecture 4

G52CPP C++ Programming Lecture 9

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Objects as a programming concept

Chapter 4: Writing Classes

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

MIDTERM REVIEW. midterminformation.htm

// constructor: takes a String as parameter and creates a public Cat (String x) { name = x; age = 0; lives = 9; // cats start out with 9 lives }

EECS168 Exam 3 Review

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

Object Oriented Design: Identifying Objects

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

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

Programming Language Concepts: Lecture 2

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

Chapter 6 Introduction to Defining Classes

Software Design and Analysis for Engineers

Object-Oriented Programming in Java

And Even More and More C++ Fundamentals of Computer Science

ECE 122. Engineering Problem Solving with Java

Chapter 7 User-Defined Methods. Chapter Objectives

Programming Language Concepts: Lecture 2

The Object Oriented Paradigm

Computer Science II (20073) Week 1: Review and Inheritance

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

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

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

THE UNIVERSITY OF WESTERN AUSTRALIA. School of Computer Science & Software Engineering CITS1001 OBJECT-ORIENTED PROGRAMMING AND SOFTWARE ENGINEERING

Lecture 12: Barrier Synchronization

Objects and State. COMP1400 Week 9. Wednesday, 19 September 12

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Scope of this lecture. Repetition For loops While loops

Software and Programming 1

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

Notes on Chapter Three

Assignment 19 Deadline: Nov pm COSC211 CRN15376 Session 15 (Nov. 7)

Robots. Byron Weber Becker. chapter 6

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Declarations and Access Control SCJP tips

CPS122 Lecture: Defining a Class

Scope of this lecture

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

Chapter 4. Defining Classes I

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

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

Software and Programming 1

Software and Programming 1

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Software and Programming 1

Computer Science 1 Ah

Do not start the test until instructed to do so!

Objects and Classes Lecture 2

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

TaxiBot New attributes Variables Math! TaxiBot

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

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

CS1083 Week 2: Arrays, ArrayList

Object Communication

Java for Non Majors. Final Study Guide. April 26, You will have an opportunity to earn 20 extra credit points.

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

CSC Java Programming, Fall Java Data Types and Control Constructs

AP COMPUTER SCIENCE A

Defensive Programming

Final Examination Semester 3 / Year 2012

CS112 Lecture: Working with Numbers

CS1004: Intro to CS in Java, Spring 2005

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

Packages Inner Classes

More on Objects and Classes

LECTURE 06 FUNCTIONS

Assignment 1 due Monday at 11:59pm

Lecture 13 & 14. Single Dimensional Arrays. Dr. Martin O Connor CA166

CS111: PROGRAMMING LANGUAGE II

Class, Variable, Constructor, Object, Method Questions

Java Review. Fundamentals of Computer Science

Transcription:

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

Main Concepts fields constructors methods parameters assignment statements

Ticket Machines (an external view) Exploring the behaviour of a typical ticket machine. Use the naive-ticket-machine project. Machines supply tickets of a fixed price. How is that price determined? How is money entered into a machine? How does a machine keep track of the money that is entered?

Ticket machines Demo

Ticket machines (an internal view) Interacting with an object gives us clues about its behaviour. Looking inside allows us to determine how that behaviour is provided or implemented. All Java classes have a similar-looking internal view.

Basic class structure public class TicketMachine Inner part of the class omitted. The outer wrapper of TicketMachine public class ClassName Fields Constructors Methods The contents of a class

Fields Fields store values for an object. They are also known as instance variables. Use the Inspect option to view an object s fields. Fields define the state of an object. public class TicketMachine private int price; private int balance; private int total; Further details omitted. visibility modifier type private int price; variable name

Constructors Constructors initialise an object. They have the same name as their class. public TicketMachine(int ticketcost) price = ticketcost; balance = 0; total = 0; They store initial values into the fields. They often receive external parameter values for this.

Passing data via parameters

Assignment Values are stored into fields (and other variables) via assignment statements: variable = expression; price = ticketcost; A variable stores a single value, so any previous value is lost.

Accessor methods Methods implement the behaviour of objects. Accessors provide information about an object. Methods have a structure consisting of a header and a body. The header defines the method s signature. public int getprice() The body encloses the method s statements.

Accessor methods visibility modifier return type method name public int getprice() return price; start and end of method body (block) parameter list (empty) return statement

Test public class CokeMachine int private price; public CokeMachine() price = 300 ; public int getprice () return Price; - What is wrong here? (there are five errors!)

Mutator methods Have a similar method structure: header and body. Used to mutate (i.e., change) an object s state. Achieved through changing the value of one or more fields. Typically contain assignment statements. Typically receive parameters.

Mutator methods visibility modifier return type method name parameter public void insertmoney(int amount) balance = balance + amount; field being mutated assignment statement

Printing from methods public void printticket() // Simulate the printing of a ticket. System.out.println("##################"); System.out.println("# The BlueJ Line"); System.out.println("# Ticket"); System.out.println("# " + price + " cents."); System.out.println("##################"); System.out.println(); // Update the total collected with the balance. total = total + balance; // Clear the balance. balance = 0;

String concatenation 4 + 5 9 "wind" + "ow" "window" "Result: " + 6 "Result: 6" "# " + price + " cents" "# 500 cents" overloading

Quiz System.out.println(5 + 6 + "hello"); 11hello System.out.println("hello" + 5 + 6); hello56

Reflecting on the ticket machines Their behaviour is inadequate in several ways: No checks on the amounts entered. No refunds. No checks for a sensible initialisation. How can we do better? We need more sophisticated behaviour.

Making choices public void insertmoney(int amount) if(amount > 0) balance = balance + amount; else System.out.println("Use a positive amount: " + amount);

How do we write 'refundbalance'?

Local variables Fields are one sort of variable. They store values through the life of an object. They are accessible throughout the class. Methods can include shorter-lived variables. They exist only as long as the method is being executed. They are only accessible from within the method.

Scope and life time The scope of a local variable is the block it is declared in. The lifetime of a local variable is the time of execution of the block it is declared in.

Local variables A local variable No visibility modifier public int refundbalance() int amounttorefund; amounttorefund = balance; balance = 0; return amounttorefund;

Review Class bodies contain fields, constructors and methods. Fields store values that determine an object s state. Constructors initialise objects. Methods implement the behaviour of objects.

Review Fields, parameters and local variables are all variables. Fields persist for the lifetime of an object. Parameters are used to receive values into a constructor or method. Local variables are used for short-lived temporary storage.

Review Objects can make decisions via conditional (if) statements. A true or false test allows one of two alternative courses of actions to be taken.