Question

TO BE DONE IN JAVA Your task is to complete the AddTime program so that it...

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:

  • Complete addTimeUnit() in AddTime so that it processes a string input in hh:mm:ss format and adds a new Time unit to the ArrayList member variable.
  • Complete calculateTotalTime() so that it calculates the total time from the times in the list, does any required conversions of time values and returns a new Time object storing that total time.

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;

    }

Homework Answers

Answer #1

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

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Complete the program below. The program takes in two positive integers, n and m, and should...
Complete the program below. The program takes in two positive integers, n and m, and should print out all the powers of n that are less than or equal to m, on seperate lines. Assume both n and m are positive with n less than or equal to m. For example, if the input values are n = 2 and m = 40 the output should be: 2 4 8 16 32 Starter code: import java.util.Scanner; public class Powers {...
IN JAVA Methods*: Calorie estimator Write a method ActivityCalories that takes a string indicating an activity...
IN JAVA Methods*: Calorie estimator Write a method ActivityCalories that takes a string indicating an activity (sit, walk, jog, bike, swim) and duration in minutes (integer), and returns the estimated calories expended (double). Calories per minute for a 150 lb person (source): sit: 1.4 walk: 5.4 run: 13.0 bike: 6.8 swim: 8.7 If the input is sit 2, the output is 2.8 Hints: Use an if-else statement to determine the calories per minute for the given activity. Return the calories...
Complete the // TODO sections in the EasyRental class. Create a method that implements an iterative...
Complete the // TODO sections in the EasyRental class. Create a method that implements an iterative sort that sorts the vehicle rentals by color in ascending order (smallest to largest) Create a method to binary search for a vehicle based on a color, that should return the index where the vehicle was found or -1 You are comparing Strings in an object not integers. Ex. If the input is: brown red white blue black -1 the output is: Enter the...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
Java Program: You will be traversing through an integer tree to print the data. Given main(),...
Java Program: You will be traversing through an integer tree to print the data. Given main(), write the methods in the 'IntegerBinaryTree' class specified by the // TODO: sections. There are 6 methods in all to write. Ex: If the input is: 70 86 60 90 49 62 81 85 38 -1 the output should be: Enter whole numbers to insert into the tree, -1 to stop Inorder: 38 - 49 - 60 - 62 - 70 - 81 -...
Java Program: You will be inserting values into a generic tree, then printing the values inorder,...
Java Program: You will be inserting values into a generic tree, then printing the values inorder, as well as printing the minimum and maximum values in the tree. Given main(), write the methods in the 'BSTree' class specified by the // TODO: sections. There are 5 TODOs in all to complete. Ex: If the input is like ferment bought tasty can making apples super improving juice wine -1 the output should be: Enter the words on separate lines to insert...
Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or...
Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or start with the link below: https://www.thoughtco.com/using-the-arraylist-2034204 import java.util.Scanner; public class DaysOfWeeks { public static void main(String[] args) { String DAY_OF_WEEKS[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; char ch; int n; Scanner scanner = new Scanner(System.in); do { System.out.print("Enter the day of the Week: "); n = scanner.nextInt() - 1; if (n >= 0 && n <= 6) System.out.println("The day of the week is " + DAY_OF_WEEKS[n] + ".");...
Write a java program that calculates the number of duplicate words in a sentence. You may...
Write a java program that calculates the number of duplicate words in a sentence. You may assume that the input consists only 52 English characters with a single white-space as the separator between words (not punctuations in the inputted sentence). You need consider uppercase and lowercase letters the same. You may get the input from the keyboard. (You need to use Set and/or Hashset). What I have so far import java.util.HashSet; import java.util.Scanner; public class Duplicate { int counter; int...
Determine how all of your classes are related, and create a complete UML class diagram representing...
Determine how all of your classes are related, and create a complete UML class diagram representing your class structure. Don't forget to include the appropriate relationships between the classes. GameDriver import java.util.Scanner; public class GameDriver { Scanner in = new Scanner(System.in); public static void main(String[] args) { Move move1 = new Move("Vine Whip", "Grass", 45, 1.0f); Move move2 = new Move("Tackle", "Normal", 50, 1.0f); Move move3 = new Move("Take Down", "Normal", 90, 0.85f); Move move4 = new Move("Razor Leaf", "Grass",...
1. Complete a class Clock that represents time on a 24-hour clock, such as 00:00, 15:30,...
1. Complete a class Clock that represents time on a 24-hour clock, such as 00:00, 15:30, or 23:59 ○ Time is measured in hours (00 – 23) and minutes (00 – 59) ○ Times are ordered from 00:00 (earliest) to 23:59 (latest) Complete the first constructor of the class Clock ● It takes two arguments: h and m and creates a new clock object whose initial time is h hours and m minutes ● Test cases: Clock clock1 = new...