# Complicated Method

• 03-17-2010, 11:22 AM
Desmond
Complicated Method
Hi, I need help with the following problem

timePeriodMinutes:
This method takes two timestamps (each timestamp is represented by the number of milliseconds that elapsed since January 1, 1970 00:00:00). The method computes the time period between the two timestamps (i.e., the difference between them) in minutes. When computing the time period in minutes, the method rounds up the resulting value. For instance, given inputs 1000000 and 2000000, the method returns 17; and given inputs 2000000 and 1080000, the method returns 16.
The following is the method prototype for the method:
long timePeriodMinutes(long timestamp1, long timestamp2)
If timestamp1 or timestamp2 is negative, the method should indicate an error condition by returning -1.
class studentSubmission {
long timePeriodMinutes(long timestamp1, long timestamp2) {
}
}

I have managed to come with my version of the solution which runs (shown below). However I am failing to conform to the strict requirement of structuring the method according to instructions above.

Code:

```import java.util.Scanner; public class studentSub { public static void main(String[] args) { double timePeriodMinutes; long timestamp1, timestamp2; System.out.println("Enter Two timestamps :"); Scanner keyboard = new Scanner(System.in); timestamp1 = keyboard.nextInt(); timestamp2 = keyboard.nextInt(); if ((timestamp1<0) || (timestamp2<0)) {   System.out.println("Error -1"); } else {   timePeriodMinutes = Math.ceil((timestamp1-timestamp2)/(1000 *60)); timePeriodMinutes = Math.abs(timePeriodMinutes); System.out.println(timePeriodMinutes + "  minutes"); } } }```

• 03-17-2010, 11:32 AM
JosAH
I don't see a method; according to the specs there should be a method in your code:

Code:

```static long timePeriodMinutes(long timestamp1, long timestamp2) { // your code goes here ... }```
b.t.w. rounding up the time difference in minutes for values t1 and t2 can be more easily done like this: (t2-t1+59000)/60000, i.e. you add 59 seconds to the difference so it will be rounded up to the next minute after dividing.

kind regards,

Jos
• 03-17-2010, 11:38 AM
Desmond
That is what I am struggling with, I just can't get it right with the prototype method
• 03-17-2010, 11:44 AM
JosAH
Quote:

Originally Posted by Desmond
That is what I am struggling with, I just can't get it right with the prototype method

You do know how to define methods don't you? It should look something like this:

Code:

```public class YourClass {   public static void main(String[] args) {       // a bit of code here and       // a call to the following method:   }   static long timePeriodMinutes(long timestamp1, long timestamp2) {       // your code goes here ...   } }```
kind regards,

Jos
• 03-17-2010, 12:01 PM
Desmond
I had the following, which has a problem compiling

Code:

```import java.util.Scanner; { public class studentSubmissions { public static void main(String[] args); long timePeriodMinutes; long timestamp1, timestamp2; System.out.println("Enter Two timestamps :"); Scanner keyboard = new Scanner(System.in); timestamp1 = keyboard.nextInt(); timestamp2 = keyboard.nextInt(); timePeriodMinutes = new studentSubmission1().timePeriodMinutes(timestamp1, timestamp2); System.out.println(timePeriodMinutes); } } class studentSubmission1 { long timePeriodMinutes(long timestamp1, timestamp2); long timePeriodMinutes; if (timestamp1<0) || (timestamp2<0) return -1; else timePeriodMinutes = (timestamp1-timestamp2)/ (1000 *60); timePeriodMinutes = abs(timePeriodMinutes); timePeriodMinutes = Math.ceil(timePeriodMinutes); return timePeriodMinutes; } }```
PS. I am very new to Java
• 03-17-2010, 12:31 PM
JosAH
Use the structure I outlined in my reply #4 and don't invent other classes; the way you did it will never work.

kind regards,

Jos