Answer to Question #278162 in Java | JSP | JSF for barley

Question #278162

reate a simple text-based console game that implements at least three (3) interfaces


1
Expert's answer
2021-12-11T01:58:54-0500
import java.util.*;
import java.lang.*;

interface Number {
  int readNumber();
}
interface Choice implements Number {
  boolean find(int target);
}
interface ConsoleGame implements Choice {
  void success(int target);
}
public class Game implements ConsoleGame {
  @Override
  int readNumber() {
    Random random = new Random();
    System.out.printf(
      "===========================\n" +
      "        Game Start         \n" + 
      "===========================\n"
    );
    return random.nextInt();
  }
  @Override
  boolean find(int target) {
    Scanner scanner = new Scanner(System.in);
    System.out.printf("Enter number: ");
    int number = scanner.nextInt();
    if (number < target)
      System.out.printf("Lower than the target!\n");
    if (number > target) 
      System.out.printf("Upper than the target!\n");
    return number == target;
  }
  @Override
  void success(int target) {
    System.out.printf(
      "You got the target: %d" +
      "===========================\n" +
      "          YOU WON          \n" +
      "===========================\n", target
    );
  }
  public static void main(String[] args) {
    int target = readNumber();
    while (!find(target)) {
      System.out.printf("Try again!");
    }
    success(target);
  }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS