ComputerScienceExpert

(11)

$18/per page/

About ComputerScienceExpert

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Applied Sciences,Calculus See all
Applied Sciences,Calculus,Chemistry,Computer Science,Environmental science,Information Systems,Science Hide all
Teaching Since: Apr 2017
Last Sign in: 103 Weeks Ago, 4 Days Ago
Questions Answered: 4870
Tutorials Posted: 4863

Education

  • MBA IT, Mater in Science and Technology
    Devry
    Jul-1996 - Jul-2000

Experience

  • Professor
    Devry University
    Mar-2010 - Oct-2016

Category > Programming Posted 24 May 2017 My Price 9.00

Design a class named Clock

I have this assignment due and I don't understand how to fix it.  Below is the question and my answer.

Design a class named Clock. You should use your IDE for this exercise. The class contains private data fields for startTime and stopTime, a no argument constructor that initializes the startTime to the current time, a method named start() that resets the startTime to the given time, a stop() method that sets the endTime to the given time and a getElapsedTime() method that returns the elapsed time in seconds. Create a TestClock class to construct a Clock instance and return the elapsed time. Command line arguments should be used to send the start and end times. You should use the java.time classes. Here is sample run:

java TestClock 11:45:12 11:48:13 Elapsed time in seconds is: 181

 

import java.time.LocalTime;
import java.time.Duration;


/**
 *
 * @author 
 */
public class Homework4 {//program name

 public class Clock {
 private LocalTime startTime;//start time
 private LocalTime stopTime;//stop time

 public Clock() {

 //initialize start time and stop time

 startTime=LocalTime.of(0, 0, 0);

 stopTime=LocalTime.of(0, 0, 0);
 }//end Clock

 public void start(String start) {//sets start time
 String[] time = start.split(":");// split Hours, minutes, seconds
 startTime = LocalTime.of(Integer.parseInt(time[0]),Integer.parseInt(time[1]) ,Integer.parseInt(time[2]));//set the start time
 }//end mark

 public void stop(String sp) {//sets stop time
 String[] time = sp.split(":");// split Hours, minutes, seconds
 stopTime = LocalTime.of(Integer.parseInt(time[0]),Integer.parseInt(time[1]) ,Integer.parseInt(time[2]));//set the stop time
 }

 public long getElapsedTime() {//getElapsedTime method
 long expired = Duration.between(startTime,stopTime).getSeconds();//find the duration between the given two times
 System.out.println( startTime + " " + stopTime);//print start and stop times
 return expired;//returns expired time
 }//end getElapsedTime

 public class TestClock {

 

 }//end class TestClock

 }//end class Clock

 /**
 * @param args the command line arguments
 */
 public static void main(String[] args) {//begin main program
 String beginTime = "11:45:12";//start time
 String endTime = "11:48:13";//stop time

 

 Clock clockOne = new Clock();// create Clock instance
 clockOne.start(beginTime);//call start
 clockOne.stop(endTime);//call stop

 System.out.println("Elapsed time in seconds is:" + clockOne.getElapsedTime());//print elapsed time

 }//end main

}//end Homework4 class

 

I keep getting an error here:

Clock clockOne = new Clock();// create Clock instance
 clockOne.start(beginTime);//call start
 clockOne.stop(endTime);//call stop

 System.out.println("Elapsed time in seconds is:" + clockOne.getElapsedTime());//print elapsed time

 }//end main

}//end Homework4 class

 

How do I fix this?  

Answers

(11)
Status NEW Posted 24 May 2017 02:05 AM My Price 9.00

-----------

Not Rated(0)
Relevent Questions