Welcome! Log In Create A New Profile

Advanced

Solutions example exam paper

Posted by mcmukendi 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Solutions example exam paper
May 17, 2010 07:06PM
Does anyone have the solutions to the example exam paper (tutorial letter 103)?
avatar Re: Solutions example exam paper
May 18, 2010 12:17PM
That would be nice.
Re: Solutions example exam paper
May 18, 2010 03:52PM
Okay well lets start something.

1.1)
QCoreApplication - Provides event loop for console applications.
QTextStream - Provides simple handling of input and output.

1.2)
Language: C++ (QT)
int main(int argc, char** argv) { for (int a=argc; a>1; a--) { std::cout << argv[a]; } return 1; }
Re: Solutions example exam paper
May 18, 2010 04:18PM
waxxxd agree with you

Q2.1 When this function terminates, the three pointers will be removed from (stack) memory, but the three Thing objects will remain allocated on the heap without any means of accessing them, thus creating memory leaks.

Q2.2
void foo() {
QObject moat;
Thing* tp1 = new Thing(1, &moat);
Thing* tp2 = new Thing(2, &moat);
Thing* tp3 = new Thing(3, &moat);
}

Q3.2.1 Make num protected in the Thing class.
Re: Solutions example exam paper
May 18, 2010 05:21PM
Hey guys

I'm struggling with 5.2. The buttons and the window display fine, and I've connected the buttons to the slot. It runs perfectly until I click on one of the buttons, then it crashes. I think it's because I'm messing with button1 and button2, that are private in the header, but I don't know how to work around it. Anyone know what I'm doing wrong? Here's my code:

Language: C++ (QT)
ButtonClass::ButtonClass() { setWindowTitle("Button Fun" ); QPushButton *button1 =new QPushButton("This" ); QPushButton *button2=new QPushButton("That" );   QHBoxLayout* buttons=new QHBoxLayout() ; buttons->addWidget(button1); buttons->addWidget(button2);   QHBoxLayout* main=new QHBoxLayout(); setLayout(main); main->addLayout(buttons);   QObject::connect(button1, SIGNAL(clicked()),this, SLOT(something())); QObject::connect(button2, SIGNAL(clicked()),this, SLOT(something())); }   void ButtonClass::something() { if (button1->isEnabled()==true) { button1->setEnabled(true); } else if (button2->isEnabled()) { button1->setEnabled(false);; }   }

Any help would be appreciated.

Thanks!

J
Re: Solutions example exam paper
May 18, 2010 05:59PM
3.1)
Overload - Multiple functions with the same name but different in the number or type of arguments in the parameter list.
Override - Having a function from a deriving class with the same name and arguments as a function in a base class.
Partial Override - Where a derived class overrides an inherited function and calls the inherited function itself.

3.2.1)
The problem is that there is no visibility to num from Ding. To allow visibility to num by changing Thing we would make place num under the list of protected members of Thing.

3.2.2)
Language: C++ (QT)
virtual QString toString() { return QString("Thing %1").arg(num); }


3.2.3)
Language: C++ (QT)
QTextStream cout(stdin); foreach(QString item, thingList) { cout << item << endl; }
Re: Solutions example exam paper
May 18, 2010 06:29PM
@Justice

Language: C++ (QT)
ButtonClass::ButtonClass() { setWindowTitle("Button Fun" ); QPushButton *button1 =new QPushButton("This"); //Error - Should be button1 = new QPushButton("This"); QPushButton *button2=new QPushButton("That"); //Error - Should be button2 = new QPushButton("That");   QHBoxLayout* buttons=new QHBoxLayout() ; buttons->addWidget(button1); buttons->addWidget(button2);   QHBoxLayout* main=new QHBoxLayout(); setLayout(main); main->addLayout(buttons);   QObject::connect(button1, SIGNAL(clicked()),this, SLOT(something())); QObject::connect(button2, SIGNAL(clicked()),this, SLOT(something())); }   void ButtonClass::something() { if (button1->isEnabled()==true) { button1->setEnabled(true);   } else if (button2->isEnabled()) { button1->setEnabled(false);   } }

Justice you have redeclared button1 and button2 in the constructor and used them in connecting up to a slot. Also your logic for the something function is incorrect. Will post my version shortly.
Re: Solutions example exam paper
May 18, 2010 06:59PM
Thanks Waxxxd!

It was just my declaration of button1 and button2 that was wrong, but your suggestion worked wonders. My program works fine now (I fixed something() as well)

Here's the code:
Language: C++ (QT)
ButtonClass::ButtonClass() {       setWindowTitle("Button Fun" ); button1=new QPushButton("This" ); button2=new QPushButton("That" );   QHBoxLayout* buttons=new QHBoxLayout(); buttons->addWidget(button1); buttons->addWidget(button2);     QHBoxLayout* main=new QHBoxLayout(); setLayout(main); main->addLayout(buttons);   QObject::connect(button1, SIGNAL(clicked()),this, SLOT(something())); QObject::connect(button2, SIGNAL(clicked()),this, SLOT(something()));     }   void ButtonClass::something() { if (button1->isEnabled()==true) { button1->setEnabled(false); button2->setEnabled(true); } else if (button2->isEnabled()==true) { button2->setEnabled(false); button1->setEnabled(true); }     }
Re: Solutions example exam paper
May 18, 2010 07:00PM
When the user clicks a button, it should be disabled (only enabled buttons can be clicked); and if the other button is already disabled, the other button should be enabled.

Language: C++ (QT)
void Button::something() {   if (QObject::sender() == button1) { if (!button2->isEnabled()) { button2->setEnabled(true);   } button1->setEnabled(false); } else if (QObject::sender() == button2) { if (!button1->isEnabled()) { button1->setEnabled(true);   } button2->setEnabled(false); } }   Constructor by Justice as above minus the offending lines as marked.
Re: Solutions example exam paper
May 18, 2010 07:23PM
Question 4.1

Composition

Colored in diamond point
At least one data member of containing class should be of type cointained class
Containing class responsible for destroying and creating of the contained class

Aggregation
hollow diamong point
Containing class has a pointer to an instance of the contained class as one of its data members
Contained class can exist independantly of containing class

Question 4.2

I am not quite sure

Question 4.3

used to indicate a on-to-one or a one -to-many relationship. * = many, 1 = one

Question 5.1
MOC stand for - Meta Object Compiler
MOC tool read a header file. If it finds one or more class declarations that contain the Q_Object macro it produces a C++ source file containing meta-object code for those classes. Among other things meta object code is required for the signal and slot mechanism. Q-Object macro must appear inside the class definition so that MOC will know to generate code for it.
generates additional functions for each QObject derived class that uses the macro.
Re: Solutions example exam paper
May 18, 2010 07:45PM
5.1 forgot to add what MOC stand for - Meta Object Compiler
Anonymous User
Re: Solutions example exam paper
May 19, 2010 07:44AM
Phenomenal guys and girls! Great effort thanks!

6.1 Design patterns attempt to express simple and elegant solutions to various problems in object-oriented software development as observed over time in an abstract way.

6.2 QObjects manages and can be managed by other QObjects since each QObject represents the composite and component classes rolled into one. Each QObject maintains a list of its children and when the QObject is destroyed, so are all its children. This relationship is obviously a composition relationship and not the aggregation relationship as in the classic composite pattern.

6.3 Tut 102 p21
avatar Re: Solutions example exam paper
May 19, 2010 09:59AM
@ Justice

Your solution is correct for ButtonClass:: something() but I'd like to offer a slightly more efficient one.

You're not really concerned about the specific values that ->isEnabled() returns. You're more concerned by setting the enabled status to the opposite of what it was, so go with that fact only. Let the program deal with the specific values. Note: this technique is quite common when you need to flip (or "toggle" ) the state of a binary value. It is not as intuitive to a first-time reader of the code so you must give a comment to explain what's going on.

Language: C++ (QT)
void ButtonClass::something() { // set button1 to the opposite enabled state that is was on button1->setEnabled(!button1->isEnabled());   // set button2 state to be the opposite of button1';s enabled state button2->setEnabled(!button1->isEnabled()); }

With this approach, you eliminate both if statements and two function calls. You add two negation operations but they're only a single machine code statement each so no biggy.
(if you can't see them in this font, there's a negation operator (operator!) in each statement)

Another thing, all QWidget objects are enabled by default. You should set the correct disabled state in the constructor.

@ waxxxd
Use QObject::sender() as a last resort and almost never in a public slot. It's not necessary for this question. You can get all the information you need to perform the correct flip without resorting to finding out what caused the slot to be triggered. You're effectively coupling the slot to the signal, and you should always try to avoid coupling, it makes maintenance of code much harder.

If I decided to expand the requirements of the question to include a menu that allows you to change the state of the buttons' enabled state, you'd have to redesign your slot function.
Anonymous User
Re: Solutions example exam paper
May 19, 2010 10:21AM
@robanaurochs

Thank you for your input and a brilliant explanation, your solution is extremely elegant!
Re: Solutions example exam paper
May 19, 2010 10:40AM
@robanaurochs

Thanks for the suggestion, your code is quite efficient and, as you say, easy to read and maintain.

However, for this question it doesn't quite do the trick. The button you click is supposed to be disabled first. So if I start the program and click on "that", "that" is supposed to be disabled and "this" remain enabled. If I click on "this" first, the same applies.

With your code, "this" is always disabled first, even if you click on "that" first. (This will differ of course, depending on how you assigned button 1 and button2, but the same principle applies).

My code is actually also wrong, since it always disables the same button first regardless of what you click on.

Waxxxd's code is the only solution I've seen that answers the specifics of the question.

I'll definitely keep your code in mind for the future though, it's really simple and effective.
Re: Solutions example exam paper
May 19, 2010 11:56AM
Thanks for the suggestions Rob , I do subscribe to the defensive programming school of thought and assume nothing. I understand that coupling is extremely bad practice and should be avoided.
Re: Solutions example exam paper
May 19, 2010 12:16PM
QUESTION ABOUT NATASCHA'S ANSWER FOR 2.2

2.2)
Language: C++ (QT)
void foo() { Thing moat; Thing* tp1 = new Thing(1, &moat); Thing* tp2 = new Thing(2, &moat); Thing* tp3 = new Thing(3, &moat); }

Thing doesn't have a default constructor, so I don't think "Thing moat;" will work as a stack object.

Shouldn't we rather just use
Language: C++ (QT)
QObject moat;

or maybe

Language: C++ (QT)
Thing moat(0, 0);

?

What do you guys think?
avatar Re: Solutions example exam paper
May 19, 2010 01:24PM
@Justice

My apologies. I'm not a student of this course so I don't have the question requirements, I just popped in and had a look at the forum. I made the assumption that one button would be disabled from the start and all that was needed was that the buttons should swap their enabled status when pressed (a bit like a traffic light with only two lights).

If the ordering is important, you would indeed need to query which button caused the slot to be called, like waxxxd did, or you could create separate slots for each button. This latter version would be wasteful since you'd duplicate code.

Another solution that removes the reliance on QObject::sender() is to use QButtonGroup (setting exclusivity to false). You can add the buttons to the group, connect the QButtonGroup::buttonClicked(QAbstractButton *) signal to a ButtonClass:: something(QAbstractButton *) slot. (just make it private or protected).

Language: C++ (QT)
void ButtonClass::buttonClicked(QAbstractButton *button){ button1->setEnabled(button != button1); button2->setEnabled(button != button2); }

This also has the advantage that you can add as many other buttons to the button group and analogously add an extra line to the slot for each button you add. The behaviour will be that whichever button is clicked will be disabled and the rest will be enabled.
Re: Solutions example exam paper
May 19, 2010 01:39PM
@ Justice --- Q2.2 - moat could be declared as QObject or Thing. Both will work.
Re: Solutions example exam paper
May 19, 2010 03:34PM
@robanaurochs

No worries mate, thought you knew the question. Your solutions all work very well, but I'll think I'll stick to what I know for now. Don't want to learn too many new things going into the exam, it'll just confuse me winking smiley

Anyone got question 7.3 to work? I can't seem to get it running. Maybe it's a trick question and we're supposed to say "it won't display anything because it's broken". I'm probably wrong and am just doing something stupid. Anyone have any ideas?
Re: Solutions example exam paper
May 19, 2010 08:47PM
Okay in the case of 2.2 Thing does not have a default constructor so that wont work. Declaring moat to be of type QObject will do the trick.
Re: Solutions example exam paper
May 19, 2010 09:36PM
@Justice & waxxxd

Maybe just a technicality but shouldn't line one for Question 5.2 read:

ButtonFun::ButtonFun()
{

instead of

ButtonClass::ButtonClass ()
{
?
Re: Solutions example exam paper
May 19, 2010 09:53PM
@jeanf

Yeah it really doesn't matter. I just named my class ButtonClass when I wrote it in Qt, but in the exam you would obviously use the class name they gave you. The code is the most important thing.
Re: Solutions example exam paper
May 20, 2010 09:26AM
It's something like this.

7.1 Similarities: Both change the way the interface looks.
Differences: Layouts have a rigid structure and cannot be changed easily while a UI in active, while docks can be moved around and placed wherever the user wants to.

7.2 Similarities: Both forms of dialogs are popup boxes. DIfferences: InputDialog expect user interaction to input certain data, while Dialog is displaying a message to inform the user of something.

7.4 Action lists are Action items added to menus or toolbars used to facilitate user interaction with a UI.

With this message I am out. Good luck everyone, please pop back around after the exam and post your thoughts on the exam and some of the answers you gave.

Best and luck.
Anonymous User
Re: Solutions example exam paper
May 20, 2010 09:41AM
Good luck to you too and to everyone else here! Will probably pop back here later tonight then...
Re: Solutions example exam paper
May 20, 2010 10:43AM
Changed my answer for

Partial Override - Where a derived class overrides an inherited function and calls the inherited function itself.

as in
Language: C++ (QT)
void InseruceAccount::deposit(double amount, QDate date) { m_TransactionDate = date; Account::deposit(amount); }
Re: Solutions example exam paper
May 13, 2011 07:42AM
can someone email me the 2010 tutorial letter 103?

44699530@mylife.unisa.ac.za
Sorry, only registered users may post in this forum.

Click here to login