Results 1 to 6 of 6
  1. #1
    Desmond is offline Member
    Join Date
    Mar 2010
    Posts
    17
    Rep Power
    0

    Default 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.
    Please submit your code below.
    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.


    Java 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");
    }
    }
    }

    Please someone help

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    I don't see a method; according to the specs there should be a method in your code:

    Java 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

  3. #3
    Desmond is offline Member
    Join Date
    Mar 2010
    Posts
    17
    Rep Power
    0

    Default

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

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Desmond View Post
    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:

    Java 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

  5. #5
    Desmond is offline Member
    Join Date
    Mar 2010
    Posts
    17
    Rep Power
    0

    Default

    I had the following, which has a problem compiling

    Java 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

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    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

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2010, 05:12 PM
  2. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 07:20 PM
  3. writng event listners ( seems complicated)
    By Basit56 in forum AWT / Swing
    Replies: 1
    Last Post: 08-25-2009, 09:11 AM
  4. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •