• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do you think this way in Java

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}


 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
A magnificient life is loaded with tough challenges. En garde tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic