Question:
i need help writing a bool function for Microsoft Visual Studio 2005!?
63godtoh
2007-03-19 14:17:24 UTC
i have an assignment in my computer science class. i have to write a program that takes in the expiration date of a food product, the current date, and tells whether or not the product is expired or not.

i have to use 3 bool functions ( 'isvalid' returns if a date is valid, has parameters that represent one date) ( 'thesame' tells if the two dates are the same, has parameters that represent two dates) ( 'earlier' returns true if the first date is earlier than the second date and false otherwise, has parameters that represent two dates) and a void function ( 'output_date' which takes parameters that represent one date and outputs it to the screen)
here is the output:
Enter expiration date in the form mm/dd/yyyy: 2/1/2017
Enter today's date in the form mm/dd/yyyy: 2/15/2013
The expiration date you entered is 2/01/2017
The current date you entered is 2/15/2013
The expiration date is later than today, this food is ok!

i just need help writing a bool function. idk how to make one
Four answers:
Nitesh G
2007-03-19 14:29:44 UTC
try this



http://code.google.com/
csanon
2007-03-19 22:05:35 UTC
With all due respect to the question poster, if you don't understand our explanations, it may be a sign that you need to read your course material. If you ask about bool functions, but don't understand us when we talk about functions in general, and return types for functions, we can only ask what exactly do you know?



Also, Rex was is no way vague. In fact, the opposite as seen by his long and detailed answer. It is you in fact, who is terribly vague. You haven't even specified your programming language of choice (talk about vague). You refuse to specify what you don't understand about a bool function. For that matter, what do you mean by a bool function. Defining a function with a bool return type? Using a function with a bool return type?



Please take a good look at your assignment. It's not so much that you have to write a function that returns a boolean. Instead, you are given functions and must use them to determine if an expiration date is past the current date. This is really just a series of function calls.



Thus, you should read up on the section for functions, passing arguments to functions, and calling functions. You could write it all in one line of code, but I suspect you will want to have temporary variables. Hopefully, you know how to declare and define a variable, and you will have to make sure it is the correct return type of the function.



If you don't understand the previous paragraph, please read your course material. Or ask a more specific question we can help you on.
Rex M
2007-03-19 21:22:48 UTC
public bool MyFunctionName(DateTime expiry)

{

//logic here

return boolean;

}



OK, let's try again here. A bool function is a function (also known as a method) that returns a boolean value. Using c#, we construct a function/method like so:



accessor returntype functionname(parameters)

{

programming logic

return value

}



Parameters are defined as DataType parameterName, and each parameter is separated by a comma.



Now, for simplicity's sake, we will use the public accessor. We want to return a bool value. We will call the function/method MyFunctionName, and you say it needs to accept the expiration date of a food product, and the current date. Since it is pointless to have a function accept the current date (it can just look it up by itself), we will have it just accept the expiration date:



public bool MyFunctionName(DateTime expiry)

{



}



Now, lets try writing the boolean function "thesame":



public bool TheSame(DateTime date1, DateTime date2)

{

if(date1 == date2)

{

return true;

}

else

{

return false;

}

}



This can be simplified by writing:



public bool TheSame(DateTime date1, DateTime date2)

{

return date1==date2;

}



date1==date2 is an expression; think of it as declaring to the computer that the value of date1 equals the value of date2. If that statement ends up being true, the computer will return true. If it's false, the computer will return false.



I hope that gives you enough information to write the other methods in your project. I also hope you can be a little more appreciative in the future when strangers offer to help you do your homework for free.
Smutty
2007-03-20 19:43:21 UTC
I guess Rex M's answer was detailed and perfect.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...