TO BE DONE IN JAVA
Your task is to complete the AddTime program so that it
takes multiple sets of hours, minutes and seconds
from the user in hh:mm:ss format and then computes
the total time elapsed in hours, minutes and seconds. This
can be done using the Time object already given to
you.
Tasks:
Ensure that the returned time values are within these ranges:
- seconds should be between 0 and 60. any value greater than 60 (ie. 80 seconds) should be converted to minutes with leftover seconds in the seconds slot. (ie. for 80 seconds, add 1 to minutes and set seconds to 20).
- minutes should be between 0 and 60, with values greater than 60 converted in the same manner as seconds.
- hours do not have an upper limit and can be left as-is.
You may add whatever methods you wish to the `AddTime` and `Time` classes but do not change the signatures of any existingmethods.
Requested files
AddTime.java
import java.util.ArrayList;
import java.util.Scanner;
public class AddTime {
private ArrayList<Time> time;
private Scanner scnr;
public AddTime() {
time = new ArrayList<>();
scnr = new Scanner(System.in);
}
public String promptInput() {
String userInput = scnr.next();
return userInput;
}
//Reminder: input format: (hh:mm:ss)
public void addTimeUnit(String input) {
//TODO:
}
public Time calculateTotalTime() {
//TODO:
}
}
Time.java
public class Time {
private int hours;
private int minutes;
private int seconds;
public Time(int hour, int minute, int second) {
hours = hour;
minutes = minute;
seconds = second;
}
public int getHours() {
return hours;
}
public int getMinutes() {
return minutes;
}
public int getSeconds() {
return seconds;
}
***Please upvote or thumbsup if you liked the answer***
Screenshot of the Java code:-
Demonstration and output:-
Creata a main method like this where creating five time instances and adding them
public static void main(String[] args) {
AddTime addTime = new
AddTime();
addTime.addTimeUnit(addTime.promptInput());
addTime.addTimeUnit(addTime.promptInput());
addTime.addTimeUnit(addTime.promptInput());
addTime.addTimeUnit(addTime.promptInput());
addTime.addTimeUnit(addTime.promptInput());
Time t =
addTime.calculateTotalTime();
System.out.println(t.getHours() + "
" + t.getMinutes() + " " + t.getSeconds()
);
}
Java code to copy:-
package timeConverter;
import java.util.ArrayList;
import java.util.Scanner;
public class AddTime {
private ArrayList<Time> time;
private Scanner scnr;
public AddTime() {
time = new ArrayList<>();
scnr = new Scanner(System.in);
}
public String promptInput() {
String userInput = scnr.next();
return userInput;
}
//Reminder: input format: (hh:mm:ss)
public void addTimeUnit(String input) {
//Processing the input in hh:mm::ss format
String[] timeUnits = input.split(":");
//Converting them to integers
int hours = Integer.parseInt(timeUnits[0]);
int minutes = Integer.parseInt(timeUnits[1]);
int seconds = Integer.parseInt(timeUnits[2]);
//Creating an instance of Time class
Time t = new Time(hours,minutes,seconds);
//Addting the instance to the arraylist
time.add(t);
}
public Time calculateTotalTime() {
int hours = 0,minutes = 0,seconds = 0;
//Iterating through the times in the arraylist
for(Time t:time)
{
hours += t.getHours();
minutes += t.getMinutes();
seconds += t.getSeconds();
}
minutes += seconds/60;
seconds = seconds % 60;
hours += minutes/60;
minutes = minutes % 60;
//Creating a new time object from the values
Time tot_time = new Time(hours,minutes,seconds);
return tot_time;
}
}
Get Answers For Free
Most questions answered within 1 hours.