Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
80 views34 pages

Object Oriented Programming Object Oriented Programming: Lecture-7 Instructor Name

Here are the key points about modulu (%) operator: - It returns the remainder after dividing the left operand by the right operand - It is useful for rolling over values, like incrementing a clock from 59 to 0 - When value reaches the limit, incrementing it would make it go past the limit - Using value = (value + 1) % limit keeps it within the limit by rolling it back to 0 - E.g. if limit is 60 and value is 59, 59 + 1 is 60 but 60 % 60 is 0, so it rolls over So in this NumberDisplay class, the modulu operator is used to roll over the value back to 0 when it reaches the limit, simulating

Uploaded by

mohammad bilal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views34 pages

Object Oriented Programming Object Oriented Programming: Lecture-7 Instructor Name

Here are the key points about modulu (%) operator: - It returns the remainder after dividing the left operand by the right operand - It is useful for rolling over values, like incrementing a clock from 59 to 0 - When value reaches the limit, incrementing it would make it go past the limit - Using value = (value + 1) % limit keeps it within the limit by rolling it back to 0 - E.g. if limit is 60 and value is 59, 59 + 1 is 60 but 60 % 60 is 0, so it rolls over So in this NumberDisplay class, the modulu operator is used to roll over the value back to 0 when it reaches the limit, simulating

Uploaded by

mohammad bilal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Object Oriented Programming

Instructor Name:
Lecture-7
Today’s Lecture

 Object Interaction

 Modularization & Abstraction

2
Object Interaction & Collaboration

Message Passing
 An Application is set of Cooperating Objects that communicate with
each other

 Objects communicate by sending messages

 When an object sends a message to another object, an operation is


invoked in the receiving object

 The aim of modelling object interaction is to determine the most


appropriate scheme of message passing between objects to support a
particular user requirement

 In Java , “Message Passing” is done by calling methods.


3
Object Interaction & Collaboration

A Digital Clock Example


 An Application is set of Cooperating Objects that communicate with
each other

 Objects communicate by sending messages

 When an object sends a message to another object, an operation is


invoked in the receiving object

 The aim of modelling object interaction is to determine the most


appropriate scheme of message passing between objects to support a
particular user requirement

 In Java , “Message Passing” is done by calling methods.


4
Object Interaction & Collaboration

A Digital Clock Example

 Can you guess the implementation of clock?

5
Object Interaction & Collaboration

A Digital Clock Example


 First Idea: to implement a whole clock display in single class.

 If the problem is bigger than a digital clock then??

 Another approach (Abstraction and Modularization):

 Divide the problem in sub-components and then again sub-sub-


components and so on, until then individual problem are small
enough to be easy to deal with (Modularization).

 Once sub-problem solved, don’t think about in detail any more


(Abstraction)

6
Object Interaction & Collaboration

Abstraction & Modularization Example


 Suppose you want to go to Spain on holiday

 You can divide the problem into modules:

 Getting to the airport

 Flying from Britain to Spain

 Getting from Spanish airport to your hotel

 Each module can be solved independently, which simplifies things

 Use a taxi company to get to the airport, a travel agent to get to


Spain and a shuttle bus to get to your hotel

7
Object Interaction & Collaboration

Abstraction & Modularization Example


 You can have a hierarchy of modules

 Getting to the airport has modules:

 Find the phone number of a taxi company

 Book a taxi

 Set your alarm

 Setting your alarm has modules…

 Key points:

 Dividing a problem into smaller problems simplifies things

 If modules are independent they are easier to solve


8
Object Interaction & Collaboration

Abstraction & Modularization Example


 Do not start by planning how to set your Alarm

 Do not start by planning how to get to the Airport

 Instead, abstract over these details

 Assume you will figure out later how to set your alarm and get to
the airport

 Start with the highest-level decision:

 Where in Spain do you want to go?

 Madrid,Malaga, Granada…?

9
Object Interaction & Collaboration

Abstraction & Modularization Example


 Now look at the next-highest decision:

 How should I get to Malaga? Fly from Bristol, Gatwick, Heathrow…?

 At this point think about flights and airports, but abstract over how to get
to the airport

 Now you can plan how to get to the airport

 But still abstract over how to set your alarm

 Eventually you can deal with how to set your alarm

 Key point:

 Working top-down as in this example is usually a good strategy

10
Object Interaction & Collaboration

Abstraction & Modularization Example


 Engineers in car company designing a car.
 Many Thinking areas
 Shape of body
 Size and location of engine
 Number and size of seats in the passenger area.
 Exact spacing of wheel
 An other engineer, whose job is to design engine
 Many thinking areas
 Clyinder
 Injection Mechansim
 Carburetor
 Elelctronics
 Spark plug (one Engineer will desgin it) 11
Object Interaction & Collaboration

Further Narrow Down the Problem Basic Class Structure


 Engineer may think of the spark plug as a complex artifact
of many parts. He might have done complex studies. To
determine exactly what kind of metal to use for the
contacts or what kind of material and production process
to use for the insulation.

12
Abstraction

Understanding Abstraction
 A designer at the highest level will regard a wheel as a single part.
Another engineer much further down the chain may spend her days
thinking about the chemical composition necessary to produce the right
materials to make the tires. For the tire engineer, the tire is a complex
thing. The car company will just buy the tire from the tire company and
then view it as a single entity. This is abstraction.
• =================================================

 The engineer in the car company abstracts from the details of the tire
manufacture to be able to concentrate on the details of the construction of,
say, the wheel. The designer designing the body shape of the car
abstracts from the technical details of the wheels and the engine to
concentrate on the design of the body (he will just be interested in the
size of the engine and the wheels).
13
Abstraction

Abstraction in Software
 In object-oriented programming, these components and subcomponents
are objects. If we were trying to construct a car in software, using an
object-oriented language, we would try to do what the car engineers do.

 Instead of implementing the car as a single, monolithic object,

 we would first construct separate objects for an engine, gearbox, wheel,


seat, and so on, and then assemble the car object from those smaller
objects.

 Now, back to our digital clock.

14
Abstraction & Modularization

Abstraction
 Abstraction is the ability to ignore details of parts to focus attention
on a higher level of a problem.

Modularization
 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

15
Modularization

Modularization in the Digital Clock Example


 One way to look at it is to consider it as consisting of a single display with
four digits (two digits for the hours, two for the minutes).
 we can see that it could also be viewed as two separate two-digit displays
(one pair for the hours and one pair for the minutes).
 One pair starts at 0, increases by 1 each hour, and rolls back to 0 after
reaching its limit of 23. The other rolls back to 0 after reaching its limit of 59.
 The similarity in behavior of these two displays might then lead us to
abstract away even further from viewing the hours display and minutes
display distinctly. Instead, we might think of them as being objects that can
display values from zero up to a given limit. The value can be incremented,
but, if the value reaches the limit, it rolls over to zero.
 Now we seem to have reached an appropriate level of abstraction that we can
represent as a class: a two-digit display class.

16
Modularization

Modularization in the Digital Clock Example

17
Modularization

Implementing Digital Clock

18
Abstraction - NumberDisplay

Implementing Digital Clock

public class NumberDisplay


{
private int limit;
private int value;

Constructor and methods omitted.


}

19
Abstraction - NumberDisplay

Using an Abstract NumberDisplay

20
Abstraction - ClockDisplay

Implementing Digital Clock

public class ClockDisplay


{
private NumberDisplay hours;
private NumberDisplay minutes;

Constructor and methods omitted.


}

NumberDisplay class is use in ClockDisplay class. Here we have has-


a relationship
21
Abstraction - NumberDisplay

NumberDisplay Source Code


public class NumberDisplay
{
private int limit;
private int value;

public NumberDisplay(int rollOverLimit)


{
limit = rollOverLimit;
value = 0;
}

public void increment() {


value = (value + 1) % limit;
}

What modulu (%) operotor do here? 22


Abstraction - NumberDisplay

NumberDisplay Source Code


public int getValue()
{
return value;
}

public void setValue(int replacementValue)


{
if((replacementValue >= 0) &&(replacementValue < limit))
{
value = replacementValue;
}
}

23
Abstraction - NumberDisplay

NumberDisplay Source Code


public String getDisplayValue()
{
if(value < 10)
{
return "0" + value;
}
else
{
return "" + value;
}
}
}

24
Objects Creating Objects

ClockDisplay Source Code


public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayString;

public ClockDisplay() {
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);


}
}

25
Objects Creating Objects

ClockDisplay Source Code


public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayString;

public ClockDisplay() {
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
}

public void timeTick() {


minutes.increment();
if(minutes.getValue() == 0)
{
hours.increment();
}
updateDisplay();
}
……… 26
Objects Creating Objects

ClockDisplay Source Code


public void setTime(inthour,intminute)
{
hours.setValue(hour);
minutes.setValue(minute);
updateDisplay();
}

public String getTime()


{
return displayString;
}

private void updateDisplay()


{
displayString = hours.getDisplayValue() + ":"+
minutes.getDisplayValue();
}
27
}
Objects Creating Objects

28
Internal & External Call

29
Internal & External Call

30
Reading Input From Keyboard

The Scanner Class


 There are various ways to read input from the keyboard, the java.util.Scanner
class is one of them.

 The Java Scanner class breaks the input into tokens using a delimiter that is
whitespace bydefault. It provides many methods to read and parse various
primitive values.

 Java Scanner class is widely used to parse text for string and primitive types
using regular expression.

31
Reading Input From Keyboard

The Scanner Class - Methods

32
Reading Input From Keyboard

The Scanner Class – Example


import java.util.Scanner;
class ScannerTest{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter your rollno");
int rollno=sc.nextInt();
System.out.println("Enter your name");
String name=sc.next();
System.out.println("Enter your fee");
double fee=sc.nextDouble();
System.out.println("Rollno:"+rollno+" name:"+name+" fee:"+
fee);
sc.close();
}
} 33
34

You might also like