Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…

Slides:



Advertisements
Similar presentations
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Example - The.
Advertisements

Java Coding OOP David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Towards Event-driven programming &
Everyday Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction to.
Introduction to UML David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …Unified Modeling Language.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
Object-Oriented PHP (1)
Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.
Java Coding 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Decisions, decisions…!
Java is an Object-Oriented Language b In structured programming languages, methods define the structure of the programs, they are basic building blocks.
Java Coding 3 David Davenport Computer Eng. Dept.,
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
Review: OOP & Arrays David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …from CS101.
Robo David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction to the engineering.
1 OOP  Software usually tries to model real world problems  What does the world look like?
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Java Coding David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey.
Java Coding 4 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Method madness.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
Object Oriented Software Development
Java Coding 3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Over & over again!
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Writing Classes (Chapter 4)
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
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.
CS 106 Introduction to Computer Science I 03 / 19 / 2007 Instructor: Michael Eckmann.
Object-Oriented Programming (OOP). Implementing an OOD in Java Each class is stored in a separate file. All files must be stored in the same package.
Computer Science II 810:062 Section 01. How is CS I different from CS II? When you teach Java there are a series of decisions that have to be made…
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
1 Programming Paradigms Object Orientated Programming Paradigm (OOP)
Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Some important Java interfaces +
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
Basic Object-Oriented Concepts – towards implementation CS3340.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Objects We are all familiar with the idea of an object. We are surrounded by them: cars, books, people, houses, cats, etc. Objects have attributes, e.g.
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
OOP Basics Classes & Methods (c) IDMS/SQL News
CSE 1201 Object Oriented Programming Introduction.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
© 2004 Pearson Addison-Wesley. All rights reserved6-1 Program Development In your CS102 project, and in many others, the basic activities are:  requirements.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object-Oriented Programming Concepts
Java Coding – part 2 David Davenport Computer Eng. Dept.,
Java Coding 3 – part2 David Davenport Computer Eng. Dept.,
Java Coding 5 David Davenport Computer Eng. Dept.,
Chapter 4: Writing Classes
Java Coding 4 David Davenport Computer Eng. Dept.,
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Object oriented vs procedural programming
Java Coding 6-extra David Davenport Computer Eng. Dept.,
CS2011 Introduction to Programming I Objects and Classes
Java Coding 6 – part2 David Davenport Computer Eng. Dept.,
CSE 1201 Object Oriented Programming
Object-Oriented Programming
Java Coding 4 (part2) David Davenport Computer Eng. Dept.,
Java Coding 6_part3 David Davenport Computer Eng. Dept.,
Java Coding 6 David Davenport Computer Eng. Dept.,
Java Coding 6 David Davenport Computer Eng. Dept.,
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept.,
Presentation transcript:

Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…

IMPORTANT… Students… This presentation is designed to be used in class as part of a guided discovery sequence. It is not self- explanatory! Please use it only for revision purposes after having taken the class. Simply flicking through the slides will teach you nothing. You must be actively thinking, doing and questioning to learn! Instructors… You are free to use this presentation in your classes and to make any modifications to it that you wish. All I ask is an saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you, David. David

From the beginning… History of programming paradigms GoTo Programming (spaghetti code!) Structured Programming Object-Oriented Programming ??? Aspect-Oriented… Paradigm changes response to Need to build ever larger programs Correctly On time On budget

Key Attributes of OOP Abstraction, Encapsulation, Inheritance, Polymorphism What? Ease of reuse Speeds implementation & facilitates maintenance. Component-based approach Easy to use existing code modules Easy to modify code to fit circumstances! A natural way to view/model world Makes design quicker, easier & less error-prone.

The world as we see it… Look around & what do you see? Things (books, chairs, tables, people!) Actually, see individual things! Ayse, David, my textbook, that chair, Mehmet’s pencil, etc. The world is a set of things interacting with each other.

Describing the world (1) Describe a particular person Ayse has long blond hair, green eyes, is 1.63m tall, weighs 56Kg and studies computer engineering. Now lying down asleep. Mehmet studies electronics, has short black hair and brown eyes. He is 180cm and 75 kilos. Now running to class! Notice how all have specific values of name, height, weight, eye colour, state, … IndividualCategory

Describing the world (2) Type/category determine an object’s properties & functionality Person has name, height, weight, can run, sleep, … Category gives default properties “Ayse is a person with green eyes.” We infer/assume she has two of them, as well as two legs, arms, nose, mouth, hair, can speak, run, sleep, etc! Can concentrate on “relevant” properties CategoryIndividual Category

Java OOP terminology Class - Category Properties/states Functionality/Services (examines/alters state) data methods object - Individual/unique thing (an instance of a class) Particular value for each property/state & functionality of all members of class.

main Java OOP Software Software System Set of objects Which interact with each other One object will send a message to another object asking it to do a particular task. The first object does not need to know how the task is done (only how to request that it be done.) This corresponds to calling one of the second object’s methods! Created (instantiated) from class definitions Person GüneşDavid “David” David: Say your name

In more detail Create & manipulate person objects Name: “Güneş” Age: 18 Salary: 500 Comments: “Good student” Name: “David” Age: 22 Salary: 2000 Comments: “Teaches CS101” Person name, age, salary, comments sayName, getNetSalary getComments setComments increaseAge …

Coding Java Classes // header public class Person { // properties // constructors // methods } public void sayName() { System.out.println( name); } Stringname; intage; doublesalary; Stringcomments; public Person( StringtheName, inttheAge ) { name = theName; age = theAge; comments = “”; }

Coding Java Classes public double getNetSalary( int baseRate) { double tax; tax = compoundInterest( baseRate); return salary – tax * 1.10; } public String getName() { return name; } public String getComments() { return comments; } public void setComments( String someText) { comments = someText; } “get” & “set” methods for some properties (no setName!) Variables which are not parameters or properties must be defined locally. public void increaseAge() { age = age + 1; }

Creating & Using Objects Always Declare variable to “hold” object Create object using “new” statement Call object’s methods “Güneş” name: 18 age: 0.0 salary: “” comments: aStudent {Person} Person aStudent; aStudent = aStudent.sayName(); Put this in method of another class, (e.g main method) new Person( “ Güneş ”, 18);

Creating & Using Objects Person aStudent; aStudent = new Person( “ Güneş ”, 18); Person friend; friend = new Person( “David”, 22); “ Güneş ” name: 18 age: 0.0 salary: “” comments: aStudent {Person} “ David ” name: 22 age: 0.0 salary: “” comments: friend {Person} friend.increaseAge(); aStudent.setComments( “Good student”); 23 “Good student”

Simplified Person Class package myworld; // Person - simple example only! // Author: David, CS101 public class Person { // properties Stringname; intage; // constructors public Person( String theName, int theAge) { name = theName; age = theAge; } // methods public void increaseAge() { age = age + 1; } public void sayNameAndAge() { System.out.println( name + "\t" + age ); } Declare properties note private/package access. Give initial values to each of the properties Define (instance) methods that examine/change properties

Simplified PersonTest import myworld.Person; // PersonTest - demo Person class // Author: David, CS101 public class PersonTest { public static void main( String[] args) { // VARIABLES PersonaStudent; Personfriend; // PROGRAM CODE aStudent = new Person( "Güneş", 18); friend = new Person( "David", 22); aStudent.sayNameAndAge(); friend.sayNameAndAge(); friend.increaseAge(); aStudent.increaseAge(); friend.increaseAge(); System.out.println(); aStudent.sayNameAndAge(); friend.sayNameAndAge(); } } // end of class PersonTest Declare variables to hold Person objects Create Person objects & put them into the variables Use objects by calling their methods

Examples: existing classes Random class Random die; die = new Random(); int face = die.nextInt(6) + 1; System.out.println( face); StringTokenizer class StringTokenizer tokens; tokens = new StringTokenizer( “to be or not to be”); while ( tokens.hasMoreTokens() ) { aWord = tokens.nextToken(); System.out.println( aWord); } System.out.println( “done”);

Writing Your Own Classes Coins Dice Traffic lights TV Video Wallet Robo Music CD Time/Date (in various formats!)

Jargon Class is a template/blueprint for creating objects/instances of its type Class has Properties, attributes, states, fields, … Methods, functionality, services, … Create/instantiate an instance/object of a class Constructor called automatically by “new”, to give initial values to all properties