8 Replies - 7100 Views - Last Post: 22 June 2009 - 03:37 AM Rate Topic: -----

#1 villalandron  Icon User is offline

  • D.I.C Head

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

iterations in hotel occupancy program

Post icon  Posted 20 June 2009 - 12:08 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 java.util.Scanner;


public class Hotel 
{

	public static void main(String[] args) 
		{
		   Scanner keyboard = new Scanner(System.in);
		   
			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
			int rooms; //Number of available rooms in each floor
			int occrooms;  //Number of rooms occupied
			int totalrooms;  //Number of total rooms
			double occrate;  //Occupancy rate
			int vacant; //Number of vacant rooms

			System.out.println("How many floors are in the hotel? ");	 //Get the number of floors in the hotel
			
			 do	//Validate the number entered.
			 	{
				System.out.println("The number of floors " + "must be at least " + MIN_FLOORS);
				floors = keyboard.nextInt();

				}
				
			while (floors < MIN_FLOORS);
			
			System.out.println("How many rooms are on each floor?");	//Get the number of rooms in each floor
		 
			 do	  //Validate the number entered.
			 	{
				System.out.println("The number " + "of rooms must be greater than " + MIN_ROOMS);
				rooms = keyboard.nextInt();

				}
				
			while(rooms < MIN_ROOMS);	//Get the amount of rooms occupied.
			
			System.out.println("How many rooms are occupied?");
			occrooms = keyboard.nextInt();

			totalrooms = (floors * rooms);

			occrate = ((double)occrooms / totalrooms);

			vacant = (totalrooms - occrooms);
			//Display the results
			System.out.println("There " + "are " + totalrooms + " total rooms in the hotel, the number of rooms occupied are " +
					occrooms + ", " + vacant + " rooms are vacant, and the occupancy rate for the hotel is " + occrate + ".");


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




Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: iterations in hotel occupancy program

#2 villalandron  Icon User is offline

  • D.I.C Head

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

Re: iterations in hotel occupancy program

Posted 20 June 2009 - 02:42 PM

View Postvillalandron, on 20 Jun, 2009 - 11:08 AM, said:

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 java.util.Scanner;


public class Hotel 
{

	public static void main(String[] args) 
		{
		   Scanner keyboard = new Scanner(System.in);
		   
			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
			int rooms; //Number of available rooms in each floor
			int occrooms;  //Number of rooms occupied
			int totalrooms;  //Number of total rooms
			double occrate;  //Occupancy rate
			int vacant; //Number of vacant rooms

			System.out.println("How many floors are in the hotel? ");	 //Get the number of floors in the hotel
			
			 do	//Validate the number entered.
			 	{
				System.out.println("The number of floors " + "must be at least " + MIN_FLOORS);
				floors = keyboard.nextInt();

				}
				
			while (floors < MIN_FLOORS);
			
			System.out.println("How many rooms are on each floor?");	//Get the number of rooms in each floor
		 
			 do	  //Validate the number entered.
			 	{
				System.out.println("The number " + "of rooms must be greater than " + MIN_ROOMS);
				rooms = keyboard.nextInt();

				}
				
			while(rooms < MIN_ROOMS);	//Get the amount of rooms occupied.
			
			System.out.println("How many rooms are occupied?");
			occrooms = keyboard.nextInt();

			totalrooms = (floors * rooms);

			occrate = ((double)occrooms / totalrooms);

			vacant = (totalrooms - occrooms);
			//Display the results
			System.out.println("There " + "are " + totalrooms + " total rooms in the hotel, the number of rooms occupied are " +
					occrooms + ", " + vacant + " rooms are vacant, and the occupancy rate for the hotel is " + occrate + ".");


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




Thank you!



Here are the changes I made in order to have the program do iterations. Now, the problem I have is that it doesn't go on proper order. What I mean by that is that when it asks for number of rooms and I enter lets say 4, it asks for how many rooms in floor 4, then floor 1 and so on instead of going from floor 1 to 4. Here's what I have:

import java.util.Scanner;
public class Hotel
{
	 public static void main (String[ ] args)
	 {
		System.out.println("Program results by Julio Villa for CSCI 2911.");
		System.out.println("This program solves Week 4, Chapter 4 assignment 7");
		
			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
			int rooms; //Number of available rooms in each floor
			int occrooms;  //Number of rooms occupied
			int totalrooms;  //Number of total rooms
			double occrate;  //Occupancy rate
			int vacant; //Number of vacant rooms
			int number;
		  {
			   Scanner keyboard = new Scanner(System.in);
	
			   System.out.print("How many floors does the hotel has? ");
			   floors = keyboard.nextInt();

			   for (number = 1; number <= floors; number++)
			   {
			   	System.out.print("How many rooms are on floor " + number++ + "? ");
			   	rooms = keyboard.nextInt();
			   	
			   	System.out.print("How many rooms on floor" + floors++ + " are occupied? ");
			   	occrooms = keyboard.nextInt();
	
				totalrooms = (floors * rooms);
				occrate = ((double)occrooms / totalrooms);
				vacant = (totalrooms - occrooms);
			   
				System.out.print("The occupancy rate is: " + occrate + ".");
			   }
			   	}
			   }
			   
			 }

	

Was This Post Helpful? 0
  • +
  • -

#3 Get.carter()  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 31
  • Joined: 17-June 09

Re: iterations in hotel occupancy program

Posted 21 June 2009 - 07:10 AM

Often when you get assignements like this at first glance they seem a bit head wrecking and its easy to get confused.
A simple pen and paper exercise and simplicity is your friend here.
we only need to obtain 1 variable to start us off which u have.
And we need to obtain a further 2 which seems to be the problem.
Try to simplify your for loop as:


for (int i = 1; i <= floors;i++)
	{
	   System.out.print("How many rooms are on floor " + i + "? ");
		rooms = keyboard.nextInt();
				   
	   System.out.print("How many rooms on floor" + i + " are occupied? ");
	   occRooms = keyboard.nextInt();
		  //
		  //
	 }




You shouldn't normally declare temp variables such as numbers out side your statement block when a simple i is gtg.
You need to add some lines of code to the loop then close it and then do your calculations. Again always try to think of the simplest way to do it.
Btw if you look at your for loop carefully you may spot a problem.

Regards carter,
Was This Post Helpful? 0
  • +
  • -

#4 villalandron  Icon User is offline

  • D.I.C Head

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

Re: iterations in hotel occupancy program

Posted 21 June 2009 - 07:57 AM

View PostGet.carter(), on 21 Jun, 2009 - 06:10 AM, said:

Often when you get assignements like this at first glance they seem a bit head wrecking and its easy to get confused.
A simple pen and paper exercise and simplicity is your friend here.
we only need to obtain 1 variable to start us off which u have.
And we need to obtain a further 2 which seems to be the problem.
Try to simplify your for loop as:


for (int i = 1; i <= floors;i++)
	{
	   System.out.print("How many rooms are on floor " + i + "? ");
		rooms = keyboard.nextInt();
				   
	   System.out.print("How many rooms on floor" + i + " are occupied? ");
	   occRooms = keyboard.nextInt();
		  //
		  //
	 }




You shouldn't normally declare temp variables such as numbers out side your statement block when a simple i is gtg.
You need to add some lines of code to the loop then close it and then do your calculations. Again always try to think of the simplest way to do it.
Btw if you look at your for loop carefully you may spot a problem.

Regards carter,



Just tried that and I got two error messages so far. First one says variable occrooms might have not been initialized and second one says variable occrate might have not been initialized. Here's what I have:

import java.util.Scanner;
public class Hotel
{
	 public static void main (String[ ] args)
	 {

			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
			int rooms; //Number of available rooms in each floor
			int occrooms;  //Number of rooms occupied
			int totalrooms;  //Number of total rooms
			double occrate;  //Occupancy rate
			int vacant; //Number of vacant rooms
			int number;
		  {
			   Scanner keyboard = new Scanner(System.in);
	
			   System.out.print("How many floors does the hotel has? ");
			
				do	//Validate the number entered.
			 	{
				System.out.println("The number of floors must be at least " + MIN_FLOORS);
				floors = keyboard.nextInt();

				}
				 while (floors < MIN_FLOORS);
			
				 for (int i = 1; i <= floors;i++)
	{
	   System.out.print("How many rooms are on floor " + i + "? ");
		rooms = keyboard.nextInt();
				   
	   System.out.print("How many rooms on floor" + i + " are occupied? ");
	   int occRooms = keyboard.nextInt();
		  
		  totalrooms = (floors * rooms);
		  occrate = ((double)occrooms / totalrooms);
		  vacant = (totalrooms - occrooms);
	 }
			   
				System.out.print("The occupancy rate is: " + occrate + ".");
			   }
			   	}
			   }


Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10821
  • View blog
  • Posts: 40,340
  • Joined: 27-December 08

Re: iterations in hotel occupancy program

Posted 21 June 2009 - 08:09 AM

When you declare local variables, you have to initialize them by assigning them an initial value. For example, with Strings, Objects and Primitives.

String x = "";
Object o = null;
int x = 0; //all numbers are initialized to 0
boolean b = false;
char c = '';


Was This Post Helpful? 0
  • +
  • -

#6 villalandron  Icon User is offline

  • D.I.C Head

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

Re: iterations in hotel occupancy program

Posted 21 June 2009 - 08:19 AM

View Postmacosxnerd101, on 21 Jun, 2009 - 07:09 AM, said:

When you declare local variables, you have to initialize them by assigning them an initial value. For example, with Strings, Objects and Primitives.

String x = "";
Object o = null;
int x = 0; //all numbers are initialized to 0
boolean b = false;
char c = '';




Sorry, I'm still a noob. You lost me there.
Was This Post Helpful? 0
  • +
  • -

#7 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: iterations in hotel occupancy program

Posted 21 June 2009 - 08:30 AM

Do you see at the top of your class where you declare all your variables?
This is declaring a variable
final int MIN_FLOORS;


When you give it a value, like you did for this variable, that is called initialising it. e.g.
final int MIN_FLOORS = 1;


So your error is asking you to initialise occrooms. So you need to give it an initial value, problably 0. So change
int occrooms;


to
int occrooms = 0;

Was This Post Helpful? 0
  • +
  • -

#8 villalandron  Icon User is offline

  • D.I.C Head

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

Re: iterations in hotel occupancy program

Posted 21 June 2009 - 08:39 AM

View Postnick2price, on 21 Jun, 2009 - 07:30 AM, said:

Do you see at the top of your class where you declare all your variables?
This is declaring a variable
final int MIN_FLOORS;


When you give it a value, like you did for this variable, that is called initialising it. e.g.
final int MIN_FLOORS = 1;


So your error is asking you to initialise occrooms. So you need to give it an initial value, problably 0. So change
int occrooms;


to
int occrooms = 0;


Excellent. This just cleared many things for me. I am beginning to see the light at the end of the tunnel. The erros are gone! Now, it seems that my calculations are wrong because it is not giving proper calculations for occupancy rate. Here's what I have:

import java.util.Scanner;
public class Hotel
{
	 public static void main (String[ ] args)
	 {

		
			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
			int rooms; //Number of available rooms in each floor
			int occrooms = 0;  //Number of rooms occupied
			int totalrooms;  //Number of total rooms
			double occrate = 0;  //Occupancy rate
			int vacant; //Number of vacant rooms
			int number;
		  {
			   Scanner keyboard = new Scanner(System.in);
	
			   System.out.print("How many floors does the hotel has? ");
			
				do	//Validate the number entered.
			 	{
				System.out.println("The number of floors must be at least " + MIN_FLOORS);
				floors = keyboard.nextInt();

				}
				 while (floors < MIN_FLOORS);
			
				 for (int i = 1; i <= floors;i++)
	{
	   System.out.print("How many rooms are on floor " + i + "? ");
		rooms = keyboard.nextInt();
				   
	   System.out.print("How many rooms on floor " + i + " are occupied? ");
	   int occRooms = keyboard.nextInt();
		  
		  totalrooms = (floors * rooms);
		  occrate = ((double)occrooms / totalrooms);
		  vacant = (totalrooms - occrooms);
	 }
			   
				System.out.print("The occupancy rate is: " + occrate + ".");
			   }
			   	}
			   }


Was This Post Helpful? 0
  • +
  • -

#9 Get.carter()  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 31
  • Joined: 17-June 09

Re: iterations in hotel occupancy program

Posted 22 June 2009 - 03:37 AM

You need to do your calculations once the loops finished not everytime it runs.Take that code out below and replace it with 2 other lines of code. Atm it seems to me that your overwriting your values each time the loop runs. You need to get and keep the data before doing your calculations. Just forget about your calculations for now and think about the purpose of the loop. You only need 2 totals which is rooms and occrate.Once you obtain them you can manipulate them whatever way you choose.


	{
	   System.out.print("How many rooms are on floor " + i + "? ");
		rooms = keyboard.nextInt();
				   
	   System.out.print("How many rooms on floor " + i + " are occupied? ");
	   int occRooms = keyboard.nextInt();
						  \\code here.
						   \\ code here.
					}}}}}close loop here.
		  ***************************
		  totalrooms = (floors * rooms);
		  occrate = ((double)occrooms / totalrooms);
		  vacant = (totalrooms - occrooms);
	*****************************************************
 }
			   
				System.out.print("The occupancy rate is: " + occrate + ".");
			   }
			   	}
			   }


Regards carter.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1