3 Replies - 255 Views - Last Post: 02 October 2012 - 10:02 PM Rate Topic: -----

#1 apurdion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-October 12

Error in calculation

Posted 02 October 2012 - 02:44 PM

The question was:
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.

I am having trouble getting the Occupancy Rate calculation to work.

import javax.swing.JOptionPane;
import java.text.DecimalFormat;

/**
   This program calculates a running total.
*/

public class Occupancy
{
   public static void main(String[] args)
   {
		int floors;		//Number of floors
		int rooms;		//Number of rooms
		int occupied;		//Occupied rooms
		
		int totalOccupied;	//Accumulator
		int totalRooms;		//Accumulator
		
		String input;        	// To hold the user's input
		int vacant;					//To hold the number of vacant rooms
		
		final int MIN_FLOORS = 1;     //Minimum amount of floors
		final int MIN_ROOMS = 10;     //Minimum amount of rooms per floor
		
		double occRate;            //Occupancy Rate
		
		int totalFloors;	//accumulator
		
		// Get the number of floors.
      input = JOptionPane.showInputDialog("How many floors are in the hotel?");
		
      floors = Integer.parseInt(input);
		
		 //Validate the number of floors entered.
      while (floors < MIN_FLOORS)
		{
			input =JOptionPane.showInputDialog("You have Entered a value less than the 1"+
														"How many Floors are in the Hotel? ");
		}
		// Set the accumulator to 0.
      totalRooms = 0;
		totalOccupied = 0;
		occRate = 0;
		
		DecimalFormat formatter = new DecimalFormat("#,##0.0");
		
			
		//Get the number of rooms per floor and calculate a running total
		for (int count = 1; count <= floors; count++)
		{
			input = JOptionPane.showInputDialog("Enter the number of rooms on floor #" + count + ":");
			
			rooms = Integer.parseInt(input);
			
			while (rooms < 10)
			{
				input = JOptionPane.showInputDialog("You have entered the number of rooms less than 10 per floor! Please enter the number of floors:");
			}
			totalRooms += rooms;		// Add rooms to totalRooms.
			
			input = JOptionPane.showInputDialog("Enter the number of rooms that are OCCUPIED on floor #" + count + ":");
			
			occupied = Integer.parseInt(input);
			totalOccupied += occupied;		// Add occupied to totalOccupied.
		}
		
				
		
		//Calculate the number of vacant rooms.
		vacant = totalRooms - totalOccupied;
		
		//Calculate the Occupancy rate.
		
		occRate = (double)(totalOccupied / totalRooms);

		
		// Display the results.
      JOptionPane.showMessageDialog(null, "There are " + totalRooms + " rooms in the hotel.  " +
                                    
												 "Out of those, " + totalOccupied + " are occupied, " +
												  + vacant + " are vacant, &" +
                                    " the occupancy rate is " + occRate + "%" );
		System.out.println(occRate);
		System.exit(0);
	
   }
}


This post has been edited by modi123_1: 02 October 2012 - 02:53 PM
Reason for edit:: fixed title - this isn't a challenge.


Is This A Good Question/Topic? 0
  • +

Replies To: Error in calculation

#2 Kakerergodt  Icon User is offline

  • D.I.C Head

Reputation: 87
  • View blog
  • Posts: 201
  • Joined: 01-May 12

Re: Error in calculation

Posted 02 October 2012 - 02:51 PM

You need to cast before dividing:
occRate = ((double)totalOccupied/totalRooms);

..should do the trick.
Was This Post Helpful? 0
  • +
  • -

#3 ihatesegfault  Icon User is offline

  • New D.I.C Head

Reputation: 17
  • View blog
  • Posts: 47
  • Joined: 24-September 12

Re: Error in calculation

Posted 02 October 2012 - 06:17 PM

also, in your validation loops, you only read in a String to input. You don't re-parse it. So variables rooms and floors don't change after the initial input. so if a user enters even one non-valid value, they get stuck in the validation loop.
Was This Post Helpful? 0
  • +
  • -

#4 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: Error in calculation

Posted 02 October 2012 - 10:02 PM

You also might not want "occupied" to be > "rooms"
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1