Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: how to make a program take time...

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to make a program take time...

    Part of my assignment is to make a program where the user chooses 1 of the 7 basic running times and an example program is executed for that running time. Pardon my style as I have not programmed in Java for quite some time...
    import java.util.Scanner;
     
    class Runtimes
    {
    public static void main(String[]args)
    {
    String choice;
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter one of the following:\n 'a' : constant.\n 'b': logarithmic \n 'c': linear\n 'd': n log n\n 'e': quadratic\n 'f': cubic\n 'g': exponential");
    choice = scan.nextLine();
    long start, end, elapsed;
     
    if(choice.equals("a"))
    {
    int test;
    for(int i = 0; i < 10; i++)
    {
    start = System.currentTimeMillis();
    test = 5 + 8 * 4 + 7;
    end = System.currentTimeMillis();
    System.out.println( "n = " + i + "elapsed time: " + (end - start));
    }
     
    }
     
    if(choice.equals("b"))
    {
    start = System.currentTimeMillis();
    end = System.currentTimeMillis();
    }
     
    if(choice.equals("c"))
    {
     for(int i = 1; i < 10; i++)
     {
     int [] numbers = new int[i];
     start = System.currentTimeMillis();
     for(int j = 0; j < i; j++)
    {
     
     }
    end = System.currentTimeMillis();
    System.out.println( "n = " + i + "elapsed time: " + (end - start));
    }
    }
     
    if(choice.equals("d"))
    {
    start = System.currentTimeMillis();
    end = System.currentTimeMillis();
    }
     
    if(choice.equals("e"))
    {
    start = System.currentTimeMillis();
    end = System.currentTimeMillis();
    }
     
    if(choice.equals("f"))
    {
    start = System.currentTimeMillis();
    end = System.currentTimeMillis();
    }
     
    if(choice.equals("g"))
    {
    start = System.currentTimeMillis();
    end = System.currentTimeMillis();
    }
     
     
    }
    }
    for now I'm just working with linear and constant... My problem is that each step takes less than a millisecond to execute so it comes out with just 0s. I need to find a way to make it wait. Please help, thanks in advance.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,354
    Thanks
    182
    Thanked 832 Times in 775 Posts
    Blog Entries
    5

    Default Re: how to make a program take time...

    Thread has a static function sleep which allows you to put the current thread to sleep for a specified duration. It takes the number of milliseconds to sleep as the parameter:

    try{
        Thread.sleep(1000);//sleep for 1 second
    }catch (InterruptedException e){
     
    }

Similar Threads

  1. Help with a program to make a landscape picture
    By noseeds in forum Java Theory & Questions
    Replies: 1
    Last Post: December 15th, 2009, 10:25 PM
  2. How to make user press enter to continue in program?
    By BC2210 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 3rd, 2009, 06:08 AM
  3. SQL Time Calculation
    By r12ki in forum JDBC & Databases
    Replies: 3
    Last Post: July 31st, 2009, 04:54 AM
  4. Replies: 9
    Last Post: June 27th, 2009, 05:05 PM
  5. Java program to write game
    By GrosslyMisinformed in forum Paid Java Projects
    Replies: 3
    Last Post: January 27th, 2009, 03:33 PM