In: Computer Science
ISYS 350, Assignment 2,
Part 1:
Create a C# Form with a textbox and a button. The box is for a user to enter a number
of seconds. And when the user clicks the button, the program displays the equivalent
number of hours, minutes and seconds using a MessageBox. Show method. If the
seconds entered is less than 60, your program should only display the seconds; if the
seconds is a least 60 and less than 3600, your program should display minutes and
seconds; if the second is at least 3600, your program should display hours, minutes
and seconds.
Use the following data to test your program:
47 seconds: 47 seconds (don’t show 0 hour and 0 minute)
645 seconds: 10 minutes, 45 seconds (don’t show 0 hour)
7565 seconds: 2 hours, 6 minutes, 5 seconds
Requirements:
1. Input validation: The number of seconds cannot exceed 86400. You must use
textbox’s Validating event to do the validation.
2. Turn in the form’s screenshot and the code. (Note: You can use the Alt
+ PrintScrn keys to capture only the active window.)
Part 2: An electric company charges to their customers based on Kilowatt-Hours
(Kwh) used. The rules to compute the charge are:
First 100 Kwh, 35 cents per Kwh
Each of the next 100 Kwh (up to 200 Kwh), 45 cents per Kwh
(the first 100 Kwh used is still charged at 35 cents each)
Each of the next 300 Kwh (up to 500 Kwh) 65 cents per Kwh
All Kwh over 500, 80 cents per KH
Create a C# Form with a textbox to enter Kwh used, a read-only textbox to display the
electricity charges, and a button to compute the charges. The Kwh used could be a
number with decimals.
Requirements:
1. Input validation: Use the KWH textbox validating event to ensure the KWH cannot
exceed 2000. Test your program with (1) Kwh=4500, (2) Kwh = 350
2. Turn in the form’s screenshot and the code
Code:-
1)
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);
MessageBox.Show(ConvertToTime(mySeconds));
}
public string ConvertToTime(int timeInSeconds)
{
int myHours = mySeconds / 3600;
mySeconds %= 3600;
int myMinutes = mySeconds / 60;
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";
}
else if (myHours == 0)
{
return myMin + " Minutes" + " ," + mySec + " Seconds";
}
else
{
return myHou + " Hours" + " , " + myMin + " Minutes" + " , " + mySec + " Seconds";
}
}
private void txtTime_MouseLeave(object sender, EventArgs e)
{
mySeconds = System.Convert.ToInt32(txtTime.Text);
if (mySeconds > 86400)
{
MessageBox.Show("Enter valid time in seconds (less than 86400)");
}
}
}
}
Code Screenshots:-
2)
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 * 35;
}
else if (electricityInKwh <= 200)
{
amount = 2500 + ((electricityInKwh - 100) * 45);
}
else if (electricityInKwh <= 500)
{
amount = 6000 + ((electricityInKwh - 200) * 65);
}
else
{
amount = 16000 + ((electricityInKwh - 500) * 80);
}
return amount.ToString();
}
}
}
Code Screenshots:-
Please UPVOTE thank you...!!!