|
ADT
Feb 12, 2002 0:14:57 GMT -5
Post by Sylph on Feb 12, 2002 0:14:57 GMT -5
what is it?
|
|
|
ADT
Feb 12, 2002 1:49:14 GMT -5
Post by Evotamer on Feb 12, 2002 1:49:14 GMT -5
This is from the CSC148 book last year...
Analogy Imagine you had to understand in detail how a carburetor, fuel-injection systems, and frond whee drive work in order to drive a car.
Even if you do understand these things, do you think about them whenyou drive?
Fortunately, modern cars provide nice abstractiosn like a simple gas pedal that hides all details and allows us to concentrate on the road.
Now with this relation said... abstract data types are classes that give a user to a program an interface for what to do. i.e. A driver knows how to use the gas pedal but doesn't know what the gas pedal is doing behind the scenes. The car manufacturer knows how the pedal works and all the mechanisms that work together to make things work...
An abstract data type is similar... suppose you wanted to make a program to make shapes...You might have a class like shapes... in which you might have some functions like draw(), make(), stretch(), etc...
However not all shapes are equal.. much like not all car pedals work the same way... BUT.. as a user we know how to use these 'functions' quite abstractly... We know how to press the gas pedal to make the car move...
So in object oriniented programming, we can define something similar to the way cars are made (by the fact that all cars have similar gas pedal functions). So we can define a shape abstractly giving every shape functions we know that will work.
we define these something like: public abstract class shape { public abstract void draw(); public abstract void stretch(); . . . }
similarily we can define extentions of class shape to meet the needs of each specific shape... i.e. a circle will be drawn differently than a square.
So abstract data types give a user to a program the general functions of our program... we can run these simple functions and the program internally will perform these tasks hidden away from the user. The user would not need to know how these functions work, they just need to know how to call them.
Umm.. does this help?
Ciao
|
|
|
ADT
Feb 12, 2002 17:06:39 GMT -5
Post by Sylph on Feb 12, 2002 17:06:39 GMT -5
thanks a lot!
|
|
|
ADT
Feb 13, 2002 0:03:03 GMT -5
Post by gabbie on Feb 13, 2002 0:03:03 GMT -5
wow, nice explaination...really clear.. i kinda get it now..so it kinda simplifies it for the user, so the user will only need to know how to call the methods, not the coding behind it, but what's the point of using it ? either way, having the implementation on one class or another, the user still doesn't see it, even by using abstract classes, yes, you just declare the method for the user but what's the use ? i can simply implement and declare it in the main class and the user still wouldn't have to know what it does, just how to call the method. Thanks alot for the explaination though. really good
|
|
|
ADT
Feb 13, 2002 2:20:45 GMT -5
Post by Evotamer on Feb 13, 2002 2:20:45 GMT -5
The point of doing it is so that the user can mess around with your program design... the design is yours.. how you implement it is yours... and the user shouldn't need to know what's going on... if the user knows.. then there wouldn't be a point in having a programmer create programs What a ADT mostly does is it hides the details.. and these details are only ment for the eyes of the programmer(s). And this allows them to keep their stuff secret
|
|
|
ADT
Feb 13, 2002 11:26:16 GMT -5
Post by Sylph on Feb 13, 2002 11:26:16 GMT -5
does it have to be declared abstract, e.g., public abstract class Something{} ?
or can it be public class SomethingElse implements Iterator{ // methods } ?
|
|
|
ADT
Feb 13, 2002 20:13:33 GMT -5
Post by gabbie on Feb 13, 2002 20:13:33 GMT -5
icic... thanks thanks.. juss a quick question.. need to know queues-array, circular, stacks, memory models, iterators, interface, comments, exceptions, contract by design, a lil bit of link list, is that all ?
|
|
|
ADT
Feb 13, 2002 20:45:12 GMT -5
Post by Evotamer on Feb 13, 2002 20:45:12 GMT -5
Here's an example from one of my assignments last year:
// Parent class for all arithmetic expressions that involve only variable // 'x', numerical constants, and the usual arithmetic operators + - * / ^. public abstract class Expression {
// Compute and return the value of this expression, for the value of // variable 'x' given as argument. public abstract double eval(double x);
// Return the maximum stack depth required to evaluate this expression // (as explained in the assignment handout). public abstract int stackDepth();
// Return the number of operators in this expression. public abstract int opCount();
// Return an expression that represents the derivative of this // expression, following the rules given in the handout. public abstract Expression diff();
// Return the expression that results from simplifying this expression, // following the rules given in the handout. public abstract Expression simplify(); // Returns the the value of the expression x^n. Where x is a double // and n is an integer. public static double power(double x, int n) { // if the exponent is 0, return 1. if (n == 0) { return 1; // if the exponent is negative return (1 / x) ^n } else if (n < 0) { return ( 1 / (x * power (x, (-1*n)- 1))); // anything else, calculate the powers recursively } else { return (x * power (x, n-1)); } } } // class Expression
// Parent class for expressions that contain no operator. public abstract class SimpleExpression extends Expression {
// Stack Depth always 1 public int stackDepth() { return 1; }
// Return whatever it is public Expression simplify() { return this; } // no operands in simple expressoins public int opCount() { return 0; } } // class SimpleExpression
// Represents the variable x. public class VariableExpression extends SimpleExpression { // Using Java's default constructor (no initialization required).
// Return a String representation of this expression. public String toString() { return "x"; }
// Compute and return the value of this expression, for the value of // variable 'x' given as argument. public double eval(double x) { return x; } public Expression diff() { return new ConstantExpression(1); } } // class VariableExpression
This code compiles runs and stuff..... I forgot what it does.. but I think with all the files together it gets the derivative of a function in pretext normal form.
|
|
|
ADT
Feb 13, 2002 20:52:42 GMT -5
Post by Evotamer on Feb 13, 2002 20:52:42 GMT -5
Sorry if that seems rather vague, I've been living off coffee the last week or so with barely any sleep (So I don't have time to really go through this)... umm the code I showed is pretty easy to trace... minus one or two calls to methods that are elsewhere in the program... But the structure is all there...
|
|