Download CS 111 Homework No. 3

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CS 111 Homework No. 3 (Java)
Solution
Note that you may have different, yet correct solutions!
1. (Problem 3.4) Write a Java application that creates and prints a random
phone number of the form xxx-xxx-xxxx. Include the dashes in the
output. Do not let the first digits contain an 8 or 9 (but don’t be more
restrictive than that), and make sure that the second set of three digits is
not greater than 742. Hint: Think through the easiest way to construct the
phone number. Each digit does not have to be determined separately.
import java.util.Random;
public class Hwk3_Q1 /*phone number xxx-xxx-xxxx */
{
public static void main(String[] args)
{
Random generator = new Random();
/* generate first part which doesnot contain a 8 or 9 */
int num1,num2,num3;
num1 = generator.nextInt(8); /* generate a random int between 0-7 */
num2 = generator.nextInt(8); /* generate a random int between 0-7 */
num3 = generator.nextInt(8); /* generate a random int between 0-7 */
int part1 =num1*100+num2*10+num3;
/* generate second part which is not greater than 742 */
int part2 = generator.nextInt(743);
/*generate third part of 4 digits */
int part3 =generator.nextInt(1000);
/* print the results in the required format: print preceding 0's also */
System.out.printf("%03d-%03d-%04d\n",part1,part2,part3);
}
}
2. (Problem 5.15) Design and implement an application that reads a string
from the user, then determines and prints how many of each lowercase
vowel (a, e, I, o, and u) appear in the entire string. Have a separate
counter for each vowel. Also count and print the number of non-vowel
characters.
import java.util.*;
public class Hwk3_Q2 {
public static void main(String[] args)
{
Scanner oku=new Scanner(System.in);
/* read one line of string */
System.out.println("Please type your text and press the enter key: ");
String txt = oku.nextLine();
/* define and initialize the six counters */
int cnt_a=0;
int cnt_e=0;
int cnt_i=0;
int cnt_o=0;
int cnt_u=0;
int cnt_others=0; //counter for non-vowel characters
/* define a integer for holding the number of characters in the string */
int cnt_all = txt.length();
/* check all char's in txt and update the 6 counters appropriately */
for (int k=0;k<cnt_all;k++) {
char c=txt.charAt(k);
if (c=='a') cnt_a++;
else if (c=='e') cnt_e++;
else if (c=='i') cnt_i++;
else if (c=='o') cnt_o++;
else if (c=='u') cnt_u++;
else if (c!='A' && c!='E' && c!='I' && c!='O' && c!='U')
cnt_others++;
}
/* display the values of the counters */
System.out.println("The character,\'a\' appears "+cnt_a+" times in the input text.");
System.out.println("The character,\'e\' appears "+cnt_e+" times in the input text.");
System.out.println("The character,\'i\' appears "+cnt_i+" times in the input text.");
System.out.println("The character,\'o\' appears "+cnt_o+" times in the input text.");
System.out.println("The character,\'u\' appears "+cnt_u+" times in the input text.");
System.out.println("The number of non-wovel characters in the input text is "+
cnt_others+".");
}
}
3. (Problem 5.16) Design and implement an application that plays the RockPaper-Scissors game against the computer. When played between two
people, each person picks one of three options (usually shown by a hand
gesture) at the same time, and a winner is determined. In the game, Rock
beats Scissors, Scissors beats Paper, and Paper beats Rock. The program
should randomly choose one of the three options (without revealing it),
and then prompt for the user’s selection. At that point, the program
reveals both choices and prints a statement indicating if the user won, the
computer won, or if it was a tie. Continue playing until the user chooses
to stop, then print the number of user wins, losses, and ties.
import java.util.*;
public class Hwk3_Q3 /* rock-scissors-Stone game */
{
static final int scissors=3, paper=2, rock=1;
static String genS (int value) {
String S="";
if (value==scissors)
S="scissors";
else if (value==paper)
S="paper";
else if (value==rock)
S="rock";
return S;
}
public static void main(String[] args)
{
Scanner oku=new Scanner(System.in);
System.out.println("To start the game,enter a number;\n\t 1 for Rock;"+
"\n\t 2 for Paper;\n\t 3 for Scissors;\n\t anythingelse to quit"+
"\nYour users_choice: ");
int users_choice=oku.nextInt();
int wins=0;
int losses=0;
int ties=0;
int computer;
while (users_choice==1 || users_choice==2 || users_choice==3){
computer=(int) (1+Math.random()*3);
String su,sc;
su = genS(users_choice);
sc = genS(computer);
System.out.println("Your have chosen "+su+".");
System.out.println("The computer has chosen "+sc+" randomly.");
if (users_choice==computer){
ties++;
System.out.println("This is a tie!");
}
else /* no tie */
if (
(users_choice==rock && computer==scissors) ||
(users_choice==scissors && computer==paper) ||
(users_choice==paper && computer==rock) ) {
wins++;
System.out.println("You won!");
}
else { /*lost */
losses++;
System.out.println("You lost!");
}
System.out.println("To start the game,enter a number;\n\t 1 for Rock;"+
"\n\t 2 for Paper;\n\t 3 for Scissors;\n\t anythingelse to quit"+
"\nYour users_choice: ");
users_choice=oku.nextInt();
} /*while loop */
/* determine if the game is played and then display the counters */
if ( (wins+losses+ties) == 0 )
System.out.println("The game was not played at all!");
else {
System.out.println ("You have won "+wins+" times!");
System.out.println ("You have lost "+losses+" times!");
System.out.println ("There were "+ties+" ties in the game!");
} /*else*/
}/* main method */
} /* class */