programming ii - part 1 – object oriented programming with java sebastian uchitel su2

17
Programming II Programming II - Part 1 – - Part 1 – Object Oriented Object Oriented Programming with Java Programming with Java Sebastian Uchitel Sebastian Uchitel www.doc.ic.ac.uk/~su2 www.doc.ic.ac.uk/~su2

Upload: imogen-palmer

Post on 17-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Programming II - Part 1 – Object Oriented Programming with Java Sebastian Uchitel su2

Programming IIProgramming II- Part 1 –- Part 1 –

Object Oriented Object Oriented Programming with JavaProgramming with Java

Sebastian Uchitel Sebastian Uchitel

www.doc.ic.ac.uk/~su2www.doc.ic.ac.uk/~su2

Page 2: Programming II - Part 1 – Object Oriented Programming with Java Sebastian Uchitel su2

© S. Uchitel, 2004 2

Course StructureCourse Structure

LecturesLectures Room 308.Room 308. Monday 14:00 and Friday 10:00. Monday 14:00 and Friday 10:00.

TutorialsTutorials Room 344 (and 308 if we need more space)Room 344 (and 308 if we need more space) Monday 15:00Monday 15:00

LabLab 4 labs dedicated to topics covered in Programming II.4 labs dedicated to topics covered in Programming II. Week 2 (unassessed), and weeks 3, 5, and 6.Week 2 (unassessed), and weeks 3, 5, and 6.

AssessmentAssessment 2 assessed assignments (one for each part of the course)2 assessed assignments (one for each part of the course) 2h exam, 3 questions out of 42h exam, 3 questions out of 4

Page 3: Programming II - Part 1 – Object Oriented Programming with Java Sebastian Uchitel su2

© S. Uchitel, 2004 3

Part I - AimsPart I - Aims

1.1. Learn Learn concepts and fundamentals of the Object concepts and fundamentals of the Object Oriented paradigmOriented paradigm..

2.2. Apply in a real programming language (Java)Apply in a real programming language (Java) these concepts and fundamentalsthese concepts and fundamentals

3.3. Learn Learn JavaJava

4.4. Practice problem solvingPractice problem solving using Java and OO using Java and OO principles.principles.

Page 4: Programming II - Part 1 – Object Oriented Programming with Java Sebastian Uchitel su2

© S. Uchitel, 2004 4

Part I - TextbooksPart I - Textbooks

Java Software Solutions – Foundations of Program Design Java Software Solutions – Foundations of Program Design (3(3rdrd ed.), Lewis and Loftus, Addison Wesley. ed.), Lewis and Loftus, Addison Wesley.

Thinking in Java (3Thinking in Java (3rdrd ed.), Bruce Eckel, Prentice Hall. ed.), Bruce Eckel, Prentice Hall. Freely available at Freely available at http://www.mindview.net/Books/TIJ/http://www.mindview.net/Books/TIJ/

Java Documentation:Java Documentation: http://java.sun.com/docs/books/tutorial/http://java.sun.com/docs/books/tutorial/ http://java.sun.com/j2se/1.4.2/docs/api/http://java.sun.com/j2se/1.4.2/docs/api/

Page 5: Programming II - Part 1 – Object Oriented Programming with Java Sebastian Uchitel su2

© S. Uchitel, 2004 5

Part I - OutlinePart I - Outline

IntroductionIntroduction MotivationMotivation From Kenya to JavaFrom Kenya to Java

Objects and Classes: A User’s PerspectiveObjects and Classes: A User’s Perspective What are they?What are they? How do I create objects from classes, and how do I use them?How do I create objects from classes, and how do I use them? Where do I get classes from in the first place?Where do I get classes from in the first place?

Objects and Classes: A Developers PerspectiveObjects and Classes: A Developers Perspective How do I create my own classes?How do I create my own classes? How do I design “good” classes?How do I design “good” classes?

Inheritance and PolymorphismInheritance and Polymorphism Interfaces and Abstract ClassesInterfaces and Abstract Classes CollectionsCollections ExceptionsExceptions

Page 6: Programming II - Part 1 – Object Oriented Programming with Java Sebastian Uchitel su2

© S. Uchitel, 2004 6

Why Object Oriented Programming?Why Object Oriented Programming?

Programming is about two thingsProgramming is about two things Problem solvingProblem solving Managing complexityManaging complexity

OO Programming provides OO Programming provides conceptual framework conceptual framework and programming constructsand programming constructs for managing for managing complexitycomplexity

In principle with imperative and functional In principle with imperative and functional languages you can implement anything but...languages you can implement anything but... Functional languages: input and output? acceptance in Functional languages: input and output? acceptance in

industry?industry? Imperative languages do not manage complexity well. Imperative languages do not manage complexity well.

More difficult to understand/maintain/extend.More difficult to understand/maintain/extend.

Page 7: Programming II - Part 1 – Object Oriented Programming with Java Sebastian Uchitel su2

© S. Uchitel, 2004 7

Why learn Java?Why learn Java?

Why Why JavaJava? ? Java is an OO programming language. Java is an OO programming language. Widespread in Industry for development of traditional and Widespread in Industry for development of traditional and

web-based applicationsweb-based applications Why not Why not KenyaKenya??

Kenya is not an OO programming language.Kenya is not an OO programming language. Developed at Imperial for educational purposes.Developed at Imperial for educational purposes. Outside Imperial College VERY few people know it.Outside Imperial College VERY few people know it. Not used in IndustryNot used in Industry

Why not other OO programming languages?Why not other OO programming languages? C++C++? Complex (and confusing) mix between OO and ? Complex (and confusing) mix between OO and

imperative programming, memory management, and imperative programming, memory management, and more...more...

SmalltalkSmalltalk? Pure? But, low acceptance in Industry, and ? Pure? But, low acceptance in Industry, and more...more...

Page 8: Programming II - Part 1 – Object Oriented Programming with Java Sebastian Uchitel su2

© S. Uchitel, 2004 8

From Kenya...From Kenya...

void main() {void main() {

// Requests two numbers and prints the larger// Requests two numbers and prints the larger

println("Type in your 2 numbers -> ");println("Type in your 2 numbers -> ");

println("The winner is " + println("The winner is " +

bigger(readInt(), readInt()));bigger(readInt(), readInt()));

}}

int bigger(int a, int b){int bigger(int a, int b){

//post: returns the larger of the 2 values//post: returns the larger of the 2 valuesif (a > b) {return a;}if (a > b) {return a;}

else {return b;}else {return b;}

}}

To tool...

Page 9: Programming II - Part 1 – Object Oriented Programming with Java Sebastian Uchitel su2

© S. Uchitel, 2004 9

... to Java... to Java

import kenya.io.InputReader;import kenya.io.InputReader;

public classpublic class Bigger {Bigger {public static voidpublic static void main( main(String [] argsString [] args) {) { System.out.System.out.println("Type in your 2 numbers -> ");println("Type in your 2 numbers -> "); System.out.System.out.println("The winner is "println("The winner is " + +

bigger(bigger(InputReader.InputReader.readInt(), readInt(), InputReader.InputReader.readInt()));readInt()));}}

static static int bigger(int a, int b){int bigger(int a, int b){//post: returns the larger of the 2 values//post: returns the larger of the 2 valuesif (a > b) {return a;}if (a > b) {return a;}else {return b;}else {return b;}}}

}}

Page 10: Programming II - Part 1 – Object Oriented Programming with Java Sebastian Uchitel su2

© S. Uchitel, 2004 10

From Kenya to JavaFrom Kenya to Java

By adding the bits and pieces of the previous By adding the bits and pieces of the previous slide, you can now write Java programs for all the slide, you can now write Java programs for all the Kenya programs you did in the previous term!Kenya programs you did in the previous term!

However, in the next 10 lectures you will learn However, in the next 10 lectures you will learn what these bits and pieces are! what these bits and pieces are!

Recommendations:Recommendations: Throw the Kenya compiler away!Throw the Kenya compiler away! Write Java from the very first day! Write Java from the very first day!

Page 11: Programming II - Part 1 – Object Oriented Programming with Java Sebastian Uchitel su2

© S. Uchitel, 2004 11

What’s new: ClassesWhat’s new: Classes

import kenya.io.InputReader;import kenya.io.InputReader;

public classpublic class Bigger {Bigger {public static voidpublic static void main( main(String [] argsString [] args) {) { System.out.System.out.println("Type in your 2 numbers -> ");println("Type in your 2 numbers -> "); System.out.System.out.println("The winner is "println("The winner is " + +

bigger(bigger(InputReader.InputReader.readInt(), readInt(), InputReader.InputReader.readInt()));readInt()));}}

static static int bigger(int a, int b){int bigger(int a, int b){//post: returns the larger of the 2 values//post: returns the larger of the 2 valuesif (a > b) {return a;}if (a > b) {return a;}else {return b;}else {return b;}}}

}}

ClassesClasses(In Java, nearly (In Java, nearly

everything appearseverything appears within one)within one)

Page 12: Programming II - Part 1 – Object Oriented Programming with Java Sebastian Uchitel su2

© S. Uchitel, 2004 12

What’s new: ObjectsWhat’s new: Objects

import kenya.io.InputReader;import kenya.io.InputReader;

public classpublic class Bigger {Bigger {public static voidpublic static void main( main(String [] argsString [] args) {) { System.out.System.out.println("Type in your 2 numbers -> ");println("Type in your 2 numbers -> "); System.out.System.out.println("The winner is "println("The winner is " + +

bigger(bigger(InputReader.InputReader.readInt(), readInt(), InputReader.InputReader.readInt()));readInt()));}}

static static int bigger(int a, int b){int bigger(int a, int b){//post: returns the larger of the 2 values//post: returns the larger of the 2 valuesif (a > b) {return a;}if (a > b) {return a;}else {return b;}else {return b;}}}

}}Objects, object Objects, object

methods and callsmethods and calls

Page 13: Programming II - Part 1 – Object Oriented Programming with Java Sebastian Uchitel su2

© S. Uchitel, 2004 13

What’s new: Command line parametersWhat’s new: Command line parameters

import kenya.io.InputReader;import kenya.io.InputReader;

public classpublic class Bigger {Bigger {public static voidpublic static void main( main(String [] argsString [] args) {) { System.out.System.out.println("Type in your 2 numbers -> ");println("Type in your 2 numbers -> "); System.out.System.out.println("The winner is "println("The winner is " + +

bigger(bigger(InputReader.InputReader.readInt(), readInt(), InputReader.InputReader.readInt()));readInt()));}}

static static int bigger(int a, int b){int bigger(int a, int b){//post: returns the larger of the 2 values//post: returns the larger of the 2 valuesif (a > b) {return a;}if (a > b) {return a;}else {return b;}else {return b;}}}

}}Command line Command line

parametersparameters

Page 14: Programming II - Part 1 – Object Oriented Programming with Java Sebastian Uchitel su2

© S. Uchitel, 2004 14

What’s new: PackagesWhat’s new: Packages

import kenya.io.InputReader;import kenya.io.InputReader;

public classpublic class Bigger {Bigger {public static voidpublic static void main( main(String [] argsString [] args) {) { System.out.System.out.println("Type in your 2 numbers -> ");println("Type in your 2 numbers -> "); System.out.System.out.println("The winner is "println("The winner is " + +

bigger(bigger(InputReader.InputReader.readInt(), readInt(), InputReader.InputReader.readInt()));readInt()));}}

static static int bigger(int a, int b){int bigger(int a, int b){//post: returns the larger of the 2 values//post: returns the larger of the 2 valuesif (a > b) {return a;}if (a > b) {return a;}else {return b;}else {return b;}}}

}} PackagesPackages

Page 15: Programming II - Part 1 – Object Oriented Programming with Java Sebastian Uchitel su2

© S. Uchitel, 2004 15

What’s new: Visibility and EncapsulationWhat’s new: Visibility and Encapsulation

import kenya.io.InputReader;import kenya.io.InputReader;

public classpublic class Bigger {Bigger {public static voidpublic static void main( main(String [] argsString [] args) {) { System.out.System.out.println("Type in your 2 numbers -> ");println("Type in your 2 numbers -> "); System.out.System.out.println("The winner is "println("The winner is " + +

bigger(bigger(InputReader.InputReader.readInt(), readInt(), InputReader.InputReader.readInt()));readInt()));}}

static static int bigger(int a, int b){int bigger(int a, int b){//post: returns the larger of the 2 values//post: returns the larger of the 2 valuesif (a > b) {return a;}if (a > b) {return a;}else {return b;}else {return b;}}}

}}Visibility & Visibility &

EncapsulationEncapsulation

Page 16: Programming II - Part 1 – Object Oriented Programming with Java Sebastian Uchitel su2

© S. Uchitel, 2004 16

What’s new: Class scope What’s new: Class scope

import kenya.io.InputReader;import kenya.io.InputReader;

public classpublic class Bigger {Bigger {public static voidpublic static void main( main(String [] argsString [] args) {) { System.out.System.out.println("Type in your 2 numbers -> ");println("Type in your 2 numbers -> "); System.out.System.out.println("The winner is "println("The winner is " + +

bigger(bigger(InputReader.InputReader.readInt(), readInt(), InputReader.InputReader.readInt()));readInt()));}}

static static int bigger(int a, int b){int bigger(int a, int b){//post: returns the larger of the 2 values//post: returns the larger of the 2 valuesif (a > b) {return a;}if (a > b) {return a;}else {return b;}else {return b;}}}

}} Class scopeClass scope

Page 17: Programming II - Part 1 – Object Oriented Programming with Java Sebastian Uchitel su2

© S. Uchitel, 2004 17

What’s new: Much more!What’s new: Much more!

InheritanceInheritance PolymorphismPolymorphism Dynamic BindingDynamic Binding CastingCasting OverloadingOverloading ConstructorsConstructors Abstract Classes Abstract Classes InterfacesInterfaces and much more!and much more!