In this program, there will be 3 classes. Your test program will input a start a
ID: 3547410 • Letter: I
Question
In this program, there will be 3 classes. Your test program will input a start and end number of seconds. Call the CounterController only from your test program. The controller will create an instance of CounterModel with the initial value and call increment until the end value is reached. The CounterModel will wait 1 second, increment its counter and then display its counter by calling CounterView. Use Thread.sleep(1000); to delay 1 second.
Example Output:
--------------------Configuration: <Default>--------------------
This progam tests a timer.
Enter a start number of seconds
5
Enter an end number of seconds
11
CounterController Starting.
6...
7...
8...
9...
10...
11...
Continue?(y/n)
y
Enter a start number of seconds
0
Enter an end number of seconds
4
CounterController Starting.
1...
2...
3...
4...
Continue?(y/n)
n
Process completed.
In this program, there will be 3 classes. Your test program will input a start and end number of seconds. Call the CounterController only from your test program. The controller will create an instance of CounterModel with the initial value and call increment until the end value is reached. The CounterModel will wait 1 second, increment its counter and then display its counter by calling CounterView. Use Thread.sleep(1000); to delay 1 second.Explanation / Answer
import java.util.Scanner;
public class Timer extends Thread
{
private NumberDisplay number;
public static final int>
public static boolean finished = true;
private int endSec;
private int startSec;
private String displayString;
public int Timer(int secArg)
{
this.startSec = secArg;
number = new NumberDisplay(startSec);
// number = new setTimer(startSec);
updateDisplay();
}
public void timeTick()
{
number.increment();
if(number.getValue() == 0) {
number.increment();
}
updateDisplay();
}
public void run(){}
public void countdown()
{
if(number.getDisplayValue().equals(endSec)){
finished = false;
System.out.println("Timer is completed.");
}else {
number.increment();
if(number.getValue() == 0) {
number.increment();
}
}
updateDisplay();
}
public void setTimer(int number)
{
number.setValue(number);
updateDisplay();
}
public String getTime()
{
return displayString;
}
private void updateDisplay()
{
displayString = number.getDisplayValue();
}
public static void main(String args[]){
int startSec = 0;
int endSec = 0;
Scanner keyboard = new Scanner(System.in);
System.out.println("This program will test a timer.");
System.out.println("Enter a start number of seconds.");
startSec = keyboard.nextInt();
System.out.println("Enter an end number of seconds.");
endSec = keyboard.nextInt();
Timer timer = new Timer(startSec);
timer.start();
while(finished){
System.out.println("Time Is : " + timer.getTime());
timer.countdown();
try {
Thread.sleep(ONE_SECONDS);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Number
{
private int endSec;
private int value;
public Number(int rollOverLimit)
{
this.endSec = rollOverLimit;
value = 0;
}
public int getendSec(){
return endSec;
}
public int getValue()
{
return value;
}
public String getDisplayValue()
{
if(value < 10) {
return "0" + value;
}
else {
return "" + value;
}
}
public void setValue(int replacementValue)
{
if((replacementValue >= 0) && (replacementValue < endSec)) {
value = replacementValue;
}
}
public void increment()
{
value = (value + 1) % endSec;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.