Need help with this Java programming homework.. Thank you will rate and 1500 poi
ID: 3555445 • Letter: N
Question
Need help with this Java programming homework.. Thank you will rate and 1500 points.
Task: To implement a simple 24 hour clock class.
The other constructor Clock (hrs: int, mins: int, secs: int) will take values for all 3 fields and set
the clock to those values (given that the all the 3 values are valid). If the values supplied are
invalid, the all values are set to 0. http://i.imgur.com/nyRVGnk.png
The clock class will have 3 fields: hour, minutes, and seconds (all will be integers). The value of
hours will range from 0-24, minutes: 0-60 and seconds: 0-60.
? The default constructor will reset all the fields to 0
? The copy constructor will create a duplicate of the Clock instance supplied as a parameter.
? The method setTime will set the clock to a given time.
? getHours, getMinutes, getSeconds methods will return the current hr, min or the second value
of the clock respectively.
? Print method will display the current time.
? incrementSeconds method will increment the value of current seconds by 1. If the current
second = 60, it will get updated to 0 and the value of minute will be incremented.
? incrementMinutes method will increment the value of current minutes by 1. If the current
minute = 60, it will get updated to 0 and the value of hour will be incremented.
? incrementHours method will increment the value of current hours by 1. If the current minute =
24, it will get updated to 0.
? incrementSecondsBy will increment the value of the current seconds by the amount given, if this
results in a value > 60 the minutes will be incremented and the remainder stored as the current
seconds.
? incrementHoursBy will increment the value of the current seconds by the amount given, if this
results in a value > 24 the hours will be adjusted appropriately
? incrementMinutesBy will increment the value of the current minutes by the amount given, if
this results in a value > 60 the hours will be incremented and the remainder stored as the
current minutes.
? Equals method will check the equality between the current instance of time and the passed
instance of clock.
? print() will print the time in terms of AM and PM . if the current time is 13:00:00 this method
will print 1 o
Explanation / Answer
// File : animation/textclock/TextClock1.java // Purpose: Show use of Timer, Calendar to implement a clock. // Enhancements: Center text, 12 hour with AM/PM, .... // Author : Fred Swartz, 1999 ... 2007-03-02, Placed in public domain import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.Calendar; //////////////////////////////////////////////////////////////// TextClock1 class TextClock1 extends JFrame { //============================================================== fields private JTextField _timeField; // set by timer listener //========================================================== constructor public TextClock1() { //... Set characteristics of text field that shows the time. _timeField = new JTextField(5); _timeField.setEditable(false); _timeField.setFont(new Font("sansserif", Font.PLAIN, 48)); JPanel content = new JPanel(); content.setLayout(new FlowLayout()); content.add(_timeField); this.setContentPane(content); this.setTitle("Text Clock 1"); this.pack(); this.setLocationRelativeTo(null); this.setResizable(false); //... Create timer which calls action listener every second.. // Use full package qualification for javax.swing.Timer // to avoid potential conflicts with java.util.Timer. javax.swing.Timer t = new javax.swing.Timer(1000, new ClockListener()); t.start(); } /////////////////////////////////////////////// inner class ClockListener class ClockListener implements ActionListener { public void actionPerformed(ActionEvent e) { //... Whenever this is called, get the current time and // display it in the textfield. Calendar now = Calendar.getInstance(); int h = now.get(Calendar.HOUR_OF_DAY); int m = now.get(Calendar.MINUTE); int s = now.get(Calendar.SECOND); _timeField.setText("" + h + ":" + m + ":" + s); //... The following is an easier way to format the time, // but requires knowing how to use the format method. //_timeField.setText(String.format("%1$tH:%1$tM:%1$tS", now)); } } //================================================================= main public static void main(String[] args) { JFrame clock = new TextClock1(); clock.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); clock.setVisible(true); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.