Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, could someone help me with this JAVA problem? This is what I have so far, an

ID: 3728170 • Letter: H

Question

Hi, could someone help me with this JAVA problem? This is what I have so far, and I'm really stuck. I don't know how to make my timer run, and every time I click reset, an error appears. Thank you

Notes

Use java.lang.Thread

You may assume that the Thread.sleep is accurate enough for this implementation. That is, you can assume that every call the Thread.sleep(10) is 10 milliseconds long. So, after calling Thread.sleep(10), the display can be updated by 0.01 seconds

the result should look something like this

default:

click start then click pause

when click continue

when click reset everything go back

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class MyTimer extends Thread implements ActionListener{
  
private JFrame win;
private double timer;
private JButton start;
private JButton reset;
private JLabel timerLabel;
  
//private javax.swing.JLabel millisecond;
//static int milliseconds = 0;
//static int seconds = 0;
//static boolean state = true;
  
public MyTimer(){
  
win = new JFrame("My Timer");
win.setLocation(25, 25);
win.setSize(400, 300);
win.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  
// Create the label
timer = 0;
JLabel timerLabel = new JLabel(String.format("%.2f seconds", timer));
timerLabel.setHorizontalAlignment(JLabel.CENTER);
Font font = new Font("Arial", Font.BOLD, 36);
timerLabel.setFont(font);
win.getContentPane().add(timerLabel);
  
  
  
// create a toolbar
JPanel toolbar = new JPanel();
win.add(toolbar, BorderLayout.NORTH);
  
  
  
// create Start button
start = new JButton("Start");
toolbar.add(start);
start.addActionListener(this);
  
  
  
// create Reset button
reset = new JButton("Reset");
toolbar.add(reset);
reset.addActionListener(this);
  
  
win.setVisible(true);
  
}
  
public void run() {
while(start.getText() == "Pause") {
try {
  
timerLabel.setText(String.format("%.2f seconds", timer));
timer += 0.01;
Thread.sleep(100);
}
  
  
catch(InterruptedException e) {}
// do nothing
  
}
}
public void actionPerformed(ActionEvent e) {
String text = e.getActionCommand();
  
if(text.equals( "Start")) {
start.setText("Pause");
}
  
else if(text.equals("Pause")) {
start.setText("Continue");
}
  
else if(text.equals("Continue")) {
start.setText("Pause");
}
  
else if(text.equals("Reset")) {
  
// reset timer
timer = 0;
timerLabel.setText(String.format("%.2f seconds", timer));
  
// restore text back to "Start"
start.setText("Start");
}
  
}
  
  
  
/**
* The application method
* @param args The command-line arguments
*/
public static void main(String[] args) {
  
new MyTimer();
}
  
}

0.00 seconds

Explanation / Answer

package com.journaldev.threads;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class MyTimerTask extends TimerTask {

@Override
public void run() {
System.out.println("Timer task started at:"+new Date());
completeTask();
System.out.println("Timer task finished at:"+new Date());
}

private void completeTask() {
try {
//assuming it takes 20 secs to complete the task
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
  
public static void main(String args[]){
TimerTask timerTask = new MyTimerTask();
//running timer task as daemon thread
Timer timer = new Timer(true);
timer.scheduleAtFixedRate(timerTask, 0, 10*1000);
System.out.println("TimerTask started");
//cancel after sometime
try {
Thread.sleep(120000);
} catch (InterruptedException e) {
e.printStackTrace();
}
timer.cancel();
System.out.println("TimerTask cancelled");
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote