java help!!!

AKIRA

Tank
Joined
Feb 6, 2006
Messages
3,000
Reaction score
2
ok, i have a mid term tomorrow and im really stuck on wtf abstract data types are.

in my notes it says "many abstractions can't be completely defined and implemented in a single file. If an interface is used for the specification and a class for the implementation, there are at least 2 files"

wtf are the implementation and specification?? What is the difference between an interface and a class? Isn't all programming done in classes? How would you write code that isn't in a class...where would you code the interface if it's different then the class?


fack im so screwed i hate programming!!!
 
Sorry, but Tetsuo's face followed by ???????? is just funny.

And I don't know java.
 
not having programmed java yet, feel free to correct me anyone.

the general definition of an abstract data type is a set of values and the set of operations that can be performed on them. So, some ADT's could be integers, floating point numbers, lists, matrices, tree's, etc...

For instance, a floating point number you can add, subtract, multiply, etc... all the details of how that's done with calculating the mantissa, finding the exponent, checking for errors and all that is hidden away. This is the power of abstraction, you don't have to know how it works, you just have to know how to use the interface which in this example is the +, -, * operators.

Back to your specific questions, I think when they differentiate between the specification and the implementation they're talking about the function prototypes where the functions are declared (for c++, this would be in the .h files). The implementation is where the code that is run when the function is called is (in the .cpp files for c++).
 
Okay, interfaces are basically classes in which none of the methods are implemented. They are basically just headers for methods and data types, with no data manipulation at all.

Any class can implement (or specify) an interface simply by saying Class x implements interface y

Once a class implements an interface, it is obligated to at least define all of the methods in the interface. It can then call and use all of the methods in the interface in any manner that it likes, as long as it maintains the original headers to the methods in the interface.

Example:

Interface X
{
public void method(int z);
}

Class Q implements X
{
private int f;

public void method(int z)
{f=z;}

}
 
Okay, interfaces are basically classes in which none of the methods are implemented. They are basically just headers for methods and data types, with no data manipulation at all.

Any class can implement (or specify) an interface simply by saying Class x implements interface y

Once a class implements an interface, it is obligated to at least define all of the methods in the interface. It can then call and use all of the methods in the interface in any manner that it likes, as long as it maintains the original headers to the methods in the interface.

Example:

Interface X
{
public void method(int z);
}

Class Q implements X
{
private int f;

public void method(int z)
{f=z;}

}

awesome...i think i get it.

Ok let's see if i do lol..so a sample question they gave us is:

As part of the package called Currency, design an interface called CanDollar which specifies an abstraction of Canadian currency values. CanDollar objects are immutable. It's possible to convert a currency value into an integer value of dollars. It's possible to determine the number of dollars and the number of cents in a CanDollar value. It's also possible to determine the total number of cents in a CanDollar value. Candian dollar amounts may be added or subtracted. Canadian dollar amounts may also be multiplied by a factor (e.g. an interest rate). Adding or subtracting a CanDollar object results in another CanDollar object. Multiplying a CanDollar by a double results in another CanDollar. Finally, it's possible to determine if 2 CanDollar objects are equal.


lol long ass question.. so..am i supposed to go about it like:

package Currency;

public interface CanDollar{

make up a whole bunch of methods

}
??

im not sure, in an interface are you just calling methods?... like in the example it says it's possible to convert a currency value into an integer value so would i make up a method like: public int convertCanDollar(); ??

lol **** im confused..thanks for the help so far i think you're helping!! :D
 
Back
Top