Hi all,
I wonder if you can help me with this.
I am looking at a fairly long (well at least for me) java program, about 700 lines. Basically it's a very very basic simulation of an ATM that can run on a computer.
Now, in all the classes I have declared so far, I have invariably used variables of primitive type and sometimes reference-type, the latter usually declared in the
java file that contains the main method in. This time I have come across this situation:
Take a few classes:

public class ATM{
    ...
} 
public class Screen{
    ...
}

public class Keypad{
    ...
}

etc..
In class ATM we declare a few variables of primitive type and also a few reference-type variables, like so:

public class ATM{
    private int account;
    private double amount;
    private Screen screen;
    private Keypad keypad;  
} 

These reference-type declaration confuse me a bit. I know the reason why these have been declared (ATM needs to exchange information with Screen, Keypad etc) but I don't understand the mechanism. Does anybody have a short working example (not 700 lines :-)) that I can look at so I understand this correctly before delving into the ATM program?
thanks

It's not clear what it is you don't understand... I suspect you already know the answer but are looking for a problem that isn't really there?
Maybe this will help?...

When you set up a new ATM it will need a Screen abd a Keypad, eg

public ATM() { // constructor
   screen = new Screen();
   keypad = new KeyPad();
   ...

then later in the code you will be able to do stuff like (just made this up)

   int pin = keypad.getPin();
   ...
   screen.displayBalance();

Does that help? If not maybe you can clarify exectly what's worrying you.

HI thanks. Well, I think what worries me is the fact that in the class ATM I am declaring a variable of type Screen and Keypad. That's what's essentially troubling me and confusing me a bit.

Because you want your ATM to be able to do things like

    int pin = keypad.getPin();
    or
    screen.displayBalance();

in the ATM class, you need references to its Keypad ans Screen objects. Otherwize the ATM won't have any link to its screen/keypad and it won't be able to use them.
It really is as simple as that.

ah ok, well yes it is quite simple actually.

But surely, I can declare int pin = getPin(); inside the Keypad class, rather than inside the ATM class. Now, I appreciate what you say about the ATM not being able to access its keypad and screen, but couldn't those classes be completely independent and still be able to access ATM? I mean, I am not trying to be annoying, I am just trying to understand this.

I guess I am a bit confused because in all the (simple) programs I have coded so far I have never come across this, and I had never needed to "interweave" classes this way.

Its a hierarchy. At the bottom are simple components like keypad, screen, cash dispenser, mainframe access. Each knows how to do it's own limited set of functions. None of them needs to know about the others, or even know about the ATM.
Above these is the "master" ATM object, that owns one screen, one keypad etc and orchestrates the whole thing to produce a useful result. It doesn't need to know how any of those components work, it just knows what functions they can provide (their public methods). eg Here's part of the code for ATM's doCashWithdrawal() method

int amountRequired = keypad.getAmount();
boolean isOk = mainframeAccess.validateBalanceAvailable(amountRequired);
if (isOK) {
   cashDispenser.dispense(requestedAmount);
   screen.display("Take your money");
} else
   screen.display("Request refused");
}

The goal of all this is to divide the code into pieces that are. as far as possible, independent of each other, and thus easier to write, read, and maintain. Here'a a good article on the subject:
http://msdn.microsoft.com/en-us/magazine/cc947917.aspx

thanks JamesCherrill, I think I understand. I had a look at your link, I will try to bear those advices in mind! :-)
cheers

HI chaps,
sorry to reopen this, but I really fell like I have to.
I have gone through the ATM progam and now most of the things have become clearer (thanks also to JamesCherrill's explanation). I realized that what had confused me wasn't the syntax (which is in fact the same as anything else
and really easy to understand as you have pointed out) but it was that so far I had used only 2 classes in my programs: 1 with all the variables and methods and the class that contains the main method.
In this case instead, the author uses more than just two classes, in fact each component of the ATM is a class and performs actions adn therefore if a class A wants to use class B's methods then I have to first create a variable of
class B, then instantiate the B object and use the method in B to perform an action in class A.

That's right.
Were you just looking for confirmation, or is there a new question lurking in there somewhere?

No sorry, I was just confirming :-)! thanks a lot for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.