When you execute the CompareLoopTimes program shown in Figure 6-29, you will occasionally see a negative number
Question:
When you execute the CompareLoopTimes program shown in Figure 6-29, you will occasionally see a negative number output when the nanoseconds values retrieved fall in different seconds. Modify the program to fix this problem, and save the file as CompareLoopTimes3.java. (Hint: It might take hundreds or thousands of executions for you to “catch” the program near the end of a second in order to test your modifications. For testing purposes, you can assign values to the start and stop times instead of retrieving them from the LocalDateTime class.)
Transcribed Image Text:
import java.time.*; public class CompareloopTimes { public static void main (String[] args) { int startTime, endTime; final int REPEAT = 100_000; final int FACTOR = 1_000_000; LocalDateTime now; now = Loca1DateTime.now(); startTime = now.getNano(%; for (int x = 0; x <= REPEAT; ++x) for (int y = 0; y <= REPEAT; ++y); now = LocalDateTime.now (); endTime = now.getNano (); System.out.println("Time for loops starting from 0: "+ ((endTime - startTime) / FACTOR) + " milliseconds"); now = LocalDateTime.now (); startTime = now.getNano(); for (int x = REPEAT; x >= 0; --x) for (int y = REPEAT; y >= 0; --y); now = LocalDateTime.now(); endTime = now.getNano (); System.out.println("Time for loops ending with 0: ((endTime - startTime) / FACTOR) + " milliseconds"); } } Figure 6-29 The CompareLoopTimes application
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 73% (15 reviews)
import javatime public class CompareLoopTimes3 public static void mainString args int star...View the full answer
Answered By
Atuga Nichasius
I am a Highly skilled Online Tutor has a Bachelor’s Degree in Engineering as well as seven years of experience tutoring students in high school, bachelors and post graduate levels. I have a solid understanding of all learning styles as well as using asynchronous online platforms for tutoring needs. I individualise tutoring for students according to content tutoring needs assessments.
My strengths include good understanding of all teaching methods and learning styles and I am able to convey material to students in an easy to understand manner. I can also assists students with homework questions and test preparation strategies and I am able to help students in math, gre, business , and statistics
I consider myself to have excellent interpersonal and assessment skills with strong teaching presentation verbal and written communication
I love tutoring. I love doing it. I find it intrinsically satisfying to see the light come on in a student's eyes.
My first math lesson that I taught was when I was 5. My neighbor, still in diapers, kept skipping 4 when counting from 1 to 10. I worked with him until he could get all 10 numbers in a row, and match them up with his fingers.
My students drastically improve under my tutelage, generally seeing a two grade level improvement (F to C, C to A, for example), and all of them get a much clearer understanding!
I am committed to helping my students get the top grades no matter the cost. I will take extra hours with you, repeat myself a thousand times if I have to and guide you to the best of my ability until you understand the concept that I'm teaching you.
5.00+
2+ Reviews
10+ Question Solved
Related Book For
Question Posted:
Students also viewed these Computer science questions
-
1. Write a Java program that calls a method called reverse3 that accepts an ArrayList of integer values as a parameter and reverses each successive sequence of three values in the list. If the list...
-
Great Slate, a medium-sized slate flooring and finishes company, is located in Castleton, VT. Great Slate maintains more than a thousand customers who order slate products as often as weekly or as...
-
The water usage in a small town is measured in gallons per hour. A plot of this rate of usage is shown in Figure 6 for the hours midnight through noon for a particular day. Estimate the total amount...
-
Data Set 14 in Appendix B lists highway fuel consumption amounts (mi/gal) for cars categorized by size (small, midsize, large). If we use those highway fuel consumption amounts arranged into the...
-
Replacement times for TV sets are normally distributed with a mean of 8.2 years and a standard deviation of 1.1 years (based on data from "Getting Things Fixed," Consumer Reports). Estimate the...
-
A 90% interest in Saxton Corporation was purchased by Palm Incorporated on January 2, 2019. The capital stock balance of Saxton Corporation was $3,000,000 on this date, and the balance in retained...
-
With the rider in the racing position, how much more power is required to pedal a bicycle at \(15 \mathrm{mph}\) into a \(20-\mathrm{mph}\) head-wind than at \(15 \mathrm{mph}\) through still air?...
-
On November 15, 2008, Solanski Inc. imported 500,000 barrels of oil from an oil company in Venezuela. Solanski agreed to pay 50,000,000 bolivars on January 15, 2009. To ensure that the dollar outlay...
-
Suppose you are tracking objects floating in the ocean. The ocean currents can be variable, so you've modeled the system as a directed graph G, where each vertex representing a region of ocean can...
-
Of a finance companys loans, 1% are defaulted (not completely repaid). The company routinely runs credit checks on all loan applicants. It finds that 30% of defaulted loans went to poor risks, 40% to...
-
Which of the following has the lowest precedence? a. < b. == c. && d. ||
-
a. Write an application that counts by three from 3 through 300 inclusive, and that starts a new line after every multiple of 30 (30, 60, 90, and so on). Save the file as CountByThrees.java. b....
-
List the classification of each of the following accounts as A (asset), L (liability), OE (owners equity), R (revenue), or E (expense). Write Debit or Credit to indicate the increase side, the...
-
In the LinkedList implementation of Section 16.1, we use a flag isAfterNext to ensure that calls to the remove and set methods occur only when they are allowed. It is not actually necessary to...
-
Repeat Exercise P15.3 , but use a Map for the coefficients. Data from exercise P15.3 Write a class Polynomial that stores a polynomial such as: p(x) = 5x0 + 9x-x-10 as a linked list of terms. A term...
-
Why is the collection of the values of a map not a set?
-
Reimplement Exercise E15.4 so that the keys of the map are objects of class Student. A student should have a first name, a last name, and a unique integer ID. For grade changes and removals, lookup...
-
Modify the binary search algorithm so that it returns an array of length 2 with the lowest index and highest index of those element(s) that equal the searched value. For example, when searching for...
-
Astro Languet established Languet Products Co. as a sole proprietorship on January 5, 2017. At the company's year end of December 31, 2017, the accounts had the following balances (in thousands): A...
-
Explain the term "Equivalent Units". Why are they calculated in process costing? [4 Marks] [minimum 350 words]
-
Which assignment is correct in Java? a. int value = (float) 4.5; b. float value = 4 (double); c. double value = 2.12; d. char value = 5c;
-
Which assignment is correct in Java? a. double money = 12; b. double money = 12.0; c. double money = 12.0d; d. All of the above are correct.
-
Which assignment is correct in Java? a. char aChar = 5.5; b. char aChar = W; c. char aChar = '*'; d. Two of these are correct.
-
Pangasa Co. paid its annual worker accident insurance premium of $48,000 on its manufacturing facility in January. The company expects two worker accidents to happen during the year, and to make...
-
3. The game of baseball is often analyzed using Markov models. The state of the game can be represented by listing bases that have runners and keeping track of how many outs there are. There are...
-
Thornton Electronics currently produces the shipping containers it uses to deliver the electronics products it sells. The monthly cost of producing 9,300 containers follows. Unit-level materials...
Study smarter with the SolutionInn App