Science homework help

Science homework help. Part I: Calculate Time in Hours/Minutes/Seconds
In this part created a C# form with textbox which is used for accepting user input i.e. time in seconds and a button to to calculate the time. Also, mouse leave event of a textbox is handled. Below is the screen shot of the same form i.e. CalculateTime.cs [Design]
 
 
Following is the code for CalculateTime.cs class
using System;
using System.Windows.Forms;
namespace ProgramToConvertSecondsToMinutesHours
{
public partial class CalculteTime : Form
{
int mySeconds;
public CalculteTime()
{
InitializeComponent();
}
private void btnTime_Click(object sender, EventArgs e)
{
mySeconds = System.Convert.ToInt32(txtTime.Text); //converted user input in int
MessageBox.Show(ConvertToTime(mySeconds)); // messagebox to display result
}
public string ConvertToTime(int timeInSeconds) // method used to convert time
{
int myHours = mySeconds / 3600; //3600 Seconds in 1 hour
mySeconds %= 3600;
int myMinutes = mySeconds / 60; //60 Seconds in a minute
mySeconds %= 60;
string mySec = mySeconds.ToString(),
myMin = myMinutes.ToString(),
myHou = myHours.ToString();
if (myHours < 10) { myHou = myHou.Insert(0, “0”); }
if (myMinutes < 10) { myMin = myMin.Insert(0, “0”); }
if (mySeconds < 10) { mySec = mySec.Insert(0, “0”); }
if (myMinutes == 0 && myHours == 0)
{
return mySec + ” Seconds”; // return time in seconds
}
else if (myHours == 0)
{
return myMin + ” Minutes” + ” ,” + mySec + ” Seconds”; // return time in minutes and seconds
}
else
{
return myHou + ” Hours” + ” , ” + myMin + ” Minutes” + ” , ” + mySec + ” Seconds”; //return time in hours, minutes and seconds
}
}
private void txtTime_MouseLeave(object sender, EventArgs e) // mouseleave event is handled to check for input
{
mySeconds = System.Convert.ToInt32(txtTime.Text);
if (mySeconds > 86400)
{
MessageBox.Show(“Enter valid time in seconds (less than 86400)”); // displaying error message with the help of messagebox
}
}
}
}
Output : When we enter 47 as an input we will get the output as messagebox which shown in below screen.
Input 1 : 47
 
Input 2: 645
 
Input 3: 86822
 
 
As per the above demonstration , you can try for different input values and check the output.
Part II :- Calculate Electricity Charges.
For this demonstration taken two textboxes , i.e. one for taking input eletricity from used and other textbox for displaying electricity charge and one button to calculate electricity charge. Below screen is the screen for the same.
Electricity_Charge.cs [Design]
 
Following is the code for above form
using System;
using System.Windows.Forms;
namespace ProgramToConvertSecondsToMinutesHours
{
public partial class Electricity_Charge : Form
{
public Electricity_Charge()
{
InitializeComponent();
}
private void Electricity_Charge_Load(object sender, EventArgs e)
{
}
private void txtElectricity_MouseLeave(object sender, EventArgs e)
{
double electricityInKwh = double.Parse(txtElectricity.Text);
if (electricityInKwh > 2000)
{
MessageBox.Show(“Electricity value can not exceed than 2000”);
}
}
private void btnCharge_Click(object sender, EventArgs e)
{
double electricityInKwh = double.Parse(txtElectricity.Text);
txtElectricityCharge.Text=calculateCharge(electricityInKwh);
}
public string calculateCharge(double electricityInKwh)
{
double amount;
if (electricityInKwh <= 100)
{
amount = electricityInKwh * 25;
}
else if (electricityInKwh <= 200)
{
amount = 2500 + ((electricityInKwh – 100) * 35);
}
else if (electricityInKwh <= 400)
{
amount = 6000 + ((electricityInKwh – 200) * 50);
}
else
{
amount = 16000 + ((electricityInKwh – 400) * 60);
}
return amount.ToString();
}
}
}
Output :
Input 1 : KWH more then 2000
Input 2 : KWH having valid value i.e. less than 2000
 
 

Science homework help