• Post Reply Bookmark Topic Watch Topic
  • New Topic

While loop with a sentinel value. (Meeting room capacity and fire law regulation)  RSS feed

 
Andrey Moore
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone.

Here is my h/w assignment and I am stuck on some steps.


Program determines whether a meeting room is in violation of fire law regulations regarding the maximum room capacity.
* The program reads in the maximum room capacity and the number of people to attend the meeting.
* The program prints out either a message that the meeting is legal or a message that the meeting is illegal.
* Also display the number of people that need to be excluded or how many additional people can attend.
* Finally display the total number of legal and illegal meetings that were processed.
* Use a sentinel to terminate the input.
*
* Use the following data to test your program in your submission:
*
* First run:
* 40 40
* 40 25
* 40 45
* -1
* Second run:
* -1


First question: Is it possible to not use an if in the loop? Prof. says assignment requires a sentinel value that needs two read statements, one above the while, and second inside the while but as the last statements in the loop body. Otherwise, I will lose points.

Second question: When I entering the number of attendees more than room capacity I am getting negative number in the result when print it. I can't figure it out what to do with formula to get positive in both cases when meeting is legal and illegal.

Any help.

Thank you.



 
Tim Cooke
Marshal
Posts: 4044
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Andrey, welcome to the Ranch!

You probably missed our rules about cross-posting on your way in. We allow it but please BeForthrightWhenCrossPostingToOtherSites and tell us where you have cross posted to.

Cross-post: http://www.javaprogrammingforums.com/loops-control-statements/45792-while-loop-sentinel-value-meeting-room-capacity-fire-law-regulation.html
 
Andrey Moore
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Tim.

I totally missed this. Sorry about that.
Next time i will post a link.
Just trying to find the right answer ASAP.
 
Andrey Moore
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have solved a problem from my second questions.

But the first one is still open.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

Please show us how you solved the first problem.
 
Andrey Moore
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still workong on first.

I solved a second problem
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For your first problem, here is the algo for the solution:

You have a while loop, which is a pretest structure: you have a test before you to through the loop, so you have to setup a conditional before you enter the loop. To do this you have a pre-read before the while loop and then another regular read in the body of the while loop to set the conditional for the cycle through the while loop.

 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrey Moore wrote:I am still workong on first.

I solved a second problem
I meant second. Sorry for the mistake.

What is the || operator doing on line 21?
 
Andrey Moore
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To fix second problem I multiply negative result on -1 when print.

System.out.println("Meeting illegal: " + people*(-1) + " people need to be excluded");

That gives me a positive number in the result.

" What is the || operator doing on line 21? "

I was trying to assign a sentinel value for both inputs. When you enter sentinel for one of those it shoud leave the loop. But it is not working. Is is still asking to enter sentinel for bouth to exit.
 
Andrey Moore
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I will try.


Les Morgan wrote:For your first problem, here is the algo for the solution:

You have a while loop, which is a pretest structure: you have a test before you to through the loop, so you have to setup a conditional before you enter the loop. To do this you have a pre-read before the while loop and then another regular read in the body of the while loop to set the conditional for the cycle through the while loop.

 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrey Moore wrote:. . . When you enter sentinel for one of those it shoud leave the loop. But it is not working. Is is still asking to enter sentinel for bouth to exit.
That is because you have used ||
It is not a case of the loop exiting; the loop always exits. It is a case of the loop starting again. (That is what Les Morgan caalled a pre‑test.) If you use || then you are telling it to repeat unless both sentinels are not equal to −1. You probably wanted && instead. If you want to exit the loop consider a do loop, which has its test at the end; if the condition evaluates to false the loop exits.
Also consider whether you want −1 as your sentinel. Consider using non‑negative numbers for normal repetition. Then you would check whether your sentinels are >= 0.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!