0

I have class called Running with start time and end time:

public class Running {
    private double startTime;
        private double endTime;

    public Running(double startTime, double endTime) {
        this.startTime = startTime;
        this.endTime = endTime;
    }

}

And in my main class:

    Running runningA = new Running(1, 10);
    Running runningB = new Running(5,9);
    Running runningC = new Running(4, 8);

How can I calculate in the right way between which time most of them are running?

For example:

between 5 to 8 all of them are running

between 1 to 3 only one run

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
KotRog
  • 121
  • 9
  • Why all of them ? 13/19 seems outside of 5/10. Could you clarify what the values are ? Are 5-10 duration ? – azro Jun 21 '20 at 14:07
  • Sorry I wasn't very clear - that was actually an hypothetic example for exaplining the situation. I realized know It's only confusing so I deleted the example from the question – KotRog Jun 21 '20 at 14:14
  • 1
    That made the thing even more unclear because now we just don't know what you want. So take your times and give at least 2 examples of input and their expected output – azro Jun 21 '20 at 14:14
  • Hey KotRog, sorry but your question is very confusing.Not clear what does this mean, How can I calculate in the right way between which time most of them run? – ihimanshu19 Jun 21 '20 at 14:16
  • there are 5 ranges, for what sub-range does most of those ranges overlap – Joakim Danielson Jun 21 '20 at 14:18
  • Ya, I'm not a native speaker so I had hard time to find the words to describe my problem. I added more "simple" situation with examples, Hope It will be more clear. but it's simply what @JoakimDanielson said, when the most of them are overlap – KotRog Jun 21 '20 at 14:23
  • 1
    What kind of values are your startTime and endTime fields ? Hours of the day? It is really unclear to me. – Quanta Jun 21 '20 at 14:58
  • “class member” has a specific meaning in Java. So I rewrote your title for clarity and to avoid abusing that term. – Basil Bourque Jun 21 '20 at 16:06
  • Duplicate of: [*How to check a timeperiod is overlapping another time period in java*](https://stackoverflow.com/q/17106670/642706) – Basil Bourque Jun 21 '20 at 16:08

2 Answers2

3

I'm not sure why the Running class holds the start time and end time as doubles rather than ints, but okay.

Using your example:

Running runningA = new Running(1, 10);
Running runningB = new Running(5, 9);
Running runningC = new Running(4, 8);

We create a List<Running> to hold the 3 instances of Running. Then we do the following:

  1. Create a Map<Integer, Integer>

  2. Go through the List<Running> and find the minimum start time and the maximum end time.

  3. Start a time counter at the minimum start time.

  4. Go through the List<Running> and see which instances have a time counter in their range. The first instance has a 1, for example. So we put a (time counter, 1) in the map.

  5. Increment the time counter and repeat step 4 until the maximum end time.

  6. The Map contains the answer for each time. Iterate through the Map and summarize which time periods have 1 instance, which time periods have 2 instances, and so on.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
1

You can create a method in your class called 'isBetween' that returns if the object is between passed startTime and endTime:

public class Running {
    private double startTime;
    private double endTime;

public Running(double startTime, double endTime) {
    this.startTime = startTime;
    this.endTime = endTime;
}

public boolean isBetween(int startTime, int endTime) {
    
    return return startTime>=this.startTime && endTime<=this.endTime && startTime<=endTime;

}

@Override
public String toString() {
   
    return "Start time: " + this.startTime + " End time: " + this.endTime ;
}

}

And in your main method, you can verify if only one object is between two times, or if all objects in a list are between two given numbers:

public static void main(String[] args) {
    
    Running runningA = new Running(1, 10);
    Running runningB = new Running(5,9);
    Running runningC = new Running(4, 8);
    
    List<Running> list = new ArrayList<>();
    
    list.add(runningA);
    list.add(runningB);
    list.add(runningC);
            
//      for only one object:
        System.out.println(runningA.isBetween(5, 8)); //print true if object is between 5 and 8
        
//      for all objects in a list:      
        list.stream().filter(n -> n.isBetween(5, 8)).forEach(System.out::println); //print all objects between 5 and 8 
        
}
  • This will not yield the right result, the inBetween method is wrong since not both the start and end time for an object needs to be within a range. Only one of them needs to. – Joakim Danielson Jun 21 '20 at 20:50
  • You say that because when comparing, startTime can be greater than endTime. I corrected adding startTime<=endTime in expression. Thank you. – Grégori Fernandes de Lima Jun 22 '20 at 12:27