Question: This lab is structured differently than previous labs. For this lab you are provided with three Java files - Project12.java and ClockView.java hold code that

This lab is structured differently than previous labs. For this lab you are provided with three Java files - Project12.java and ClockView.java hold code that you do not need to touch. You will only be writing code in the file SimpleClock.java. Create a new project named "Project 12" and import all three of these source code files into your project workspace. All three of these files must be in the same Project folder, and must all be in the same default namespace in your Eclipse workspace.

Open the SimpleClock.java file. In this file is a partial implementation of the SimpleClock class. You must fill in the code for the constructor, as well as for the methods set, tick and time, following the instructions provided for each of these methods in the comments for these methods. When you think that you have them working, run the Project12.java program. You should see output like the following: Clock starts at time: 12:00:00 AM Clock has been set to time: 11:59:00 PM TICK: 11:59:01 PM TICK: 11:59:02 PM TICK: 11:59:03 PM TICK: 11:59:04 PM TICK: 11:59:05 PM TICK: 11:59:06 PM TICK: 11:59:07 PM ... Many lines of output like the above ... TICK: 12:58:58 AM TICK: 12:58:59 AM TICK: 12:59:00 AM Clock finally reads: 12:59:00 AM Once you have that working, try running ClockView.java. It should pop up a window with a midnight time showing and two buttons - Tick and Reset. The Tick button should increment the clock by one second, the reset button should set it back to midnight. Play with this interface to make sure it works as expected.

Project 12 Skeleton:

/* * Project12.java * * A program that tests the SimpleClock class. This program instantiates a * new SimpleClock object, sets it to 11:59:00PM and then repeatedly ticks * seconds to show the passage of an hours worth of time. YOU DO NOT NEED * TO MODIFY THIS CLASS. Make your modifications to the SimpleClock.java * file instead. * * See the write-up for Project 12 for more details. * * @author (name) * */ package osu.cse1223; public class Project12 { public static void main(String[] args) { SimpleClock clock = new SimpleClock(); System.out.println("Clock starts at time: " + clock.time()); clock.set(11, 59, 00, false); System.out.println("Clock has been set to time: " + clock.time()); for (int j = 0; j < 60; j++) { for (int i = 0; i < 60; i++) { clock.tick(); System.out.println("TICK: " + clock.time()); } } System.out.println("Clock finally reads: " + clock.time()); } }

Clockview File:

package osu.cse1223; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; /** * ClockView.java * * This program creates a simple graphical clock with two buttons - Tick and * Reset. If the SimpleClock class has been implemented properly, a default time * will be displayed and the Tick button will increment the time by one second * each time it is pressed. The reset button will reset the time back to * 12:00:00AM (midnight). YOU DO NOT NEED TO MODIFY ANY CODE IN THIS CLASS. * Instead, look at the SimpleClock.java file and fill in the skeleton there to * get this program to work. * * * @author (name) * */ public class ClockView extends JFrame { /* -----------------Private Member variables --------------------- */ private static final long serialVersionUID = 1L; private static final int ROWS_IN_GRID = 2; private static final int COLS_IN_GRID = 1; private static final int BUTTON_ROWS = 1; private static final int BUTTON_COLS = 2; private final SimpleClock clock; private final JLabel face; /** * Constructor. Takes a SimpleClock as an argument and builds a graphical * interface using that clock the model. The Tick button increments the * clock by 1 second, while the Reset button sets the clock back to midnight * (12:00:00AM). * * * @param clock * - the clock instance used to store the time for the view */ public ClockView(SimpleClock clock) { super("SimpleClock Demo"); this.clock = clock; this.face = new JLabel("" + this.clock.time() + ""); this.setLayout(new GridLayout(ROWS_IN_GRID, COLS_IN_GRID)); this.add(this.face); JPanel buttonPanel = new JPanel( new GridLayout(BUTTON_ROWS, BUTTON_COLS)); JButton tickButton = new JButton("Tick"); tickButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { clock.tick(); ClockView.this.face .setText("" + clock.time() + ""); } }); buttonPanel.add(tickButton); JButton resetButton = new JButton("reset"); resetButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { clock.set(12, 0, 0, true); ClockView.this.face .setText("" + clock.time() + ""); } }); buttonPanel.add(resetButton); this.add(buttonPanel); this.pack(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public static void main(String[] args) { // TODO Auto-generated method stub ClockView v = new ClockView(new SimpleClock()); } } SimpleClock File:

/** * SimpleClock.java * * A class that implements a simple * * @author (name) * */ package osu.cse1223; public class SimpleClock { /* -------- Private member variables --------------------- */ private int hours; private int minutes; private int seconds; private boolean morning; /* -------- Constructor --------------------------------- */ /** * The constructor should set the intial value of the clock to 12:00:00AM. */ public SimpleClock() { // TODO - code for constructor here. } /* --------- Instance methods ------------------------- */ /** * Sets the time showing on the clock. * * @param hh * - the hours to display * @param mm * - the minutes to display * @param ss * - the seconds to display * @param morning * - true for AM, false for PM */ public void set(int hh, int mm, int ss, boolean morning) { // TODO - code for set here } /** * Advances the clock by 1 second. Make sure when implementing this method * that the seconds "roll over" correctly - 11:59:59AM should become * 12:00:00PM for example. */ public void tick() { // TODO - code for tick here } /** * Returns a string containing the current time formatted as a digital clock * format. For example, midnight should return the string "12:00:00AM" while * one in the morning would return the string "1:00:00AM" and one in the * afternoon the string "1:00:00PM". * * @return - the current time formatted in AM/PM format */ public String time() { // TODO - code for time here // Note - the return statement below is included only to // remove errors from the skeleton. Remove this comment and // replace it with the correct return value. return ""; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!