Question

Please help with Java programming! This code is to implement a Clock class. Use my beginning...

Please help with Java programming!

This code is to implement a Clock class.

Use my beginning code below to test your implementation.

public class Clock {

// Declare your fields here

/**

* The constructor builds a Clock object and sets time to 00:00

* and the alarm to 00:00, also.

*

*/

public Clock() {

setHr(0);

setMin(0);

setAlarmHr(0);

setAlarmMin(0);

}

/**

* setHr() will validate and set the value of the hr field

* for the clock.

*

* @param h the hour to attempt setting

*/

private void setHr( int h ) {

int tmp = 0; // default hour in case h is out of range

if ( h >= 0 && h < 24 ) {

tmp = h; // h is valid, so set it to tmp

}

hr = tmp; // set the hr field to tmp's value

}

/**

* setMin() will validate and set the value of the min field

* for the clock.

*

* @param m the min to attempt setting

*/

private void setMin( int m ) {

}

/**

* setAlarmHr() will validate and set the value of the hr field

* for the alarm.

*

* @param h the hour to attempt setting

*/

/**

* setAlarmMin() will validate and set the value of the min field

* for the alarm.

*

* @param m the min to attempt setting

*/

/**

* getHr() simply returns the value of the hr field.

*

* @return The value of the hour for the clock

*/

private int getHr() {

return hr;

}

/**

* getMin() simply returns the value of the min field.

*

* @return The value of the min for the clock

*/

/**

* getAlarmHr() simply returns the value of the alarmHr field.

*

* @return The value of the hour for the alarm

*/

/**

* getAlarmMin() simply returns the value of the alarmMin field.

*

* @return The value of the min for the alarm

*/

/**

* setAlarm() allows the user of a Clock object to set the alarm

* time.

*

* It should call setAlarmHr() and setAlarmMin() to set the alarm.

*

* @param h an int value for the hour

* @param m an int value for the minute

*/

public void setAlarm( int h, int m ) {

}

/**

* setTime() allows the user of a Clock object to set the current

* time.

*

* It should call setHr() and setMin() to set the time.

*

* @param h an int value for the hour

* @param m an int value for the minute

*/

/**

* alarmSound() will return true if the alarm time has been reached,

* false otherwise.

*

* @return true if the alarm and clock times are equal, false

* otherwise

*/

public boolean alarmSound() {

}

/**

* tick() will increment the current time of the clock, adding

* one minute to the clock's current time, correctly handling if the

* clock advances to the next hour or the next day.

*

* You should use getHr() and getMin() as well as setHr() and

setMin()

* to implement this method.

*/

public void tick() {

}

/**

* toString() will return a String that is the current time

* formatted as HH:MM.

*

* @return A formatted String in HH:MM format.

*/

public String toString() {

return String.format( "%02d:%02d", getHr(), getMin());

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code in java:

Clock.java file:

public class Clock {

private int hr;

private int min;

private int alarmHr;

private int alarmMin;

Clock(){

setHr(0);

setMin(0);

setAlarmHr(0);

setAlarmMin(0);

}

private void setHr(int h) {

if(h>=0 && h<=23)

this.hr=h;

else

this.hr=0;

}

private void setMin(int m) {

if(m>=0 && m<=59)

this.min=m;

else

this.min=0;

}

private int getHr() {

return this.hr;

}

private int getMin() {

return this.min;

}

private void setAlarmHr(int h) {

if(h>=0 && h<=23)

this.alarmHr=h;

else

this.alarmHr=0;

}

private void setAlarmMin(int m) {

if(m>=0 && m<=59)

this.alarmMin=m;

else

this.alarmMin=0;

}

private int getAlarmHr() {

return this.alarmHr;

}

private int getAlarmMin() {

return this.alarmMin;

}

void setTime(int h,int m) {

this.setHr(h);

this.setMin(m);

}

void setAlarm(int h,int m) {

this.setAlarmHr(h);

this.setAlarmMin(m);

}

boolean alarmSound() {

if(this.getHr()==this.getAlarmHr() && this.getMin()==this.getAlarmMin())

return true;

else

return false;

}

void tick() {

if(this.min+1==60) {

this.hr=this.hr+1;

this.min=0;

if(this.hr>23)

this.hr=0;

}

else

this.min=this.min+1;

}

public String toString() {

return String.format( "%02d:%02d", getHr(), getMin());

}

}

Tester.java file:

import java.util.Scanner;

public class Tester {

public static void main(String[] args) {

// TODO Auto-generated method stub

Clock c=new Clock();

int hr,min,alarmHr,alarmMin;

Scanner sc=new Scanner(System.in);

System.out.print("Enter current time, hours:");

hr=sc.nextInt();

System.out.print("Enter current time, minutes:");

min=sc.nextInt();

c.setTime(hr, min);

System.out.print("Enter alarm time, Hours:");

alarmHr=sc.nextInt();

System.out.print("Enter alarm time, Minutes:");

alarmMin=sc.nextInt();

c.setAlarm(alarmHr,alarmMin);

System.out.print("How many minutes should the clock run for:");

int dur=sc.nextInt();

System.out.println("The time is:"+c.toString());

for(int i=1;i<dur;i++) {

c.tick();

System.out.println("The time is:"+c.toString());

if(c.alarmSound())

System.out.println("The alarm is ringing...");

}

}

}

OUTPUT:

Enter current time, hours: 13 Enter current time, minutes:40 Enter alarm time, Hours:13 Enter alarm time, Minutes: 50 How many minutes should the clock run for:5 The time is:13:40 The time is 13:41 The time is 13:42 The time is 13:43 The time is 13:44

Add a comment
Know the answer?
Add Answer to:
Please help with Java programming! This code is to implement a Clock class. Use my beginning...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help displaying two zeroes in hours:minutes:seconds. In Java. I need some help for the...

    I need help displaying two zeroes in hours:minutes:seconds. In Java. I need some help for the time to display correctly. I want it to display double zeroes. 06:00:00 and 12:00:00. public class Clock { String name; static int uid=100; int id; int hr; int min; int sec; public Clock() {   this.name="Default";   this.id=uid;   this.hr=00; this.min=00; this.sec=00; uid++; } public Clock(String name, int hr, int min, int sec) { if (hr<=24 && min <=60 && sec <=60) {   this.hr = hr; this.min...

  • Need help with this java code supposed to be a military time clock, but I need...

    Need help with this java code supposed to be a military time clock, but I need help creating the driver to test and run the clock. I also need help making the clock dynamic. public class Clock { private int hr; //store hours private int min; //store minutes private int sec; //store seconds public Clock () { setTime (0, 0, 0); } public Clock (int hours, intminutes, int seconds) { setTime (hours, minutes, seconds); } public void setTime (int hours,int...

  • [Java] We have learned the class Clock, which was designed to implement the time of day...

    [Java] We have learned the class Clock, which was designed to implement the time of day in a program. Certain application in addition to hours, minutes, and seconds might require you to store the time zone. Please do the following: Derive the class ExtClock from the class Clock by adding a data member to store the time zone. Add necessary methods and constructors to make the class functional. Also write the definitions of the methods and constructors. Write a test...

  • JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time a...

    JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time at your location. Also include a getTime method that returns a string with the hours and minutes by calling the getHours and getMinutes methods. Provide a subclass WorldClock whose constructor accepts a time offset. For example, if you livein California, a new WorldCLock(3) should show the time in NewYork, three time zones ahead. Include an Alarm feature in the Clock class....

  • This is a Java programming assignment and I want help with the Time class. See below...

    This is a Java programming assignment and I want help with the Time class. See below for assignment details: For this question you must write a java class called Time and a client class called TimeClient. The partial Time class is given below. (For this assignment, you will have to submit 2 .java files: one for the Time class and the other one for the TimeClient class and 2 .class files associated with these .java files. So in total you...

  • Java Programming The program template represents a complete working Java program with one or more key...

    Java Programming The program template represents a complete working Java program with one or more key lines of code replaced with comments. Read the problem description and examine the output, then study the template code. Using the problem-solving tips as a guide, replace the /* */ comments with Java code. Compile and execute the program. Compare your output with the sample output provided. Modify class Time2 to include a tick method that increments the time stored in a Time2 object...

  • Write in C++ please In Chapter 10, the class clockType was designed to implement the time...

    Write in C++ please In Chapter 10, the class clockType was designed to implement the time of day in a program. Certain applications, in addition to hours, minutes, and seconds, might require you to store the time zone. Derive the class extClockType from the class clockTypeby adding a member variable to store the time zone. Add the necessary member functions and constructors to make the class functional. Also, write the definitions of the member functions and the constructors. Finally, write...

  • need help with this JAVA lab, the starting code for the lab is below. directions: The...

    need help with this JAVA lab, the starting code for the lab is below. directions: The Clock class has fields to store the hours, minutes and meridian (a.m. or p.m.) of the clock. This class also has a method that compares two Clock instances and returns the one that is set to earlier in the day. The blahblahblah class has a method to get the hours, minutes and meridian from the user. Then a main method in that class creates...

  • Given java code is below, please use it! import java.util.Scanner; public class LA2a {      ...

    Given java code is below, please use it! import java.util.Scanner; public class LA2a {       /**    * Number of digits in a valid value sequence    */    public static final int SEQ_DIGITS = 10;       /**    * Error for an invalid sequence    * (not correct number of characters    * or not made only of digits)    */    public static final String ERR_SEQ = "Invalid sequence";       /**    * Error for...

  • // I need help with the following questions. Please use java programming ECLIPSE language to solve...

    // I need help with the following questions. Please use java programming ECLIPSE language to solve the questions. YOU ONLY NEED TO DIRECTLY COPY IT IN YOUR ECLIPSE APPLICATION AND RUN IT. I NEED THOSE PART WHICH IS SAYS --> "TO BE COMPLETED" I NEED HELP WITH [GET*] AND [REPLACE ALL] AND [ADD INT DOUBLE] PLEASE. import java.util.ArrayList; public class CustomArrayList { //instance variables public int[] data; //data.length gives the capacity public int nItems; //nItems gives items currently in the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT