// CS100, Spring 2000, Solutions to Review questions for Prelim 3 // 1. Solution#1 //One way of doing it.. class RockPaperScissors { static void main(String[] args) { final int ROCK = 0, PAPER = 1, SCISSORS = 2, CHOICES = 3; final String choices = "rps"; // user's single-letter choices //Create array to determine winners boolean[][] win = // win[i][j] is whether i beats j { // ROCK PAPER SCISSORS { false, false, true }, // ROCK { true, false, false }, // PAPER { false, true, false } }; // SCISSORS final int TIE = 0, WIN = 1, LOSE = 2, OUTCOMES = 3; int[] outcomes = new int[OUTCOMES]; // # of ties, wins, losses so far final String[] LABELS = { "tie(s)", "human win(s)", "computer win(s)" }; boolean go = true; // whether to keep playing TokenReader in = new TokenReader(System.in); while (go) { int computer = (int) (Math.random()*CHOICES); // computer's choice System.out.print("what is your choice of " + choices + "?"); String s = in.readString().toLowerCase(); char c = s.charAt(0); int human = choices.indexOf(c); // human's choice if (c == 'q') go = false; else if (human<0) System.out.println("invalid choice"); else { System.out.print("computer chose " + choices.charAt(computer)); System.out.print(" you chose " + c); int outcome; // outcome of this game if (computer == c) outcome = TIE; else if (win[computer][human]) outcome = LOSE; else outcome = WIN; System.out.println("; outcome -- " + LABELS[outcome]); outcomes[outcome]++; } } for (int i = 0; i < OUTCOMES; i++) System.out.println(outcomes[i] + " " + LABELS[i]); } } // Solution#2 //Another way.. public class RockPaperScissors { public static void main(String[] args) { TokenReader in = new TokenReader(System.in); String s; int ties=0,compwin=0, humanwin=0; //keep playing till bored do{ //Computer does a random selection betwee Rock(R),Paper(P) and Scissors(S) double selector=Math.random()*3; char Comp; Comp= (selector>2)? 'R':((selector>1)? 'P':'S'); //Prompt for input until correct input System.out.println("Select one of Paper,Rock and Scissors"); s=in.readString(); s=s.toUpperCase(); char Human=s.charAt(0); while(Human!='R'&& Human!='P' && Human!='S'){ System.out.println("Select one of Paper,Rock and Scissors"); s=in.readString(); s=s.toUpperCase(); Human=s.charAt(0); } //blurt out choices System.out.println("My choice: " +Comp); System.out.println("Your Choice: "+Human); //decide winner int winsel=Human-Comp; //winsel={1,2,-3}=>comp wins, {-1,-2,3}=>human wins winsel=winsel*((2.5-winsel)>0? 1:-1); //now winsel={1,2,3}=>comp wins, {-1,-2,-3}=>human wins if(winsel<0){ System.out.println("OK, You win this time"); humanwin++;} else{ if(winsel>0){ System.out.println("I win!!"); compwin++;} else{ System.out.println("Its a Tie!!"); ties++;}} System.out.println("Play More?"); s=in.readString();} while(s.charAt(0)=='y'||s.charAt(0)=='Y'); System.out.println("Report: We had "+(ties+humanwin+compwin) +" games."); System.out.println(" I won "+compwin+" times "); System.out.println(" You won "+humanwin+ " times "); System.out.println(" There were "+ties+" Tied games"); } } // 2. class Histogram { //print a formatted number upto 3 digits static void print(int n) { String s = "" + n; while (s.length() < 3) s = " " + s; System.out.print(s + " "); } static void main(String[] args) { TokenReader in = new TokenReader(System.in); int n = in.readInt(); // next input to process int next = n; // next number to print histogram for if (n>=0) print(n); while (n>0) { if (n == next) { System.out.print("*"); n = in.readInt(); } else { System.out.println(); if (n>next) next++; else next--; print(next); // note: this will print an unneeded line } } System.out.println(); } } // OR class Histogram { static void print(int n) { String s = "" + n; while (s.length() < 3) s = " " + s; System.out.print(s + " "); } static void main(String[] args) { TokenReader in = new TokenReader(System.in); int n = in.readInt(); // next input to process int next = n; // next number to print histogram for String bar = ""; // bar of stars to print for next while (n>0) { if (n == next) { bar += "*"; n = in.readInt(); } else { print(next); System.out.println(bar); bar = ""; if (n>next) next++; else next--; } } if (bar.length()>0) // print final bar { print(next); System.out.println(bar); } } } OR // Using arrays: Review question 2 public class rvw2arr { public static void main(String[] args) { TokenReader in = new TokenReader(System.in); final int LIMIT=1000; int j,k,max_num=0; int[] num = new int[LIMIT]; //atmost 3 digits long System.out.println("Input a sequence of non-negative #'s terminated by -1"); //read numbers till -ve, calculate max num[0]=in.readInt(); for(k=1;k=0;k++){ max_num=Math.max(max_num,num[k-1]); num[k]=in.readInt();} //allocate max+1 counts int[] num_count = new int[max_num+1]; //cont occurences for(k=0;k=0;k++) num_count[num[k]]++; //print out counts for(k=0;k<=max_num;k++){ System.out.print(" "+ k + " "); for (j=0;j