Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how to do this java coding?

+4 votes
asked Mar 15, 2021 by Iqbal Syazwan Sharil (160 points)

Write a class program based on the information given below. This class will simulate the operation of an elevator. The elevator can travel between 1st and 15th floors of the building it is situated in.

Class Name:

  • Elevator


  • private int currentFloor;
  • private static final int MAXFLOOR = 15;

ONE Constructor:

  • to set the value of currentFloor to 1. This method does not have any parameters.

ONE Member method:

  • void request (int newFloor)

 -This method will be used to alter the position of the elevator. This method consists of an if-else statement having three parts:

If an incorrect floor number (i.e. newFloor) is given, no action is taken;

if a floor above  the current position is selected, the elevator moves up;

and if a floor below  the current position is selected, the elevator moves down.

For the movement up and down, the method can use while loop to update the position one floor at a time and report the elevator’s movement using System.out.println().

For example, if an object of Elevator calls the request method as below:



then, the following output will be displayed for the above call:

Starting at floor 1

     Going up -  now at floor 2

     Going up -  now at floor 3

     Going up -  now at floor 4

     Going up -  now at floor 5

     Going up -  now at floor 6

Stopping at floor 6

Starting at floor 6

     Going down -  now at floor 5

     Going down -  now at floor 4

     Going down -  now at floor 3

Stopping at floor 3

3 Answers

+5 votes
answered Mar 18, 2021 by Peter Minarik (55,660 points)
This looks like a homework assignment of some sort.

The best course of action is that you give your best try to solve it. If you get stuck on something, you can share your code and ask specific questions.

Good luck!
commented Mar 18, 2021 by KKN (1,110 points)
Why the dislike? He has a point :P
0 votes
answered Mar 31 by MK NIKHIL (160 points)
class Elevator
    private int currentFloor;
    private static final int MAXFLOOR = 15;

        currentFloor = 1;

    void request(int newFloor)
        if(!(newFloor >= 1 && newFloor <= 15))
        else if(newFloor > currentFloor)
            System.out.println("Starting at floor "+currentFloor++);
            while(currentFloor <= newFloor)
                System.out.println("\tGoing up - now at floor "+currentFloor++);
            System.out.println("Stopping at floor "+currentFloor);
        else //if(newFloor > currentFloor)
            System.out.println("Starting at floor "+currentFloor--);
            while(currentFloor>= newFloor)
                System.out.println("\tGoing down - now at floor "+currentFloor--);
            System.out.println("Stopping at floor "+currentFloor);

public class Main
public static void main(String[] args) {
Elevator e1 = new Elevator();
commented Mar 31 by Peter Minarik (55,660 points)
The requirements say: "If an incorrect floor number (i.e. newFloor) is given, no action is taken;"

However, when an incorrect floor is detected, you exit the application. That's not quite what the requirement was.

I don't think your code handles quite correctly when the destination is the current floor. I would categorize it as an invalid destination and would not print anything at all.

Other than this, it looks all right to me. :) Nice job.
0 votes
answered Apr 7 by userdotexe (1,330 points)

I'm pretty sure when you load up this site, it says not to ask how to complete an entire assignment (such as (but not limited to) homework) but anyway

(If you haven't already) learn some small things about java, like conditionals, IO, objects, constructors, classes, functions, and maybe loops. These will most likely help you solve this problem.

Good luck.


Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.