I need a java program written for the following question with a separate driver
ID: 3621321 • Letter: I
Question
I need a java program written for the following question with a separate driver and class fileThe distance a vehicle travels can be calculated as follows:
Distance = Speed * Time
Design a class that stores the speed of a vehicle in mph and the number of hours it has traveled. It should have a method named getDistance that returns the distance, in miles, that the vehicle has traveled.
Demonstrate the class in a program that uses 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 40mph for a three-hour time period, it should display a report similar to the one shown here.
Hour Distance Traveled
------------------------------------------------
1 40
2 80
3 120
Input validation: Do not accept a negative number for speed and do not accept any value less than one for time traveled.
Explanation / Answer
please rate - thanks
sorry I don't know how to do this as a class with a driver program
import java.util.*;
public class main
{
public static void main(String[] args)
{Scanner in=new Scanner(System.in);
int speed,time,i;
System.out.print("Enter speed: ");
speed=in.nextInt();
while(speed<0)
{System.out.println("invalid entry-must be non negative");
System.out.print("Enter speed: ");
speed=in.nextInt();
}
System.out.print("Enter time: ");
time=in.nextInt();
while(time<1)
{System.out.println("invalid entry-must be >=1");
System.out.print("Enter time: ");
time=in.nextInt();
}
System.out.println("Hour Distance Traveled ----------------------");
for(i=1;i<=time;i++)
System.out.println(i+" "+getDistance(i,speed));
}
public static int getDistance(int i,int speed)
{return i*speed;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.