Visual Studio 2012 C# If you know a vehicle\'s speed and the amount of time it h
ID: 672060 • Letter: V
Question
Visual Studio 2012 C#
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.
This is what I have so far:
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;
namespace HW5CH5_1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void calculateButton_Click(object sender, EventArgs e)
{
double vehicle;
int hours;
int count = 1;
if (double.TryParse(vehicleSpeedTextBox.Text, out vehicle))
{
if (int.TryParse(hoursTextBox.Text, out hours))
while (count <= hours)
vehicle = vehicle * hours;
outputListBox.Items.Add("After hour " + count + " the distance is " + vehicle.ToString());
count = count + 1;
}
}
}
}
It's not working for me. Can someone correct this? Correct it in the code or make a completely new one similar to this one so I can understand it please.
Explanation / Answer
or
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim Speed, hrDistance, totalDistance, partialHours As Double
Dim Hrs As Double
Dim Cnt, wholeHours As Integer
Speed = InputBox("Enter the car speed in MPH")
If Not IsNumeric(Speed) And Len(Speed) > 0 Then
MsgBox("You must enter a numeric value. Try again from start.")
Exit Sub
End If
Speed = Val(Speed)
Hrs = InputBox("Enter the time travel in hours.")
If Not IsNumeric(Hrs) And Len(Hrs) > 0 Then
MsgBox("You must enter a numeric value. Try again from start.")
Exit Sub
End If
Hrs = Val(Hrs)
totalDistance = Hrs * Speed
wholeHours = Hrs 1
partialHours = Hrs Mod 1
ListBox1.Items.Add("Vehicle speed: " & Speed)
ListBox1.Items.Add("Hours travelled : " & Hrs)
ListBox1.Items.Add("--------------------...
ListBox1.Items.Add(" ")
For i = 1 To wholeHours
hrDistance = Speed
ListBox1.Items.Add("Distance travelled in hour " & i & ": " & hrDistance & " miles")
Next
ListBox1.Items.Add("The distance travelled in the last " & Hrs - wholeHours & " hour : " & Speed * (Hrs - wholeHours))
ListBox1.Items.Add(" ")
ListBox1.Items.Add(" ")
ListBox1.Items.Add("The total distance travelled is : " & totalDistance * " miles")
End Sub
End Class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.