Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab 2: AlgorithmA is one approach to computing the sum of ( n ) consecutive integers. Prepare two more methods to handle the remaining two

Lab 2: AlgorithmA is one approach to computing the sum of \( n \) consecutive integers. Prepare two more methods to handle the remaining two algorithms outlined in the following table.

Figure 4-1

Three algorithms for computing the sum \( 1+2+\ldots+n \) for an integer \( n>0 \)

Remember to compute the runtime for each of the remaining two algorithms based on the implementation of AlgorithmA.

In the main method, call AlgorithmB and AlgorithmC using \( n \) values of \( 10,100,1000 \), and 10000. Display the runtimes of all three algorithms given the different \( n \) values in a table format. Refer to the following sample output for how this table can be designed.

image text in transcribed

Purpose: Part of assessing the efficiency of algorithms may be measuring the runtime of programs. In this lab, you will practice measuring the runtime of a section of Java code. One possible approach is to use System.nanoTime(), where the current time is stored as a long integer equal to the number of nanoseconds. The runtime of sections of code can be determined by computing the time before and after those sections. In-Class 2: Suppose AlgorithmA is the name of a method that contains the algorithm you want to measure. Use the following sample code to prepare AlgorithmA. public static long AlgorithmA(int n){ long start Time, endTime, elapsedTime; start Time = System . nanoTime () ; int sum =0; for (int i=1;i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions