please help me - throttle::throttle(int size)
-
// Precondition: size > 0;
-
// Postcondition: Throttle has size positions above the shutoff position, and its current position is off.
-
{
-
assert(size>0);
-
top_position = size;
-
position = 0;
-
}
i have to fiil code
is this ccorrect??
15 1966
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.
please help me - throttle::throttle(int size)
-
// Precondition: size > 0;
-
// Postcondition: Throttle has size positions above the shutoff position, and its current position is off.
-
{
-
assert(size>0);
-
top_position = size;
-
position = 0;
-
}
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: -
if (size < 1)
-
{
-
return -1;//error code to say that precondition not met
-
}
-
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 - // FILE: throttle.cpp
-
// CLASS IMPLEMENTION FILE of throttle
-
-
#include "throttle.h"
-
-
throttle::throttle()
-
//fill in precondition and postcondition
-
-
//precondition: none
-
//postcondition:
-
{
-
top_position = 1;
-
position = 0;
-
}
-
-
throttle::throttle(int size)
-
// Precondition: size > 0;
-
// Postcondition: Throttle has size positions above the shutoff position, and its current position is off.
-
{
-
//fill in correct code
-
top_position = 1;
-
position = 0;
-
-
-
}
-
-
void throttle::shut_off()
-
//fill in precondition and postcondition
-
//precondition: none( because member functions no precondition right??)
-
//postcondition: the throttle has been turned off.
-
{
-
position = 0;
-
}
-
-
void throttle::shift(int amount)
-
// Precondition: None
-
// 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.
-
{
-
// fill in correct code
-
position += amount;
-
if (position <0 )
-
position = 0;
-
else if (position > top_postion)
-
postion = top_postion;
-
-
-
-
}
-
-
bool throttle::is_on() const;
-
// Precondition: None
-
// Postcondition: false if position is zero, true otherwise
-
{
-
// fill in correct code
-
-
return (postion > 0);
-
}
File: usingthrottle.cpp - #include "throttle.h"
-
#include <iostream>
-
using namespace std;
-
//creates two throttles and shifts the second throttle an inputted amount.
-
void main(){
-
throttle t1;
-
int value;
-
cout << "Type in a throttle size." << endl;
-
cin >> value;
-
throttle t2(value);
-
cout<< “Type in a shift amount.” << endl;
-
cin >> value;
-
t2.shift(value);
-
if (t2.is_on())
-
cout << “Throttle on” << endl;
-
else
-
cout << “Throttle off” << endl;
-
}
please help meeeeeeeeeeeee
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.
i did not use assert statment.please tell me about other answer's r correct or not.
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. -
// FILE: throttle.cpp
-
// CLASS IMPLEMENTION FILE of throttle
-
-
#include "throttle.h"
-
-
throttle::throttle()
-
//fill in precondition and postcondition
-
-
//precondition: none
-
//postcondition: i
-
{
-
top_position = 1;
-
position = 0;
-
}
-
-
throttle::throttle(int size)
-
// Precondition: size > 0;
-
// Postcondition: Throttle has size positions above the shutoff position, and its current position is off.
-
{
-
//fill in correct code
-
top_position = 1;
-
position = 0;
-
-
-
}
-
-
void throttle::shut_off()
-
//fill in precondition and postcondition
-
//precondition: none( because member functions no precondition right??)
-
//postcondition: the throttle has been turned off.
-
{
-
position = 0;
-
}
-
-
void throttle::shift(int amount)
-
// Precondition: None
-
// 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.
-
{
-
// fill in correct code
-
position += amount;
-
if (position <0 )
-
position = 0;
-
else if (position > top_postion)
-
postion = top_postion;
-
-
-
-
}
-
-
bool throttle::is_on() const;
-
// Precondition: None
-
// Postcondition: false if position is zero, true otherwise
-
{
-
// fill in correct code
-
-
return (postion > 0);
-
}
-
File: usingthrottle.cpp -
#include "throttle.h"
-
#include <iostream>
-
using namespace std;
-
//creates two throttles and shifts the second throttle an inputted amount.
-
void main(){
-
throttle t1;
-
int value;
-
cout << "Type in a throttle size." << endl;
-
cin >> value;
-
throttle t2(value);
-
cout<< “Type in a shift amount.” << endl;
-
cin >> value;
-
t2.shift(value);
-
if (t2.is_on())
-
cout << “Throttle on” << endl;
-
else
-
cout << “Throttle off” << endl;
-
}
-
please help me.
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
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: -
// FILE: throttle.cpp
-
// CLASS IMPLEMENTION FILE of throttle
-
-
#include "throttle.h"
-
-
throttle::throttle()
-
//fill in precondition and postcondition
-
-
//precondition: none
-
//postcondition: i
-
what does 'i' mean? -
{
-
top_position = 1;
-
position = 0;
-
}
-
-
throttle::throttle(int size)
-
// Precondition: size > 0;
-
// 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? -
current position is off.
-
{
-
//fill in correct code
-
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. -
top_position = 1;
-
position = 0;
-
-
-
}
-
-
void throttle::shut_off()
-
//fill in precondition and postcondition
-
//precondition: none( because member functions no precondition right??)
-
//postcondition: the throttle has been turned off.
-
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. -
{
-
position = 0;
-
}
-
-
void throttle::shift(int amount)
-
// Precondition: None
-
// 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.
-
This looks good given the function name. -
{
-
// fill in correct code
-
position += amount;
-
if (position <0 )
-
position = 0;
-
else if (position > top_postion)
-
postion = top_postion;
-
-
-
-
}
-
-
bool throttle::is_on() const;
-
// Precondition: None
-
// Postcondition: false if position is zero, true otherwise
-
This looks good given the function name. -
{
-
// fill in correct code
-
-
return (postion > 0);
-
}
-
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
// 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.
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.
// 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
Thank u Adrian
No prob. Glad to help. Did you enjoy Gilligan's Island? :D
Adrian
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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.
...
|
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. ...
|
by: Mahishapura |
last post by:
How do i write GetCommandLineArgs implemention? Can any one help
|
by: StGo |
last post by:
How can i read/write file's custom attributs(like subject,author...) in
C#???
Thanks :))
|
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....
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
| |