5 Replies - 607 Views - Last Post: 03 October 2012 - 08:02 PM Rate Topic: -----

#1 itsjay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-October 12

Hotel Occupancy rate not working

Posted 03 October 2012 - 07:06 PM

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'm getting a wrong occupancy rate. the 'rooms' and 'occupied' keeps resetting after each loop.

i'm very new to this so please help!

Thanks
/*

this program determines the occupancy rate of a hotel

*/
import java.util.Scanner;


	public class HotelOccupancy
	{
		
		public static void main ( String [] args) 
		{

Scanner keyboard = new Scanner(System.in);


	int floors = 0;					//number of floors
	int rooms =0;					//number of rooms
	int occupied = 0;				//number of occupied rooms
	final int MIN_FLOOR = 1;		//minimum number of floors
	final int MIN_ROOMS = 10;		// minimum number of rooms
	double occupancy = 0;			//occupancy rate
	int total;					//total number of rooms
	int vacant = 0;					//number of vacant rooms
	
	//number of floors
	System.out.println("Enter the number of floors");

	
	//to validate the number of floors
	do
	{
		floors = keyboard.nextInt();
		System.out.println("Floors have to be more than 1");
	}
	while (floors <= MIN_FLOOR);
	
	//to insert a blank line
	System.out.println();
	
		for (int i = 0; i < floors; ++i)
	{
		// number of rooms on each floor
		System.out.println("Please enter the number of rooms on floor " + i);
			rooms = keyboard.nextInt();
		
		//room occupied per floor
		System.out.println("How many rooms on floor " + i + " are occupied");
			occupied = keyboard.nextInt();
			
		
			
	}
	
	//calculations
	total = (floors * rooms);
	vacant = (total - occupied);
	occupancy = ((double)occupied / total);	
	
	
	
	
			
	
}	
}



Is This A Good Question/Topic? 0
  • +

Replies To: Hotel Occupancy rate not working

#2 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Hotel Occupancy rate not working

Posted 03 October 2012 - 07:24 PM

You need to use an array to store the values for each floor.
Was This Post Helpful? 0
  • +
  • -

#3 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 176
  • Joined: 23-September 12

Re: Hotel Occupancy rate not working

Posted 03 October 2012 - 07:25 PM

You could change the code within your loop to something like:

rooms += keyboard.nextInt();

and

occupied += keyboard.nextInt();

The += is equivalent to:

rooms = rooms + keyboard.nextInt();

You would do this because you want the total number of rooms and the total number of occupied rooms. Your calculations section would also have to change accordingly so that total is simply equal to rooms. In fact, you could probably just get rid of 'total' altogether. Hope this helps.
Was This Post Helpful? 2
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Hotel Occupancy rate not working

Posted 03 October 2012 - 07:26 PM

This formula is false
total = (floors * rooms);
the number of room can be different on every floor so you can't just multiply the number of room on the last floor with the number of floors
Was This Post Helpful? 0
  • +
  • -

#5 itsjay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-October 12

Re: Hotel Occupancy rate not working

Posted 03 October 2012 - 07:43 PM

Thanks Nathan that did the trick!!
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Hotel Occupancy rate not working

Posted 03 October 2012 - 08:02 PM

You do not have to use an array at all, keep it simple !!! first programming law
Just a for() loop that cumulates at each iteration/floor:
total number of rooms, number of room occupied

after the loop you'll have totat number of room in the hotel (who cares on how they are distributed between the floors) total number of occupied room
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1