Home > OS >  Distance traveled program in java
Distance traveled program in java

Time:11-06

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

//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  ;
      }
   }
}

CodePudding user response:

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;
   }

CodePudding user response:

You need to fill in the constructor of Distance to set the values for speed and hours

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

CodePudding user response:

Change Distance Constructor to set speed and hours during creation of the object

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

Change the Distance Method to

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

And Replace

// 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  ;
  }

with

Distance d = new Distance(speed,maxHours);
      
      for (int i = 1;i<=maxHours;i  ) {
        System.out.println("Hour " i " " "Distance Traveled" " " d.getDistance(i));  


     

No need to complicate with extra variables just change the getDistance method with a parameter of an hour, so it will return the distance at a certain Hour, and a for loop changing the hour from 1 to the current hour will give you accurate results.

  •  Tags:  
  • java
  • Related