473,386 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

implemention file

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
15 1966
Ganon11
3,652 Expert 2GB
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
5,000 Expert 4TB
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
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
please help meeeeeeeeeeeee
Feb 21 '07 #5
Ganon11
3,652 Expert 2GB
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
i did not use assert statment.please tell me about other answer's r correct or not.
Feb 21 '07 #7
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
1,251 Expert 1GB
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
I need help throttle.cpp
Feb 21 '07 #10
AdrianH
1,251 Expert 1GB
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
// 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
5,000 Expert 4TB
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
1,251 Expert 1GB
// 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
Thank u Adrian
Feb 21 '07 #15
AdrianH
1,251 Expert 1GB
Thank u Adrian
No prob. Glad to help. Did you enjoy Gilligan's Island? :D


Adrian
Feb 21 '07 #16

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

Similar topics

2
by: matt | last post by:
I have compiled some code, some written by me, some compiled from various sources online, and basically i've got a very simple flat file photo gallery. An upload form, to upload the photos and give...
5
by: Dave Smithz | last post by:
Hi There, I have a PHP script that sends an email with attachment and works great when provided the path to the file to send. However this file needs to be on the same server as the script. ...
7
by: Joseph | last post by:
Hi, I'm having bit of questions on recursive pointer. I have following code that supports upto 8K files but when i do a file like 12K i get a segment fault. I Know it is in this line of code. ...
1
by: Mahishapura | last post by:
How do i write GetCommandLineArgs implemention? Can any one help
3
by: StGo | last post by:
How can i read/write file's custom attributs(like subject,author...) in C#??? Thanks :))
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
13
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming...
1
by: Roy | last post by:
Hi, I have a problem that I have been working with for a while. I need to be able from server side (asp.net) to detect that the file i'm streaming down to the client is saved...
3
by: Shapper | last post by:
Hello, I created a script to upload a file. To determine the file type I am using userPostedFile.ContentType. For example, for a png image I get "image/png". My questions are: 1. Where can...
1
by: mars | last post by:
Which file is the "printf" implemention in glibc? The stdio.c is very small. It seems using __printf_chk (__USE_FORTIFY_LEVEL - 1, __VA_ARGS__) And __printf_chk using __nldbl___vfprintf_chk, but...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.