This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array questions and classes

 
Mich Robinson
Ranch Hand
Posts: 260
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to build a hotel booking app. I can code but not in Java (or anything else OO). I have a few questions :

1) If I create an array is it possible to just keep adding to it or do I always have to have a max number of records?
String Booking[] = new String[NUM_DAYS];

2) Would I keep track of what record I'm up to with a booking_cnt variable or is there a better way?

3) If booking was a class with variables that pointed to a customer class etc how would I point the booking instance at the customer instance.

4) Presume I just then move any functions (methods) that work on a booking into the booking class etc.

5) Presume "this" will be available whenever I create a new class - can I then use the "this" to fire off mouse trigger events or does this have to be in a frame etc? ie with
addMouseListener(this);
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) If I create an array is it possible to just keep adding to it or do I always have to have a max number of records?
String Booking[] = new String[NUM_DAYS];


The size of an array in Java is fixed at the time of declaration. You'd be better off using java.util.ArrayList, it offers the level of abstraction that you seem to be looking for.

2) Would I keep track of what record I'm up to with a booking_cnt variable or is there a better way?


See 1

3) If booking was a class with variables that pointed to a customer class etc how would I point the booking instance at the customer instance.

You would use the Booking constructor or a setter method.



4) Presume I just then move any functions (methods) that work on a booking into the booking class etc.
There's not really a question there, but that's a good idea.

5) Presume "this" will be available whenever I create a new class - can I then use the "this" to fire off mouse trigger events or does this have to be in a frame etc? ie with
addMouseListener(this);
I really am not sure what you mean by this. The concept of a mouseListener is tightly coupled to the concept of some type of GUI.
 
Henry Wong
author
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) If I create an array is it possible to just keep adding to it or do I always have to have a max number of records?
String Booking[] = new String[NUM_DAYS];


Arrays in Java are fixed size, so you can't "keep adding". Take a look at the collections classes -- particularly classes that implement java.util.List for object types that you can "keep adding" to.

[EDIT: just started answering the first question, and already beaten to the answers... ]
Henry
 
Mich Robinson
Ranch Hand
Posts: 260
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help guys - it's kind of fun learning about new concepts and then trying to immediately code them.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic