1 Object Oriented Programming Lecture II Classes, Objects, Abstract Data Types, Representation Invariants, Mutables and Immutables.

Slides:



Advertisements
Similar presentations
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Advertisements

Interfaces CSC 171 FALL 2004 LECTURE 14. Project 1 review public class Rational { private int numerator, denominator; public Rational(int numerator, int.
Abstract Data Type Fraction Example
Singleton vs utility class  at first glance, the singleton pattern does not seem to offer any advantages to using a utility class  i.e., a utility class.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Written by: Dr. JJ Shepherd
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
The Fundamental Property of Rational Expressions
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
1 CS2200 Software Development Lecture 27: More Testing A. O’Riordan, 2008 K. Brown,
Road Map Introduction to object oriented programming. Classes
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
Some basic I/O.
Subclasses. Inheritance class Animal { int row, column; // will be inherited private Model model; // private prevents inheritance Animal( ) {... } //
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
MIT AITI 2003 Lecture 7 Class and Object - Part I.
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 10 Thinking in Objects.
COSC 1P03 Data Structures and Abstraction 4.1 Abstract Data Types The advantage of a bad memory is that one enjoys several times the same good things for.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CS1101: Programming Methodology Recitation 3 – Control Structures.
Non-static classes Part 2 1. Methods  like constructors, all non-static methods have an implicit parameter named this  for methods, this refers to the.
CS61B L03 Building Objects (1)Garcia / Yelick Fall 2003 © UCB  Dan Garcia ( Kathy Yelick  (
Programming With Java ICS Chapter 8 Polymorphism.
Case Study - Fractions Timothy Budd Oregon State University.
Class Example - Rationals Rational numbers are represented by the ratio of two integers, a numerator and a denominator, e.g., 2/3. This is opposed to irrational.
What does a computer program look like: a general overview.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
10-Nov-15 Java Object Oriented Programming What is it?
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Copyright Curt Hill Variables What are they? Why do we need them?
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Non-static classes 1.  a utility class has features (fields and methods) that are all static  all features belong to the class  therefore, you do not.
CS1020 Data Structures and Algorithms I Lecture Note #9 Abstract Data Type (The Walls)
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
OOP Basics Classes & Methods (c) IDMS/SQL News
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
Last Revision. Question1 Novice Java programmers often write code similar to, class C { public int x;... }... C[] a = new C[10]; for(int i = 0; i < a.length;
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Chapter 10 Thinking in Objects
COMPSCI 107 Computer Science Fundamentals
Chapter 10 Thinking in Objects
Chapter 10 Thinking in Objects
Lecture 2: Data Types, Variables, Operators, and Expressions
Chapter 10 Thinking in Objects
Review Session.
CS360 Windows Programming
Computer Science II Exam 1 Review.
CS139 – Fall 2010 How far we have come
Chapter 9 Thinking in Objects
null, true, and false are also reserved.
Building Java Programs
Arrays of Objects Fall 2012 CS2302: Programming Principles.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Implementing Non-Static Features
Java Classes and Objects 3rd Lecture
Building Java Programs
Chapter 9 Thinking in Objects
Building Java Programs
Chapter 10 Thinking in Objects
CMPE212 – Reminders Assignment 2 due next Friday.
Presentation transcript:

1 Object Oriented Programming Lecture II Classes, Objects, Abstract Data Types, Representation Invariants, Mutables and Immutables

2 Terminology of Classes and Objects The Class construction –The structure for representation of objects of similar kind Methods – how to compute on data (Algorithms) Constructor – init the state of an object Variables – the state of an object (Data) Objects –An object has a unique identity, a state and a representation We instantiate objects out of classes and use them together in a program

3 Definition of Object Equality Objects are equal when they have the same state (usually comparing variables) Objects are identical when they share class identity For objects, the expression r1 == r2 tests identity, NOT equality –identity comparison made by ’ == ’ operator in Java Equality is tested by the equals() method –object equality is defined by the programmer

4 References in Java Java uses message passing –all objects are passed by reference –but primitive types are passed by value Advice –be careful when objects and especially data collections are shared (such ”problems” will appear in the lab project)

5 Wrapper Classes What is a wrapper and why is it needed? –Primitives are not objects, therefore object oriented techiques cannot be applied on primitive types –Wrapper classes are ”type” containers for primitives Examples are: Integer, Float, Byte... etc. –These are all of immutable type, and arithmetic operators can not directly be applied –To calculate, we need to unwrap... and then wrap again. If a, b, c are Integer type, then c = new Integer(a.intValue() + b.intValue());

6 Accessing Object Internals The state of an object can be mutable or immutable. –mutable, when internal values (variables) can be changed –immutable, when internal values are not changed Accessors –methods that doesn’t change any values, only return values public int getVal() {return value;} Mutators –Methods that can change the value of the state variables public void setVal(int scalar){value = scalar;}

7 Example: The Point Class Public Class Point{ int x,y; Point(int x1,int y1){ x = x1, y = y1; } public void move(int x1,int y1){ x = x1; y = y1; } public int getX(){return x;} public int getY(){return y;} }

8 Example Cnt’d Public Class Point{ int x,y; Point(int x1,int y1){ x = x1, y = y1; } public void move(int x1,int y1){ x = x1; y = y1; } public int getX(){return x;} public int getY(){return y;} public boolean repOk(){x != null && y != null;} } State representation State initializer Mutator Accessors

9 Representation Invariant The representation invariant of a class is –a state condition that always is guaranteed to be legal, for all object instances, whenever the object is in its stable state Conditions for the representation invariant –1. The invariant must be established when creating a new object –2. Mutators must always preserve the invariant For simple testing of an invariant –implement a method boolean repOk()

10 Abstract Data Types Consider the following problem: –we need to express and compute on rational numbers like: 1/3, 2/3, 11/6...etc. –we want to be able to formulate arithmetic expressions, such as 1/3+2/3 = 3/3 (= 1) 2/3+4/5 = (2*5/3*5) + (4*3/5*3) = 22/15 Question: How can we do this?

11 The Abstract Type Rational Constructors: Rational(), Rational(int), Rational(int,int); Arithmetic operations: plus(Rational a, Rational b) minus(Rational a, Rational b) div(Rational a, Rational b) times(Rational a, Rational b) Other methods: toString(),equals(Object x),compareTo(Object x)

12 Computing with Rational public static void main(String[] args){ Rational sum = 0; for(int i;i<100;i++){ sum += 1/i; } System.out.println(sum); } We need to define toString(); We can’t overload objects with primitive types!

13 A Second Try with Rational Public static void main(String[] args){ Rational sum = new Rational(); for(int i = 1;i<100;i++){ sum = Rational.sum(sum,new Rational(1,i)); } System.out.println(sum); }

14 Representation Invariant for Rational Before implementation, we need to make a few decisions –1. What are legal values for a Rational? all integer fractions a/b, where b ≠ 0 –2. We also decide to allow Rationals to have either positive or negative How do we handle sign representation? –3. Let the numerator carry the (-) sign

15 Lets do some implementation... Public Class Rational{ private num,den; public Rational(){ this(0,1); } public Rational(int num){ this.(num,1); } public Rational(int num, int den){ this.num = num; this.den = den; } The state of Rational is ”hidden” (private) Hey, wait a minute...

16 Taking Care of the Invariant Public Rational(int num,int den){ if(den == 0){ throw new ArithmeticException(”zero denominator is illegal”); }else{ this.num = den<0 ? –num:num; this.den = Math.abs(den); simplify(); } Numerator carries the sign! Auxillary method for deriving canonical representation of Rationals

17 What about this Simplify thing? Simplify() is a private help method to compute the Greates Common Divisor (GCD) of a rational number. Private void simplify(){ int d = gcd(num,den); num = num/d; den = den/d; } Private static gcd(int a, int b){ if (a==0) return b; else return gcd(b%a,a); }

18 Static Methods We want to use Rational Arithmetic anywhere in our programs. A solution is to make methods static. public static Rational plus(Rational a, Rational b){ return new Rational(...); } Rational Arithmetics can now be used anywhere without instantiating Rational.

19 Extending Our Number Represenation What about complex numbers? A complex number has two components – a real and an imaginary part Interesting observation, each component can be expressed by a rational...

20 Designing Complex Class What operators do we need to support? –{+, -, *, /} What is the representation invariant? –real and imaginary component ≠ null When are two complex numbers equal? –We define this to be when magnitude and direction of a = magnitude and direction of b

21 Complex Interface Constructors: Complex(); Complex(Rational re, Rational im); Arithmetic operations: plus(Complex a, Complex b); minus(Complex a, Complex b); times(Complex a, Complex b); div(Complex a, Complex b); Auxillary: conjugate(Complex c); Helpful when computing division!! Overload the arithmetic operators in Rational

22 The Complex Constructor class Complex{ private Rational real; private Rational imag; Complex(){ this(new Rational(), new Rational()); } Complex(Rational real, Rational imag){ this.real = real; this.imag = imag; } ?

23 Complex Arithmetic

24 Exercise 1 & 2 1. Implement the Class Rational 2. Implement the Class Complex