Inf1-OP. Creating Classes. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. February 26, School of Informatics

Similar documents
Class Foo. instance variables. instance methods. Class FooTester. main {

Inf1-OOP. Data Types. Defining Data Types in Java. type value set operations. Overview. Circle Class. Creating Data Types 1.

Inf1-OOP. Encapsulation and Collections. Perdita Stevens, adapting earlier version by Ewan Klein. March 2, School of Informatics

Inf1-OOP. Encapsulation. Object Oriented Programming. Encapsulation Accessing Data Immutability. Encapsulation and Collections.

Encapsulation. Inf1-OOP. Getters and Setters. Encapsulation Again. Inheritance Encapsulation and Inheritance. The Object Superclass

Inf1-OOP. Inheritance and Interfaces. Ewan Klein, Perdita Stevens. January 12, School of Informatics

Inf1-OP. Classes with Stuff in Common. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein.

Inf1-OP. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. March 12, School of Informatics

ECOM 2324 COMPUTER PROGRAMMING II

Inf1-OOP. OOP Exam Review. Perdita Stevens, adapting earlier version by Ewan Klein. March 16, School of Informatics

Inf1-OP. Inf1-OP Exam Review. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 20, School of Informatics

Chapter 4 Defining Classes I

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

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

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

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

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

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

In Java there are three types of data values:

Tutorials. Inf1-OP. Learning Outcomes for this week. A Foundation for Programming. Conditionals and Loops 1

Object Oriented Programming in C#

Inf1-OP. Inf1-OP Exam Review. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. March 16, School of Informatics

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

Simple Java Reference

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

Chapter 4: Writing Classes

Object Oriented Programming

Programming II (CS300)

Primitive vs Reference

CS 231 Data Structures and Algorithms, Fall 2016

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Object Oriented Programming

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

Java Object Oriented Design. CSC207 Fall 2014

Rigidity of arrays. Inf1-OP. ArrayList. ArrayList: Methods. Declaration. Collections

Principles of Object Oriented Programming. Lecture 4

Chapter 9. Objects and Classes

Software engineering as managing change. Inf1-OP. Data representation. How can we make change easier and cheaper?

Inf1-OP. Classes and Objects. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. November 23, School of Informatics

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Comments are almost like C++

Inheritance and Polymorphism

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

Programming in the Large II: Objects and Classes (Part 1)

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

Making New instances of Classes

CS1004: Intro to CS in Java, Spring 2005

Inf1-OP. Course Overview. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. February 26, School of Informatics

PIC 20A The Basics of Java

CSE 341, Autumn 2015, Ruby Introduction Summary

UFCE3T-15-M Object-oriented Design and Programming

CS 1302 Chapter 9 (Review) Object & Classes

Introduction to Programming

Anatomy of a Class Encapsulation Anatomy of a Method

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

Introduction to Programming Using Java (98-388)

SSE3052: Embedded Systems Practice

Full file at

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

Math Modeling in Java: An S-I Compartment Model

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

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Chapter 1 Getting Started

COMP-202 Unit 8: Defining Your Own Classes. CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation

CS112 Lecture: Working with Numbers

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

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

Lecture Notes Chapter #9_b Inheritance & Polymorphism

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

Oracle 1Z Java SE 8 Programmer I. Download Full Version :

Chapter 6 Introduction to Defining Classes

Inheritance and Polymorphism

Week 3 Classes and Objects

Designing data types. Fundamentals of Computer Science I

Programming II (CS300)

Classes Classes 2 / 36

ITI Introduction to Computing II

The high-level language has a series of primitive (builtin) types that we use to signify what s in the memory

JAVA: A Primer. By: Amrita Rajagopal

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות

Arrays. Inf1-OP. Arrays. Many Variables of the Same Type. Arrays 1. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein

Programming II (CS300)

Classes Classes 2 / 35

Inf1-OP. Arrays 1. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. February 26, School of Informatics

Classes and Methods לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Recommended Group Brainstorm (NO computers during this time)

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

Primitive Data, Variables, and Expressions; Simple Conditional Execution

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

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

CSCE 156 Computer Science II

Designing data types. Overview. Object Oriented Programming. Alan Kay. Object Oriented Programming (OOP) Data encapsulation. Checking for equality

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

Object-Oriented Programming Concepts

Initializers: Array initializers can be used with class base types as well. The elements of the initializer can be expressions (not just constants).

Transcription:

Inf1-OP Creating Classes Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein School of Informatics February 26, 2018

Creating classes Last time we saw how to use a class: create a new object, using new; send the object messages from its interface, to invoke its behaviour; we understood that the object might change its state; and that state and behaviour interdepend; but we did not expect to have access to the state, and we did not know or need to care exactly how the behaviour was implemented. This time we will see how to define a class, including its state and behaviour, and how new objects should be created.

Classes and Clients Class Foo instance variables constructor instance methods

Classes and Clients Class Foo instance variables constructor instance methods main { Class FooTester Foo f = new Foo(...); baz = f.dosomething( ); client of Foo

Classes and Clients Client code: In general, a client program calls a method of some class C. Example: class FooTester is a client of Foo because it calls the dosomething() instance method on Foo objects.

Classes and Clients Client code: In general, a client program calls a method of some class C. Example: class FooTester is a client of Foo because it calls the dosomething() instance method on Foo objects. Test-first design methodology: 1. Think about the methods a client would call on instances of class C. 2. Design the API for class C. 3. Implement a client CTester for C which tests the desired behaviour. 4. Implement C so that it satisfies CTester.

CircleTester Create a Circle object c1. Call a method to get the area of that object: c1.getarea() public class CircleTester { public static void main(string[] args) { Circle c1 = new Circle(); double area1 = c1.getarea(); System.out.printf("Area of circle c1 is %5.2f\n", area1); Circle c2 = new Circle(5.0); double area2 = c2.getarea(); System.out.printf("Area of circle c2 is %5.2f\n", area2); Expected Output % java CircleTester Area of circle c1 is 3.14 Area of circle c2 is 78.54

The Circle Class public class Circle { instance variables constructor instance methods

The Circle Class: Instance Methods public class Circle { instance variables constructor public double getarea(){ return radius * radius * Math.PI;

The Circle Class: Instance Methods public class Circle { instance variables constructor public double getarea(){ return radius * radius * Math.PI; getarea() is an instance method of the class Circle. How does it know about radius?

The Circle Class: Instance Variables public class Circle { private double radius; constructor public double getarea(){ return radius * radius * Math.PI; radius is an instance variable of the class Circle.

The Circle Class: Instance Variables public class Circle { private double radius; constructor public double getarea(){ return radius * radius * Math.PI; radius is an instance variable of the class Circle. Instance variables are declared outside methods and have scope over the whole class.

The Circle Class: Instance Variables public class Circle { private double radius; constructor public double getarea(){ return radius * radius * Math.PI; radius is an instance variable of the class Circle. Instance variables are declared outside methods and have scope over the whole class. An instance method of a class can use any instance variable of that class.

The Circle Class: Instance Variables public class Circle { private double radius; constructor public double getarea(){ return radius * radius * Math.PI; radius is an instance variable of the class Circle. Instance variables are declared outside methods and have scope over the whole class. An instance method of a class can use any instance variable of that class. Instance variables do not have to be initialized; they get default values (e.g., 0 for int, false for boolean, null for all reference types).

The Circle Class: Instance Variables public class Circle { private double radius; constructor public double getarea(){ return radius * radius * Math.PI; radius is an instance variable of the class Circle. Instance variables are declared outside methods and have scope over the whole class. An instance method of a class can use any instance variable of that class. Instance variables do not have to be initialized; they get default values (e.g., 0 for int, false for boolean, null for all reference types). How does a Circle object s radius get set?

The Circle Class: Constructors public class Circle { private double radius; public Circle(double newradius){ radius = newradius; public double getarea(){ return radius * radius * Math.PI; Constructor has same name as the class;

The Circle Class: Constructors public class Circle { private double radius; public Circle(double newradius){ radius = newradius; public double getarea(){ return radius * radius * Math.PI; Constructor has same name as the class; used to initialize an object that has been created: new Circle(5.0);

The Circle Class: Constructors public class Circle { private double radius; public Circle(double newradius){ radius = newradius; public double getarea(){ return radius * radius * Math.PI; Constructor has same name as the class; used to initialize an object that has been created: new Circle(5.0); must not have a return type (not even void).

The Circle Class: Anatomy public class Circle { constructor name instance variable private double radius; public Circle(double newradius){ radius = newradius; instance variable declaratio constructor instance method public double getarea(){ return radius * radius * Math.PI; instance variable

The Circle Class: Constructors Alternative notation: public class Circle { private double radius; instance variable public Circle(double radius){ this.radius = radius; parameter public double getarea(){ return radius * radius * Math.PI;

The Circle Class: Client public class Circle { private double radius; public Circle(double radius){ this.radius = radius; public double getarea(){ return radius * radius * Math.PI; Class CircleTester public static void main(string[] args) { Circle c1 = new Circle(1.0); double area1 = c1.getarea(); System.out.printf("Area of circle c1 is %5.2f\n", area1); Circle c2 = new Circle(5.0); double area2 = c2.getarea(); System.out.printf("Area of circle c2 is %5.2f\n", area2); client of Circle

Interim Summary We looked at: using client programs to motivate our classes, and to test them instance variables: represent data that is particular to an object (i.e., an instance!); have scope over the whole class; can hold mutable state; can be manipulated by any instance method in the class. instance methods: like static methods, but can only be called on some object o; have access to the data that is specific to o. constructors: we create a new object of class Foo with the keyword new; we initialize an object of type Foo by calling the constructor for that type; the constructor is used to store data values in the object s instance variables.

Instance variable defaults: Person class public class Person { String name; public void assignname(string n){... public static void main(string[] args) { Person p = new Person(); p.assignname("lee"); System.out.println(p.name);

Two versions of Person Version 1: public class Person { String name; public void assignname(string n) { if (name.length() == 0) name = n; Version 2: public class Person { String name = ""; public void assignname(string n) { if (name.length() == 0) name = n;

Another two versions of Person Version 3: public class Person { String name; public void assignname(string n) { if (name.equals(null)) name = n; Version 4: public class Person { String name; public void assignname(string n) { if (name == null) name = n;

Comparing versions of Person Which of the versions will execute properly? Version 1 Version 2 Version 3 Version 4

Brief interlude: Format Strings How to gain more fine-grained control over print strings.

println can be Clunky The student named Lee is aged 18. Using string concatenation System.out.println("The student named " + name + " is aged " + age + ".");

String with Format Specifiers, 1 Target String "The student named Lee is aged 18."

String with Format Specifiers, 1 Target String "The student named Lee is aged 18." String with Gaps "The student named _ is aged _."

String with Format Specifiers, 1 Target String "The student named Lee is aged 18." String with Gaps "The student named _ is aged _." String with Format Specifiers "The student named %s is aged %s."

String with Format Specifiers, 1 Target String "The student named Lee is aged 18." String with Gaps "The student named _ is aged _." String with Format Specifiers "The student named %s is aged %s." %s is a placeholder for a string. Called a format specifier. Each format specifier in a string gets replaced by an actual value.

String with Format Specifiers, 2 arg1 String.format("The student named '%s' is aged %s.", name, age); arg2

String with Format Specifiers, 3 Define a Format String String str = String.format("The student named %s is aged %s.", name, age); System.out.println(str); Output The student named Lee is aged 18.

printf, 1 Shorter version System.out. printf ("The student named %s is aged %s.", name, age); Output The student named Lee is aged 18.

printf, 2 Convert char to String System.out.printf(" %s is for Apple.", A ); Output A is for Apple.

printf, 2 Round to 2 decimal places System.out.printf("The value of pi is %f", Math.PI); System.out.printf("The value of pi is %.2f", Math.PI); Output The value of pi is 3.141593 The value of pi is 3.14

printf, 2 Round to 2 decimal places System.out.printf("The value of pi is %f", Math.PI); System.out.printf("The value of pi is %.2f", Math.PI); Output The value of pi is 3.141593 The value of pi is 3.14 Include a newline System.out.printf("The value of pi is %f\n", Math.PI);

Hotel Reservation System Goal: create a data type to manage hotel bookings Each hotel room has a number and a room rate. Each hotel room is associated with a representation of the days of a single month, indicating which days the room has already been booked for.

Hotel Reservation System: Client public class HotelRoomReserver { public static void main(string[] args) { int startdate = Integer.parseInt(args[0]); int duration = Integer.parseInt(args[1]); HotelRoom rm1 = new HotelRoom(1, 65); HotelRoom rm2 = new HotelRoom(2, 65); HotelRoom rm3 = new HotelRoom(3, 75); HotelRoom[] rooms = { rm1, rm2, rm3 ; for (int i = 0; i < rooms.length; i++) { HotelRoom r = rooms[i]; if (r.isavailable(startdate, duration)) { r.printbookings();

Hotel Reservation System: Client public class HotelRoomReserver { public static void main(string[] args) { int startdate = Integer.parseInt(args[0]); int duration = Integer.parseInt(args[1]); create and initialize objects HotelRoom rm1 = new HotelRoom(1, 65); HotelRoom rm2 = new HotelRoom(2, 65); HotelRoom rm3 = new HotelRoom(3, 75); HotelRoom[] rooms = { rm1, rm2, rm3 ; invoke constructor for (int i = 0; i < rooms.length; i++) { HotelRoom r = rooms[i]; if (r.isavailable(startdate, duration)) { object name r.printbookings(); invoke method on r

Hotel Room Data Type Goal: create a data type to manage hotel bookings Set of values: type value remarks int room number int room rate expressed in boolean[] booked dates true at index i iff room is booked for day i

Hotel Room Data Type Goal: create a data type to manage hotel bookings API: public class HotelRoom HotelRoom(int num, int rate) boolean isavailable(int sd, int d) available from day sd until day sd + d? void printbookings() show bookings for whole month String tostring() string representation Assumptions: Simplify by only considering a single month; skip index 0 in the bookings so that indexes and days of month line up; if someone is booked from day i to day j, they depart from hotel on the morning of j, so room only has to be free on days i (j-1).

Arrays of Objects Array of HotelRoom objects HotelRoom rm1 = new HotelRoom(1, 65); HotelRoom rm2 = new HotelRoom(2, 65); HotelRoom rm3 = new HotelRoom(3, 75); HotelRoom[] rooms = { rm1, rm2, rm3 ; Array of HotelRoom objects: alternative HotelRoom[] rooms = new HotelRoom[3]; rooms[0] = new HotelRoom(1, 65); rooms[1] = new HotelRoom(2, 65); rooms[2] = new HotelRoom(3, 75); Allocate memory for the array with new. Allocate memory for each object with new.

HotelRoom Class, version 1 public class HotelRoom { private final int roomnumber; private int roomrate; public HotelRoom(int num, int rate){ roomnumber = num; roomrate = rate; public boolean isavailable(int startdate, int duration){ return true;

HotelRoom Class, version 1 public class HotelRoom { private final int roomnumber; private int roomrate; public HotelRoom(int num, int rate){ roomnumber = num; roomrate = rate; instance variables constructor public boolean isavailable(int startdate, int duration){ return true; instance method

More on Instance Variables Always use access modifier private (more on this later) Use modifier final for instance variables that never change after initial assignment. modifiers public class HotelRoom { private final int roomnumber; private int roomrate;...

Hotel Reservation System Version 1 % java HotelReserver 12 3 Rooms available from 12 to 15 ============================== HotelRoom@5f893efe HotelRoom@2b86c6b2 HotelRoom@1d5ee671

Hotel Reservation System Version 1 % java HotelReserver 12 3 Rooms available from 12 to 15 ============================== HotelRoom@5f893efe HotelRoom@2b86c6b2 HotelRoom@1d5ee671 How do we get a more informative output string when we call System.out.println() on a HotelRoom object?

HotelRoom Class, version 2 public class HotelRoom { private final int roomnumber; private int roomrate; public HotelRoom(int num, int rate){ roomnumber = num; roomrate = rate; public boolean isavailable(int startdate, int duration){ return true; public String tostring(){ return String.format("Room Number:\t%s\nRoom Rate:\t %s.00\n", roomnumber, roomrate);

Hotel Reservation System Version 2 % java HotelReserver 12 3 Rooms available from 12 to 15 ============================== Room Number: 1 Room Rate: 65.00 Room Number: 2 Room Rate: 65.00 Room Number: 3 Room Rate: 75.00

HotelRoom Class, version 3 public class HotelRoom { private final int roomnumber; private int roomrate; private boolean[] booked; public HotelRoom(int num, int rate){ roomnumber = num; roomrate = rate; booked = HotelUtils.occupy(); call an external utility method which randomly flips false to true. public boolean isavailable(int startdate, int duration){ boolean available = true; for (int i = startdate; i < startdate + duration; i++) { available = available &&!booked[i]; return available; public String tostring(){ return String.format("\nRoom Number:\t%s\nRoom Rate:\t %s.00", roomnumber, roomrate);

HotelRoom Class, version 4 public class HotelRoom { private final int roomnumber; private int roomrate; private boolean[] booked; public HotelRoom(int num, int rate){ roomnumber = num; roomrate = rate; booked = HotelUtils.occupy(); public boolean isavailable(int startdate, int duration){ boolean available = true; for (int i = startdate; i < startdate + duration; i++) { available = available &&!booked[i]; return available; another external utility method public void printbookings(){ HotelUtils.displayBookings(booked); public String tostring(){ return String.format("\nRoom Number:\t%s\nRoom Rate:\t %s.00", roomnumber, roomrate);

Version 4 Version 4 % Rooms available from 12 to 15 ============================== Room Number: 2 Room Rate: 65.00 1: [ ][X][ ][X][X][X][ ] 8: [ ][ ][X][ ][ ][ ][ ] 15: [X][ ][ ][X][ ][ ][ ] 22: [X][X][X][ ][ ][ ][X] 29: [X][X] Recall that guests will leave on morning of 15 th, so room doesn t have to be free on day 15.

Interim Summary Some new features: We implemented a tostring() method for HotelRoom: Java always implicitly calls this method whenever it executes commands like System.out.println(). Every class gets a default version of tostring(), but it s often useful to give our own classes a more specific implementation which gets used instead of the default. We created and used an array of type HotelRoom[]; i.e. HotelRoom[] rooms = { rm1, rm2, rm3 ;

More on Constructors Circle1: Omitting the constructor public class Circle1 { private double radius; public double getarea(){ return radius * radius * Math.PI;

More on Constructors Circle1: Omitting the constructor public class Circle1 { private double radius; public double getarea(){ return radius * radius * Math.PI; Circle1 c = new Circle1(1.0) causes compile-time error. Circle1 c = new Circle1() does work though c.getarea() returns 0.00! If you don t explicitly add a constructor, Java will automatically add a no-argument constructor for you.

More on Constructors Circle again public class Circle { private double radius; public Circle(double newradius){ radius = newradius; public double getarea(){ return radius * radius * Math.PI; What happens if we call Circle c = new Circle()? This also causes a compile-time error we only get the no-arg default constructor if there s no explicit constructor already defined.

More on Constructors Generally considered good programming style to provide a no-arg constructor for your classes. No-arg Constructor: Version 1 public class Circle3 { private double radius; public Circle3(double newradius){ radius = newradius; public Circle3(){ radius = 1.0; public double getarea(){ return radius * radius * Math.PI;

More on Constructors No-arg Constructor: Version 2 public class Circle4 { private double radius; public Circle4(double newradius){ radius = newradius; public Circle4(){ this(1.0); public double getarea(){ return radius * radius * Math.PI; this(1.0); call another constructor of this class, and supply the value 1.0. Must be the first line of the constructor.

Encapsulation...or, why do instance variables have to be private?

Dalek Encapsulation: Unprotected Dalek public class Dalek { public double batterycharge = 5; public void batteryrecharge(double c) {... public void move(int distance) {... Disabling the Dalek: Dalek d = new Dalek(); // start off with a // well-charged battery d.batterycharge = Double.NEGATIVE_INFINITY; d.batteryrecharge(1000); // battery charge still -Infinity!

Dalek Encapsulation: Protected Dalek! public class Dalek { private double batterycharge = 5; public void batteryrecharge(double c) {... public void move(int distance) {... Disabling the Dalek: Dalek d = new Dalek(); // start off with a // well-charged battery d.batterycharge = Double.NEGATIVE_INFINITY; //Triggers compile-time Error Exception...: Unresolved compilation problem: The field Dalek.batteryCharge is not visible

Changing Internal Representation Encapsulation: Keep data representation hidden with private access modifier. Expose API to clients using public access modifier. Advantage: can switch internal representations without changing client. Encapsulated data types: Don t touch data to do whatever you want. Instead, ask object to manipulate its data.

Immutability Immutable data type: object s internal state cannot change once constructed. mutable Picture Dalek Java arrays immutable String primitive types

Immutability: Advantages and Disadvantages Immutable data type: object s value cannot change once constructed. Advantages: Makes programs easier to debug (sometimes) Limits scope of code that can change values Pass objects around without worrying about modification Better for concurrent programming. Disadvantages: New object must be created for every value.

The final Modifier Final: declaring a variable to by final means that you can assign it a value only once, in initializer or constructor. E.g., Daleks come in three versions, Mark I, Mark II and Mark III. public class Dalek { private final int mark; private double batterycharge;... this value can be change by invoking the instance method batteryrecharge() this value doesn't change once the object is constructed Advantages: Helps enforce immutability. Prevents accidental changes. Makes program easier to debug. Documents the fact that value cannot change.

Getters and Setters Encapsulation: instance variables should be private public class Student { private String firstname; private String lastname; private String matric; public Student(String fn, String ln, String m) { firstname = fn; lastname = ln; matric = m;

Getters and Setters Encapsulation: instance variables should be private public class StudentTester { public static void main(string[] args) { Student student = new Student("Fiona", "McCleod", "s01234567"); System.out.println(student.firstName); student.matric = "s13141516"; we cannot assign to this variable! we cannot read this variable!

Getters and Setters Encapsulation: instance variables should be private We use instance methods to mediate access to the data in private instance variables, as needed. Accessor methods: just read the data Mutator methods: modify the data Java convention: given an instance variable mydata, use getmydata() method to read the data, and setmydata() method to write to the data. Often called getters and setters respectively.

Getters and Setters public class Student { private String firstname, lastname, matric, tutgroup; public Student(String fn, String ln, String m) {... public String getfirstname() { return firstname; public String getlastname() { return lastname; public String getmatric() { return matric;

Getters and Setters Eclipse will generate setters and getters for you!

Summary: Object Orientation Data type: set of values and collections of operations on those values. In OOP: classes. Simulating the physical world Java objects can be used to model real-world objects Not necessarily easy to choose good modelling primitives, or to get model that reflects relevant parts of reality. Examples: geometric figures, hotel rooms,... Extending the Java language Java doesn t have a data type for every possible application. User-defined classes enable us to add our own abstractions.

Summary: designing a Java class Use client code to motivate and test classes. instance variables: represent data that is particular to an object (i.e., an instance!); have scope over the whole class; can hold mutable state; can be manipulated by any instance method in the class. instance methods: like static methods, but can only be called on some object o; have access to the data that is specific to o. constructors: we create a new object of class Foo with the keyword new; we initialize an object of type Foo by calling the constructor for that type; the constructor can be used to store data values in the object s instance variables.

Summary: Access Control Encapsulation and visibility: All the instance variables and methods (i.e., members) of a class are visible within the body of the class. Access modifiers: control the visibility of your code to other programs. public: member is accessible whenever the class is accessible. private: member is only accessible within the class. default: amounts to public for current purposes. Benefits of encapsulation: Loose coupling Protected variation Exporting an API: the classes, members etc, by which some program is accessed any client program can use the API the author is committed to supporting the API

Reading Java Tutorial Reread anything up to pp121 that you re not happy with yet. We haven t talked about inheritance, interfaces or packages (yet), but everything else should be looking familiar.