hi norm,
but the whole codes has been posted few days ago. i remembered you asked for the whole codes :).
the for loop is just the addition of the codes.
hi norm,
but the whole codes has been posted few days ago. i remembered you asked for the whole codes :).
the for loop is just the addition of the codes.
check all the buttons pressed and when it finds it, go to that floor.
How does the code you posted change the movement to "go to that floor"?
There are no comments in the code so I have no idea what this statement is supposed to do:
if(app.control.bp == false) {
What is the Timer (tm) for?
hi norm,
sry bout that. this is the codes with explanation. thanks. sry keep trouble you.
//this is to find the current floor pressed
curFloor = currentFloor(current);
//loop if the elevator needs to be stopped for a while
if ((app.control.bp[curFloor] == true) && (yco % height == 0)) {
//i become current floor
int i = curFloor;
//for loop to check all the floors below the current floor
for(i = curFloor; i >= 0; i--) {
//if all the floors below not being pressed
if(app.control.bp[i] == false) {
//move up (this is part i am not sure)
}
}
//display pick passenger at which floor currently
String line0 = ("Elevator is picking up passenger at floor " +
app.control.b[curFloor].getActionCommand().substring(1));
app.state.setText(line0);
//stop the timer
stop();
//start new timer to give time to load passengers
tm = new Timer(this.drag, this);
tm.setInitialDelay(this.delay);
tm.setDelay(this.drag);
//start the timer
start();
//clear all the state
app.control.bp[curFloor] = false;
app.control.b[curFloor].setBackground(Color.cyan);
}
Another thing, where are the stop and start methods defined?
Nothing can be done without the whole program with definitions for all the stuff you use in the short code you've posted.
Can you explain what problem the code you posted is supposed to solve?
Have you designed what the code is supposed to do
Or are you writing code without any design?
hi norm,
Another thing, where are the stop and start methods defined?
- timer is not the issue here. but if you think it leads to the problemm, it defined under the elevator class
Nothing can be done without the whole program with definitions for all the stuff you use in the short code you've posted.
-do you want me to post all the codes here?
Can you explain what problem the code you posted is supposed to solve?
-
#
//if all the floors below not being pressed
#
if(app.control.bp[i] == false) {
#
//move up (this is part i am not sure) <----- This one
#
}
the problem is i tried to make it go up instead go down because after i press floor 5, it will check that i never press 1, 2, 3, 4 and its supposed to go up.instead..its still go down.
Have you designed what the code is supposed to do
Or are you writing code without any design?
- Yes. Absolutely. Without design. the codes wont even start.
Thanks.
hi norm,
the method performed event:
public void actionPerformed(ActionEvent e) {
//register listener for slider
app.getSlider().addChangeListener(this);
//change moving direction when hits the top and bottom
if (yco == topy) {
up = false;
String line1 = "Elevator is going down";
app.state.setText(line1);
}
if (yco == bottomy) {
up = true;
String line2 = "Elevator is going up";
app.state.setText(line2);
}
//adjust Y coordinate to simulate elevator movement (up/down)
if (up) {
up();
} else {
down();
}
//this is to find the current floor pressed
curFloor = currentFloor(current);
//loop if the elevator needs to be stopped for a while
if ((app.control.bp[curFloor] == true) && (yco % height == 0)) {
//i become current floor
int i = curFloor;
//for loop to check all the floors below the current floor
for(i = curFloor; i >= 0; i--) {
//if all the floors below not being pressed
if(app.control.bp[i] == false) {
yco -= dy0;
//move up (this is part i am not sure)
}
else {
yco += dy0;
}
}
//display pick passenger at which floor currently
String line0 = ("Elevator is picking up passenger at floor " +
app.control.b[curFloor].getActionCommand().substring(1));
app.state.setText(line0);
//stop the timer
stop();
//start new timer to give time to load passengers
tm = new Timer(this.drag, this);
tm.setInitialDelay(this.delay);
tm.setDelay(this.drag);
//start the timer
start();
//clear all the state
app.control.bp[curFloor] = false;
app.control.b[curFloor].setBackground(Color.cyan);
}
//repaint the panel
repaint();
//update the state of elevator
}
thanks.the codes with explanation is done with "//".
i am able to give further explanation about the codes and design if necessary.thanks.
Does the code compile and execute?
Are there any problems with compiling or execution?
hi norm,
compile no problem.
its just i could not get elevator detect that someone pressed another button. its supposed to go to that button instead going to lowest floor first and then go to that floor.
keep thinking of the logic but still cant get it right.
Please explain your logic. Until that is correct, coding is useless.
my logic is using for loop to check all the check all floors that is not being pressed and then move up the elevator to the button that is pressed only.
my logic is using for loop to
1)check all the check all floors that is not being pressed
2)then move up the elevator to the button that is pressed only.
Why do you only move up? What if you are at floor 5 and button 3 is pressed?
currently i just want to take it step by step. because now even i cant move the elevator up. :)
I'm not sure if I understand.
If the elevator is moving up and stops and someone presses a button on a higher floor the elevator is to move up to that floor. Your old program works that way. No change would be necessary.
Your statement of the logic did NOT say if the direction that the elevator was moving was to be tested or not.
yeah.my old program works that way. it will go all the way up and all the way down and stop when can find pressed button.
so the improvement here is to make the elevator not moving up or down all the way but move according to the button pressed. if no buttons pressed, just move up and down.
meaning i must test the moving elevator?thanks.
You need to test which direction the elevator is moving and change it if the button press is in the other direction.
hi,
i managed to make the elevator notice that there is another button being pressed and it will go up to go that floor.
but there is some issues here. i think its because of my for loop..its only checked for all non press buttons, but floors checked is pressed, it will not make the elevator move to that floor.
Again what is your logic to "make the elevator move to that floor"
There are four cases:
The elevator is above the floor and moving up
The elevator is above the floor and moving down
The elevator is below the floor and moving down
The elevator is below the floor and moving up
How do you detect each of these situations and
what do you change to make the elevator move where you want it to go?
Don't forget that you are allowed to press more than 1 button. And that an elevator with no buttons pressed either waits where it is, or returns to the ground floor and waits - it doesn't just "move up and down".
hi all,
i think i solved that already..
now the issue that i have is to convert the codes to run as applications and applet.
the problem is the applet can run. but it does not seem to accept the codes..and now cant even see the println to test for any variables.
and i followed the standard of creating applicationa and applet.
The print outs for applets go to the browser's java console.
aha..its my mistake. thanks norm and james.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.