انت هنا الان : شبكة جامعة بابل > موقع الكلية > نظام التعليم الالكتروني > مشاهدة المحاضرة

Object interaction: Creating cooperating objects

Share |
الكلية كلية تكنولوجيا المعلومات     القسم قسم البرامجيات     المرحلة 2
أستاذ المادة احمد خلفة عبيد العجيلي       09/03/2019 10:21:38
Object-oriented programming with Java
Ahmed Al-Ajeli
Object interaction
Creating cooperating objects
Ahmed Al-Ajeli
Lecture 4
2
Concepts to be covered
•abstraction
•modularization
•classes define types
•object references
•object types
•primitive types
Object-oriented programming with Java
Ahmed Al-Ajeli
3
A digital clock
4
Abstraction and modularization
•Abstraction is the ability to ignore details of parts to focus attention on a higher level of a problem.
•Modularization is the process of dividing a whole into well-defined parts, which can be built and examined separately, and which interact in well-defined ways.
Object-oriented programming with Java
Ahmed Al-Ajeli
5
Modularizing the clock display
One four-digit display?
Or two two-digit displays?
6
Modeling a two-digit display
•We call the class NumberDisplay.
•Two integer fields:
–The current value.
–The limit for the value.
•The current value is incremented until it reaches its limit.
•It ‘rolls over’ to zero at this point.
Object-oriented programming with Java
Ahmed Al-Ajeli
7
Implementation - NumberDisplay
public class NumberDisplay
{
private int limit;
private int value;
public NumberDisplay(int limit)
{
this.limit = limit;
value = 0;
}
...
}
8
Accessor and mutator methods
public int getValue()
{
return value;
}
public void setValue(int replacementValue)
{
if((replacementValue >= 0) &&
(replacementValue < limit)) {
value = replacementValue;
}
}
Object-oriented programming with Java
Ahmed Al-Ajeli
9
Logic operators
•Logic operators operate on boolean values (true or false) and produce a new boolean value as a result. The logical operators:
&& (and) || (or) ! (not)
- a && b is true if both a and b are true,
and false otherwise.
- a || b is true if either a or b or both are
true, and false if they are both false.
- !a is true if a is false and false if a is
true.
10
Source code: NumberDisplay
public String getDisplayValue()
{
if(value < 10) {
return "0" + value;
}
else {
return "" + value;
}
}
Object-oriented programming with Java
Ahmed Al-Ajeli
11
increment method
public void increment()
{
value = value + 1;
if(value == limit) { // Keep the value within the limit. value = 0; }
}
12
The modulo operator
•The division operator (/), when applied to int operands, returns the result of an integer division.
•The modulo operator (%) returns the remainder of an integer division.
•E.g., generally: 17 / 5 gives result 3, remainder 2
•In Java: 17 / 5 == 3 17 % 5 == 2
Object-oriented programming with Java
Ahmed Al-Ajeli
13
Quiz
•What is the result of the expression 8 % 3
•For integer n >= 0, what are all possible results of: n % 5
•Can n be negative?
14
Alternative increment method
public void increment()
{
value = (value + 1) % limit;
}
Check that you understand how the rollover works in this version.
Object-oriented programming with Java
Ahmed Al-Ajeli
15
Implementation - ClockDisplay
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
Constructor and
methods omitted.
}
16
Primitive types
Data can be classified under many different types. Primitive type is the one predefined by the Java language.
Object-oriented programming with Java
Ahmed Al-Ajeli
17
Classes as types
•In addition to primitive types, every class is a unique data type; e.g. String, TicketMachine, NumberDisplay.
•Data types, therefore, can be composites and not simply values.
18
Object diagram
Object-oriented programming with Java
Ahmed Al-Ajeli
19
Objects creating objects (1)
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayString;
public ClockDisplay()
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);

}
}
20
Objects creating objects (2)
public NumberDisplay(int rollOverLimit);
in class NumberDisplay:
formal parameter
hours = new NumberDisplay(24);
in class ClockDisplay:
actual parameter
Object-oriented programming with Java
Ahmed Al-Ajeli
21
ClockDisplay object diagram
22
Quiz: What is the output?
•int a; int b; a = 32; b = a; a = a + 1; System.out.println(b);
•Student a; Student b; a = new Student(“Mohammed“,”Moh1440”); b = a; a.changeName(“Ahmed”); System.out.println(b.getName());
Object-oriented programming with Java
Ahmed Al-Ajeli
23
Primitive types vs. object types
32
object type
primitive type
SomeObject obj;
int i;
24
Primitive types vs. object types
32
ObjectType a;
int a;
ObjectType b;
32
int b;
b = a;

المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .
الرجوع الى لوحة التحكم