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

Abstract Data Type - Advanced Programming - Lecture Slides, Slides of Computer Science

These are the Lecture Slides of Advanced Programming and its key important points are: Abstract Data Type, Fraction Example, Public Interface, Implementation, Header File, Adt Operations, Classes of Operations, Constructors, Accessors, Destructors, Modifiers, Fraction Variable

Typology: Slides

2012/2013

Uploaded on 03/20/2013

dharmanand
dharmanand 🇮🇳

3.3

(3)

61 documents

1 / 27

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Abstract Data Type
Fraction Example
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download Abstract Data Type - Advanced Programming - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Abstract Data Type

Fraction Example

Abstract Data Type Example

  • An Abstract Data Type is a data type that is defined by

the programmer, not the language.

  • Like any data type, it is a set of values with operations

that act on those values.

  • Working with ADTs involves three different

components:

1. The public interface , or specification, defines the ADT

and how it is used.

2. The implementation , implements the ADT in code.

3. The client uses the ADT to perform a task.

ADT Operations

  • Several classes of operations are required in order to effectively used ADT.
  1. Constructors – used to create instances of the data type.
  2. Destructors – used to destroy an instance of the data type
  3. Accessors – used to access attributes of the data type. (Such as “get” functions)
  4. Modifiers – used to modify attributes of the data type. (Such as “set” functions)

Object-oriented languages provide some of this functionality within the language. In C you must do it yourself.

Fraction ADT Specification

• The specification for the Fraction ADT requires a

discussion of what operations are required.

Constructors:

Fraction() creates a new variable of type Fraction

with the default value of 0/1.

Fraction(int n, int d) creates a new variable of type

Fraction with the default value of n/d.

Note that you need to handle all combinations of

positive and negative for n and d in order to

determine the sign of the fraction.

Fraction ADT (cont)

Modifiers:

f.reduceFract() – reduces a fraction to lowest

terms. Notice that this is a modifier, and

cannot be used in an expression.

setNumerator(int);

setDenominator(int);

setSign(char);

Fraction ADT Operations

These Operations are designed to be used in an expression. They never modify their arguments. They always return type Fraction.

f = Fraction::negateFract(f) // returns -f s = Fraction::addFract(f1,f2) // returns f1 + f d = Fraction::subFract(f1,f2) // returns f1 – f p = Fraction::mulFract(f1,f2) // returns f1 * f q = Fraction::divFract(f1,f2) // returns f1 / f i = Fraction::compareFract(f1,f2) // f1 < f2 returns -1, // f1 = f2 returns 0, // f1 > f2 returns 1

Design

While we’ve defined an ADT interface , we can’t

just start coding quite yet. There are more

design decisions to be made before writing

code for the implementation.

A common error among programmers is to not

place enough emphasis on the design step.

This is the difference between a

“programmer” and an “analyst”.

Data-centric analysis begins with the idea that all the operations will be acting on FractionsFraction ADT Design: Data

that have some internal representation. The representation shall be shared among all the operations. Each operation is responsible for maintaining your design rules regarding the representation of Fraction information.

typedef enum{ POS, NEG } SignType;

class Fraction { private: // data members int numerator; /* numerator and denominator are declared as / int denominator; / int to simplify the algorithms, but they / SignType sign; / will always be stored >= 0 */ public: // member functions would follow private: // utility functions would follow };

All operations must preserve these rules.

Fraction ADT Algorithms

• You are responsible for designing algorithms that

implement all the operations. What process

must you follow to implement the operations?

a c ad + bc  +  = b d bd

Sum: (^) then, reduce

a c ac  *  =  b d bd

Product: (^) then, reduce

Subtraction involves adding the opposite, and Division involves multiplying by the reciprocal.

Fraction ADT Algorithms

  • Reducing a fraction involves finding the Greatest Common Divisor, and then dividing all the terms by that amount.

Euclid’s Algorithm: if x < y, then swap x and y While y is not zero remainder = x mod y x = y y = remainder

When you’re done, x is the GCD.

ADT Specification

• In C++, the ADT Specification resides in a header

file, names like fraction.h.

/******** Constructors, Destructors, and Clone **********/ Fraction Fraction(); / returns 0/1 / Fraction Fraction( int n, int d ); / returns n/d / // void ~Fraction( Fraction f ); Implementation not needed // Fraction(const Fraction &); Implementation not needed

Accessors

/*********** Accessors

int getNumerator();

int getDenominator();

char getSign();

/******** Fraction Operations **********/Operations

static Fraction negateFract( const Fraction &f1); static Fraction addFract( const Fraction &f1, const Fraction &f2 ); static Fraction subFract( const Fraction &f1, const Fraction &f2 ); static Fraction mulFract( const Fraction &f1, const Fraction &f2 ); static Fraction divFract( const Fraction &f1, const Fraction &f2 ); static int compareFract( const Fraction &f1, const Fraction &f2 ); /* returns -1 if f1 < f 0 if f1 == f +1 if f1 > f */

Input/Output

/******************* I/O ********************/

static int getFract(

istream &infile, Fraction &f);

/* returns 1 if a valid fraction is read

0 if an invalid fraction is read EOF if end of file is detected

*/

static void putFract(

ostream &outfile, const Fraction &f );