Hi Guys, I am a Java beginner and struggling but i have got so far with my own a
ID: 3546397 • Letter: H
Question
Hi Guys, I am a Java beginner and struggling but i have got so far with my own application but now i need your help, the program question is below:
(USING METHODS)
THIS IS WHAT I DID, IT COMPILES BUT NOTHING HAPPENS AFTERWARDS---PLEASE HELP!!!
import java.util.*;
public class E2Hotel
{
public static void main(String [] args)
{
int floors, occupiedRooms=0, totalRooms = 0,
totalRoomsonLevel=0,OccupiedRoomsonLevel=0,vacantRooms=0;
double OccupancyRate=0;
floors = getFloors();
floors = testFloors(floors);
for (int i = 1; i<=floors; i++)
{
totalRoomsonLevel = getRoomsonLevel(i);
totalRoomsonLevel = testRoomsonLevel(totalRoomsonLevel);
OccupiedRoomsonLevel = getOccupiedRooms(i);
}
OccupiedRoomsonLevel = testOccupiedRooms(OccupiedRoomsonLevel,floors);
totalRooms = gettotalRooms(totalRooms,totalRoomsonLevel);
occupiedRooms = getoccupiedRooms(occupiedRooms,OccupiedRoomsonLevel);
vacantRooms = getvacantRooms(totalRooms,occupiedRooms);
OccupancyRate = getRate(occupiedRooms,totalRooms);
System.out.println("Total hotel rooms: " +totalRooms);
System.out.print(" Total vacant rooms: "+vacantRooms);
System.out.println("Occupancy rate: " +OccupancyRate);
}
public static int getFloors()
{
int f;
Scanner keyboard = new Scanner(System.in);
System.out.print("How many floors are present:");
f = keyboard.nextInt();
return f;
}
public static int testFloors(int f)
{
{
Scanner keyboard = new Scanner(System.in);
while (f < 1)
System.out.println("Please enter number of floors greater than 0");
f = keyboard.nextInt();
}
return f;
}
public static int getRoomsonLevel(int x)
{
int y;
Scanner keyboard = new Scanner(System.in);
System.out.println("Level: "+x+"How many rooms are there:");
y = keyboard.nextInt();
return y;
}
public static int testRoomsonLevel(int x)
{
Scanner keyboard = new Scanner(System.in);
while (x < 10)
{
System.out.print("Please enter of rooms greater than 10");
x = keyboard.nextInt();
}
return x;
}
public static int getOccupiedRooms(int i)
{
int y;
Scanner keyboard = new Scanner(System.in);
System.out.println("Level: "+i+" How many are occupied:");
y = keyboard.nextInt();
return y;
}
public static int testOccupiedRooms(int x, int y)
{
int b;
Scanner keyboard = new Scanner(System.in);
while(x > y)
{
System.out.println("Invalid input,please try again");
b = keyboard.nextInt();
}
return x;
}
public static int gettotalRooms(int totalr, int totalfl)
{
return totalr += totalfl;
}
public static int getoccupiedRooms(int occur, int occurl)
{
return occur += occurl;
}
public static int getvacantRooms(int totalr, int occur)
{
int vacant;
vacant = totalr - occur;
return vacant;
}
public static double getRate(int occur, int totalr)
{
double rate;
rate = 100.0 * (occur/totalr);
return rate;
}
}
Explanation / Answer
#include <iostream>
#include "stdafx.h"
using namespace std;
int main ()
{
int numFloors=0, //store number of floors in hotel
numRooms=0, //store number of rooms in each floor
numOccupied=0, //store number of rooms occupied in each floor
totRooms = 0, //store total of rooms in hotel
totOccupied = 0, //store total of occupied rooms in hotel
totUnoccupied=0; //store total of unoccupied rooms in hotel
double occupRate=0.0; //store hotel occupancy rate
cout << "How many floors are in the hotel? "; //Prompt user for number of floors
cin >> numFloors
for (int floor = 1; floor <= numFloors; floor++) //initiate loop
cout << "How many rooms are in the #" << floor << " floor? "; //Prompt user for number of rooms
cin >> numRooms;
totRooms += numRooms; //number of rooms in each floor will be added to totRooms
cout << "How many rooms are occupied? "; //Prompt user for number of rooms occupied
cin >> numOccupied;
totOccupied += numOccupied; //number of rooms occupied in each floor will be added to totOccupied
totUnoccupied = totRooms - totOccupied; //store total of unoccupied rooms in hotel
occupRate= static_cast<double>(totOccupied) / totRooms; //store occupancy rate
cout << " The hotel has " << totRooms << " rooms. ";
cout << totOccupied << " rooms are occupied. ";
cout << totUnoccupied << " rooms are unoccupied. ";
cout << fixed << showpoint << setprecision(1);
cout << occupRate*100 << "% of the rooms are occupied. "; //display occupancy rate in percentage format
return 0;
}