Question

I need help displaying two zeroes in hours:minutes:seconds. In Java.

Time: 6:0:0 Time: 6:59:59 Time: 12:0:0 :

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 = min;
this.sec = sec;
}
else{
System.out.println("Please enter correct Time format");
return;
}
  this.name = name;
  this.id = uid;
uid++;


}
public String getName() {
return name;
}

public void setName(String name) {
  this.name = name;
}
public int getId() {
return this.id;
}
public void setId(int id) {
  this.id = id;
}
public int getHr() {
return hr;
}
public void setHr(int hr) {
  this.hr = hr;
}
public int getMin() {
return min;
}
public void setMin(int min) {
this.min = min;
}
public int getSec() {
return sec;
}
public void setSec(int sec) {
this.sec = sec;
}
public void printTime(){
System.out.println("Name: "+this.name+" ID: "+this.id+" Time:
"+this.hr+":"+this.min+":"+this.sec);


}
public static void main(String args[]){


Clock clock1 = new Clock("A",6,0,0);
clock1.printTime();
clock1.setHr(06);
clock1.setMin(59);
clock1.setSec(59);
clock1.printTime();


Clock clock2 = new Clock("My Alarm",12,00,00);
clock2.printTime();
}
}

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

Please find the working code snippet. Let me know if you face any issues during compilation. Thanks.

Following are the two corrections made:

1. Import java util formatter class.

2. Format the hour, min & sec using String.format().

Code Snippet:

import java.util.Formatter;

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 = min;
this.sec = sec;
}
else{
System.out.println("Please enter correct Time format");
return;
}
  this.name = name;
  this.id = uid;
uid++;


}
public String getName() {
return name;
}

public void setName(String name) {
  this.name = name;
}
public int getId() {
return this.id;
}
public void setId(int id) {
  this.id = id;
}
public int getHr() {
return hr;
}
public void setHr(int hr) {
  this.hr = hr;
}
public int getMin() {
return min;
}
public void setMin(int min) {
this.min = min;
}
public int getSec() {
return sec;
}
public void setSec(int sec) {
this.sec = sec;
}
public void printTime(){
System.out.println("Name: "+this.name+" ID: "+this.id+" Time:"+ String.format("%02d", this.hr)+":"+String.format("%02d", this.min)+":"+String.format("%02d", this.sec));
}

public static void main(String args[]){


Clock clock1 = new Clock("A",6,0,0);
clock1.printTime();
clock1.setHr(06);
clock1.setMin(59);
clock1.setSec(59);
clock1.printTime();


Clock clock2 = new Clock("My Alarm",12,00,00);
clock2.printTime();
}

}

Screenshot of the Output:

D:\>cd D:\javaljdk-14.0.2\bin D:\javaljdk-14.0.2\bin>javac D:\javaprograms\clock.java D:\javaljdk-14.0.2\bin>java D:\javaprog

Add a comment
Know the answer?
Add Answer to:
I need help displaying two zeroes in hours:minutes:seconds. In Java. I need some help for the...
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
  • 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...

  • I need help converting the following two classes into one sql table package Practice.model; impor...

    I need help converting the following two classes into one sql table package Practice.model; import java.util.ArrayList; import java.util.List; import Practice.model.Comment; public class Students {          private Integer id;    private String name;    private String specialties;    private String presentation;    List<Comment> comment;       public Students() {}       public Students(Integer id,String name, String specialties, String presentation)    {        this.id= id;        this.name = name;        this.specialties = specialties;        this.presentation = presentation;        this.comment = new ArrayList<Commment>();                  }       public Students(Integer id,String name, String specialties, String presentation, List<Comment> comment)    {        this.id= id;        this.name...

  • Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int...

    Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • 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. * *...

  • Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the...

    Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the beginning(root) of the linked list. 2. A removeFromBeginning() function, that removes the node from the beginning of the linked list and assigns the next element as the new beginning(root). 3. A traverse function, that iterates the list and prints the elements in the linked list. For the insertAtBeginning(Node newNode) function: 1. Check if the root is null. If it is, just assign the new...

  • 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...

  • Java Do 68a, 68b, 68c, 68d. Show code & output. public class Employee { private int...

    Java Do 68a, 68b, 68c, 68d. Show code & output. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

  • Java Programming Question

    After pillaging for a few weeks with our new cargo bay upgrade, we decide to branch out into a new sector of space to explore and hopefully find new targets. We travel to the next star system over, another low-security sector. After exploring the new star system for a few hours, we are hailed by a strange vessel. He sends us a message stating that he is a traveling merchant looking to purchase goods, and asks us if we would...

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