wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Logic problem or invoke method problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Logic problem or invoke method problem" Watch "Logic problem or invoke method problem" New topic
Author

Logic problem or invoke method problem

Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
I'm am trying to invoke boolean method isFirstClassFull and the method needs to detect if first class seats are all filled up checking each component of the array to make sure there is still an * left (* means that particular seat is empty). When firstclass is filled up (X means the seat is taken therefore firstClass array rows 1 and 2 should be full of X's) the boolean method should return printing and then send the user back to choose whether they would like E - Economy or F - First Class. Any Ideas on how to make this program run properly will be helpful and please let me know if anything I wrote is unclear thank you.
Vineet Kakati
Greenhorn

Joined: May 03, 2010
Posts: 21
make following change in function

Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
I tried it but Once the firstClass is full of seats it doesn't run the S.O.P("sorry full"); I think I may have a problem with invoking the method isFirstClassFull in the main method.
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
Anyway, if i got that right, if * means that the seat is empty, then isFirstClassFull should return false not true.
Well i would write your method in the following way: (There's a ; you have to delete at line 284 of your code)



Bye,
Nicola
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

The semil-colon after if (seatNumber[row][col] == '*') is not welcome.


[My Blog]
All roads lead to JavaRanch
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
Sorry I noticed that a little bit after I tried compiling the code but if I switch the false and true around it just starts off at the begining giving me the S.O.P("sorry full"); am I invoking the boolean method the wrong way?
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
If you write the method as in my last post and you change your lines 114 and 115 as follows




your logic is correct. So, if ther's still a problem, find it in another place of your code, not there.




Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

When we read "isFirstClassFull", we logically expect true to be returned when the seats are full. isFirstClassFull ? Yes, they are. No, they aren't. The way you are using it makes it very difficult to understand. You should use Nicola's version and call :

Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
I found the problem, it does work but only if the entire array is full of X's, the problem is first class spands from rows one and two, if rows 1 and two are full the message wont print out because rows 1 through 13 need to be full of X's in order for the message to print out. I need to have the message print out if only first class is full (rows 1 & 2). Any ideas?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Why don't you define what first class is ? For example, having a variable holding the number of rows allocated to the first class.
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
Good idea Would it look something like this?


Yeah I tried that I don't think it works.
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
As Christophe said, you could define a constant representing the rows dedicated to first class seats, let's say FIRST_CLASS_ROWS (in your case FIRST_CLASS_ROWS=2 ) , and use it in your external loop's end condition.

Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
As i wrote the method, the condition is

Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
lol sure since I'm going to make a method for each I might as well specify which rows lol thank you. I think its time to go out for a coffee run I can't believe i didn't think of that!
Oh did you just edit that last message and simplify another statement?
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308


Actually i supposed they were the first two, but if they are not...well it doesn't change much.

Decide which are the two first class rows and at each outer loop you test whether the row is a first class row or not.



Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
Actually I might have messed of some of my code accidently but I think I got it back but now after the results I get and your last comment I'm feeling confused again. Here is what I have and it prints out sorry full right away.
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
Wich are the first class rows?
In the example you posted they are row 0 and 1. Are they the rows you expected?

and change true with false.
Copy and paste my code please


Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
Awesome it works! Yes I believe that i wanted rows 0 and 1 but now i'll need to make one of these for rows 2 through 6 and 7 through 14 (I think those would be the correct row numbers) will it still work the same for the other methods that I want to create for the smoking and non smoking rows if I just copy and paste the isFirstClassFull method and change the names of a few things or will have have to specify that i need rows 2-6 since these are not the first rows that pop up in the array, does that make sense?
Never mind I'll just change
[code]
for (row = 2; row < FIRST_CLASS_ROWS; row++) //<=======this to two istead of starting out at 0
for (col = 0; col < seatNumber[row].length; col++)
if (seatNumber[row][col] == '*')
[/code/
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Dustin, a little advice. Enclose your loops (for loops, while loops...) and if/else blocks with {}. This will avoid you a lot of bad surprise, and increase readability.
For example:
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
Thank you Actually thats the second time I heard about using the curly braces, I will try to start using them more naturally but for now I'll finish up the program first and then add them in. According to the specifications of the assignment I need seperate methods to assign firstClass, smoking, and non-smoking. So I decided to split everything up and I'm not sure what I have to return.
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
The method isSmokingFull() will always return true. And you don't want that to happen.
Remember that you defined SMOKING_ROWS = 6.

Tell me which are the rows left for Smokers. You start from row 7 until which row?

Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
Oh Sure I see what you mean its rows 7 up to 12 since it starts counting with 0, 1, 2, 3, 4.... So I need to change the value from 6 to 12 thank you, but what about the other methods that I just created, was I going about creating them the right way? I'm getting an error at End of assignFirstClass Method its saying that I need to return some sort of value, did I create these extra methods wrong?
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
You say that the method returns a String at line 147



and then you write



at line 164

You did not return a String.

Make a choice: Or your method returns void or you must return a String.
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
I just wasn't sure if that method is supposed to be a public static void or a public static (something), I figured since those methods are supposed to output a message letting the user know there are no more spots left, its a String because it will return "Sorry this class is full" message. Is that proper thinking or should I make it void?
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
If you don't need to return anything (And as i see you don't seem to) mark it void.
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
Perfect! I Just want to thank everyone for all their help! I feel like a learned a lot about Java today!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Logic problem or invoke method problem