Question

In: Computer Science

Please answer in JAVA I am using java.time.Duration class. 1.). How can I add all of...

Please answer in JAVA

I am using java.time.Duration class.

1.). How can I add all of the given duration time?

for example:

Duration t1 = Duration.ofMinutes(2).plusSeconds(30)

Duration t2 = Duration.ofMinutes(3).plusSeconds(20)

then the result would be total = Duration.ofMinutes(5).plusSeconds(50)

*** and how would it be if it exceeds 59 minutes and its an hour.

2). How would I be able to add it all up on an ArrayList? does a for loop and simple add() works?

Solutions

Expert Solution

I have modified the answer according to your need. I hope this is what you wanted.

-----------------------------------------------------------------code--------------------------------------------------------------

import java.util.*;
import java.time.Duration;

public class Sample2{
   public static Duration totaltime( ArrayList<Duration> arrlist) {
       Duration time = null;
       for(int i=0;i<arrlist.size();i++){  
               if(i==0) {
                time=time.ofSeconds(i).plusSeconds(arrlist.get(i).getSeconds());//getting time in second from arraylist for 1st elemen
               }
               else {
               time=time.plusSeconds(arrlist.get(i).getSeconds());}
           }
   return time;      
   }
   public static void main(String args[]) {
       ArrayList<Duration> arrlist = new ArrayList<Duration>();
       Duration time;
       long k;
       Duration t1 = Duration.ofMinutes(2).plusSeconds(30);
       Duration t2 = Duration.ofMinutes(3).plusSeconds(30);
       Duration t3 = Duration.ofMinutes(5).plusSeconds(20);
       arrlist.add(t1);
       arrlist.add(t2);
       arrlist.add(t3);
       time=totaltime(arrlist);
       System.out.println(time);
      
      
   }  
   }

I hope this what you want. if you still have any doubt please let me know.

you can add given duration time using plusMinutes() and plusSeconds() methods. Duration class (Java) automatically converts 60 minutes into 1 hours. I have taken example with 3 time duration. it is shown below in code as well as in screenshot of this programs output

----------------------------------------------------code----------------------------------------------------------------------

import java.util.*;
import java.time.Duration;
public class Test{
   public static void main(String args[]) {
       Duration t1 = Duration.ofMinutes(2).plusSeconds(30);
       Duration t2 = t1.plusMinutes(3).plusSeconds(50);
       Duration t3 = t2.plusMinutes(54).plusSeconds(50);   
       System.out.println(t3);       
   }  
   }

2) yes you can easily add item into array list using for loop and add method. I have shown this in example.

code-------------------------------------------

import java.util.Scanner;
import java.util.*;

public class Main{
   public static void main(String[] args) {
       ArrayList<Integer> arrlist = new ArrayList<Integer>();// arraylist
          Scanner sc=new Scanner(System.in);// scanner class object sc
          int num;
          for (int i = 0; i < 4; i++) {               // it will add 4 items into arraylist
                 System.out.println("Enter number to add item in arraylist");
                 num=sc.nextInt();//reading input into num variable
                 arrlist.add(num);//adding item into arraylist
          }        
          System.out.println(arrlist); //printing array list
   }
}

---------------------------------------------Screenshots-----------------------------------------------------------------------------


Related Solutions

IN JAVA: I am using binary and linear search methods in java. How can I Generate...
IN JAVA: I am using binary and linear search methods in java. How can I Generate a new array with 10000 elements, and initialize the elements to random values using Math.random() and how can i sort the array using Arrays.sort(). I just need examples, no exact code is necessary. The array must be of doubles.
This is a java program I am trying to figure out how to add a couple...
This is a java program I am trying to figure out how to add a couple methods to a program I am working on. I need to be able to: 1. Remove elements from a binary tree 2. Print them in breadth first order Any help would appreciated. //start BinaryTree class /** * * @author Reilly * @param <E> */ public class BinaryTree { TreeNode root = null; TreeNode current, parent; private int size = 0, counter = 0; public...
PLEASE EXPLAIN ANSWER. USING JAVA via jGRASP i am getting an error messge a. Create a...
PLEASE EXPLAIN ANSWER. USING JAVA via jGRASP i am getting an error messge a. Create a class named Sandwich. Data fields include a String for the main ingredient (such as tuna), a String for bread type (such as wheat), and a double for price (such as 4.99). Include methods to get and set values for each of these fields. Save the class as Sandwich.java. b. Create an application named TestSandwich that instantiates one Sandwich object and demonstrates the use of...
In java please create a class with a method to be able to add ints into...
In java please create a class with a method to be able to add ints into a linked List and to be able to print out all the values at the end. public class Node { int data; Node next;
Add a generic Node class to the Java project. In the class Declare the fields using...
Add a generic Node class to the Java project. In the class Declare the fields using the generic type parameter, follow the book specification Define the accessor method getLink( ) Define the constructor, follow the book implementation Note: at the definition the name of the constructor is Node, however every time you use it as a type must postfix the generic type like Node<T> Define the addNodeAfter( ) method as explained in the PP presentation, use the generic type as...
I am in beginners java course so using the most simple/basic JAVA please complete the following...
I am in beginners java course so using the most simple/basic JAVA please complete the following CODE SEGMENTS. (2 parts) FIRST PART: Using ITERATIVE create a METHOD MAX that returns the max element in an ArrayList of ints and prints all of the elements. *you have to use an iterative in the method.* (just need to write the METHOD ONLY not a whole program(will do in 2nd part), assume you are given any numbers/integers. SECOND PART: Now write a class...
I am using NetBeans IDE Java to code and I am seeking comment to the code...
I am using NetBeans IDE Java to code and I am seeking comment to the code as well (for better understanding). Magic squares. An n x n matrix that is filled with the numbers 1, 2, 3, …, n2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. Write a program that reads in 16 values from the keyboard, displays them in a 4...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog {...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog { String catalog_name; ArrayList<Item> list; Catalog(String cs_Gift_Catalog) { list=new ArrayList<>(); catalog_name=cs_Gift_Catalog; } String getName() { int size() { return list.size(); } Item get(int i) { return list.get(i); } void add(Item item) { list.add(item); } } Thanks!
I am having trouble figuring out how to To-Do list using java. Can anyone guide me...
I am having trouble figuring out how to To-Do list using java. Can anyone guide me through the steps for achieving this?
Please use java to answer the below question. (i) Create a class Pencil with the attributes...
Please use java to answer the below question. (i) Create a class Pencil with the attributes brand (which is a string) and length (an integer) which are used to store the brand and length of the ruler respectively. Write a constructor Pencil (String brand, int length) which assigns the brand and length appropriately. Write also the getter/setter methods of the two attributes. Save the content under an appropriate file name. Copy the content of the file as the answers. (ii)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT