Create a package called clocks in your csc205 project. Define a Java interface c
ID: 3752159 • Letter: C
Question
Create a package called clocks in your csc205 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)
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)
* @version (9/17)
import java.util.*; //To use the GregorianCalendar class
import java.text.*;
public class Clock implements IClock
private int second; //current time in hrs, mins and seconds
private int minute;
private int hour;
public Clock()
// The clock gets current time from the GregorianCalendar class
GregorianCalendar date = new GregorianCalendar();
second = date.get(Calendar.SECOND);
minute = date.get(Calendar.MINUTE);
hour = date.get(Calendar.HOUR);
// Write the getHour, getMinute and getSecond 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 terminal output. Something like this:
Current time is hh hours, mm minutes and ss seconds
Create a javafx class called ClockGUI extends Application in the clocks package with the following specifications. It will just have three labels: hours, minutes and seconds, and three uneditable text fields and a ShowTime Button as shown in the picture below:
Before Button-Click After Button Clock
Use the following template to create your javafx application: (Fill in the blanks). Use the LoanCalculator program as a model.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
public class ClockGUI extends Application
{
//Declare GUI Components
public static void main(String[] args) {
launch(args);
}
public void createGUIComponents()
{
//Instantiate GUI Components and build GUI
}
public void attachHandlers()
{
showButton.setOnAction(new EventHandler<ActionEvent>()
{
public void handle(ActionEvent e)
{
Clock ck = new Clock();
hoursTF.setText(""+ck.getHour());
minutesTF.setText(""+ck.getMinute());
secondsTF.setText(""+ck.getSecond());
}
});
}
public void start(Stage stage) {
createGUIComponents();
attachHandlers();
Scene scene = new Scene(pane);
stage.setScene(scene);
stage.setTitle("Clocks View");
stage.setWidth(200);
stage.setHeight(150);
stage.show();
}
}
Explanation / Answer
public class Clock
{
private int myHour;
private int myMinute;
private boolean myMorning;
/**
* Construct a <CODE>Clock</CODE> that is initially set to midnight.
* <b>Postcondition:</b>
* This <CODE>Clock</CODE> has been initialized with an initial time of
* midnight.
**/
public Clock( )
{
myHour = 12;
myMinute = 0;
myMorning = true;
}
/**
* Move this <CODE>Clock</CODE>'s time by a given number of minutes.
* @param minutes
* the amount to move this <CODE>Clock</CODE>'s time
* <b>Postcondition:</b>
* This <CODE>Clock</CODE>'s time has been moved forward by the indicated
* number of minutes. Note: A negative argument moves this
* <CODE>Clock</CODE> backward.
**/
public void advance(int minutes)
{
final int MINUTES_PER_DAY = 24*60;
final int MINUTES_PER_HOUR = 60;
// Change the minutes so that 0 <= minutes < MINUTES_PER_DAY
if (minutes < 0)
minutes += MINUTES_PER_DAY * (1 - minutes/MINUTES_PER_DAY);
if (minutes >= MINUTES_PER_DAY)
minutes -= MINUTES_PER_DAY * (minutes/MINUTES_PER_DAY);
// Advance the clock any full hours
while (minutes+myMinute >= 60)
{
minutes -= MINUTES_PER_HOUR;
myHour++;
if (myHour == 12)
myMorning = !myMorning;
else if (myHour == 13)
myHour = 1;
}
// Advance any remaining minutes
myMinute += minutes;
}
/**
* Test whether the time on one clock is earlier than the time on another.
* @param c1
* a <CODE>Clock</CODE>
* @param c2
* another <CODE>Clock</CODE>
* @return
* Returns <CODE>true</CODE> if the time on <CODE>c1</CODE> is earlier
* than the time on <CODE>c2</CODE> over a usual day (starting at midnight
* and continuing through 11:59 P.M.); otherwise returns <CODE>false</CODE>.
**/
public static boolean earlier(Clock c1, Clock c2)
{
// Check whether one is morning and the other is not.
if (c1.isMorning( ) && !c2.isMorning( ))
return true;
else if (c2.isMorning( ) && !c1.isMorning( ))
return false;
// Check whether one is 12 o’clock and the other is not.
else if ((c1.getHour( ) == 12) && (c2.getHour( ) != 12))
return true;
else if ((c2.getHour( ) == 12) && (c1.getHour( ) != 12))
return false;
// Check whether the hours are different from each other.
else if (c1.getHour( ) < c2.getHour( ))
return true;
else if (c2.getHour( ) < c1.getHour( ))
return false;
// The hours are the same, so check the minutes.
else if (c1.getMinute( ) < c2.getMinute( ))
return true;
else
return false;
}
/**
* Get the current hour of this <CODE>Clock</CODE>.
* @return
* the current hour (always in the range 1...12)
**/
public int getHour( )
{
return myHour;
}
/**
* Get the current minute of this <CODE>Clock</CODE>.
* @return
* the current minute (always in the range 0...59)
**/
public int getMinute( )
{
return myMinute;
}
/**
* Check whether this <CODE>Clock</CODE>'s time is before noon.
* @return
* If this <CODE>Clock</CODE>'s time lies from 12:00 midnight
* to 11:59 A.M. (inclusive), then the return value is
* <CODE>true</CODE>; otherwise the return value is <CODE>false</CODE>.
**/
public boolean isMorning( )
{
return myMorning;
}
/**
* Set the current time of this <CODE>Clock</CODE>.
* @param hour
* the hour to set this <CODE>Clock</CODE> to
* @param minute
* the minute to set this <CODE>Clock</CODE> to
* @param morning
* indication of whether the new time is before noon
* <b>Postcondition:</b>
* This <CODE>Clock</CODE>'s time has been set to the given hour and
* minute (using the usual 12-hour notation). If the third parameter,
* <CODE>morning</CODE>, is <CODE>true</CODE>, then this time is from
* 12:00 midnight to 11:59 A.M. Otherwise this time is from 12:00 noon
* to 11:59 P.M.
* @exception IllegalArgumentException
* Indicates that the <CODE>hour</CODE> or <CODE>minute</CODE> is illegal.
**/
public void setTime(int hour, int minute, boolean morning)
{
if ((1 > hour) || (hour > 12))
throw new IllegalArgumentException("Illegal hour: " + hour);
if ((0 > minute) || (minute > 59))
throw new IllegalArgumentException("Illegal minute: " + minute);
myHour = hour;
myMinute = minute;
myMorning = morning;
}
public static <T extends Clock> boolean someMorning(Vector<T> clocks)
{
for (T next : clocks)
{
if (next.isMorning( ))
return true;
}
return false;
}
}
ClockTest.java
import static org.junit.Assert.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
public class ClocksTest {
@Test
public void realClockWorks() throws InterruptedException {
testRunningClock(Clocks.realClock(), System.currentTimeMillis());
}
@Test
public void stoppedAtWithDateWorks() throws Exception {
Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2012-03-18");
assertEquals(date.getTime(), Clocks.stoppedAt(date).nowInMillis());
assertEquals(date.getTime(), Clocks.stoppedAt(date).nowAsDate().getTime());
assertEquals(date.getTime(), Clocks.stoppedAt(date).nowAsCalendar().getTimeInMillis());
}
@Test
public void stoppedAtWithMillisWorks() throws Exception {
Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2012-03-18");
assertEquals(date.getTime(), Clocks.stoppedAt(date.getTime()).nowInMillis());
assertEquals(date.getTime(), Clocks.stoppedAt(date.getTime()).nowAsDate().getTime());
assertEquals(date.getTime(), Clocks.stoppedAt(date.getTime()).nowAsCalendar().getTimeInMillis());
}
@Test
public void startingAtWithMillisWorks() throws Exception {
Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2012-03-18");
testRunningClock(Clocks.startingAt(date.getTime()), date.getTime());
}
@Test
public void startingAtWithDateWorks() throws Exception {
Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2012-03-18");
testRunningClock(Clocks.startingAt(date), date.getTime());
}
@Test
public void startingInWithMillisWorks() throws Exception {
Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2012-03-18");
long offset = date.getTime() - System.currentTimeMillis();
testRunningClock(Clocks.startingIn(offset), date.getTime());
}
@Test
public void startingInWithTimeUnitWorks() throws Exception {
Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2012-03-18");
long offset = date.getTime() - System.currentTimeMillis();
testRunningClock(Clocks.startingIn(offset * 1000L, TimeUnit.MICROSECONDS), date.getTime());
}
private void testRunningClock(Clock clock, long timeReference) throws InterruptedException {
long t0 = System.currentTimeMillis();
assertTrue(clock.nowInMillis() - timeReference < 50L);
assertTrue(clock.nowAsDate().getTime() - timeReference < 50L);
assertTrue(clock.nowAsCalendar().getTimeInMillis() - timeReference < 50L);
Thread.sleep(100L);
long t1 = System.currentTimeMillis();
assertTrue(clock.nowInMillis() - (timeReference + (t1 - t0)) < 50L);
assertTrue(clock.nowAsDate().getTime() - (timeReference + (t1 - t0)) < 50L);
assertTrue(clock.nowAsCalendar().getTimeInMillis() - (timeReference + (t1 - t0)) < 50L);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.