2 Replies - 5691 Views - Last Post: 11 May 2009 - 12:02 AM Rate Topic: -----

#1 villalandron  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 24-September 08

One error on Hotel Occupancy Program

Post icon  Posted 10 May 2009 - 07:10 PM

Hello! I am creating this program that calculates the occupancy rate for each floor of a hotel. The program should start by asking for the number of floors that the hotel has. A loop should then iterate once for each floor. During each iteration, the loop should ask the user for the number of rooms on the floor and the number of them that are occupied. After all the iterations, the program should display the number of rooms the hotel has, the number of them that are occupied, the number that are vacant, and the occupancy rate for the hotel.
Input Validation: Do not accept a number less than one for the number of floors. Do not accept a number less than 10 for the number of rooms on a floor. I am getting one error code. Here is what I got!

import java.util.Scanner;
public class HotelOccupancy
{
   
			   Floor[] floor;

			   HotelOccupancy()  // the error appears here and it says invalid method declaration, return type required.
		  {
			   Scanner keyboard = new Scanner(System.in);

			   System.out.print("How many floors does the hotel has? ");
			   int nbFloor = keyboard.nextInt();

			   floor = new Floor[nbFloor];

			   for(int i = 0; i < nbFloor; i++)
		  {
			   
			   System.out.print("Number of rooms on floor " + (i+1) + ": ");
			   int nbRoom = keyboard.nextInt();

			   System.out.print("Number of rooms occupied on that floor: ");
			   int nbOccupied = keyboard.nextInt();

			   floor[i] = new Floor(nbRoom, nbOccupied);
	}
	}

void displayOccupancy()
		  {
			  for(int i = 0; i < floor.length; i++)
			  System.out.println("Floor " + (i+1) + " is used at: " + floor[i].used() + "%");
		  }

		  public static void main(String[ ] args)

		  {

			  HotelOccupancy ho = new HotelOccupancy();
			  ho.displayOccupancy();

		  }

		  class Floor
		  {

			  int nbRoom;
			  int nbOccupied;

			  Floor(int nbRoom, int nbOccupied)
		  {
			  this.nbRoom = nbRoom;
			  this.nbOccupied = nbOccupied;
		  }

		  double used()
		  {
			  return ((double) nbOccupied / (double) nbRoom) * 100.0;
		  }
		  }
}



Any help is greatly appreciated. Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: One error on Hotel Occupancy Program

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: One error on Hotel Occupancy Program

Posted 10 May 2009 - 07:20 PM

Ran it without problem
What error do you get ?
One thing for sure... for all question you ask, you expect an int as answer. Your program will crash if user inputs 1.2345 or "the hell"
Was This Post Helpful? 0
  • +
  • -

#3 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,619
  • Joined: 15-May 08

Re: One error on Hotel Occupancy Program

Posted 11 May 2009 - 12:02 AM

Appears to be working - just built and ran it. Perhaps be really specific with what causes a crash or error, of course IOExceptions are unhandled so as pbl said if you enter something the program does not expect it is going to spit it right out
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1