// purpose: simulate an alarm clock and improve class skills import java.awt.event.*; // used for GUI import javax.swing.*; // used for GUI import java.awt.*; // used for GUI public class AlarmClock { public static void main( String[] args ) { AlarmClock c = new AlarmClock(); ClockGUI gui = new ClockGUI( c ); } // attributes int hour = 0; int minute = 0; public AlarmClock() { this.setTime( 0, 0 ); } public AlarmClock( int h, int m ) { this.setTime( h, m ); } public void setTime( int h, int m ) { // sets hour to h and minute to m } public void setHour( int h ) { } public void setMinute( int m ) { } public boolean isAM() { // reports whether its AM return false; } public boolean isPM() { // reports whether its PM return false; } public String toString() { // produces a representation of form hh : mm AM/PM return "HH:MM AM"; } public void tick() { // moves time along by one minute } public int getHour() { return this.hour; } public int getMinute() { return this.minute; } } class ClockGUI { // attributes JLabel timeLabel = new JLabel(); JButton tickButton = new JButton( "Tick" ); JButton setTimeButton = new JButton( "Set Time" ); JLabel meridienLabel = new JLabel(); JTextField hourField = new JTextField( 3 ); JTextField minuteField = new JTextField( 3 ); AlarmClock clock; JLabel timesLabel = new JLabel(); JFrame window = new JFrame(); // constructor public ClockGUI( AlarmClock alarmClock ) { clock = alarmClock; window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); window.setAlwaysOnTop( true ); window.setSize( 450, 200 ); window.setLayout( new GridLayout( 4, 1) ); window.setBackground( new Color( 255, 255, 205 ) ); JPanel panel0 = new JPanel(); panel0.setLayout( new FlowLayout() ); JLabel space = new JLabel( "" ); panel0.add( space ); window.add( panel0 ); JPanel panel1 = new JPanel(); panel1.setLayout( new FlowLayout() ); panel1.add( timeLabel ); timesLabel.setText("Time: " + clock.hour + ":" + clock.minute); panel1.add(timesLabel); panel1.add( meridienLabel ); window.add( panel1 ); JPanel panel2 = new javax.swing.JPanel(); panel2.setLayout( new FlowLayout() ); panel2.add( new JLabel( "Hour:" ) ); panel2.add( hourField ); panel2.add( new JLabel( "Minute:" ) ); panel2.add( minuteField ); panel2.add( setTimeButton ); window.add( panel2 ); JPanel panel3 = new JPanel(); panel3.setLayout( new FlowLayout() ); panel3.add( tickButton ); window.add( panel3 ); window.setTitle( clock.toString() ); window.setVisible( true ); tickButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { clock.tick(); timesLabel.setText("Time: " + clock.hour + ":" + clock.minute); window.setTitle( clock.toString() ); } } ); setTimeButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { String sh = hourField.getText(); String sm = minuteField.getText(); if ( sh.matches( "^\\d+$" ) && sm.matches( "^\\d+$" ) ) { int h = Integer.parseInt( sh ); int m = Integer.parseInt( sm ); clock.setTime( h, m ); } else if ( sh.matches( "^\\d+$" ) ) { int h = Integer.parseInt( sh ); clock.setHour( h ); } else if ( sm.matches( "^\\d+$" ) ) { int m = Integer.parseInt( sm ); clock.setMinute( m ); } timesLabel.setText("Time: " + clock.hour + ":" + clock.minute); window.setTitle( clock.toString() ); } }); } public void updateTime() { timeLabel.setText( clock.toString() ); } }