Visual Studio 2012 C# Original Question If you know a vehicle\'s speed and the a
ID: 673481 • Letter: V
Question
Visual Studio 2012 C#
Original Question
If you know a vehicle's speed and the amount of time it has travelled, you can calculate the distance it has travelled as follows:
* Distance = Speed * Time
For example, if a train travels 40 miles per hour for 3 hours, the distance travelled is 120 miles. Create an application with a form of the Calculator.
When the user clicks the calculate button, the application should display an input box asking the user for the speed of the vehicle in miles-per-hour, followed by another input box asking for the amount of time, in hours, that the vehicle has travelled. Then it should use a loop to display in a list box the distance the vehicle has travelled for each hour of that time period.
ORIGINAL ANSWER:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace HW
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void calculateButton_Click(object sender, EventArgs e)
{
int count = 1;
int vehicle;
int hours;
// read text put into text boxes
if (int.TryParse(vehicleSpeedTextBox.Text, out vehicle))
{
if (int.TryParse(hoursTextBox.Text, out hours))
{
// loop number of hours displayed
while (count <= hours)
{
int distance = vehicle * count;
try
{
// write to file
//name file
StreamWriter DistanceFile;
DistanceFile = File.CreateText("Distance File.txt");
for (count = 1; count <= hours; count++)
{
distance = vehicle * count;
// convert listbox to string
outputListBox.Text = distance.ToString();
outputListBox.Items.Add("After hour " + count + " the distance is " + distance.ToString());
DistanceFile.WriteLine("After hour " + count + " the distance is " + distance.ToString());
}
DistanceFile.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}
private void exitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Modify the Distance Calculator program that you wrote before so it writes its output to a file instead of diplaying it in a ListBox control. Open the file in notepad or Visual Studio to confirm the output.
Explanation / Answer
This code is fine
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.