6 Replies - 1074 Views - Last Post: 04 November 2007 - 10:05 PM Rate Topic: -----

#1 Hope1981  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-November 07

java

Posted 04 November 2007 - 03:27 PM

THis program runs perfectly however I am having hard time trying to figure out why my occupancy rate calcualtion is not working since when I run the program for occupancy rate I get 0.0
what is wrong with it I have no idea and by staring at the program I am not getting any lcuk
pls help
thanks

Program by: Hope

FileName: Hope_10

Program Description: Hotel Occupancy

Problem Statement: 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. Program should not accept a number
less than one for the number of floors, and a number less than 10 for the number
of rooms on a floor



import java.util.Scanner;
public class Hope_10
{
	 public static void main (String[ ] args)
	 { 
	 	//Scanner Object 	
		  Scanner keyboard = new Scanner(System.in);		
	   
	   //Declaring variables
		int Floors=0;
		int Rooms;
		int Occupied;
		double Occupancy_Rate=0;
		int Count;
		int Total_Rooms=0;
		int Total_Occupied=0;
		int Rooms_Vacant=0;
		
	System.out.println("How many floors are in the hotel? ");
		Floors = keyboard.nextInt(); 
		System.out.println();//Blank space	
		
		//Validate the Input for floors 
			   while (Floors<1)
			  {
			System.out.println("Number of floor must be greater than zero!");
			System.out.println();//Blank Space
			System.out.println("How many floors are in the hotel? ");
			Floors = keyboard.nextInt();
			System.out.println();//Blank Space   
		}	   
	   for (Count=1;Count<Floors+1;Count++)
		{
			System.out.println("How many rooms are there on floor "+Count+"? "); 
			Rooms = keyboard.nextInt(); 
			System.out.println();//Blank Space
			
			//Validate input of rooms - 10 or more
			while (Rooms<10)
			{
				System.out.println("Number of rooms must be greater than ten!");
				System.out.println();//Blank Space
				System.out.println("How many rooms are on floor "+Count+"? "); 
				Rooms = keyboard.nextInt();			  
			}	   		 	   	
			
			System.out.println();//Blank Space
			System.out.print("How many rooms on floor "+Count+" are occupied? ");
			Occupied=keyboard.nextInt();
			
			Total_Rooms+=Rooms;
			Total_Occupied+=Occupied;
		}
		//All floors counted
		//Calculation for the occupancy rate
		Occupancy_Rate = (Total_Occupied / Total_Rooms)*100;
		//Calculation for vacant
		Rooms_Vacant = Total_Rooms-Total_Occupied;
		   	
		System.out.println();//Blank Space
		System.out.println("There are "+ Total_Rooms+" rooms, "+Total_Occupied+" of them are occupied");
		System.out.println("There are "+Rooms_Vacant+ " vacant, and occupancy rate is "+Occupancy_Rate+ "%.");
	 }  
}
~edit: code tags PB

This post has been edited by jayman9: 04 November 2007 - 10:02 PM


Is This A Good Question/Topic? 0
  • +

Replies To: java

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3872
  • View blog
  • Posts: 11,405
  • Joined: 18-April 07

Re: java

Posted 04 November 2007 - 06:50 PM

It is just how you are doing the calculation. Remember when you are trying to calculate a double make sure that all the operands are doubles as well. Especially in the case of integer division like you are trying to do here. Remember 1 / 3 is not .33333 it is zero when you are using integer. So you can cast them to double datatypes before using them and the result will be double.

// Calculate a double by casting our integers to doubles
Occupancy_Rate = ((double)Total_Occupied / (double)Total_Rooms)*100.0;



Then to format your answer into a nice decimal form you could do this...

DecimalFormat form = new DecimalFormat("###.0#");
			   
System.out.println();//Blank Space
System.out.println("There are "+ Total_Rooms+" rooms, "+Total_Occupied+" of them are occupied");
System.out.println("There are "+Rooms_Vacant+ " vacant, and occupancy rate is "+ form.format(Occupancy_Rate) + "%.");



Be sure to also import the DecimalFormat object at the top using the line java.text.DecimalFormat;

Enjoy!

"At DIC we be hotel occupancy calculating coding ninjas!" :snap:
Was This Post Helpful? 0
  • +
  • -

#3 dontKnowJava  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 221
  • Joined: 29-September 07

Re: java

Posted 04 November 2007 - 06:54 PM

View PostHope1981, on 4 Nov, 2007 - 03:27 PM, said:

THis program runs perfectly however I am having hard time trying to figure out why my occupancy rate calcualtion is not working since when I run the program for occupancy rate I get 0.0
what is wrong with it I have no idea and by staring at the program I am not getting any lcuk
pls help
thanks

Program by: Hope

FileName: Hope_10

Program Description: Hotel Occupancy

Problem Statement: 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. Program should not accept a number
less than one for the number of floors, and a number less than 10 for the number
of rooms on a floor



import java.util.Scanner;
public class Hope_10
{
	 public static void main (String[ ] args)
	 { 
	 	//Scanner Object 	
		  Scanner keyboard = new Scanner(System.in);		
	   
	   //Declaring variables
		int Floors=0;
		int Rooms;
		int Occupied;
		double Occupancy_Rate=0;
		int Count;
		int Total_Rooms=0;
		int Total_Occupied=0;
		int Rooms_Vacant=0;
		
	System.out.println("How many floors are in the hotel? ");
		Floors = keyboard.nextInt(); 
		System.out.println();//Blank space	
		
		//Validate the Input for floors 
			   while (Floors<1)
			  {
			System.out.println("Number of floor must be greater than zero!");
			System.out.println();//Blank Space
			System.out.println("How many floors are in the hotel? ");
			Floors = keyboard.nextInt();
			System.out.println();//Blank Space   
		}	   
	   for (Count=1;Count<Floors+1;Count++)
		{
			System.out.println("How many rooms are there on floor "+Count+"? "); 
			Rooms = keyboard.nextInt(); 
			System.out.println();//Blank Space
			
			//Validate input of rooms - 10 or more
			while (Rooms<10)
			{
				System.out.println("Number of rooms must be greater than ten!");
				System.out.println();//Blank Space
				System.out.println("How many rooms are on floor "+Count+"? "); 
				Rooms = keyboard.nextInt();			  
			}	   		 	   	
			
			System.out.println();//Blank Space
			System.out.print("How many rooms on floor "+Count+" are occupied? ");
			Occupied=keyboard.nextInt();
			
			Total_Rooms+=Rooms;
			Total_Occupied+=Occupied;
		}
		//All floors counted
		//Calculation for the occupancy rate
		Occupancy_Rate = (Total_Occupied / Total_Rooms)*100;
		//Calculation for vacant
		Rooms_Vacant = Total_Rooms-Total_Occupied;
		   	
		System.out.println();//Blank Space
		System.out.println("There are "+ Total_Rooms+" rooms, "+Total_Occupied+" of them are occupied");
		System.out.println("There are "+Rooms_Vacant+ " vacant, and occupancy rate is "+Occupancy_Rate+ "%.");
	 }  
}
~edit: code tags PB



ints dont take decimals so java rounds it off easy fix is to make one of ur vars in the Occupancy_Rate clac a double like this Occupancy_Rate = ((double)Total_Occupied / Total_Rooms)*100;
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3872
  • View blog
  • Posts: 11,405
  • Joined: 18-April 07

Re: java

Posted 04 November 2007 - 07:26 PM

Just one clarification to what dontknowjava said, it actually doesn't round. Converting from a float or double to int just truncates the decimal portion. No rounding takes place. 9 / 10 which would yield .90 is not rounded to 1. It is still 0 when converted to integer.

But dontknowjava appears to have the same advice I had. So I have to be right. hehe :)
Was This Post Helpful? 0
  • +
  • -

#5 dontKnowJava  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 221
  • Joined: 29-September 07

Re: java

Posted 04 November 2007 - 07:47 PM

View PostMartyr2, on 4 Nov, 2007 - 07:26 PM, said:

Just one clarification to what dontknowjava said, it actually doesn't round. Converting from a float or double to int just truncates the decimal portion. No rounding takes place. 9 / 10 which would yield .90 is not rounded to 1. It is still 0 when converted to integer.

But dontknowjava appears to have the same advice I had. So I have to be right. hehe :)



thats exactly what i meant just didnt want to type a whole lot to explain it, but martyr know what hes talking about. how do i know? i can think of at least 2 java problems he helped me with.
Was This Post Helpful? 0
  • +
  • -

#6 Hope1981  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-November 07

Re: java

Posted 04 November 2007 - 08:02 PM

View PostMartyr2, on 4 Nov, 2007 - 06:50 PM, said:

It is just how you are doing the calculation. Remember when you are trying to calculate a double make sure that all the operands are doubles as well. Especially in the case of integer division like you are trying to do here. Remember 1 / 3 is not .33333 it is zero when you are using integer. So you can cast them to double datatypes before using them and the result will be double.

// Calculate a double by casting our integers to doubles
Occupancy_Rate = ((double)Total_Occupied / (double)Total_Rooms)*100.0;



Then to format your answer into a nice decimal form you could do this...

DecimalFormat form = new DecimalFormat("###.0#");
			   
System.out.println();//Blank Space
System.out.println("There are "+ Total_Rooms+" rooms, "+Total_Occupied+" of them are occupied");
System.out.println("There are "+Rooms_Vacant+ " vacant, and occupancy rate is "+ form.format(Occupancy_Rate) + "%.");



Be sure to also import the DecimalFormat object at the top using the line java.text.DecimalFormat;

Enjoy!

"At DIC we be hotel occupancy calculating coding ninjas!" :snap:


thank you very much !!
I appreciate it
Was This Post Helpful? 0
  • +
  • -

#7 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 415
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: java

Posted 04 November 2007 - 10:05 PM

I have added the original text back into your original post.

Please do not remove the text from your topic just because the problem was solved. Other members may experience similar issues and can benefit from the solution that was provided.

But if you delete the text in your posts then it defeats the purpose of having this forum.

Thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1