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...
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 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...
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...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with user input of 9, 5, 2, -1, then with user input of 0, -17, then with user input of 0 1 0 -1. See "How to Use zyBooks". Also note: If the submitted code has an...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...
Hello, I am trying to create a Java program that reads a .txt file and outputs...
Hello, I am trying to create a Java program that reads a .txt file and outputs how many times the word "and" is used. I attempted modifying a code I had previously used for counting the total number of tokens, but now it is saying there is an input mismatch. Please help! My code is below: import java.util.*; import java.io.*; public class Hamlet2 { public static void main(String[] args) throws FileNotFoundException { File file = new File("hamlet.txt"); Scanner fileRead =...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT