Hello:)
Previously I have made a temperature convertor web app with code behind the button click.
I have now been asked to do one using methods/functions and am getting stuck on vaiables to pass and how I do it. Could someone explain variable naming and passing to me in a way that I can finally get it to remain in my thick head please:confused:.
Below is the pseudocode and my attempt - which I know is confused.
Thank you.
Structured English
Function (button click)
BEGIN
CALL Input_function
CALL Calc_function
CALL Display_function
END
Input_function
BEGIN
OBTAIN Init_Temp
OBTAIN Conversion_type
END
Calc_function
BEGIN
IF Conversion_type = FtoC THEN
New_Temp = (Init_temp -32)/1.8
END IF
IF Conversion_type = CtoF THEN
New_Temp = (Init_temp *1.8) + 32
END IF
END
Display_function
BEGIN
DISPLAY New_TEMP
END
And my attempt ...
namespace TemperatureConvertorPractice
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnConvert_Click(object sender, EventArgs e)
{
float inputTemp = inputMethod(Temp); //CALL Input_function
float returnTemp = calculateTemp(newTemp); //CALL Calc_function
float newTemp = displayAnswer(answerTemp); // CALL Display-fuction
}
public float inputMethod(float Temp) // this is the method for calculating the conversion OBTAIN initialTemp and OBTAIN Conversion type
{
float temp = float.Parse(txtInitialTemp.Text);// get initial temp (converts string representation of number to float)
return temp;
}
public float calculateTemp(float initialTemp)// (float fTemp, float cTemp)this is the method for calculating the conversion
{
try
{//open try
if (rbtnFtoC.Checked == true) //if you are converting from F to C
{
float newTemp = (initialTemp - 32) * 5 / 9;//Do Calculation- formula for F to C
return newTemp; //Return Result
}
else if (rbtnCtoF.Checked == true)//if you are converting from C to F
{
float newTemp = (initialTemp * 9 / 5) + 32;//Do Calculation- formula for C to F
return newTemp;//Return Result
}
}//close try
catch (Exception w)
{
MessageBox.Show("Please enter a number because " + w.Message);//Mesage Box if letter entered
txtNewTemp.Clear();//clears Celsius text box if you enter a letter
txtInitialTemp.Clear();//clears Fahrenheit text box if you enter a letter
}
}
public float displayAnswer(float answerTemp)
{
txtNewTemp.Text = answerTemp.ToString();
return answerTemp;
}