Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: HotelOccupancy

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    29
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default HotelOccupancy

    I'm having trouble with the mathematical parts of my code and I can't seem to spot the problem(s). I've messed around but it becomes worse. Here's my code:

    package chapter5;
     
    // Imports Scanner class of utility package.
    import java.util.Scanner;
     
    // HotelOccupancy class.
    public class HotelOccupancy 
    {
    	// Main function.
    	public static void main(String[] args) 
    	{
    		// Variable declarations.
    		int floors, rooms, occupiedRooms, vacantRooms;		// Integer variables for floors, rooms, occupiedRooms & vacantRooms.
    		int totalRooms = 0;									// Set the totalRooms to zero.
    		int totalOccupiedRooms = 0;							// Set the totalOccupiedRooms to zero.
    		int totalVacantRooms = 0;							// Set the totalVacantRooms to zero.
    		double occupancyRate;								// Double variable for occupancyRate.
     
    		// Create scanner object for input.
    		Scanner keyboard = new Scanner(System.in);
     
    		// Ask the user for the number of floors.
    		System.out.print("How Many Floors in the Hotel?: ");
    		floors = keyboard.nextInt();
     
    		// Check conditions.
    		while (floors < 1) 
    		{
    			System.out.print("Invalid. Enter 1 or more: ");
    			floors = keyboard.nextInt();
    		}
     
    		if (floors >= 1)
    		{
    			for (int i = 1; i <= floors; i++)
    			{
    				// Ask the user for the number of rooms on the floor.
    				System.out.print("Enter the Number of Rooms On the Floor: " + i + ": ");
    				rooms = keyboard.nextInt();
     
    				while (rooms < 10)
    				{
    					System.out.print("Invalid. Enter 10 or more: ");
    					rooms = keyboard.nextInt();
    					totalRooms += rooms;
    				}
     
    				if (rooms >= floors)
    				{
    					// Ask the user for the number of occupied rooms.
    					System.out.print("Enter the Number of Rooms Occupied: ");
    					occupiedRooms = keyboard.nextInt();
    					vacantRooms = (rooms - occupiedRooms);
     
    					totalRooms += rooms;					// Sum of the totalRooms.
    					totalOccupiedRooms += occupiedRooms;	// Sum of the occupiedRooms.
    					totalVacantRooms += vacantRooms;		// Sum of the vacantRooms.
    				}
     
    				else
    					// Else display invalid error message.
    					System.out.println("Invalid Number for Rooms.");
    			}
     
    			// Display the total number of totalRooms.
    			System.out.println("\n Total Rooms in the Hotel Are: " + totalRooms);
     
    			// Display the number of totalOccupiedRooms.
    			System.out.println("\n Total Occupied Rooms in the Hotel Are: " + totalOccupiedRooms);
     
    			// Display the number of totalVacantRooms.
    			System.out.println("\n Total Vacant Rooms in the Hotel Are: " + totalVacantRooms);
     
    			// Calculate the occupancyRate using the modulus.
    			occupancyRate = (totalOccupiedRooms % totalRooms);
     
    			// Display the occupancyRate.
    			System.out.println("\n The Hotel Occupancy Rate is: " + occupancyRate);
    		}
     
    		else
    			// Else display invalid error message.
    			System.out.print("Invalid Number for the Floor.");
     
    		// Close the keyboard object.
    		keyboard.close();
    	}
     
    }

    And this is the output my professor is expecting:

    How many floors does the hotel have? 0
      Invalid. Enter 1 or more:2
      How many rooms does floor 1 have? 3
      Invalid. Enter 10 or more:10
      How many occupied rooms does floor 1 have? 3
      How many rooms does floor 2 have? 10
      How many occupied rooms does floor 2 have? 7
     
      Number of rooms: 20
      Occupied rooms: 10
      Vacant rooms: 10
      Occupancy rate: 0.5

    Thanks for any help/tips.

  2. #2
    Junior Member
    Join Date
    Apr 2014
    Posts
    7
    My Mood
    Inspired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    totalRooms is getting increased multiple times per floor in your code. An infinite amount of times if the user repeatedly inputs less than 10 rooms.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    7
    My Mood
    Inspired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Also, rethink your formula for occupancy rate.

  4. #4
    Junior Member
    Join Date
    Jun 2014
    Posts
    29
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: HotelOccupancy

    Okay, I got rid of
     totalRooms += rooms
    in the first while loop. And I changed the
    occupancyRate = (totalOccupiedRooms % totalRooms);
    to
    occupancyRate = ((double)totalOccupiedRooms / totalRooms);
    . And I am now getting the output he was looking for. Thank you for the tip Ozicron.