package tree; import java.util.Scanner; public class Interface { static Scanner scan; public static void main(String[] args) { Node tree = null; char choice; scan = new Scanner(System.in); printExplanatoryMessage(); do { // until interactive user signals to quit choice = printMenuAndGetAction (); // Insists on a valid response tree = performAction(tree, choice); // Take an action } while (choice != 'Q' && choice != 'q'); System.out.println(); scan.close(); } /************* P R I N T E X P L A N A T O R Y M E S S A G E *********** This function prints a message to the interactive user explaining briefly what the program is going to do. Documented and coded by W. Knight. */ static void printExplanatoryMessage() { System.out.println("\n\nThis program allows an interactive user to create, modify,"); System.out.println("display, and destroy binary trees. The program is menu driven."); System.out.println("Press the Enter key to continue."); scan.nextLine(); // Clear the input stream up through newline. } // printExplanatoryMessage() /******************** P E R F O R M A C T I O N ************************** This function performs an action on the tree based on the reponse character. Documented and coded by W. Knight and Dana Vrajitoru */ static Node performAction(Node tree, char response) { switch (response) { // Response is necessarily one of the following. case 'B' : case 'b' : tree = Node.buildABinaryTree (tree, null, scan); break; case 'C' : case 'c' : System.out.println("\nThere are " + Node.countOfZeroDataValues (tree) + " zero data values in the current tree."); break; case 'D' : case 'd' : System.out.println(); tree = null; System.out.println("\nThe tree should now be empty."); break; case 'H' : case 'h' : System.out.println("\nThe height of the tree is " + Node.height(tree)); break; case 'I' : case 'i' : Node.incrementEachDatum (tree); System.out.println("\nEvery data value should have been incremented."); break; case 'P' : case 'p' : if (tree == null) System.out.println("\nThe tree is empty."); else tree.graphPrint(); break; case 'Q' : case 'q' : break; // Do nothing. The program will quit. case 'R' : case 'r' : System.out.println(); if (tree != null) tree.reverse(); System.out.println("\nThe tree should now be the mirror image of what it was."); break; case 'S' : case 's' : if (tree != null) System.out.println("\nThe sum of the negative data values is " + tree.sumOfNegativeData ()); else System.out.println("\nThe sum of the negative data values is 0"); break; } return tree; } // performAction() /********** P R I N T M E N U A N D G E T A C T I O N ************* This function displays a menu of actions and asks an interactive user to select one. It will not return until it has read an acceptable response. Documented and coded by W.Knight. */ static char printMenuAndGetAction () { String response; System.out.println("\n"); System.out.println("Which of the following actions do you wish to take?"); System.out.println(" B)uild or modify the current tree interactively."); System.out.println(" C)ount and report number of zero data values in the tree"); System.out.println(" D)estroy the current tree."); System.out.println(" H)eight of the tree -- determine it."); System.out.println(" I)ncrement every data value in the current tree by 1."); System.out.println(" P)rint the current tree on the screen."); System.out.println(" Q)uit."); System.out.println(" R)eplace the current tree by its mirror image."); System.out.println(" S)um the negative data values in the current tree."); System.out.println(); System.out.println("Your choice:"); response = scan.nextLine(); while (true) { // loop will continue until user gives an appropriate response if (response.length() > 0 && "BbCcDdHhIiPpQqRrSs".contains(response.subSequence(0, 1))) return response.charAt(0); // EXIT FROM THE FUNCTION WITH PARAMETER HAVING OK VALUE else System.out.println("Improper Response; Please type an appropriate letter."); response = scan.nextLine(); } } // printMenuAndGetAction() }