Code Monkey home page Code Monkey logo

maven.sumofinput's Introduction

SumOfInput

Sum the Numbers

Objective:

  • Write a program which prompts the user to input a number, n.
  • The program should respond by printing the sum of the numbers 1 to n.
  • Constraint: No Guassian wizardry !
  • Extra Credit (get 256 pts) create a second method which USES the Gaussian Wizardry
  • Extra EXTRA Credit (32,767 pts) create a comparison of the two methods And time them, showing which one is faster!!!

Purpose:

  • To establish familiarity with
    • Loops
    • User input
    • Object instantation/declaration
    • Method invokation

Resources:

timing loops

To time two different Java loops, you can use the System.currentTimeMillis() method to get the current time in milliseconds before and after each loop, and then calculate the difference between the two times to get the elapsed time.

Here is an example of how to time two different Java loops:

public class LoopTimingExample {
    public static void main(String[] args) {
        long startTime, endTime, elapsedTime;

        // Loop 1
        String foo = "!";
        startTime = System.currentTimeMillis();
        for (int i = 0; i < 1000000; i++) {
            // do something
            foo = "foo" + foo + "bar";
        }
        endTime = System.currentTimeMillis();
        elapsedTime = endTime - startTime;
        System.out.println("Loop 1 elapsed time: " + elapsedTime + " ms");

        // Loop 2
        String bar = "^";
        startTime = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++) {
            // do something else
            bar = bar + "bar ";
        }
        endTime = System.currentTimeMillis();
        elapsedTime = endTime - startTime;
        System.out.println("Loop 2 elapsed time: " + elapsedTime + " ms");
    }
}

In this example, two loops are timed using the System.currentTimeMillis() method. The elapsed time for each loop is calculated by subtracting the start time from the end time, and the result is output to the console.

Timing Java loops can be useful for measuring the performance of different algorithms or code optimizations.

Unit Test

No Unit Tests (well, not yet).

What's a millisecond?

A millisecond is a unit of time that is equal to one thousandth of a second. It is commonly used in computer programming to measure the duration of operations or to time events. For example, a program might measure the time it takes to perform a calculation in milliseconds, or it might use a delay of a certain number of milliseconds to control the timing of events. In general, a millisecond is a very short amount of time, but it can be significant in certain contexts, such as real-time systems or high-performance computing.

maven.sumofinput's People

Contributors

kristofer avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.