Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email:...
-
Upload
jessie-jones -
Category
Documents
-
view
216 -
download
2
Transcript of Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email:...
![Page 1: Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr To object or not…](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e685503460f94b63e39/html5/thumbnails/1.jpg)
Java Coding 5
David Davenport
Computer Eng. Dept.,Bilkent UniversityAnkara - Turkey.
email: [email protected]
To object or not…
![Page 2: Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr To object or not…](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e685503460f94b63e39/html5/thumbnails/2.jpg)
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 email saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it.
thank you, David.
![Page 3: Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr To object or not…](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e685503460f94b63e39/html5/thumbnails/3.jpg)
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
![Page 4: Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr To object or not…](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e685503460f94b63e39/html5/thumbnails/4.jpg)
Key Attributes of OOP Abstraction, Encapsulation,
Inheritance, PolymorphismWhat?
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.
![Page 5: Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr To object or not…](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e685503460f94b63e39/html5/thumbnails/5.jpg)
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.
![Page 6: Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr To object or not…](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e685503460f94b63e39/html5/thumbnails/6.jpg)
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, …
Individual Category
![Page 7: Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr To object or not…](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e685503460f94b63e39/html5/thumbnails/7.jpg)
Describing the world (2)
Type/category determine anobject’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
Category IndividualCategory
![Page 8: Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr To object or not…](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e685503460f94b63e39/html5/thumbnails/8.jpg)
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.
![Page 9: Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr To object or not…](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e685503460f94b63e39/html5/thumbnails/9.jpg)
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
Ayse David
“David”David: Say your name
![Page 10: Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr To object or not…](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e685503460f94b63e39/html5/thumbnails/10.jpg)
In more detail Create & manipulate person objects
Name: “Ayse”Age: 18Salary: 500Comments:“Good student”
Name: “David”Age: 22Salary: 2000Comments:“Teaches CS101”
Personname, age,salary, commentssayName,getNetSalarygetCommentssetCommentsincreaseAge…
![Page 11: Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr To object or not…](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e685503460f94b63e39/html5/thumbnails/11.jpg)
Coding Java Classes
// header
public class Person {
// properties
// constructors
// methods
}
public void sayName() { System.out.println( name);
}
String name;int age;double salary;String comments;
public Person( String theName,int theAge ) {
name = theName;age = theAge;comments = “”;
}
![Page 12: Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr To object or not…](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e685503460f94b63e39/html5/thumbnails/12.jpg)
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;
}
![Page 13: Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr To object or not…](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e685503460f94b63e39/html5/thumbnails/13.jpg)
Creating & Using Objects Always
Declare variable to “hold” object Create object using “new” statement Call object’s methods
“Ayse”name:
18age:
0.0salary:
“”
comments:
aStudent{Person}
Person aStudent;aStudent =
aStudent.sayName();
Put this in method of another class,
(e.g main method)
new Person( “Ayse”, 18);
![Page 14: Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr To object or not…](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e685503460f94b63e39/html5/thumbnails/14.jpg)
Creating & Using ObjectsPerson aStudent;aStudent = new Person( “Ayse”, 18);
Person friend;friend = new Person( “David”, 22);
“Ayse”name:
18age:
0.0salary:
“”
comments:
aStudent{Person}
“David”
name:
22age:
0.0salary:
“”
comments:
friend{Person}
friend.increaseAge();aStudent.setComments( “Good student”);
23
“Good student”
![Page 15: Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr To object or not…](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e685503460f94b63e39/html5/thumbnails/15.jpg)
Examples: existing classes Random class
Random die;die = new Random();int face = die.nextInt(6) + 1;System.out.println( face);
StringTokenizer classStringTokenizer tokens;tokens = new StringTokenizer( “to be or not to be”);while ( tokens.hasMoreTokens() ) {
aWord = tokens.nextToken();System.out.println( aWord);
}System.out.println( “done”);
![Page 16: Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr To object or not…](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e685503460f94b63e39/html5/thumbnails/16.jpg)
Writing Your Own Classes Coins Dice Traffic lights TV Video Wallet Robo Music CD Time/Date (in various formats!)
![Page 17: Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr To object or not…](https://reader035.fdocuments.in/reader035/viewer/2022062718/56649e685503460f94b63e39/html5/thumbnails/17.jpg)
Jargon Class is a template/blueprint
for creating objects 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