Question:
Can someone assist me with this Java code?
Mike J
2014-03-28 08:37:19 UTC
Noobie Java Coder here in desperate need of expert resolution. I have spend countless hours/days & a sleepless night on this & just when I thought I had it I realized I was just too sleep deprived to see that the calculations were off.

I need the code to process input & report output as the 'Sample Outputs' below shows but my code shown in "Java Code.jpg" is totaling the inputs for the progression of each Nursing Home. I need it to be individual for each Home. Also I can not get my occupancy rate working at all.

Greatly Appreciated in advance.

Samle Outputs
-----------------------------------------------------
Nursing Home 1
Number of rooms: 20
Occupied rooms: 15
Vacant rooms: 5
Occupancy rate: 75%
Occupancy Rate is High

How many floors are in Nursing Home 2? 3
How many rooms does floor 1 have? 20
How many occupied rooms are on floor 1: 5
How many rooms does floor 2 have? 20
How many occupied rooms are on floor 2? 8
How many rooms does floor 3 have? 20
How many occupied rooms are on floor 3? 11


Nursing Home 2
Number of rooms: 60
Occupied rooms: 24
Vacant rooms: 36
Occupancy rate: 40%
Occupancy Rate is Low
Four answers:
Leo D
2014-03-28 13:00:39 UTC
1. Do NOT declare all of your variables at the beginning of your program. I don't know why teachers teach that =/ I remember when I moved from BASIC to C++, I used to declare my variables globally (which you sort of do in BASIC because BASIC has no concept for globals and locals). My teacher never corrected me, but it was a big mistake on my part. When I started writing more complicated programs that had more than 3 or 4 methods, I noticed that this was very inconvenient. A teacher should correct this behaviour early.



Declare your variables where and ONLY WHERE you need them. If part of your program has no reason to know what some variable equals, don't give them that variable. This is just room for errors like the logical errors that you're getting.



2. Try to split up your code accordingly, into methods that do part of each code. Generally, it's good advice to put the body of each loop into a separate method. This also helps with keeping variables where, and only where, they are important.



I can't really read your code comfortably (next time, try http://pastebin.com ), but my understanding is that you are reading in a bunch of nursing homes, and you are adding up the number of rooms total and occupied. Since you are declaring your variables too early, all of the nursing homes are being combined together.



Here is what you should be doing (pseudo-code):



    method: main

    {

        While ( There is a next nursing home, )

        {

            process_home (nursing_home_number).

        }

    }



    method: process_home, accepting (nursing_home_number).

    {

        Declare rooms_total, rooms_occupied, number_of_floors.



        Ask for number_of_floors.



        For ( i = 1 to number of floors. )

        {

            Declare floor_rooms_total, floor_rooms_occupied.



            Ask for floor_rooms_total.

            Ask for floor_rooms_occupied.



            Add floor_rooms_total to rooms_total.

            Add floor_rooms_occupied to rooms_occupied.

        }



        Declare vacant_rooms = (rooms_total - rooms_occupied).

        Declare occupancy = ((100 * rooms_occupied) / rooms_total).

        Decide whether occupancy is high, low, &c.



        Display rooms_total.

        Display occupied_total.

        Display vacant_rooms.

        Display occupancy.

        Display whether occupancy is high, low, &c.

    }



I didn't know how you where checking for the nursing rooms, so I used a While-loop. This is probably really a For-loop.



The For-loop can also be made into another method, but it's a little more complicated. This method would have to return two values. The way that you do this is language-dependent.



In some programming languages, C++ for example, arguments are passed by reference, so they can be reassigned. In Java, that is not the case. They're passed by value, so the parameters are really just local variables that contain the value of the arguments passed in. In order to avoid mistaking the two, I highly recommend declaring all parameters as final.

[Actually, almost every thing should be final:

- your parameters

- your local variables, unless they need to change like a loop counter for example, or are lazily instantiated.

- your fields, again unless they need to change

- your classes: use inheritence by composition rather than extention, and create interfaces for all of your classes. Make super-interfaces that are small and only group related behaviour together. And make sub-interfaces that are composed of those small ones.

- If it's ABSOLUTELY necessary to use inheritence by extension, still use interfaces, and make sure that as many of your methods as you can are final. This is usually only the case in some frameworks, such as the Applet framework.]



Since arguments are passed by value in Java, you will need another way of doing this. That way is by using objects. You'll also want to handle your dependencies properly, which I don't show here for simplicity, but email me and I'll help you out with that. Now, I don't know how far you are in Java, but basically, you will want to create the following:



    A nursing home class:

        - with instance fields rooms_total and rooms_occupied.

        - with instance fields NUMBER_OF_FLOORS (definitely final) and current_floor.



        - a constructor, accepting number of floors

            - It sets NUMBER_OF_FLOORS to the specified number.

            - It sets rooms_total, rooms_occupied and current_floor to 0.



        - an instance method called hasNextFloor, which returns (current_floor < NUMBER_OF_FLOORS).



        - an instance method called processNextFloor, accepting (floor_number), which does:



            If (!hasNextFloor())

            {

                throw new NoSuchElementException();

            }



            current_floor++;



            Declare floor_rooms_total, floor_rooms_occupied.



            Ask for floor_rooms_total.

            Ask for floor_rooms_occupied.



            Add floor_rooms_total to rooms_total.

            Add floor_rooms_occupied to rooms_occupied.



        - an instance method accessor called getRoomsTotal that returns rooms_total.

        - an instance method accessor called getRoomsOccupied that returns rooms_occupied.

        - an instance method called getRoomsVacant that returns (rooms_total - rooms_occupied).

        - an instance method called getOccupancy that returns ((100 * rooms_occupied) / rooms_total).

        - an instance method called getOccupancyRating that returns the occupancy rating.



Let's say this class will be called NursingHome. It should be public and saved in a file named "NursingHome.java" case sensitive.



Once you have that class made, you can simplify process_home:



    method: process_home, accepting (nursing_home_number).

    {

        Declare number_of_floors.



        Ask for number_of_floors.



        Declare current_nursing_home = new NursingHome(number_of_floors).



        While (hasNextFloor() on current_nursing_home)

        {

            processNextFloor() on current_nursing_home.

        }



        Display getRoomsTotal() on current_nursing_home.

        Display getOccupiedTotal() on current_nursing_home.

        Display getVacantRooms() on current_nursing_home.

        Display getOccupancy() on current_nursing_home.

        Display getOccupancyRating() on current_nursing_home.

    }



I hope this help! Email me if you have any questions :)
david
2014-03-28 10:34:11 UTC
You could just set the variables to 0 every time you get to the beginning of the nursing home loop.
Mike
2014-03-28 08:55:37 UTC
You should store them on a text doc and read them into an array and then just system .out the array
anonymous
2014-03-28 18:14:02 UTC
That was really good!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...