4 Replies - 2507 Views - Last Post: 26 February 2012 - 01:16 PM Rate Topic: -----

#1 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 22-September 11

Hotel Occupancy rate

Posted 25 February 2012 - 04:51 PM

Hello! I am trying to build a hotel occupancy program Write a 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. The equation is: Occupancy rate = number of rooms occupied / total number of rooms. As far as the code is concerned, I think it is good except I don't know how to make it to iterate once for each floor. Here's what I have:

import javax.swing.JOptionPane;  //needed for GUI
import java.text.DecimalFormat; //needed to format the Output

/**
	Lab 3
	I am Divy Tolia and this is Lab Assignment 3 located on page 243 problem number 7.
	The following program will:
	- Ask the user the number of floors in the hotel
	- The number of rooms per floor
	- The number of rooms that are occupied per floor
	- Calculate and Display the number of rooms that are occupied, number that are vancant,number of rooms in the hotel,
	  and the hotel room occupancy rate.

	Created by Divy Tolia.
*/
public class HotelOccupancy
{//Begin class

	public static void main(String[] args)
	{//Begin main method

			String input;                  //To hold the user's input
			final int MIN_FLOORS = 1;     //Minimum amount of floors
			final int MIN_ROOMS = 10;    //Minimum amount of rooms per floor
			int floors,                 //Number of floors in hotel
			    rooms,                 //Number of available rooms in each floor
			    occrooms,             //Number of rooms occupied
			    totalrooms,          //Number of total rooms
			    vacant;             //Number of vacant rooms
			double occrate;        //Occupancy rate

DecimalFormat formatter = new DecimalFormat("#,##0.00");// Rounds the answer to two decimal places

//Get the number of floors in the hotel
			JOptionPane.showMessageDialog(null, "How many floors are in the hotel? ","Number of Floors?",JOptionPane.INFORMATION_MESSAGE);


			//Validate the number entered.
			 do{
				input = JOptionPane.showInputDialog("The number of floors " +
						"must be at least " + MIN_FLOORS);
				floors = Integer.parseInt(input);

			  }while (floors < MIN_FLOORS);

			//Get the number of rooms in each floor
			JOptionPane.showMessageDialog(null, "How many rooms are on each floor?","Number of Rooms per Floor",JOptionPane.INFORMATION_MESSAGE);


			//Validate the number entered.
			 do{
				input = JOptionPane.showInputDialog("The number " +
						"of rooms must be greater than " + MIN_ROOMS);
				rooms = Integer.parseInt(input);

			}while(rooms < MIN_ROOMS);

			//Get the amount of rooms occupied.
			String value;
            value =
				   JOptionPane.showInputDialog(null,"How many rooms are occupied?","Rooms Occupied per Floor",JOptionPane.INFORMATION_MESSAGE);

			occrooms = Integer.parseInt(input);

			totalrooms = (floors * rooms);              //Calculate the Number of rooms

			occrate = ((double)occrooms / totalrooms); //Calculate OccupancyRate

			vacant = (totalrooms - occrooms);         //Calculate the Number of rooms Vacant

			//Display the results
			JOptionPane.showMessageDialog(null, "\tThere are " + totalrooms + "\t\nin the hotel." +
					"\t\nThe number of rooms occupied are " +occrooms+
					"\tThe Number of rooms that are vacant are"+vacant + "\t\nrooms." +
					"\t\nThe occupancy rate for the hotel is " + formatter.format(occrate),"Results",JOptionPane.INFORMATION_MESSAGE);


			//End the program.
			System.exit(0);

	}//End main method
}//End class




Is This A Good Question/Topic? 0
  • +

Replies To: Hotel Occupancy rate

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4426
  • View blog
  • Posts: 12,293
  • Joined: 18-April 07

Re: Hotel Occupancy rate

Posted 25 February 2012 - 05:27 PM

A for loop based on the number of floors perhaps?

for (int i = 0; i < floors; i++) {
   // Ask for number of rooms on the floor
   // Ask how many are occupied

   // Add both of those values to a running total for the end
}



This will loop you through each floor and during each iteration you will ask for the number of rooms (add that to a sum of all rooms) and ask for how many are occupied (add those to a sum of all occupied rooms).

Once the loop is finished, you will have a sum of all rooms and all occupied rooms. :)
Was This Post Helpful? 0
  • +
  • -

#3 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 22-September 11

Re: Hotel Occupancy rate

Posted 25 February 2012 - 05:52 PM

View PostMartyr2, on 25 February 2012 - 05:27 PM, said:

A for loop based on the number of floors perhaps?

for (int i = 0; i < floors; i++) {
   // Ask for number of rooms on the floor
   // Ask how many are occupied

   // Add both of those values to a running total for the end
}



This will loop you through each floor and during each iteration you will ask for the number of rooms (add that to a sum of all rooms) and ask for how many are occupied (add those to a sum of all occupied rooms).

Once the loop is finished, you will have a sum of all rooms and all occupied rooms. :)


okay but whats the i? and how do i define that because then i would have to change my entire code
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4426
  • View blog
  • Posts: 12,293
  • Joined: 18-April 07

Re: Hotel Occupancy rate

Posted 25 February 2012 - 06:15 PM

"i" is a simple variable. It is defined and used just for the loop. Don't over think this. Most programmers use "i" just to be short for "index". You can name it whatever you want... again it is just a variable. And no, you don't have to rewrite everything. You just have to put in the code you want to execute for each floor. Again, mainly asking for how many rooms and how many are filled.

Hint: lines 46 to 63 in your code above is what is probably going to be what is inside this loop. Just instead of asking how many rooms are on all the floors, you should be asking "How many rooms are on THIS floor?"

:)
Was This Post Helpful? 0
  • +
  • -

#5 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 22-September 11

Re: Hotel Occupancy rate

Posted 26 February 2012 - 01:16 PM

View PostMartyr2, on 25 February 2012 - 06:15 PM, said:

"i" is a simple variable. It is defined and used just for the loop. Don't over think this. Most programmers use "i" just to be short for "index". You can name it whatever you want... again it is just a variable. And no, you don't have to rewrite everything. You just have to put in the code you want to execute for each floor. Again, mainly asking for how many rooms and how many are filled.

Hint: lines 46 to 63 in your code above is what is probably going to be what is inside this loop. Just instead of asking how many rooms are on all the floors, you should be asking "How many rooms are on THIS floor?"

:)


Okay i did the thing you asked me to and the code compiles properly however the code does not run properly like it asks me the number of rooms on floor 0 and then prints out the result totally incorrectly

How do i solve that?
Here's the code:
import javax.swing.JOptionPane;  //needed for GUI
import java.text.DecimalFormat; //needed to format the Output

/**
	Lab 3
	I am Divy Tolia and this is Lab Assignment 3 located on page 243 problem number 7.
	The following program will:
	- Ask the user the number of floors in the hotel
	- The number of rooms per floor
	- The number of rooms that are occupied per floor
	- Calculate and Display the number of rooms that are occupied, number that are vancant,number of rooms in the hotel,
	  and the hotel room occupancy rate.

	Created by Divy Tolia.
*/
public class HotelOccupancy
{//Begin class

	public static void main(String[] args)
	{//Begin main method

			String input;                  //To hold the user's input
			final int MIN_FLOORS = 1;     //Minimum amount of floors
			final int MIN_ROOMS = 10;    //Minimum amount of rooms per floor
			int floors,                 //Number of floors in hotel
			    rooms,                 //Number of available rooms in each floor
			    occrooms,             //Number of rooms occupied
			    totalrooms,          //Number of total rooms
			    vacant;             //Number of vacant rooms
			double occrate;        //Occupancy rate

DecimalFormat formatter = new DecimalFormat("#,##0.00");// Rounds the answer to two decimal places

//Get the number of floors in the hotel
			JOptionPane.showMessageDialog(null, "How many floors are in the hotel? ","Number of Floors?",JOptionPane.INFORMATION_MESSAGE);


			//Validate the number entered.
			 do{
				input = JOptionPane.showInputDialog("The number of floors " +
						"must be at least " + MIN_FLOORS);
				floors = Integer.parseInt(input);

			  }while (floors < MIN_FLOORS);

			for (int i = 0; i < floors; i++)
			{
			//Get the number of rooms in each floor
			JOptionPane.showMessageDialog(null,"How many rooms are on floor " + i + "? ","Number of Rooms per Floor",JOptionPane.INFORMATION_MESSAGE);

			//Validate the number entered.
			 do{
				input = JOptionPane.showInputDialog("The number " +
						"of rooms must be greater than " + MIN_ROOMS);
				rooms = Integer.parseInt(input);

			   }while(rooms < MIN_ROOMS);

			//Get the amount of rooms occupied.
			String value;
            value =
				   JOptionPane.showInputDialog(null,"How many rooms on this floor " + i + " are occupied? ","Rooms Occupied per Floor",JOptionPane.INFORMATION_MESSAGE);

			occrooms = Integer.parseInt(input);

			totalrooms = (floors * rooms);              //Calculate the Number of rooms

			occrate = ((double)occrooms / totalrooms); //Calculate OccupancyRate

			vacant = (totalrooms - occrooms);         //Calculate the Number of rooms Vacant


			//Display the results
			JOptionPane.showMessageDialog(null, "\tThere are " + totalrooms + "\t\nin the hotel." +
					"\t\nThe number of rooms occupied are " +occrooms+
					"\tThe Number of rooms that are vacant are"+vacant + "\t\nrooms." +
					"\t\nThe occupancy rate for the hotel is " + formatter.format(occrate),"Results",JOptionPane.INFORMATION_MESSAGE);


			//End the program.
			System.exit(0);
	 	}
	}//End main method
}//End class



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1