Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Visual Studio 2012 C# If you know a vehicle\'s speed and the amount of time it h

ID: 671966 • 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 please.

Explanation / Answer

C# Program to Calculate the Distance Travelled by Reading Speed and Time

class program

{

    public static void Main()

    {

        int speed, distance, time;

        Console.WriteLine("Enter the Speed(km/hr) : ");

        speed = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Enter the Time(hrs) : ");

        time = Convert.ToInt32(Console.ReadLine());

        distance = speed * time;

        Console.WriteLine("Distance Travelled (kms) : " + distance);

        Console.ReadLine();

    }

}

java Program to Calculate the Distance Travelled by Reading Speed and Time

import javax.swing.JOptionPane;

import java.io.*;

public class DistTravel

{

      public static void main(String[] args) throws IOException

      {       

               // Declare the variables.

               String input;

               int speed, time;

                       

               // The 'Distance Traveled' equation.

               Distance();

              

               // Display Data

               displayData(speed, time);

              

               System.exit(0);

      }

     

      public static int Distance()

      {       

               // Get the vehicle speed from the user.

              

               // Declare the variables

               int speed;

               int time;

     

speed = Integer.parseInt(input);        

               String input;    // To hold user's input.

              

               input = JOptionPane.showInputDialog("What is your vehicle's speed in miles-per-hours? ");

              

              

         while (speed <= 0)

                        {

                                input = JOptionPane.showInputDialog("Your speed must be greater than zero. Please re-enter. ");

                               

                                speed = Integer.parseInt(input);

                        }

              

               // Get the hours traveled from the user.

               input = JOptionPane.showInputDialog("How many hours have you traveled, thus far? ");

              

                        time = Integer.parseInt(input);

              

         while (time <= 1)

               {

                                input = JOptionPane.showInputDialog("How many hours have you traveled for? ");

                       

                                time = Integer.parseInt(input);

               }

                               

               return speed * time;

      }

     

      public static void displayData(int speed, int time)

      {               

               for(int i = 1; i <= time; i++)

                        {

                        JOptionPane.showMessageDialog(null, "Hour(s): Distance Traveled" +

                        "----------------------------" + "Hour" + ": " + i + (speed * i) + " miles traveled");

                        }

      } }