Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Understanding Object Interaction: Java Digital Clock, Slides of Network security

The concepts of modularization and abstraction in programming, using the example of a digital clock. It explains how to divide the clock display problem into simpler sub-parts, implement number display objects, and use their interfaces to communicate between objects. The document also covers creating a clockdisplay class and using it in a clockdemo program.

Typology: Slides

2012/2013

Uploaded on 04/22/2013

sathaye
sathaye 🇮🇳

4.8

(8)

106 documents

1 / 28

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Objectives
introduce modularization and abstraction
explain how an object uses other objects
compare object and primitive types
4. Object Interaction
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download Understanding Object Interaction: Java Digital Clock and more Slides Network security in PDF only on Docsity!

  • Objectives
    • introduce modularization and abstraction
    • explain how an object uses other objects
    • compare object and primitive types

4. Object Interaction

Topics

1. Modularization and Abstraction

2. A Digital Clock

3. Using ClockDisplay

4. A More Graphical Clock

Use in Programming

• Use modularization to split a programming

problem into sub-parts (modules).

– implement the modules

• The implementation of the complete

program will be easier, since abstraction

can be used to write software in terms of

the modules.

e.g. Robot Software

Modules

("black

boxes")

Abstraction

"links" the

modules

together

using their

visible

interfaces.

e.g. Airport Control System

Classes for:

Plane, Gate, Luggage,

Passenger, etc.

Use them to create objects

such as plane1, plane2,

gate2, myLuggage

Abstraction simplifies the

communication between

the objects; only use their

visible interface.

2. A Digital Clock

• Implement a digital clock display, which

shows the hours (0-23) and minutes (0-59).

Objects Diagram

ClockDisplay object

NumberDisplay object

(for hours)

NumberDisplay object

(for minutes)

NumberDisplay Interface

• What kind of interface is needed for a

NumberDisplay class?

– get and set the number

– return the number as a string

  • useful for printing

– increment the number

  • the number will 'loop'
  • e.g. 0, 1, 2, ..., 59, 0 , 1, ... for the minutes display

public void setValue(int newValue)

/* Set currValue to the new value. If the new value is less than zero or over maxValue, don't set it.

*/

{ if ((newValue >= 0) && (newValue < maxValue))

currValue = newValue;

}

public int getValue()

{ return currValue; }

continued Docsity.com

public String getDisplayValue() // return currValue as a string { if (currValue < 10) return "0" + currValue; //pad string with leading 0 else return "" + currValue; }

public void increment() /* Increment currValue, rolling over to zero if the maxValue is reached. */ { currValue = (currValue + 1) % maxValue; }

} // end of NumberDisplay class

The ClockDisplay Class

public class ClockDisplay { private NumberDisplay hours; private NumberDisplay minutes; private String currTimeString; // the current time as a string

public ClockDisplay() // intialize the clock to 00: { hours = new NumberDisplay(24); minutes = new NumberDisplay(60); setTimeString (); }

two private

NumberDisplay

fields

create two

NumberDisplay

objects

continued Docsity.com

private void setTimeString()

/* store the current time as a string of the form "hours:minutes" */ { currTimeString = hours.getDisplayValue() + ":" + minutes.getDisplayValue(); }

a private method is

one that only other

methods in the class

can call

continued

method calling

in NumberDisplay

objects

public void minIncrement() // increment the clock by one minute; // hour increments when minutes roll over to 0 { minutes.increment(); if (minutes.getValue() == 0) // mins rolled hours.increment(); setTimeString (); } // end of minIncrement()

} // end of ClockDisplay class

Classes Diagram

uses