Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>So you have 4 classes. </p> <blockquote> <p>In the assignment it says there need to be 4 classes: <strong>Assignment5, Hotel, Room, &amp; Guest</strong>.</p> </blockquote> <p>With the division of responsibility as such:</p> <blockquote> <p>Assignment5 class is for the <strong>interaction with the user</strong>.</p> <p>Hotel class has four rooms and all methods for <strong>operating the rooms</strong>. (extra emphasis: "rooms" is plural)</p> <p>Room class has 1 guest. <strong>If the rooms is empty a guest can check in. If the guest is leaving the room needs to be emptied.</strong> (or in other word, operating a single room)</p> <p>Guest class: the guest <strong>has a firstname and a last name.</strong></p> </blockquote> <p>First, you'd probably want to identify the "state" that each object would have. IOW, you need to determine the attributes/instance fields that each object have. Let's start with an example: <em>Guest class: the guest has a <strong>firstname</strong> and a <strong>last name</strong>.</em>. Do the same for all the other classes.</p> <p>Next, you want to identify the methods that will be needed. Let's start with another example: <em>Room class has 1 guest. If the rooms is empty a guest can <strong>check in</strong>. If the guest is leaving the room needs to be <strong>emptied</strong>.</em>. On top of that, you'll need some constructors for each class.</p> <p>Next, for each method, you want to find out the arguments that the method needs, and their return values, if they need to return a value. For example, for a <strong>check in</strong> method, you'll need the Room and a Guest; and you'll need to check whether the room is empty before you can check in.</p> <p><strong>UPDATE</strong>:</p> <p><strong>My problem now is: how can i make it work that there are 4 rooms and that after checking in 1 person, checking in a second person will put it in a different room?</strong></p> <p>Your teacher has a good advice, break it into pieces. </p> <p>Basically, you have the problem: "<em>Checking in the second person should put him in a different room</em>". </p> <p>So, how do we break this down? First, we need to <em>find an empty room</em>, so we need a method for that (say <code>findEmptyRoom()</code>), and after we find a room that's available, we need to <em>check in the guest into that room</em>, so we need another method (say <code>checkIn()</code>). So, we find an empty room, then we checked the guest into that room, then we're done.</p> <p>Next step, we break <code>findEmptyRoom()</code>. Our hotel has <em>4 rooms</em> (let's say we store <code>room1</code>, <code>room2</code>, <code>room3</code>, and <code>room4</code> as member variables, alternatively, you can use an array if you already learn about it in class). To find which one of the four rooms are empty, we need to <em>ask a room if it is empty</em>; so we need another method for that (say <code>isEmpty()</code>). To find an empty room, ask room 1, if <code>room1</code> is empty return <code>room1</code>, otherwise ask room 2, if <code>room2</code> is empty return <code>room2</code>, otherwise ask room 3, if <code>room3</code> is empty, return <code>room3</code>, otherwise ask room 4, if <code>room4</code> is empty, return <code>room4</code>. Otherwise, all rooms are occupied. [note: you will need to figure out what to do if all rooms are occupied]. An array will make this process much easier, you just need to loop through the array, and ask if it's empty and return the room if it's empty, otherwise continue checking next room.</p> <p>Next, we break checkIn(). So, what do <code>checkIn()</code> need to know to fulfill the checking in process? It needs to know about two information: the <code>room</code> and the <code>guest</code>. Since <code>checkIn()</code> is an instance method of a room class, <code>room</code> is implied, so we only need one parameter, i.e. <code>guest</code>. So <code>void checkIn(Guest guest)</code> should set the guest member variable of the room. </p> <p>Next, we break isEmpty(). How do we know if a room is empty? If a room has a guest inside it, then it's occupied, otherwise it's empty. So, <code>isEmpty()</code> checks if the guest member variable refers to a valid guest, returns true if it is valid guest, otherwise returns false. In Java, <code>null</code> is often used to mark that something is not a valid object, so you can check whether the guest member variable is <code>null</code> (obviously, you'll need to set the guest member variable to null when a guest checked out).</p> <p>Do the same thing for the other processes. Break the problem down into smaller pieces until you're confident that you can work with a piece.</p> <p><strong>UPDATE2:</strong></p> <p>The error message you've got:</p> <pre><code>Java:28: checkIn(Gast) in Hotel cannot be applied to (java.lang.String) hotel.checkIn("Guido"); </code></pre> <p>is because you're passing a String (<code>"Guido"</code>) to a function that takes a Gast argument (<code>void checkIn(Gast nieuweGast) {...}</code>). You should instead, pass a Gast object: </p> <pre><code>// declare a variable called g, with type Gast Gast g; // create a new Gast object, passing "Guido" as parameter // to Gast's constructor, and assign the new object to g g = new Gast("Guido"); // call hotel.checkIn(Gast) function with g as the argument, // i.e. the Gast object we just created in the previous line hotel.checkIn(g); </code></pre> <p>or more simply:</p> <pre><code>hotel.checkIn(new Gast("Guido")); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload