Question

Create a class called Stopwatch that contains the following: - Private data fields startTime and endTime...

Create a class called Stopwatch that contains the following:

- Private data fields startTime and endTime with getter methods.

- A no-arg constructor that initializes startTime with the current time.

- A method named start() that resets the startTime to the cur- rent time.

- A method named stop() that sets the endTime to the current time.

-A method named getElapsedTime() that returns the elapsed time for the stopwatch in milliseconds.

Create a program that utilizes a StopWatch to measture how long it takes to print "Hello" 100,000 times.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package practice;

public class Stopwatch {
  
   private long startTime;
   private long endTime;
  
       //Private data fields startTime and endTime with getter methods.
   public long getStartTime()
   {
         
         
       return startTime;
   }
   public long getEndTime()
   {
         
         
       return endTime;
   }


   //- A no-arg constructor that initializes startTime with the current time.
     
   public Stopwatch()
   {
       startTime=System.currentTimeMillis();
         
         
   }

   //- A method named start() that resets the startTime to the cur- rent time.
   public void start()
   {
       startTime=System.currentTimeMillis();
         
         
   }


   //- A method named stop() that sets the endTime to the current time.
   public void stop()
   {
       endTime=System.currentTimeMillis();
         
         
   }

   //-A method named getElapsedTime() that returns the elapsed time for the stopwatch in milliseconds.
     
   public long getElapsedTime()
   {
         
         
       return endTime-startTime;
   }

   public static void main(String[] args) {
       // TODO Auto-generated method stub
      
       Stopwatch st=new Stopwatch();
      
      
       st.start();
      
       for(int i=0;i<100000;i++)
           System.out.println((i+1)+" Hello");
      
       st.stop();
      
       System.out.println("Time required to print Hello 10000 in milliseconds is : " +st.getElapsedTime());

   }

}

Sample Run:

please upvote me....

for any queries plz comment :)

Add a comment
Know the answer?
Add Answer to:
Create a class called Stopwatch that contains the following: - Private data fields startTime and endTime...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Design a class named StopWatch. The class contains: - Private data fields startTime and endTime with...

    Design a class named StopWatch. The class contains: - Private data fields startTime and endTime with getter methods. - A no-arg constructor that initializes startTime with the current time. - A method named start() that resets the startTime to the cur- rent time. - A method named stop() that sets the endTime to the current time. A method named getElapsedTime() that returns the elapsed time for the stopwatch in milliseconds. - The System.currentTimeMillis() method returns the current number of milliseconds....

  • In Python - Design a class named Time. The class contains: -Data fields hour, minute, and...

    In Python - Design a class named Time. The class contains: -Data fields hour, minute, and second that represent a time. -A no-arg constructor that creates a Time object for the current time.(the values of the data fields will respresent the current time.) -A constructor that constructs a Time object with a specified hour, minute, and second, respectively. -A constructor that constructs a Time object with a specified elapsed time since midnight, Jan 1, 1970, in milliseconds. (The values of...

  • Part I: (The Myate class) Design a class named MyDate. The class contains: • The data...

    Part I: (The Myate class) Design a class named MyDate. The class contains: • The data fields year, month, and day that represent a date. Month is 0-based, i.e., 0 is for January. • A no-arg constructor that creates a MyDate object for the current date. • A constructor that constructs a MyDate object with a specified elapsed time since midnight, January 1, 1970, in milliseconds. • A constructor hat constructs a MyDate object with the specified year, month, and...

  • Use "Eclipse IDE for Java EE Developers" Design a class named MyDate. The class contains: The...

    Use "Eclipse IDE for Java EE Developers" Design a class named MyDate. The class contains: The Date fields year, month, and day that represent a date. month is 0-based, i.e., 0 is for January. A no argument constructor that creates a MyDate object for the current date. A constructor that constructs a MyDate object with a specified elapsed time since midnight, January 1, 1970, in milliseconds. A constructor that constructs a MyDate object with the specified year, month, and day....

  • Design a class named Account that contains: A private int datafield named id(default 0) A private...

    Design a class named Account that contains: A private int datafield named id(default 0) A private double datafield named balance(default 0) A no-arg constructor that creates default account A constructor that creates an account with specified id & balance The getter and setter methods for id and balance. Create an object of account class using default constructor and update the balance to 2000$ .

  • java bluej Create a class called Vehicle, that contains five fields, current speed, power, accelerate, decelerate...

    java bluej Create a class called Vehicle, that contains five fields, current speed, power, accelerate, decelerate and top speed, all of which type is int.  Define a constructor that takes and sets the accelerate, decelerate; sets the power to 20, and top speed to 120. Leave the current speed as 0. Also define a constructor that takes no parameters. The power field should be set to the value of 30 in this constructor, top speed to 160, and accelerate and decelerate...

  • (The Account class) Design a class named Account that contains: A private int data field named...

    (The Account class) Design a class named Account that contains: A private int data field named id for the account (default 0). A private double data field named balance for the account (default 0). A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default...

  • Java - Object Oriented Programming Declare a class named Customer that has two private fields? Write...

    Java - Object Oriented Programming Declare a class named Customer that has two private fields? Write a set method to make sure that if age > 125 years or less than 0 then it is set to 0 (default value) What does it indicate when declaring a class's instance variables with private access modifier? What is the role of a constructor? The data part of by an object's instance variables is known as? Do all methods have to always return...

  • In Java Create an interface called Amount that includes a method called setPrice (). Create an a...

    In Java Create an interface called Amount that includes a method called setPrice (). Create an abstract class named Book that inherits from the interface Amount. Include a String field for the book’s title and a double field for the book’s Price . Within the class, include a constructor that requires the book title and add two getter methods — one that returns the title and one that returns the price. Include an abstract method named setPrice (). Create a...

  • Design a class named BankAccount that contains: 1. A private int data field named accountId for...

    Design a class named BankAccount that contains: 1. A private int data field named accountId for the account. 2. A private double data field named accountBalance for the account (default 0). 3. A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. 4. A private Date data field named dateCreated that stores the date when the account was created. 5. A private int data field named numberOfDeposits...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT