import java.util.Scanner; /* Java application having as its purpose to be used in testing the class ** TennisGameScore. The user is asked to enter names for the server and ** receiver in a game of tennis, and then to enter s's and r's to identify ** which player ('s' for server, 'r' for receiver) scored each point in ** the game. Characters other than 's' or 'r' are ignored, as are any ** characters that describe a point following the end of the game. ** ** Author: R. McCloskey ** Date: November 2014 */ public class TennisGameScoreTester { private static Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { String server = getString("Enter name of server: "); String receiver = getString("Enter name of receiver: "); System.out.println(); TennisGameScore g = new TennisGameScore(server, receiver); System.out.println(g.toString()); while (!g.isOver()) { System.out.print("\nEnter s or r (or a sequence thereof): "); String response = keyboard.nextLine(); for (int i=0; i != response.length() && !g.isOver(); i=i+1) { char ch = response.charAt(i); if (ch == 's') { g.pointForServer(); } else if (ch == 'r') { g.pointForReceiver(); } else { // ignore character } System.out.println(g.toString()); // display status of game } } System.out.println("\nPoints won by " + g.getServerName() + ": " + g.getServerPoints()); System.out.println("Points won by " + g.getReceiverName() + ": " + g.getReceiverPoints()); System.out.println("\nGoodbye."); } /* Displays the specified message (prompt) and returns the response ** entered by the user at the keyboard. */ private static String getString(String prompt) { System.out.print(prompt); return keyboard.nextLine(); } }