Implementing the Runnable Interface-2
Thu Dec 29 2022 18:56:17 GMT+0000 (Coordinated Universal Time)
Saved by @prettyleka #java #generics
mport java.util.Random;
// Checkpoint 1
// public class CrystalBall implements Runnable {
public class CrystalBall{
/* Instance Variables */
// Removed in checkpoint 3
/* Constructors */
// Removed in checkpoint 3
/* Instance Methods */
// Removed in checkpoint 3
public void ask(Question question) {
System.out.println("Good question! You asked: " + question.getQuestion());
this.think(question);
System.out.println("Answer: " + this.answer());
}
private void think(Question question) {
System.out.println("Hmm... Thinking");
try {
Thread.sleep(this.getSleepTimeInMs(question.getDifficulty()));
} catch (Exception e) {
System.out.println(e);
}
System.out.println("Done!");
}
private String answer() {
String[] answers = {
"Signs point to yes!",
"Certainly!",
"No opinion",
"Answer is a little cloudy. Try again.",
"Surely.",
"No.",
"Signs point to no.",
"It could very well be!"
};
return answers[new Random().nextInt(answers.length)];
}
private int getSleepTimeInMs(Question.Difficulty difficulty) {
switch (difficulty) {
case EASY:
return 1000;
case MEDIUM:
return 2000;
case HARD:
return 3000;
default:
return 500;
}
}
}
mport java.util.Random;
// Checkpoint 1
// public class CrystalBall implements Runnable {
public class CrystalBall{
/* Instance Variables */
// Removed in checkpoint 3
/* Constructors */
// Removed in checkpoint 3
/* Instance Methods */
// Removed in checkpoint 3
public void ask(Question question) {
System.out.println("Good question! You asked: " + question.getQuestion());
this.think(question);
System.out.println("Answer: " + this.answer());
}
private void think(Question question) {
System.out.println("Hmm... Thinking");
try {
Thread.sleep(this.getSleepTimeInMs(question.getDifficulty()));
} catch (Exception e) {
System.out.println(e);
}
System.out.println("Done!");
}
private String answer() {
String[] answers = {
"Signs point to yes!",
"Certainly!",
"No opinion",
"Answer is a little cloudy. Try again.",
"Surely.",
"No.",
"Signs point to no.",
"It could very well be!"
};
return answers[new Random().nextInt(answers.length)];
}
private int getSleepTimeInMs(Question.Difficulty difficulty) {
switch (difficulty) {
case EASY:
return 1000;
case MEDIUM:
return 2000;
case HARD:
return 3000;
default:
return 500;
}
}
}



Comments