Assignment 2 questuin 1

Posted by Anonymous User
Announcements Last Post
SoC Curricula 09/30/2017 01:08PM
Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
School of Computing Short Learning Programmes 11/24/2014 08:37AM
Unisa contact information 07/28/2011 01:28PM
 Anonymous User Assignment 2 questuin 1 July 30, 2011 03:17PM Rating: 0
This is quite a monster of a question. the part where I have to check if there is a vacant rooms blew me.

the function vacancy takes two two dates as parameter(arrival and departure date),the pat where I have to check if atlist one room is available on each of the dates from
the arrival to departure by calling roomsAvailable() is where I struggle. may someone give me an idea how to tackle this one? how do you increase dates from arrival to departure as I think we have to use a loop
 Re: Assignment 2 questuin 1 July 30, 2011 06:10PM Registered: 14 years ago Posts: 1,424 Rating: 0
Could you cut and paste the question, please? If you do so, students who've done this course before might also be able to help you.
 Anonymous User Re: Assignment 2 questuin 1 August 01, 2011 11:56AM Rating: 0
I did not include the question UML but I hope the description is enough to assist me. how do I implement vacancy function?

vacancy() tests takes two dates as parameters (intended to represent the arrival and departure dates, respectively), tests whether the two dates are valid (e.g. 32/12/2011 is not valid), tests whether the arrival date precedes the departure date, and if so, checks whether there is at least one room available on each of the dates from the arrival date to the day before the departure date (by calling roomsAvailable() for each of these dates).

• roomsAvailable() takes a date as parameter and returns the number of rooms available on that date. It calculates this by going through the entire list of bookings and counting how many of them include the given date (and subtracts this from the total number of rooms). Hint: Use the booked() member function of the Booking class.
 Re: Assignment 2 questuin 1 August 01, 2011 01:15PM Registered: 14 years ago Posts: 1,424 Rating: 0
Could you email me your tut letter please, there's not enough context yet.
 Anonymous User Re: Assignment 2 questuin 1 August 01, 2011 02:52PM Rating: 0
Sent
 Re: Assignment 2 questuin 1 August 03, 2011 02:56PM Registered: 11 years ago Posts: 152 Rating: 0
same here, please send me the tut to 42677718@mylife.unisa.ac.za

--------------------------------------------------------------------
In a long journey, even a matchstick is heavy...
 Re: Assignment 2 questuin 1 August 03, 2011 06:24PM Registered: 14 years ago Posts: 1,424 Rating: 0
@crunx

Sorry for the late reply. I've looked through your assignment. Nice questions.

As to your original question, yes, you are most certainly going to have to iterate over dates and a for() loop should do it. If you want your counter variable to move from one day to the next between the arrival date and the departure date, you're best using the QDate:: addDays() function.

Have a look at it and also the QtAssistant reference documentation. You'll find a lot of useful stuff in there as well as descriptions of all the classes and how to use them.
 Anonymous User Re: Assignment 2 questuin 1 August 04, 2011 05:32PM Rating: 0
Thanks, nice one there. let me try QDate::addDays(). It never crossed my mind
 Anonymous User Re: Assignment 2 questuin 1 August 04, 2011 07:38PM Rating: 0
Ooops, Not as easy as I thought.

Not quite sure if this makes sense

bool vacancy(QDate a, QDate d)
{
for(QDate date = a; date <= d; date.addDays(1))

if date == roomsAvailable(date)
return true;
}

Nope, the QtAssistant reference documentation didn't help.
 Re: Assignment 2 questuin 1 August 04, 2011 09:36PM Registered: 14 years ago Posts: 1,424 Rating: 0
@crunx

The QDate:: addDays() function won't change the internal value of the object, it returns a new QDate, offset by the number of days you specify, that you can manipulate somewhere else. In your for loop, you need to record this new date.

*Style Note: Don't be afraid of longer variable names. It makes your code easier to read. You may get penalised by the lecturers.
```Language: C++ (QT)bool vacancy(QDate arrivalDate, QDate departureDate) {
for(QDate currentDate = arrivalDate; currentDate <= departure; currentDate = currentDate.addDays(1)){

if (currentDate == roomsAvailable(currentDate))
return true;
}
return false;
}```
 Anonymous User Re: Assignment 2 questuin 1 August 05, 2011 02:22PM Rating: 0
@Robanaurochs

Your counsel is appreciated. Thanks very much.
 Anonymous User Re: Assignment 2 questuin 1 August 12, 2011 11:40AM Rating: 0
Seriously, this abstract concept is messing up with my head. Since Booking class is an abstract class, constructing addBooking has been complex. Had booking class not been abstract, addBooking could have been easier. Eg void BookingList::addBooking(Booking* b)
{
If (vacancy(arrivalDate, departureDate))
Append(p);
}
But since Booking class abstract, you cannot initiated it, and I tried this below code .
First I had to create instances of single and Sharing.

Booking* BookingList::addBooking(Person contactName, QDate arrivalDate, QDate departureDate,
Person * guest1, Person* guest2)
{
If (vacancy(arrivalDate, departureDate))
{
// create instances of single and sharing class
Single *singleGuest = new Single(contactPerson, arrivalDate, departureDate, guest);
Sharing *sharingGest= newSharing(contactPerson, arrivalDate, DepartureDate, guest1, guest2);
BookingList.append(addBooking(contactName, departureDate , arrivalDate, guest1, guest2));
}
Return BookingList;
}

My question what is wrong with my code? May someone there explain how to implement this addbookining class.
 Re: Assignment 2 questuin 1 August 12, 2011 12:53PM Registered: 14 years ago Posts: 1,424 Rating: 0
You're not quite understanding the usefulness of abstraction.

Take your bookingList class for instance. The addBooking() function must determine how many guests there are and then decide what kind of concrete booking to make. Since you need to pass through two guest Person instances, the easiest way to check what kind of booking to create is to check whether the second guest is a valid pointer or not. You do this by checking whether the pointer address is zero or not.

```Language: C++Booking *newBooking;
If (guest2 == 0){
newBooking = new Single(contactPerson, arrivalDate, departureDate, guest1);
} else {
newBooking = new Sharing(contactPerson, arrivalDate, departureDate, guest1, guest2);
}

append(newBooking);

return newBooking;```
 Anonymous User Re: Assignment 2 questuin 1 August 12, 2011 04:44PM Rating: 0
@robanaurochs

axactly how I wanted to code it. It looks simple but it never clicked for me. I definately understand the usefulness of abstraction however, I think my limited knowledge of c++ did not help to apply the concept.
I have learnt a lot and thanks again robanaurochs
 Re: Assignment 2 questuin 1 August 23, 2011 10:29AM Registered: 8 years ago Posts: 12 Rating: 0
hi guys!can anyone pliz help me with the implementation of the void BookingList::roomsAvailable(QDate d)????

blv
 Anonymous User Re: Assignment 2 questuin 1 August 23, 2011 05:50PM Rating: 0
Hi boby love,

How about posting your attempt so that people can help you basing on that? in that way, the assistance can enhance your limitations , again, you have a sense of ownership of your solution hence improving your confidence. I hope you get my point
 Re: Assignment 2 questuin 1 August 24, 2011 02:17PM Registered: 8 years ago Posts: 43 Rating: 0
Hi there! I'm new here!

I'm trying to figure out as to how to solve this little problem. I want the m_Contact = c statement inside the booking constructor definition (see below) to work properly.

class Booking // ABSTRACT CLASS

booking.h

```Language: C++ (QT){
public:

Booking(Person c, QDate a, QDate d);
QString toString();
virtual double rate() = 0;
bool booked(QDate d);
double SINGLE_PPPN;
double SHARING_PPPN;

private:
Person m_Contact;
QDate m_ArrivalDate;
QDate m_DepartureDate;
};```

booking.cpp
```Language: C++ (QT)#include<QString>
#include<QDate>

#include "booking.h"
#include "person.h"

Booking::Booking(Person c, QDate a, QDate d)

{
m_Contact = c; // IT DOESN'T WORK!!!!!
m_ArrivalDate = a;
m_DepartureDate = d;

SINGLE_PPPN = 50.0;
SHARING_PPPN = 100.0;
}

QString Booking::toString()

{
return (m_Contact.toString() + ", Arrival Date: " + m_ArrivalDate + ", Departure Date: " + m_DepartureDate);
}

bool Booking::booked(QDate d)

{
if ((m_ArrivalDate <= d) && (d < m_DepartureDate))

return true;

else

return false;
}```

This is the most boring programming module
 Re: Assignment 2 questuin 1 August 24, 2011 03:17PM Registered: 10 years ago Posts: 3,496 Rating: 1
The problem here is that you're attempting to assign the speed of light in a vacuum to some variable. What probably happened is that you picked some person faster than the speed of light, and so your computer baulks at this physical impossibility...

Which is a roundabout way of saying: "That's a pretty weird error."

It looks like it should work just fine.

Have you tried the initialization list alternative? (I see it's a constructor).
 Re: Assignment 2 questuin 1 August 24, 2011 03:45PM Registered: 8 years ago Posts: 43 Rating: 0
Whoa! Now it works oO Thanks man!!!! You really saved my life! I have been so frustated by this stupid problem. Can't believe that I didn't consider using the initialization list (facepalm)

Again, thanks man Now going to finish this boring assignment (It's late, I know )
 Re: Assignment 2 questuin 1 August 24, 2011 04:20PM Registered: 10 years ago Posts: 3,496 Rating: 1
It only occurred to me because I've probably stared at something similar for a similar number of hours at some quite recent point in time. Glad to see the shot in the dark somehow hit the mark, anyway.
 Re: Assignment 2 questuin 1 August 24, 2011 07:33PM Registered: 9 years ago Posts: 6 Rating: 0

My code looks like this:

```Language: C++ (QT)int BookingList::roomsAvailable(QDate d){
int roomsOccupied = 0;
foreach (Booking, BookingList);{
if (Booking::booked(d))
roomsOccupied++;
}
return NO_OF_ROOMS - roomsOccupied;
}```

 Re: Assignment 2 questuin 1 August 24, 2011 10:22PM Registered: 10 years ago Posts: 3,496 Rating: 1
Are you passing an instance as the first argument to your foreach loop?

Looks to me like it's just the class of that object. Maybe you can do that, but I suspect not. Look up foreach in the Qt docs and make sure, I reckon. That might sort you out.
 Re: Assignment 2 questuin 1 August 25, 2011 08:35AM Registered: 8 years ago Posts: 43 Rating: 0
xtoor Wrote:
-------------------------------------------------------
> List with a foreach loop.
>
> My code looks like this:
>
>
> int BookingList::roomsAvailable(QDate d){
> int roomsOccupied = 0;
> foreach (Booking, BookingList);{
> if (Booking::booked(d))
> roomsOccupied++;
> }
> return NO_OF_ROOMS - roomsOccupied;
> }
>
>
> I'm truly lost with using pointers, inherited

Use *this instead of BookingList as 2nd argument of foreach. It's safe.

Well, guys, I have another problem, I can't initialize NO_OF_ROOMS variable of int type inside the public section of BookingList class. I get an error message saying it's forbid to initialize data members inside the class. I want it to be initialized everytime an instance of BookingList is created. The question paper doesn't mention that you should add any constructor to BookingList class.
 Anonymous User Re: Assignment 2 questuin 1 August 25, 2011 10:32AM Rating: 0
@Spectre

I advice you to understand how foreach loop works

this how I did it
```Language: C++ (QT)foreach(Booking * b, *this){
NO_OF_ROOMS++;
if ((b->booked(d))
{
roomsOccupied ++
}
return NO_OF_ROOMS - roomsOccupied;
}```
 Anonymous User Re: Assignment 2 questuin 1 August 25, 2011 10:36AM Rating: 0
Sorry I made a mistake there.
is No_OF_Rooms++ just after foreach loop instead of rooms Occupied++
 Re: Assignment 2 questuin 1 August 25, 2011 12:11PM Registered: 8 years ago Posts: 43 Rating: 0
crunx Wrote:
-------------------------------------------------------
> Sorry I made a mistake there.
> is No_OF_Rooms++ just after foreach loop instead
> of rooms Occupied++

Hey, I have figured out how to use a foreach loop properly, thanks to QtAssistance.

As for your roomsAvailable code, Didn't you specify the maximum number of rooms in your code? How do you tell if your rooms are all fully booked? Mine is different from yours. I initialize NO_OF_ROOMS to something like 10 everytime an instance of BookingList is created.
 Anonymous User Re: Assignment 2 questuin 1 August 25, 2011 02:17PM Rating: 0
the statement No_OF_Rooms++ just after foreach loop is intended to count the number of rooms

```Language: C++ (QT)if ((b->booked(d))
{
roomsOccupied ++
}```

is to count the number of booked rooms.

so to answer your question, my program count the number of rooms so I id not initialize No_OF_Rooms. Initializing it is another way of doing it and there is nothing wrong with that.
 Re: Assignment 2 questuin 1 August 25, 2011 09:04PM Registered: 8 years ago Posts: 43 Rating: 0
crunx Wrote:
-------------------------------------------------------
> the statement No_OF_Rooms++ just after foreach
> loop is intended to count the number of rooms
>
>
> if ((b->booked(d))
> {
> roomsOccupied ++
> }
>
>
>
> is to count the number of booked rooms.
>
> number of rooms so I id not initialize
> No_OF_Rooms. Initializing it is another way of
> doing it and there is nothing wrong with that.

Yeah, I got it.

Good luck guys
 Re: Assignment 2 questuin 1 August 28, 2011 11:18PM Registered: 9 years ago Posts: 77 Rating: 0
Quote
slow eddy
Are you passing an instance as the first argument to your foreach loop?

Looks to me like it's just the class of that object. Maybe you can do that, but I suspect not. Look up foreach in the Qt docs and make sure, I reckon. That might sort you out.

slow eddy,
I did the same thing that u suggested to Spectre but no luck
 Re: Assignment 2 questuin 1 August 28, 2011 11:20PM Registered: 9 years ago Posts: 77 Rating: 0
I get undefined reference to 'vtable for Booking', anyone help!!!
Sorry, only registered users may post in this forum.