Question
JAVA PROJECT USING ECLIPSE Create a package called clocks in your project Define a Java interface called IClock that has three public int methods called
JAVA PROJECT USING ECLIPSE
Create a package called clocks in your project
- Define a Java interface called IClock that has three public int methods called getHour(), getMinute() and getSecond() (Every class that implements the IClock interface has to define these methods). Also, this interface must have a void method called tick(), and another void method called tickAhead(int numSecs);
- Define a class called Clock that implements the IClock interface: use the code given below and just write the get methods
package clocks;
/**
* This is a clock that shows local time.
*
* @author (TMR and SM)
* @version (6/19)
*/
import java.util.*; //To use the Calendar class
import java.text.*;
public class Clock implements IClock
{
// This clock stores the current time in hours, minutes and
// seconds
private int second;
private int minute;
private int hour;
public Clock()
{
// The clock gets current time from Javas Calendar class
Calendar rightNow = Calendar.getInstance();
second = rightNow.get(Calendar.SECOND);
minute = rightNow.get(Calendar.MINUTE);
hour = rightNow .get(Calendar.HOUR_OF_DAY);
}
// Write the getHour, getMinute and getSecond methods (i.e., write
// the accessor methods)
//Write the skeleton of the tick() and tickAhead() methods
}
- Write a ClockTester class that will just have a public static void method that:
- Instantiates a Clock object
- Get the hours, minutes and seconds using the get methods and print them on the screen. Something like this:
Current time is hh hours, mm minutes and ss seconds
- Now, we need to write the body for the method tick(). What this method is intended to do is to tick the clock up by one second every time it is calle You need to code this method. While considering how to write the code, note that this is a 24-hour clock (i.e., it uses what is known in the USA as Military time). So, if it is 3:59:59 PM, then your above code that displays the output should show the following line:
Current time is 15 hours, 59 minutes and 59 seconds
So, when you write the code for the method tick() to tick up the clock by one second, and change the values of the three instance variables it encapsulates appropriately as you do so, you will note that the next time you run the code to display the time it should come out as:
Current time is 16 hours, 0 minutes and 0 seconds
Also note that when it is 11:59 PM and you tick up by one second, the next time shown, if displayed as above should come out as:
Current time is 0 hours, 0 minutes and 0 seconds
- After writing the tick() method above, you need to write the body of the tickAhead(int numSecs) method. This method receives the number of seconds to tick ahead all at once, and change the time held by the clock accordingly. So, if the current time is 3:57 PM, and the tickAhead(..) method is called with parameter numSecs having value 3, then the changed time, if displayed, should come out as:
Current time is 16 hours, 0 minutes and 0 seconds
Now, when considering how to write the code for this method, note that you can assume that the tick() method is already written, and it can be called from this method. That is, the tick() method, which is a public method, can be called from the outside, but also from the inside of the class i.e., by the tickAhead(..) method. You may take this approach and use the tick() method appropriately as you code this method, or you may take a totally different approach to writing the code for this method.
- Write more code in the ClockTester class that does the following:
- Calls the tick() method once
- Displays the current time after calling this method.
- Calls the tick() method once again.
- Displays the current time after calling this method.
- Calls the tickAhead() method several times, with different values for the parameter. After each call to the tickAhead() method, display the current time. Be sure to test with at least the following parameter values to the tickAhead() method:
- 1
- 5
- 60
- 3600
- 86400
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started