Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Object Oriented Programming LP4 2006 Jerker Bengtsson Room: E516 (CC-LAB) http://www2.hh.se/staff/jebe/oop2006/index.htm Object Oriented Programming LP4 2006 Course litterature: Object Oriented Software Development in Java Principles, patterns and framworks By Xiaoping Jia Goals with the course To understand and use Object Oriented Programming principles and techniques Learn how to improve software design with aspects on maintainability, reusability, quality. Improve the programming skills by many practical exercises and labs using Java Structure of the course Lectures – 9 in total Programming exercises - 2/week Laboratorial exercises 1. A Document Search Engine 2. A User Interface for the Engine Will not be finished during scheduled time Deadlines for submission must be kept Written exam at the end of the course The Course Homepage Syllabus Lectures and planning (readings, exercises, deadlines, etc.) Lecture slides available online Information about Laboratorial exercises Course news/Noteboard Old exams available Lectures The course is much focused on practical exercises = requires disciplined work Lectures are based on the course book Exercises - two occassions/week Laborations You will work in pairs You must sign up before starting Lecture philosophy The aim with lectures is to Point out the most important aspects Help you read the course book Introduce you to the weekly exercises Prepare you for the laboratorial exercises Programming skills are improved by practice, not just by passive listening The course is worth 5 credit points 1 credit point >≈ 40 hrs of work Course contents Motivation Why OOP? How does OOP relate to other courses Terms, principles and concepts of OOP Inheritance, Abstraction, Polymorphism, Interfaces Design patterns Frameworks Abstract data collections Examination and requirements The written exam determines 2/3 of the final grade Mandatory laboratiorial exercises 1/3 of the grade 5 scheduled laboratorial exercise Each 3 hours long Lab 1 starts thursday week 14 Last laboration handed in...(TBA) Criterias for the grading of laborations can be found later on the homepage Development environment We will use Netbeans IDE 5.0 Open source environment (Sun micro systems) Module based (configurable for each user) It is free to download and install: www.netbeans.org Project handed in as a Netbeans project (....or Eclipse) If other environment is used source must be directly compilable/runnable with javac/java Otherwise submission will not be accepted Todays lecture How does the OOP course relate to the other courses? A first course in programming (Java) Primitive types, variables, program control structures, basic I/O, methods, classes etc. Algorithms & Data structures (Java) recursion, efficient algorithms for searching & sorting etc, abstract data types and structures like trees, lists etc. Low-level programming (Computer org.) Registers, memory management, implementing drivers, practice C (assembler programming?) Software development In the Software engineering course Requirements analysis In this OOP Course Design Implementation and unit testing Integration and system testing Maintenance OOP Terms & Concepts We want to create good software models of real world applications To be able to handle complex systems Decompose the system into smaller ”modules” Modular programs are: Easier to understand and design Implement, test and debug develop by several software engineer teams maintain reuse code OOP Terms & Concepts Classes The structure of a state and representation for objects of similar kind Methods – How to process 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 Objects equality Objects are equal when they have the same state. (The state variables are equal) Objects are identical when they have the same identity (Same memory reference) 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 Accessing methods and data Visibility Public – accessible to any class Protected – accessible to itself, subclasses and classes in same package Package – accessible to itself and all classes within the package Private – Accesible by the object it self Java uses message passing All objects are passed by reference Primitive types by value Accessing Objects The state of an object can be mutable or immutable. Mutable, when values (variables) can be changed Immutable, 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;} Example: Point 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;} Example: A mutable Point Public Class Point{ int x,y; Point(int x1,int y1){ x = x1, y = y1; } State representation State initializer public void move(int x1,int y1){ x = x1; y = y1; } public int getX(){return x;} public int getY(){return y;} } } Mutator Accessors public boolean repOk(){x != null && y != null;} Representation Invariant The representation invariant of a class, is a state condition that always is legal, for all object instances when the object is in stable state The invariant must be established when creating a new object Mutators must preserve the invariant For simple testing, implement a method repOk(), to test if the invariant holds Exercises first week – Defining new data types We want to express and operate on numbers like: 1/3, 2/3, 11/6...etc. We want to be able to formulate arithmetic expressions as: 1/3+2/3 = 3/3 (= 1) 2/3+4/5 = (2*5/3*5) + (4*3/5*3) = 22/15 We can do this by extending Java with a new data type: Rational Extending Java with Rational Public static void main(String[] args){ Rational sum = 0; for(int i;i<100;i++){ sum += 1/i; } System.out.println(sum); } You can’t overload objects with primitive types! Define method toString(); Extending Java 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); } Rational - Contractual Interface 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) Rational – The implementation Public Class Rational{ private num,den; The state of Rational is ”hidden” (private) public Rational(){ this(0,1); } public Rational(int num){ this.(num,1); } } public Rational(int num, int den){ this.num = num; this.den = den; } What is wrong with this??? Rational - Implementation Public Rational(int num,int den){ if(den == 0){ throw new ArithmeticException(”zero denominator is illegal”); }else{ Numerator this.num = den<0 ? –num:num; carries the sign! this.den = Math.abs(den); simplify(); } Auxillary method for } deriving Canonical representation of Rationals Auxillary method - Void simplify(); Simplify() is a private help method to compute the 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); } Simplify mutate the state of Rational. Can’t be static! 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! Exercise 2 – Complex Numbers We also want to use complex valued numbers - rationals are not enough A complex number has two components, a real and an imaginary part (re + im) The real and imaginary part respectively can be expressed by rational numbers (1/3 + j1/3) Complex arithmetic is just an extension of Rational arithmetic The Idea: We extend the Rational Class Class Complex – extends Rational 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); Overload arithmetic in Rational! Helpful when computing division!! Class Complex - Implementation 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; } } complex plus() ...Rational.plus() is overloaded by: public Complex plus(Complex a, Complex b){ Rational real = new Rational(plus(a.real,b.real)); Rational imag = new Rational(plus(a.imag,b.imag))); return new Complex(real,imag); } Calls plus() in Rational!! That is it for today... Lecture notes will be available on the web page First exercise scheduled on tuesday and wednesday this week