Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Distance traveled program in java

I was given partially completed code to finish but I can’t figure out what to do next.
The program asks the user for input to determine the distance driven.
I filled in most of the code, but I can’t seem to figure out what else to add for getDistance() to work.
could someone give me a suggestion?

The instructions are:

The distance a vehicle travels can be calculated as follows: Distance = Speed * Time For example, if a train travels 40 miles-per-hour for three hours, the distance traveled is 120 miles. Write a program that asks for the speed of a vehicle (in mph) and the number of hours it has traveled. It should use a loop to display the distance a vehicle has traveled for each hour of a time period specified by the user. For example, if a vehicle is traveling at 40 mph for a three-hour time period, it should display a report similar to the one that follows: Hour Distance Traveled

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

//Distance.java

public class Distance
{
   private double speed;   // The vehicle speed
   private int hours;      // The hours traveled

   /**
    * The constructor initializes the object
    * with a vehicle speed and number hours
    * it has traveled.
    */

   public Distance(double s, int h)
   {

   }

   /**
    * The setSpeed method sets the vehicle's
    * speed.
    */

   public void setSpeed(double s)
   {
       speed = s;
   }

   /**
    * The setHours method sets the number of
    * hours traveled.
    */

   public void setHours(int h)
   {
       hours = h;
   }

   /**
    * The getSpeed method returns the speed
    * of the vehicle.
    */

   public double getSpeed()
   {
       return speed;
   }

   /**
    * The getHours method returns the number
    * of hours traveled by the vehicle.
    */

   public int getHours()
   {
       return hours;
   }

   /**
    * The getDistance method returns the
    * distance traveled by the vehicle.
    */

   public double getDistance()
   {
       return speed * hours;
   }
}



//DistanceDemo.java

public class DistanceDemo
{
   public static void main(String[] args)
   {
      double speed;     // Vehicle's speed
      int maxHours;     // Number of hours
      int period;       // Counter for time periods

      // Create a Scanner object for keyboard input.
      Scanner keyboard = new Scanner(System.in);
  
      // Get the speed.
      System.out.print("Enter the vehicle's speed: ");
      speed = keyboard.nextDouble();
  
      // Validate the speed.
      while (speed < 1)
      {
          System.out.println("Enter a valid speed: ");
          speed = keyboard.nextDouble();
      }
  
      // Get the number of hours.
      System.out.print("Enter the number of hours the " +
                   "vehicle was in motion: ");
      maxHours = keyboard.nextInt();

      // Validate the hours.
      while (maxHours < 1)
      {
         System.out.println("Enter a vaid time");
         maxHours = keyboard.nextInt();
      }
  
      // Display the table header.
      System.out.println("Hour\tDistance Traveled");
      System.out.println("----------------------------------");
  
      // Display the table of distances.
      period = 1;
      while (period <= maxHours)
      {
         // Create a Distance object for this period.
         Distance d = new Distance(speed, period);
     
         // Display the distance for this period.
         System.out.println(period + "\t\t" + d.getDistance());
     
         // Increment period.
         period++;
      }
   }
}

>Solution :

In main function , You are setting speed and hours using Constructor.

Distance d = new Distance(speed, period);

But in Distance Class You are not initializing the variables speed and hour in Constructor.

   public Distance(double s, int h)
   {

   }

So , Write this Constructor as :

public Distance(double s, int h)
   {
      speed = s;
      hours = h;
   }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading