wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes How do you think this way in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do you think this way in Java" Watch "How do you think this way in Java" New topic
Author

How do you think this way in Java

Bruce Schmid
Greenhorn

Joined: Apr 06, 2009
Posts: 5
I have two example of code. The first has the following classes. ReservationMain, Menu, Controller, Console and Reservation. In the ReservationMain it has the following
Controller controller = new Controller(); - here we instatiated a class but then in the next line of code we do this.
Menu gui=new Menu(controller) - what is this exactly doing? What is the thought process of this?

the second example has two classes DeckOfCards and Cards

DeckOfCards has private Card card;
deckOfCards = card.create;

in Card we have the following
public Card(Rank rank, Suit suit)
{
this.rank = rank;
this.suit = suit;

}




public ArrayList<Card> create()
{
for (Suit suit :Suit.values())
{
for (Rank rank : Rank.values())
{
deck.add (new Card(rank, suit));

}
}
return deck;
}


Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Bruce Schmid wrote:I have two example of code. The first has the following classes. ReservationMain, Menu, Controller, Console and Reservation. In the ReservationMain it has the following
Controller controller = new Controller(); - here we instatiated a class but then in the next line of code we do this.
Menu gui=new Menu(controller) - what is this exactly doing? What is the thought process of this?


Menu requires a Controller instance to work. so the first thing to do is to create an instance of the Controller. The second thing is to create a new instance of the Menu, and give it a reference to the Controller you just made.

I don't know what your question on the second example was.


Steve
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Bruce Schmid wrote:Controller controller = new Controller(); - here we instatiated a class but then in the next line of code we do this.
Menu gui=new Menu(controller) - what is this exactly doing? What is the thought process of this?


The Menu object needed an instance of a Controller object in order to instantiate itself. In other words, this Menu object could not be created without the knowledge of what it will be Controlling. The thought process for this is that the same Menu class can be passed different Controllers yet will be able to be reused instead of having a seperate class for each. Also, the coder could have done this:



But then Controller will not be a local Object. I don't know what your question was on the second one. I hope that helps somewhat. Look up Polymorphism for many more examples of why this is a very important concept in OOP.


SCJA
~Currently preparing for SCJP6
 
 
subject: How do you think this way in Java
 
Similar Threads
Compare an Object to an Enum list
Deck of Cards- using another classes variables
Doubts in this sample demo class in Java 1.6 Tutorial
can't split card shuffle method into a separate class
Arraylist insights