how to return null to int method - java

iam new to programming.iam wrking on a quiz game software. here i want a count down to be printed as "3,2,1,go...go...go"
package t2;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
public class Stopwatch {
static int interval;
static Timer timer;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int delay = 1000;
int period = 1000;
timer = new Timer();
interval = 3;
System.out.println("3");
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
System.out.println(setInterval());
}
}, delay, period);
}
private static final int setInterval() {
String go="go...go...go";
if (interval == 2)
{
timer.cancel();
return go;
}
return --interval;
}
}
this says that setInterval() has int as return value.if i place System.out.print("go");
in the place of return go; it prints go and prints 1 which is out of requirement. please any buddy can tell me how to do this.

One option would be to change your setInterval() method to return a string like such:
private static final String setInterval() {
String go="go...go...go";
if (interval == 2)
{
timer.cancel();
return go;
}
--interval;
return String.valueOf(interval);
}

Change the return type of setInterval to object to take advantage of the overloaded println method and the conditional should check for 1 instead of 2. Also use print instead of println to have the output on the same line.
private static final Object setInterval() {
String go="go...go...go";
if (interval == 1)
{
timer.cancel();
return go;
}
return --interval;
}
Full Example
package t2;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
public class Stopwatch {
static int interval;
static Timer timer;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int delay = 1000;
int period = 1000;
timer = new Timer();
interval = 3;
System.out.print("3,");
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
System.out.print(setInterval() + ",");
}
}, delay, period);
}
private static final Object setInterval() {
String go = "go...go...go";
if (interval == 1) {
timer.cancel();
return go;
}
return --interval;
}
}

Related

How can I implement a thread that gets user input?

I've implemented a thread that get user input by keyboard. However, when user sets input my program returns an IllegalThreadStateException error, at line 23.
import java.util.Scanner;
public class Main {
static MyThread myThread = new MyThread();
static public boolean answered = false;
public static void main(String[] args) {
String s = "";
while (!s.equalsIgnoreCase("q")) {
int seconds = 0, average = 5;
if (seconds > average) {
myThread.stop();
String phrase = choosePhrase(seconds, average);
System.out.println(phrase);
Scanner keyboard = new Scanner(System.in);
s = keyboard.nextLine();
} else {
long createdMillis = System.currentTimeMillis();
myThread.start();
while (!answered && seconds < average) {
long nowMillis = System.currentTimeMillis();
seconds = (int) ((nowMillis - createdMillis) / 1000);
}
}
}
}
private static String choosePhrase(int seconds, int average) {
if (seconds > average + 10) {
return "¿D?";
} else if (seconds > average + 5) {
return "¿E?";
} else {
return "¿F?";
}
}
}
class MyThread extends Thread {
static String[] questions = {"¿A?", "¿B?", "¿C?"};
public void run() {
System.out.println("MyThread running");
Double d = Math.random() * 100;
int n = (int) (Math.ceil(d) % 3);
String question = questions[n];
System.out.println(question);
Scanner keyboard = new Scanner(System.in);
String s = keyboard.nextLine();
stop();
}
}
Why am I getting this exception? How do I make the thread gets and exits correctly to main function?
To make a thread exits proprely you have to use myThread.interrupt();
I've solved the issue with your suggestions:
import javax.swing.JOptionPane;
public class Main {
public static void main(String[] args) {
Latency latency = new Latency();
latency.start();
}
}
class Latency {
int seconds = 0, average = 5;
MyThread myThread;
ObjectToPass o = new ObjectToPass();
public void start() {
String s = "";
Thread t = null;
while (!s.equalsIgnoreCase("q")) {
// The program has just begun, or user has answered a question,
// or term has expired.
seconds = 0;
if (myThread != null) {
myThread.shutdown();
myThread = null;
}
if (o.answered) {
o.answered = false;
myThread = new MyThread(o, new String[]{"¿A?", "¿B?", "¿C?"});
} else {
myThread = new MyThread(o, new String[]{"¿D?", "¿E?", "¿F?"});
}
t = new Thread(myThread);
t.start();
long createdMillis = System.currentTimeMillis();
while (!this.o.getPlay() && seconds < average) {
long nowMillis = System.currentTimeMillis();
seconds = (int) ((nowMillis - createdMillis) / 1000);
}
}
}
}
class ObjectToPass {
boolean answered = true;
public synchronized boolean getPlay() {
return answered;
}
public synchronized void setPlay(boolean answered) {
this.answered = answered;
}
}
class MyThread implements Runnable {
ObjectToPass o;
static String[] questions;
public MyThread(ObjectToPass o, String[] questions) {
this.o = o;
this.questions = questions;
}
public void run() {
// System.out.println("MyThread running");
Double d = Math.random() * 100;
int n = (int) (Math.ceil(d) % 3);
String question = questions[n];
System.out.println(question);
// Scanner keyboard = new Scanner(System.in);
// String s = keyboard.nextLine();
Object[] options = {"Yes", "No"};
int oo = JOptionPane.showOptionDialog(null,
question, "Question", JOptionPane.YES_OPTION,
JOptionPane.NO_OPTION, null, options, options[0]);
this.o.setPlay(true);
}
public void shutdown() {
Thread.currentThread().interrupt();
return;
}
}

Timertask for sleep

I want to use TimerTask for Sleep, but I am unable to run this code.
The run function is called for the first time, and then it waits infinitely. Is there any issue with my code?
import java.util.Timer;
import java.util.TimerTask;
public class TimeExecutor {
class LocalSleep extends TimerTask {
private int noOfSeconds;
private int count = 0;
private Timer timer;
public LocalSleep(int noOfSeconds, Timer timer) {
this.noOfSeconds = noOfSeconds;
this.timer = timer;
}
void sleeeep() {
if (count < noOfSeconds) {
System.out.println("Count: " + count);
count++;
} else
timer.cancel();
}
#Override
public void run() {
sleeeep();
}
}
public static void main(String args[]) {
Timer timer = new Timer();
TimeExecutor t = new TimeExecutor();
timer.schedule(t.new LocalSleep(5, timer), 1000);
}
}
Working after replacing
timer.schedule(t.new LocalSleep(5, timer), 1000);
with:
timer.schedule(t.new LocalSleep(5, timer), 0, 1000);

Failed to override toString method

I wrote a timer class. And I want to override its toString method. But when I call the toString method, it still returns the super implementation. (fully qualified name of the class)
Here is my timer class:
import android.os.Handler;
import android.widget.TextView;
public class Timer implements Comparable<Timer> {
private Handler handler;
private boolean paused;
private TextView text;
private int minutes;
private int seconds;
private final Runnable timerTask = new Runnable () {
#Override
public void run() {
if (!paused) {
seconds++;
if (seconds >= 60) {
seconds = 0;
minutes++;
}
text.setText (toString ()); //Here I call the toString
Timer.this.handler.postDelayed (this, 1000);
}
}
};
//Here is the toString method, anything wrong?
#Override
public String toString () {
if (Integer.toString (seconds).length () == 1) {
return minutes + ":0" + seconds;
} else {
return minutes + ":" + seconds;
}
}
public void startTimer () {
paused = false;
handler.postDelayed (timerTask, 1000);
}
public void stopTimer () {
paused = true;
}
public void resetTimer () {
stopTimer ();
minutes = 0;
seconds = 0;
text.setText (toString ()); //Here is another call
}
public Timer (TextView text) {
this.text = text;
handler = new Handler ();
}
#Override
public int compareTo(Timer another) {
int compareMinutes = ((Integer)minutes).compareTo (another.minutes);
if (compareMinutes != 0) {
return compareMinutes;
}
return ((Integer)seconds).compareTo (another.seconds);
}
}
I can see that the text view's text is the fully qualified name of the Timer class. I even tried this.toString but it doesn't work either.
You're calling toString() from your anonymous inner class - the new Runnable() { ... }. That means you're calling toString() on your anonymous class instance, not on the Timer instance. I suspect you're getting a $1 in the output, showing that it's an anonymous inner class.
Try:
text.setText(Timer.this.toString());
... so that you call it on the enclosing Timer instance instead.
Here's a short but complete console app to demonstrate the difference:
class Test
{
public Test() {
Runnable r = new Runnable() {
#Override public void run() {
System.out.println(toString()); // toString on anonymous class
System.out.println(Test.this.toString()); // toString on Test
}
};
r.run();
}
public static void main(String[] args) {
new Test();
}
#Override public String toString() {
return "Test.toString()";
}
}
Output:
Test$1#15db9742
Test.toString()

Dispatch an event to main java

I have a CountDownTimer class that when it finishes counting down it should dispatch an event to main java (or any class that uses it) letting it know it has finished.
Here is my class:
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Label;
public class CountTimer {
private Label targetText;
private String direction;
private int minutes;
private int seconds;
private int totalSeconds;
private int timeTotal;
private int timeLoaded;
private Boolean test;
Timer timer;
public CountTimer(int min,int sec,String dir,Label targetTextField)
{
minutes = min * 60;
seconds = sec;
timeTotal = minutes + seconds;
if (dir == "down") {
totalSeconds = minutes + seconds;
}
else {
totalSeconds = 0;
}
if (targetTextField != null) {
targetText = targetTextField;
}
direction = dir;
}
public void start()
{
timer = new Timer(){
public void run(){
timeLoaded += 1;
if (direction == "up") {
totalSeconds++;
}
else {
totalSeconds--;
}
seconds = totalSeconds % 60;
minutes = (int) Math.floor(totalSeconds / 60);
String minutesDisplay = (minutes < 10) ? "0" + Integer.toString(minutes) : Integer.toString(minutes);
String secondsDisplay= (seconds < 10) ? "0" + Integer.toString(seconds): Integer.toString(seconds);
if (targetText != null) {
targetText.setText( minutesDisplay + ":" + secondsDisplay);
}
if (test=true) {
consoleLog(minutesDisplay + ":" + secondsDisplay);
}
if(timeTotal == timeLoaded) {
//DISPATCH CUSTOM EVENT TO MAIN.JAVA HERE
Window.alert("OK");
timer.cancel();
}
}
};
timer.scheduleRepeating(1000);
}
public int getTimeTotal()
{
return timeTotal;
}
public int getTimeLoaded()
{
return timeLoaded;
}
public int getProg()
{
return (int) Math.floor(timeLoaded/timeTotal*100);
}
public native final void consoleLog(String msg)/*-{
console.log(msg);
}-*/;
}
Please help, how can i do this?
I believe you are looking to implement the Publish-Subscribe pattern.
http://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern
You can allow your object to have subscribers register with it. Once you have finished your operation it can then notify the subscribers that it has finished.
Example:
public interface Subscriber {
public void notify();
}
public class Publisher {
private ArrayList<Subscriber> subscribers = new ArrayList<>();
public void addSubscriber(Subscriber s) {
subscribers.add(s);
}
public void doWork() {
System.sleep(100);
notifyAll();
}
private void notifyAll() {
for(Subscriber s : subscribers) {
s.notify();
}
}
}
There are several options.
If you have this requirement frequently, or you need to notify multiple Activities/widgets, you may consider using an EventBus:
How to use the GWT EventBus
Alternatively, your CountTimer can simply call a public static method in your main class:
MyApp.doSomething();

How to dynamically override a method in an object

If this is possible, how can I change what a method does after I might have created an instance of that class and wish to keep the reference to that object but override a public method in it's class' definition?
Here's my code:
package time_applet;
public class TimerGroup implements Runnable{
private Timer hour, min, sec;
private Thread hourThread, minThread, secThread;
public TimerGroup(){
hour = new HourTimer();
min = new MinuteTimer();
sec = new SecondTimer();
}
public void run(){
hourThread.start();
minThread.start();
secThread.start();
}
/*Please pay close attention to this method*/
private Timer activateHourTimer(int start_time){
hour = new HourTimer(start_time){
public void run(){
while (true){
if(min.changed)//min.getTime() == 0)
changeTime();
}
}
};
hourThread = new Thread(hour);
return hour;
}
private Timer activateMinuteTimer(int start_time){
min = new MinuteTimer(start_time){
public void run(){
while (true){
if(sec.changed)//sec.getTime() == 0)
changeTime();
}
}
};
minThread = new Thread(min);
return min;
}
private Timer activateSecondTimer(int start_time){
sec = new SecondTimer(start_time);
secThread = new Thread(sec);
return sec;
}
public Timer addTimer(Timer timer){
if (timer instanceof HourTimer){
hour = timer;
return activateHourTimer(timer.getTime());
}
else if (timer instanceof MinuteTimer){
min = timer;
return activateMinuteTimer(timer.getTime());
}
else{
sec = timer;
return activateSecondTimer(timer.getTime());
}
}
}
So for example in the method activateHourTimer(), I would like to override the run() method of the hour object without having to create a new object. How do I go about that?
No you can not override same method more then one time in the same class and you are creating an annonymous class of HourTimer.
hour = new HourTimer(start_time){
public void run(){
while (true){
if(min.changed)//min.getTime() == 0)
changeTime();
}
}
};
you can do it in another way by putting an if else condition inside activateHourTimer method as fallows-
private Timer activateHourTimer(int start_time){
if(Condition1){
hour = new HourTimer(start_time){
public void run(){
while (true){
//First Condition
}
}
};
}else if(Condition2){
hour = new HourTimer(start_time){
public void run(){
while (true){
//Second Condition
}
}
};
}else{
hour = new HourTimer(start_time){
public void run(){
while (true){
//Third Condition
}
}
};
}
hourThread = new Thread(hour);
return hour;
}
You can't override a method of a local (anonymous) class, but you can override the whole method via an anonymous class:
TimerGroup customTimerGroup = new TimerGroup() {
private Timer activateHourTimer(int start_time) {
hour = new HourTimer(start_time){
public void run() {
// do something different
}
};
hourThread = new Thread(hour);
return hour;
}
};

Categories