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
• Tim Cooke
• Devaka Cooray
• Ron McLeod
• Jeanne Boyarsky
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Piet Souris
• Carey Brown
• Tim Holloway
Bartenders:
• Martijn Verburg
• Frits Walraven
• Himai Minh

# Logic help for my program

Ranch Hand
Posts: 289
2
• Number of slices to send:
Optional 'thank-you' note:
Doing a building with a lift exercise

I have the maximum floor done (between 3 and 35 floors)
and basements between -1 and -5

so the user could enter 10 floors and 2 basements, which is a total of 13 floors

so ideally it would be:

10
3
2
1
0
-1
-2

Does this need to be in an array? or is there another way of doing this that you can think of?

sorry, the plan is to have the user enter how many floors up or down he goes and report the lifts position after each move.

Sheriff
Posts: 17323
300
• Number of slices to send:
Optional 'thank-you' note:
Specification is unclear.

“So ideally it would be...” — what does “it” refer to here?

Let me throw an example back at you, so you understand what you just tried to do to us.

====
So, I’m supposed to write a program. Ideally, it would be:

“Bob”, “Sally”, “Jim”, “Andy”

Should this be in an array or is there some other way to do it?
====

wayne brandon
Ranch Hand
Posts: 289
2
• Number of slices to send:
Optional 'thank-you' note:
If I have floors, -2 to +10, what will be the best way to manage location of my lift among the floors using commands like U3 or D2 for up 3 floors or down 2 floors?

Marshal
Posts: 76862
366
• Number of slices to send:
Optional 'thank-you' note:
I suggest you explain what U3 means; then look at the floor numbers; then you will probably have some idea of several different ways to implement that instruction.

lowercase baba
Posts: 13082
67
• Number of slices to send:
Optional 'thank-you' note:
2) think through the problem.  pretend you have to explain it to a young child.  How would you tell them what to do?
3) think about what you need to know.  things like: Where is the elevator car right now?  What is the top floor?  What is the bottom floor?  what do i do if the input takes me beyond the range?  Do I really need to store every floor in an array, or do i just need to know the max and min. floors, along with where I am?

Once you've thought through all this, THEN start thinking about java.

I don't see the need for an array at all.  If i know the car.currentFloor == 7, building.maxFloor == 10, and building.minFloor == -2, I can test the input to see if it takes me beyond the acceptable range.  If so, give an error, if not, update the currentFloor.

Junilu Lacar
Sheriff
Posts: 17323
300
• Number of slices to send:
Optional 'thank-you' note:
So I guess “it” really should go something like this:

Is that right?

Also, would there really be a floor 0? I have never seen building elevators with a floor 0 button.

fred rosenberger
lowercase baba
Posts: 13082
67
• Number of slices to send:
Optional 'thank-you' note:

Junilu Lacar wrote:I have never seen building elevators with a floor 0 button.

I've never see a floor 0, but I have see "Ground" or "Lobby", followed by  1, 2, 3...  then I've seen "LL1", "LL2", etc. for "lower level" floors below ground.

wayne brandon
Ranch Hand
Posts: 289
2
• Number of slices to send:
Optional 'thank-you' note:
Yes you got it, maybe i can use a switch to translate the numbers to basement 1 2
penthouse, ground floor etc

Sheriff
Posts: 8523
615
• Number of slices to send:
Optional 'thank-you' note:

Junilu Lacar wrote:Also, would there really be a floor 0? I have never seen building elevators with a floor 0 button.

Just for your curiosity, actually there are. Quite a lot in UK to be honest. Some elevators have G (meaning ground floor), some elevators write 0 (same meaning as G).

Liutauras Vilda
Sheriff
Posts: 8523
615
• Number of slices to send:
Optional 'thank-you' note:

wayne brandon wrote:I have the maximum floor done (between 3 and 35 floors)

So what's about 1 and 2, don't exist?

wayne brandon wrote:so the user could enter 10 floors and 2 basements, which is a total of 13 floors

How do you get 13 out of 10 and 2?

Sheriff
Posts: 27451
88
• Number of slices to send:
Optional 'thank-you' note:

Liutauras Vilda wrote:

wayne brandon wrote:so the user could enter 10 floors and 2 basements, which is a total of 13 floors

How do you get 13 out of 10 and 2?

Could be 10 above ground level and 2 below ground level... plenty of scope for off-by-one errors in this program.

Junilu Lacar
Sheriff
Posts: 17323
300
• Number of slices to send:
Optional 'thank-you' note:

Liutauras Vilda wrote:

wayne brandon wrote:so the user could enter 10 floors and 2 basements, which is a total of 13 floors

How do you get 13 out of 10 and 2?

Liutauras Vilda wrote:

Junilu Lacar wrote:Also, would there really be a floor 0? I have never seen building elevators with a floor 0 button.

Just for your curiosity, actually there are. Quite a lot in UK to be honest. Some elevators have G (meaning ground floor), some elevators write 0 (same meaning as G).

Have you so quickly forgotten about floor 0 then? 10 above ground, 0, and 2 below ground. That’s 13 floors.

Liutauras Vilda
Sheriff
Posts: 8523
615
• Number of slices to send:
Optional 'thank-you' note:

Junilu Lacar wrote:Have you so quickly forgotten about floor 0 then? 10 above ground, 0,

I didn't. But saying 10 floors sounds misleading, I'd have expected to see:

"So the user could enter 11 floors (since 0 is a floor too) and 2 basements, which is a total of 13 floors". Otherwise one could think that you couldn't get into the floor 0 using an elevator.

It adds to complexity (to me) to treat 0/ground floor as kind of different floor than any other.

Liutauras Vilda
Sheriff
Posts: 8523
615
• Number of slices to send:
Optional 'thank-you' note:
On the other hand, thinking from other angle, user must be at some floor out of these 13 floors as a starting point, so really at any given point in time user has only 12 choices.

Junilu Lacar
Sheriff
Posts: 17323
300
• Number of slices to send:
Optional 'thank-you' note:

Liutauras Vilda wrote:On the other hand, thinking from other angle, user must be at some floor out of these 13 floors as a starting point, so really at any given point in time user has only 12 choices.

Well, user might be able to enter U0 or D0. Although I don’t know why I’d do that if it were me. Where are we headed with this though? Seems like we’re on the train to Overthinking Town.

The problem is really simple. You have X number of floors, negative floors are subterranean, positive numbers are above ground, and 0 is ground level. You enter U for up or D for down and a number to indicate how many floors above or below your current floor.

wayne brandon
Ranch Hand
Posts: 289
2
• Number of slices to send:
Optional 'thank-you' note:
On a separate note my U3 D2 etc, should that be a method in my KeyboardUtility, or would i do the logic in the program to handle U2 and D4 checking etc, or does it matter?

Junilu Lacar
Sheriff
Posts: 17323
300
• Number of slices to send:
Optional 'thank-you' note:

wayne brandon wrote:On a separate note my U3 D2 etc, should that be a method in my KeyboardUtility, or would i do the logic in the program to handle U2 and D4 checking etc, or does it matter?

Definitely not a method in your KeyboardUtility

Liutauras Vilda
Sheriff
Posts: 8523
615
• Number of slices to send:
Optional 'thank-you' note:
I merged your stuff with the following thread. I hope that is okay by you.

wayne brandon
Ranch Hand
Posts: 289
2
• Number of slices to send:
Optional 'thank-you' note:
If I wanted to have the user enter "U" or "D" as up and down followed by a number, am I taking a string and taking the first index as a string or a character?

Campbell Ritchie
Marshal
Posts: 76862
366
• Number of slices to send:
Optional 'thank-you' note:
If youi want "U"/"D" then String; if you want 'U'/'D' then char. You can use both in a switch‑case statement. You can use both in an if‑else but the syntax will of course be different. Don't allow your if‑else to accept anything other than those two prefixes (possibly case‑insensitively).

Why have you asked that in this forum? Isn't it a continuation of your previous questions?

wayne brandon
Ranch Hand
Posts: 289
2
• Number of slices to send:
Optional 'thank-you' note:
Hi Campbell

yes it is, i prefer to keep things in separate posts, but if its a problem here i can keep it all under the same post, no problem at all.

for my "U" "D" floor numbers to move am i better off using regular expressions or is that overkill?

Campbell Ritchie
Marshal
Posts: 76862
366
• Number of slices to send:
Optional 'thank-you' note:
I would have thought regexes are overkill.

wayne brandon
Ranch Hand
Posts: 289
2
• Number of slices to send:
Optional 'thank-you' note:
Am I on the right track here?

Campbell Ritchie
Marshal
Posts: 76862
366
• Number of slices to send:
Optional 'thank-you' note:
Any use of @SuppressWarnings makes me very suspicious. I'm afraid you aren't even close. Where does that “unused” double come from? Why have you got a double at all? It would be much simpler if you simply used the first letter. I am pretty sure that you aren't going to get valueOf() to work like that.

Don't try to work out what you are doing in code. Decide before you write any code, and write down in words of one syllable how you are going to use the input. Once you have done that, you should find it easy to convert those instructions into code.

wayne brandon
Ranch Hand
Posts: 289
2
• Number of slices to send:
Optional 'thank-you' note:
ok so:

Input needs to be no less than 2 and no more than 3 spaces, ie only D1 or D12 or U2 U13 can be entered.
first letter must be a "d/D" or a "u/U".
First character must be "d/D" or "u/U" and character 2 and 3 must be a number.
if enter is pressed after first number ie D3 ,that is fine.

have i missed anything?

wayne brandon
Ranch Hand
Posts: 289
2
• Number of slices to send:
Optional 'thank-you' note:
Does this look more promising, i think that the first thing that should eliminate a command attempt ie "D2" should be length, if the command entered is less than 2 and greater than 3 characters, it should fail immediately.
Working on "U" and "D" first characters now.

Rancher
Posts: 259
13
• 1
• Number of slices to send:
Optional 'thank-you' note:
it sounds like you want to get a single String, then chop it up into 2 parts???
the first part will contain a 'U' or 'D'?
the second part will contain numbers?

to chop up the String, you can get a substring of the String:

if you need to convert the strings into an integer or a char, you can then do that as a next step.
the reason to convert everything to uppercase, is because now it's easier to deal with your input.

i don't understand what this has to do with certifications, this seems like a beginner question and should go in the beginner forum.
if you don't yet know about getting substrings, you are very far away from taking a certificate test.

wayne brandon
Ranch Hand
Posts: 289
2
• Number of slices to send:
Optional 'thank-you' note:
Thanks for the help S, yes it is a beginner question, the change section on the new post doesnt work, i normally click the first post i see to get the new topic button up then change it to beginning java, but i notice it doesnt work, i will click into beginning java in future, surprised nobody has moved it.

Paul Clapham
Sheriff
Posts: 27451
88
• Number of slices to send:
Optional 'thank-you' note:

wayne brandon wrote: i will click into beginning java in future, surprised nobody has moved it.

Thanks for the heads-up... it's moved now.

wayne brandon
Ranch Hand
Posts: 289
2
• Number of slices to send:
Optional 'thank-you' note:
I'm starting to think that this:

Should be done with a loop?

Campbell Ritchie
Marshal
Posts: 76862
366
• Number of slices to send:
Optional 'thank-you' note:
Actually you aren't far out.

wayne brandon wrote:. . . Should be done with a loop?

That is what you say if you are trying to work out how to do it before you know what you are going to do. I think you do know what you are trying to do, but don't be so timid about it. Your variable names are poor: firstCharacter isn't a good name for a String. If anything goes wrong, be assertive:-

S Fox
Rancher
Posts: 259
13
• Number of slices to send:
Optional 'thank-you' note:
if you are making an elevator, you need to have an elevator object.
that way you can do object oriented things like this...

Elevator.goUpOneFloor();
Elevator.goDownOneFloor();
Elevator.getCurrentFloor();
Elevator.requestedAtFloor(10);

if you want to be completely realistic, you have to realize that people can press multiple buttons inside the elevator, and people on various floors can press buttons too. this means the elevator will have a whole bunch of floors that it's being requested to travel to at the same time. this means you'll need either an array or a list containing all those requests. and you'll also need to figure out in what order these requests will be dealt with... do you make the elevator travel to the floor requested first, or to the nearest floor first? how would a real elevator handle this situation...

your current problem is with handling the input. you might want to make a class that represents elevator buttons and parse your input there. that way you don't have a bunch of things cluttering up the main method, and you can keep things more object oriented. something like this?

FloorButton.requestElevatorAt(2); // a person on floor #2 is requesting the elevator to come there
ElevatorButton.requestGoToFloor(2); // a person inside the elevator requests to go to floor #2

S Fox
Rancher
Posts: 259
13
• 1
• Number of slices to send:
Optional 'thank-you' note:
i should also mention, having the user submit as input 'U' or 'D' and an integer of how many floors to travel breaks the paradigm of how a real elevator would work.
the input should be modeled after a button press. i want to go to floor10, i press that button, and the elevator is who figures out how many floors to travel and if it's up or down.

Paul Clapham
Sheriff
Posts: 27451
88
• Number of slices to send:
Optional 'thank-you' note:
Yeah, that's the trouble with beginner programming assignments. To make them realistic you have to consider all of that kind of issue and then you've got Scope creep and the end result is way too complicated for the poor beginner. On the other hand if they are totally unrealistic then the poor beginner runs into Cognitive dissonance which makes it harder for them to deal with the issues at hand.

S Fox
Rancher
Posts: 259
13
• Number of slices to send:
Optional 'thank-you' note:
one of my first homeworks for java was very similar to this, and it was so complicated that nearly all the students didn't understand and failed to complete the assignment. but it's not that hard.

here's a example of how you could begin to make a button class. you can initialize each object using the constructor. the Elevator object will need to have an array of these button objects.

here's how you would use the buttons

Junilu Lacar
Sheriff
Posts: 17323
300
• Number of slices to send:
Optional 'thank-you' note:

Campbell Ritchie wrote:If anything goes wrong, be assertive:-

Not the place or time to be assertive, I'm afraid. Imagine apps that crash and burn at the slightest mistake a user makes on the input. That's bad.

Runtime exceptions like IllegalArgumentException are meant to signal a programmer error. These should be resolved during testing. For example, if a method was called with a negative number when the API stipulates that it should only be called with a 0 or positive number, then that would be a programmer error that deserves to have an IllegalArgumentException thrown at their face.

However, user input or input coming from any external source should be validated but the program should expect and handle invalid data gracefully nonetheless, without throwing a RuntimeException.

Campbell Ritchie
Marshal
Posts: 76862
366
• Number of slices to send:
Optional 'thank-you' note:

Junilu Lacar wrote:. . . Not the place or time to be assertive, I'm afraid. . . .

Not the place or time to pussy‑foot around allowing iffy input or to do the validation, I'm afraid. This current method cannot handle input validation, so it either needs a guarantee that input will be in the correct format or to throw an exception. Look at things like the Color() constructor. Agree it is a lot better to have a method to take input like this:-...and something similar for numbers. Unless you can guarantee that sort of input for the up/down method, it will have to throw an exception. If you can make that sort of guarantee, then any sort of validation, exception‑throwing, etc. becomes totally redundant. In order to make a guarantee of correct format stick, the up‑down method would need access restricted so no code can send it incorrect input.

It may be possible to write a regex for validating input, maybe something like "[DU]\\d{1,2}". It might be better validation to use while (!(input = ...).matches("[DU]\\d{1,2}"))..., but that will allow you to go down 99 floors all at once. The regex allows you to match U12 or D99 in one stage, but I am by no means sure it is correct, so it needs testing.

Junilu Lacar
Sheriff
Posts: 17323
300
• Number of slices to send:
Optional 'thank-you' note:

Campbell Ritchie wrote:Look at things like the Color() constructor.

That example totally supports what I said should happen so I don’t know if you’re disagreeing with me or helping me bolster my argument.

Calling the Color constructor with values that are not in the correct range should throw an IllegalArgumentException. The programmer is responsible for ensuring that the values being passed in are in the correct range. That means that if the values are coming from the user, the programmer should have the kind of checks you show here:

Agree it is a lot better to have a method to take input like this:-

If the programmer fails to do this kind of user input validation, then invalid input from the users will get thrown right back at their faces as an IllegalArgumentException. That’s like a parent blaming a child for getting hurt when the parent wasn’t paying attention to what the child was doing. It’s not the child’s fault, really, it’s the parent’s.

A user-friendly program will shield users from these kinds of exceptions, not throw them right in their faces. Imagine going to the ATM and trying to withdraw more money than what you have in your account and then the ATM display showing a Java exception stack trace with “IllegalArgumentException” as the root cause.

Junilu Lacar
Sheriff
Posts: 17323
300
• Number of slices to send:
Optional 'thank-you' note:

wayne brandon wrote:If I wanted to have the user enter "U" or "D" as up and down followed by a number, am I taking a string and taking the first index as a string or a character?

Since user input is read in as a String, I’d use String operations:

That’s really all the code you need to ensure that the first letter is either U, u, D, or d.