my teacher has asked us to ask a user for both a start time and stop time of a racer by asking the them to input a time (HH, mm, ss) using the JOptionPane. I am not sure how to do this. I have tried doing integer.parseint(joptionpane....) but I keep getting error messages when I input a time. Also, I know the problem is that since I'm asking a user to input a time for my Time class that it should be different. I need some help on where to get started from this point. Here is my code(I've also included my time class in it:
package Racers.java;
import javax.swing.JOptionPane;
public class Racers
{
public static void main(String[] args)
{
//racer1
String racer1;
Time startTime1;
Time stopTime1;
double elapsedTime1;
//assigning racer1
racer1 = JOptionPane.showInputDialog("Please enter the name of the first racer: ");
startTime1 = new Time();
stopTime1 = new Time();
elapsedTime1 = stopTime1.minus(startTime1).getTime();
JOptionPane.showMessageDialog(null,
"Here is the racer's name, start time, stop time, and elapsed time:\n"
+ racer1 + (" - ") + ("Start time: ") + startTime1 + ("; ") + ("Stop time: ") + stopTime1 + ("; ") + ("Elapsed time: ") + elapsedTime1 + "\n"
}//End main
}//End Racers
class Time
{
//Variable to hold seconds
double seconds;
//Constructors for class Time
public Time()
{
seconds = 0.0;
}
public Time(double newSeconds)
{
seconds = newSeconds;
}
public Time(int hours, int minutes, double newSeconds)
{
seconds = (double)(hours * 3600 + minutes * 60) + newSeconds;
}
//Observers for class Time
public double getTime()
{
//Return elapsed time
return seconds;
}
public int getHours()
{
//Compute whole hours from seconds
return (int)seconds / 3600;
}
public int getMinutes()
{
//Seconds after hours taken out
int remainingSeconds = (int)seconds % 3600;
//Compute minutes from remainder
return remainingSeconds / 60;
}
public double getSeconds()
{
//Seconds after minutes taken out
return seconds % 60.0;
}
//Returns HH:MM:SS.FFF
public String toString()
{
int hours = (int)seconds / 3600;
int minutes = (int)seconds % 3600 / 60;
return hours + ":" + minutes + ":" + seconds % 60.0;
}
//Operations for class Time
public Time plus(Time otherTime)
{
return new Time(seconds + otherTime.seconds);
}
public Time minus(Time otherTime)
{
return new Time(seconds - otherTime.seconds);
}
}//End Time
Here is the issue
startTime1 = new Time();
stopTime1 = new Time();
you are initializing start & stop time using default constructor where default constructor which returns start and stop time as zero hence you get output also zero.
So you need to use parameterized constructor to differentiate the times.
ie
startTime1 = new Time(10,10,10d); // 10hr 10 min 10 sec
stopTime1 = new Time(10,20,10d); //10hr 20 min 10 sec
then you find the difference of 10mins which is 600 seconds.
Also use below line
JOptionPane.showMessageDialog(null, "Here is the racer's name, start time, stop time, and elapsed time:\n" + racer1 + (" - ") + ("Start time: ") + startTime1 + ("; ") + ("Stop time: ") + stopTime1 + ("; ") + ("Elapsed time: ") + elapsedTime1 + "\n");
Hope you understand this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.