By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,193 Members | 826 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,193 IT Pros & Developers. It's quick & easy.

implemention file

P: 30
please help me
Expand|Select|Wrap|Line Numbers
  1. throttle::throttle(int size)
  2. // Precondition:  size > 0;
  3. // Postcondition: Throttle has size positions above the shutoff position, and its current position is off.
  4. {
  5.    assert(size>0);
  6.    top_position = size;
  7.    position = 0;
  8. }
i have to fiil code
is this ccorrect??
Feb 20 '07 #1
Share this Question
Share on Google+
15 Replies


Ganon11
Expert 2.5K+
P: 3,652
Without understanding what your class is doing, we can't tell if this is the proper implementation. I can tell you, however, that since your PRE is size > 0, you don't need to use the assert statement. A precondition is something you can assume to be true every time the function is used. Thus, you may assume that size is always > 0. If you know that it is possible to enter a non-positive number for size, you may consider a different precondition.
Feb 20 '07 #2

RedSon
Expert 5K+
P: 5,000
please help me
Expand|Select|Wrap|Line Numbers
  1. throttle::throttle(int size)
  2. // Precondition:  size > 0;
  3. // Postcondition: Throttle has size positions above the shutoff position, and its current position is off.
  4. {
  5.    assert(size>0);
  6.    top_position = size;
  7.    position = 0;
  8. }
i have to fiil code
is this ccorrect??
This code fulfills your post condition. You set your top_position to size which would probably be better named as max or max_position, and you set your current position, which could be better named to curr_position, to 0. As for asserting that size is > 0 if your precondition states it then you can safely assume it to be true, but if you are concerned about security and error checking you should really return some kind of error code from this method then you can do stuff like:

Expand|Select|Wrap|Line Numbers
  1. if (size < 1)
  2. {
  3. return -1;//error code to say that precondition not met
  4. }
  5.  
Feb 20 '07 #3

P: 30
i did not understand implemention file. i have to fill precondition and postcondition and code. I tried but i am not sure about those.




File: throttle.cpp

Expand|Select|Wrap|Line Numbers
  1. // FILE: throttle.cpp
  2. // CLASS IMPLEMENTION FILE of throttle
  3.  
  4. #include "throttle.h"
  5.  
  6. throttle::throttle()
  7. //fill in precondition and postcondition
  8.  
  9. //precondition: none
  10. //postcondition:
  11. {
  12.     top_position = 1;
  13.     position = 0;
  14. }
  15.  
  16. throttle::throttle(int size)
  17. // Precondition:  size > 0;
  18. // Postcondition: Throttle has size positions above the shutoff position, and its current position is off.
  19. {
  20.     //fill in correct code
  21. top_position = 1;
  22.     position = 0;
  23.  
  24.  
  25. }
  26.  
  27. void throttle::shut_off()
  28. //fill in precondition and postcondition
  29. //precondition: none(  because  member functions no precondition right??)
  30. //postcondition: the throttle has been turned off.
  31. {
  32.     position = 0;
  33. }
  34.  
  35. void throttle::shift(int amount)
  36. // Precondition: None
  37. // Postcondition: Throttle’s position is moved by amount (but not below 0 or above top position).  If amount would move throttle beyond 0, set to 0.  If amount would move throttle beyond top position, set throttle to top position.
  38. {
  39.     // fill in correct code
  40. position += amount;
  41. if (position <0 )
  42.    position = 0;
  43. else if (position > top_postion)
  44. postion = top_postion;
  45.  
  46.  
  47.  
  48. }
  49.  
  50. bool throttle::is_on() const;
  51. // Precondition: None
  52. // Postcondition: false if position is zero, true otherwise
  53. {
  54.     // fill in correct code
  55.  
  56. return (postion > 0);
  57. }









File: usingthrottle.cpp

Expand|Select|Wrap|Line Numbers
  1. #include "throttle.h"
  2. #include <iostream>
  3. using namespace std;
  4. //creates two throttles and shifts the second throttle an inputted amount.
  5. void main(){
  6.     throttle t1;
  7.     int value;
  8.     cout << "Type in a throttle size." << endl;
  9.     cin >> value;
  10.     throttle t2(value);
  11.     cout<< “Type in a shift amount.” << endl;
  12. cin >> value;
  13.     t2.shift(value);
  14.     if (t2.is_on())
  15.         cout << “Throttle on” << endl;
  16.     else
  17.         cout << “Throttle off” << endl;    
  18. }
Feb 21 '07 #4

P: 30
please help meeeeeeeeeeeee
Feb 21 '07 #5

Ganon11
Expert 2.5K+
P: 3,652
OK, so returning to your first question about the second constructor:

I still think you can remove the assert statement from that definition, but it looks correct otherwise.
Feb 21 '07 #6

P: 30
i did not use assert statment.please tell me about other answer's r correct or not.
Feb 21 '07 #7

P: 30
i have problem with implemention file ,precondition and postcondition. please help me. I filled precondition and postcondition and code .I do not know those r correct or not.

Expand|Select|Wrap|Line Numbers
  1. // FILE: throttle.cpp
  2. // CLASS IMPLEMENTION FILE of throttle
  3.  
  4. #include "throttle.h"
  5.  
  6. throttle::throttle()
  7. //fill in precondition and postcondition
  8.  
  9. //precondition: none
  10. //postcondition: i 
  11. {
  12.     top_position = 1;
  13.     position = 0;
  14. }
  15.  
  16. throttle::throttle(int size)
  17. // Precondition:  size > 0;
  18. // Postcondition: Throttle has size positions above the shutoff position, and its current position is off.
  19. {
  20.     //fill in correct code
  21. top_position = 1;
  22.     position = 0;
  23.  
  24.  
  25. }
  26.  
  27. void throttle::shut_off()
  28. //fill in precondition and postcondition
  29. //precondition: none(  because  member functions no precondition right??)
  30. //postcondition: the throttle has been turned off.
  31. {
  32.     position = 0;
  33. }
  34.  
  35. void throttle::shift(int amount)
  36. // Precondition: None
  37. // Postcondition: Throttle’s position is moved by amount (but not below 0 or above top position).  If amount would move throttle beyond 0, set to 0.  If amount would move throttle beyond top position, set throttle to top position.
  38. {
  39.     // fill in correct code
  40. position += amount;
  41. if (position <0 )
  42.    position = 0;
  43. else if (position > top_postion)
  44. postion = top_postion;
  45.  
  46.  
  47.  
  48. }
  49.  
  50. bool throttle::is_on() const;
  51. // Precondition: None
  52. // Postcondition: false if position is zero, true otherwise
  53. {
  54.     // fill in correct code
  55.  
  56. return (postion > 0);
  57.  
File: usingthrottle.cpp

Expand|Select|Wrap|Line Numbers
  1. #include "throttle.h"
  2. #include <iostream>
  3. using namespace std;
  4. //creates two throttles and shifts the second throttle an inputted amount.
  5. void main(){
  6.     throttle t1;
  7.     int value;
  8.     cout << "Type in a throttle size." << endl;
  9.     cin >> value;
  10.     throttle t2(value);
  11.     cout<< “Type in a shift amount.” << endl;
  12. cin >> value;
  13.     t2.shift(value);
  14.     if (t2.is_on())
  15.         cout << “Throttle on” << endl;
  16.     else
  17.         cout << “Throttle off” << endl;    
  18.  
please help me.
Feb 21 '07 #8

AdrianH
Expert 100+
P: 1,251
Without understanding what your class is doing, we can't tell if this is the proper implementation. I can tell you, however, that since your PRE is size > 0, you don't need to use the assert statement. A precondition is something you can assume to be true every time the function is used. Thus, you may assume that size is always > 0. If you know that it is possible to enter a non-positive number for size, you may consider a different precondition.
About preconditions, yes you should not need the assert() but in practice, programmers make mistakes, so the assert should be there anyway to enforce the precondition in debug mode. This will catch callers making improper use of the function call. Assertions will be removed when NDEBUG is defined so you don't loose anything by putting it in.

An assert for the post contidion may also be advisable, but when declaring variables to be used exclusivly by the post condition, you need to encompas them with
#ifndef NDEBUG
<vars here>
#endif

which can also be stated as:
#if !defined NDEBUG
<vars here>
#endif

Otherwise, if you don't the variables will be allocated but not used causing the compiler to complain about unused variables.


Adrian
Feb 21 '07 #9

P: 30
I need help throttle.cpp
Feb 21 '07 #10

AdrianH
Expert 100+
P: 1,251
i have problem with implemention file ,precondition and postcondition. please help me. I filled precondition and postcondition and code .I do not know those r correct or not.
Well, neither do I. Pre/postconditions are specific to what you are applying it to. Giving us the code without explanation doesn't do a whole lot either.

However, I will point out a few things:
Expand|Select|Wrap|Line Numbers
  1. // FILE: throttle.cpp
  2. // CLASS IMPLEMENTION FILE of throttle
  3.  
  4. #include "throttle.h"
  5.  
  6. throttle::throttle()
  7. //fill in precondition and postcondition
  8.  
  9. //precondition: none
  10. //postcondition: i 
  11.  
what does 'i' mean?
Expand|Select|Wrap|Line Numbers
  1. {
  2.     top_position = 1;
  3.     position = 0;
  4. }
  5.  
  6. throttle::throttle(int size)
  7. // Precondition:  size > 0;
  8. // Postcondition: Throttle has size positions above the shutoff position, and its 
'size' positions is the number of desecrate possible positions above 0? Is there a maximum size?
'and its' sounds like it is not complete, what are you trying to say here?
Expand|Select|Wrap|Line Numbers
  1. current position is off.
  2. {
  3.     //fill in correct code
  4.  
I hope you don't expect me to fill this out, as we don't do you work for you. And even if I could, I wouldn't have a clue as I don't know what you are trying to do.
Expand|Select|Wrap|Line Numbers
  1. top_position = 1;
  2.     position = 0;
  3.  
  4.  
  5. }
  6.  
  7. void throttle::shut_off()
  8. //fill in precondition and postcondition
  9. //precondition: none(  because  member functions no precondition right??)
  10. //postcondition: the throttle has been turned off.
  11.  
The postcondition looks right given the function name. The precondition is wrong in that member functions do have preconditions, whether that precondition is true (ie as you stated none) or something else, is entirely up to your member function. But ask yourself this, if I shut the throttle off, is there anything statement that must be true in order for me to shut the throttle off? If not, then true or none is the correct answer.
Expand|Select|Wrap|Line Numbers
  1. {
  2.     position = 0;
  3. }
  4.  
  5. void throttle::shift(int amount)
  6. // Precondition: None
  7. // Postcondition: Throttle’s position is moved by amount (but not below 0 or above top position).  If amount would move throttle beyond 0, set to 0.  If amount would move throttle beyond top position, set throttle to top position.
  8.  
This looks good given the function name.
Expand|Select|Wrap|Line Numbers
  1. {
  2.     // fill in correct code
  3. position += amount;
  4. if (position <0 )
  5.    position = 0;
  6. else if (position > top_postion)
  7. postion = top_postion;
  8.  
  9.  
  10.  
  11. }
  12.  
  13. bool throttle::is_on() const;
  14. // Precondition: None
  15. // Postcondition: false if position is zero, true otherwise
  16.  
This looks good given the function name.
Expand|Select|Wrap|Line Numbers
  1. {
  2.     // fill in correct code
  3.  
  4. return (postion > 0);
  5.  
Remember:
  • When generating preconditions, ask yourself "what statement must be true for you to be able to execute this function".
  • When generating a postcondition, ask yourself, "what statement must be true when this function returns".

These pre/postconditions can relate to parameters, member variables, global variables, what some function can return (though this should be constant no matter how many times you call it, otherwise if the internal code depends on it, you are going to get erroneous results in the function).

It is not always easy or even sometimes practical to have these checked at runtime. This doesn't usually matter except in real-time situations when you have to run debug code in a real-time situation. You should do as you were doing by putting in asserts to test your preconditions as well as your postconditions.

Wait till you get into loop and class invariants. :) Don't be afraid to come back when you have questions about those.

Hope this helps.


Adrian
Feb 21 '07 #11

P: 30
// FILE: throttle.cpp
// CLASS IMPLEMENTION FILE of throttle

#include "throttle.h"

throttle::throttle()
//fill in precondition and postcondition

//precondition: none
//postcondition: i
I wrote by mistake 'i' .I don't know what will be postcondition here.
Feb 21 '07 #12

RedSon
Expert 5K+
P: 5,000
Your pre and post conditions are something that only you can decide. It is the conditions that must be met before executing the code in your method and the conditions that are met by your code upon exiting. Stop asking us what your pre and post conditions are, we have no idea. Only you can decide what they should be.

Once you decide what your pre and post conditions are then you can write the code that will fulfill those conditions.

Adrian's post should have provided you with more then enough guidance to get started. Read it carefully, work on your program, watch a half an hour of Gilligan's Island, work on your program some more, and then post again.
Feb 21 '07 #13

AdrianH
Expert 100+
P: 1,251
// FILE: throttle.cpp
// CLASS IMPLEMENTION FILE of throttle

#include "throttle.h"

throttle::throttle()
//fill in precondition and postcondition

//precondition: none
//postcondition: i
I wrote by mistake 'i' .I don't know what will be postcondition here.
Do you have any defaults for a class instance? I would make those your postcondition.

One other thing. I'm not sure if your teacher has told you this, but it is possible to call a member function on a NULL object. In some cases this is ok, in most cases (anytime you must access a member variable of the class object means you are assuming this) it is not. You could put as a precondition for each of your member functions (excluding the constructors since it is assumed that this exists when the constructer is called due to the programme construct) a precondition that there is an object, i.e. this != NULL

Does this make sense?


Adrian
Feb 21 '07 #14

P: 30
Thank u Adrian
Feb 21 '07 #15

AdrianH
Expert 100+
P: 1,251
Thank u Adrian
No prob. Glad to help. Did you enjoy Gilligan's Island? :D


Adrian
Feb 21 '07 #16

Post your reply

Sign in to post your reply or Sign up for a free account.