Docsity

Objects and Modules - Quiz 1 Problems | CS 251L, Quizzes of Computer Science

Partial preview of the text

Download Objects and Modules - Quiz 1 Problems | CS 251L and more Quizzes Computer Science in PDF only on Docsity!

CS251 Intermediate Programming

Objects, Part 2 Lecture 6

  • Quiz 1 today

There are several reasons you might want to do this:the system.system with minimal or no effects on the other components ofimplements the same abstraction) can be substituted into aA module has the property that an equivalent module (one thatsomething really useful: Objects as Modules.Once we have objects as abstractions, we can move further to

Replace an expensive component with a cheap one.Replace a buggy component with a robust one.Replace a limited component with a high capacity one.Replace a slow component with a fast one.

many other parts of the system.it is to change any aspect of it because it is connected to so Without modularity, the more complex a system is, the harder

and reused.function, they can be detached from their original contextIf these sub-functions are not too closely tied to a given super-usually overlap to a great extent.Real systems have several functions, and their sub-functionsprogram and break it into smaller steps.down design, where you take the main function of theThe method of decomposition you may be familiar with is top-

to a speciDecomposability means a solution isn’t too highly customized

fi c problem, but has some generality to it.

Top-down design doesn't contribute to decomposability.“umbilicals.”system won’t require detaching and reattaching manyDecomposability means transplanting a module to anotheritself, and not just an inseparable part of some other problem.Decomposability means treating a sub-problem as a thing in

understand the whole system, so modularity becomes critical. Projects sometimes become so large that no one canuntil they understand all of the system.modularity, because they can’t work on any part of the systemIt’s hard to add additional programmers to projects with poorcomponent does in a straightforward way.Understandability means being to explain what a soft warethe context of the application it is a part of.A module is understandable if it makes sense by itself, out of

In math, continuity can be de

fi ned as “small changes in input

representations of data have poor continuity. Programs whose designs are closely tied to computerchanges according to their conceptualizations of the system.about the problem, where practical, because users will requestIt's good to make program structure match the way users thinktime.those changes will not require massive rework, will help overto your soft ware, and designing the soft ware in a way thatTrying to predict what kinds of changes your users will requestin small changes to the code.A module is continuous if small changes in functionality resultyield small changes in output.”

1. Decomposable?How is it...

(can take it apart and still have something)

2. Composable?

(can rearrange its pieces)

3. Understandable?

(can describe its pieces simply)

4. Continuous?

(can make simple changes easily)

5. Protected?

(failures don't spread bet ween pieces)

It deThe electric power grid is an example of good modularity.

fi nes several abstractions: generators, loads, and a

The specidistribution system.

fi cation of the system is simple: it delivers continuous

meets the speci Generators can be implemented using any technology thatGenerators can be added to/removed from the grid at will.120V AC/60Hz electric power to all outlets on the system.

fi cation: oil, coal, gas, nuclear, hydro, wind, solar,

geothermal, hamsters on wheels, cold fusion, etc.

1. Decomposable?How is it...

(can take it apart and still have something)

2. Composable?

(can rearrange its pieces)

3. Understandable?

(can describe its pieces simply)

4. Continuous?

(can make simple changes easily)

5. Protected?

(failures don't spread bet ween pieces)

speciThe history of soft ware development shows that voluntary

fi cation/implementation boundaries do not work.

boundary bet ween speciThe only way to maintain modularity is to enforce a hardtemporarily and fail when something in the system is changed.direct approach to solving a problem. These often workProgrammers sometimes take short cuts that look like thetry to use them.functions are changed frequently, which breaks programs thatfunctionality. The behavior of these "internal" systemprogrammers looking for an "edge" in performance or(Application Programming Interfaces) that are exploited byOperating systems have long had "undocumented" APIs

fi cation and implementation: make the

speci

fi cation the only part of the soft ware module that is

Java does this with access modivisible from the outside.

fi ers for

fi elds and methods.

public

and

private

are t wo access modi

fi ers Java uses to

control access to

fi elds and methods.

public

means a feature is visible and changeable throughout

private the program.

means a feature is visible and changeable from

public Things that are essential to the module's purpose should bewithin the same class.

changing the module's purpose should beThings that could be implemented differently without

private

so that

Objects'is likely to change (and force lots of reprogramming).other soft ware components won't depend upon something that

fi elds are almost always considered implementation

and are therefore marked

private

Constants ("

static final

") are usually

public

This is called encapsulation or information hiding.

One of the reasons

fi elds are almost always considered

The structure ofsoft ware elements over time.implementation is because they are one of the most changeable

fi elds might be changed because:

The data exceeded the storage capacity of the

fi elds.

The underlying computer architecture changed.are supposed to model.They don't accurately follow the real-world situation theyThey are confusing or contradictory.feature upgrade.New kinds of data are added to a system as part of a

problem.One of the most famous data format changes was the Y2K

speci Programmers didn't make any distinction bet ween the

fi cation of dates (things you need to do with dates) and

to change, it forced extensive changes to other soft ware.the implementation as data. When the implementation had

Speci

fi cation:

{ public class Time { public void addDays(int days) // Implementation

{ public int daysBetween(Time t)} // Implementation

{ public boolean comesAfter(Time t)} // Implementation

{ public String formatString(int formatType)} // Implementation

}

private long millisecondsSinceEpoch;

public speciIf other modules are dealing only with theper date, this one takes up 8+ bytes.previous implementation takes up 29+ bytesfrom this single value. And where themonth, date, hour, etc.) can be calculated Since time is linear, all the other values (year,} Implementation:

fi cation of the

(^) Time

(^) class, they

have to be rewritten or even changed.won't even notice this change, and won't

expectedcompiler to be of the proper class and, therefore, have theany object argument it receives will be guaranteed by theand the method implementation itself: the method knows that This forms a kind of contract bet ween the caller of a methodreference of one class to a reference of an incompatible class.compiler will treat as an error any attempt to assign aWhen you create a class, it becomes its own data type, and theerror, because it almost always indicates a logic error.value of an incompatible type, the compiler treats this as anIf you try to assign a variable, array element, or argument aerrors.languages to protect against certain kinds of programmingStrict typing is a system used by Java and many other

fi elds and methods.