7 Replies - 2756 Views - Last Post: 08 March 2009 - 07:46 PM Rate Topic: -----

#1 villalandron  Icon User is offline

  • D.I.C Head

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

Hotel Occupancy Program and For-loop problems

Post icon  Posted 08 March 2009 - 09:37 AM

Hello everyone! I am doing this hotel occupancy program that calculates the occupancy rate for each floor of a hotel. The program should start by asking for the number of floors in the hotel. 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 of the hotel.

Input Validation: Do not accept a number less than 1 for the number of floors. Do not accept a number less than
10 for the number of rooms on a floor.
Occupancy rate = number of rooms occupied total number of rooms

As far as I can see from this program, I believe I need to use a for loop. The problem is that I really don't know how to do it or in other words what exactly to put in the for loop. Here is what I have so far:

import java.util.Scanner;

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

			   Scanner keyboard = new Scanner(System.in); 

				
		System.out.print("How many floors does the hotel has? ");
				double floors;
				input = keyboard.nextDouble();

				System.out.print("How many rooms are on each floor? ");
				double rooms;
				input = keyboard.nextDouble();
				
				System.out.print("How many rooms are occupied? ");
				double occupied;
				input = keyboard.nextDouble();

				
	}
}



Thank you for your help!

Is This A Good Question/Topic? 0
  • +

Replies To: Hotel Occupancy Program and For-loop problems

#2 bmcginnis  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 130
  • Joined: 21-February 09

Re: Hotel Occupancy Program and For-loop problems

Posted 08 March 2009 - 10:03 AM

I'm not sure that I understand what you want to loop.
Was This Post Helpful? 0
  • +
  • -

#3 villalandron  Icon User is offline

  • D.I.C Head

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

Re: Hotel Occupancy Program and For-loop problems

Posted 08 March 2009 - 10:14 AM

The problem that I have is that I should make a loop that iterates after each floor that is entered. After each iteration, it should ask the user the number of rooms in each floor and how many are occupied. I feel like an idiot because I usually have everything pretty much done because I get to do many different exercises so that I can understand each subject. This chapter doesn't give any training exercises and I have never done these kind of loops so I don't understand them well. As far as I have read in my book, it seems I need to use a for loop. What I don't know is if the user inputs should be put inside the loops or if the loops should be inputted after the user inputs, and what kind of expressions will actually make the loop iterate once after each floor and ask the stuff entered above. Sorry if my terminology was not that well put. I am still a noob in java, and thank you for your help!
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,885
  • Joined: 06-March 08

Re: Hotel Occupancy Program and For-loop problems

Posted 08 March 2009 - 04:52 PM

Number of floors and number of rooms must be int.... you cannot have 3.1416 floors right ?
You need a Floor class to handle for evry floor the number of rooms utilised or not (another int)
The only double variable should be the percentage

import java.util.Scanner;

public class HotelOccupancy
{
	Floor[] floor;
	
	// constructor
	HotelOccupancy() {
		Scanner keyboard = new Scanner(System.in); 

		System.out.print("How many floors does the hotel has? ");
		// number of floor surely an it... you cannot have 3.14159 floor
		int nbFloor = keyboard.nextInt();

		// creating the floor array
		floor = new Floor[nbFloor];
		
		// loop to get room by floor
		for(int i = 0; i < nbFloor; i++) {
			System.out.print("Nb rooms on floor " + (i+1) + ": ");
			int nbRoom = keyboard.nextInt();
			System.out.print("Nb rooms occupied on that floor: ");
			int nbOccupied = keyboard.nextInt();
			// create floor object
			floor[i] = new Floor(nbRoom, nbOccupied);
		}
	}
	
	// display the occupancy
	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;
		}
	}
}



Was This Post Helpful? 0
  • +
  • -

#5 villalandron  Icon User is offline

  • D.I.C Head

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

Re: Hotel Occupancy Program and For-loop problems

Posted 08 March 2009 - 06:35 PM

For some reason, I typed on notepad exactly what you did here and I get 9 errors. Here is what I have:

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

			   Floor[] floor;

			   HotelOccupancy()  // error says expected.
		  {
			   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()  // error says illegal start of expression, and expected.
		  {
			  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)	// error says illegal start of expression, and .class expected

		  {

			  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;
		  }
		  }
}




Since I am a noob, I don't know what these errors mean. Did I mispelled something? Any help in this matter is appreciated. Thank you!
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,885
  • Joined: 06-March 08

Re: Hotel Occupancy Program and For-loop problems

Posted 08 March 2009 - 07:24 PM

You have the import for sScanner missing:

import java.util.Scanner;

and you have a duplicate main() method

delete these 2 lines at the beginning

	public static void main(String[ ] args)
	  {


Was This Post Helpful? 0
  • +
  • -

#7 villalandron  Icon User is offline

  • D.I.C Head

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

Re: Hotel Occupancy Program and For-loop problems

Posted 08 March 2009 - 07:37 PM

Thank you! That eliminated the majority of my errors. I only get one error. Here's what I have:

import java.util.Scanner;
public class HotelOccupancy1
{
	
			   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;
		  }
		  }
}



Thank you for your help!
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,885
  • Joined: 06-March 08

Re: Hotel Occupancy Program and For-loop problems

Posted 08 March 2009 - 07:46 PM

You renamed your main class from HotelOcupancy to HotelOccupancy1

if it is what you want:
- you will have to rename your HotelOccupancy.java file to HotelOccupancy1.java
- change all references to HotelOcupancy to HotelOccupancy1

better yo put everything back to HotelOccupancy
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1