1. Find the Errors: The following code contains many errors. The errors may be
syntax, logic, or violations of conventions discussed in class. Copy this code into
DrJava and make any necessary corrections to the code so that it compiles and produces
the required output when run. Also, make sure that all naming conventions are
followed. Copy your corrected code from DrJava into the appropriate space in
Blackboard.
// Class demonstrates the error of using a random range
//
of (2 - 12) to simulate a 2-dice roll
public class simulateDiceRolls {
public static void main(String[] args) {
Random rand;
int roll1, roll2, sum, singleDice7s = 0, pairDice7s = 0;
System.println("Simulating 10,000 rolls
of the dice");
for (int i = 0; i < 10000;) {
// roll the dice 10,000 times
roll1 = rand.nextInt(6) + 1;
// Simulate rolling 2 dice
roll2 = rand.nextInt(6) + 1;
sum = roll1 + roll2
if (sum = 7) {
// Count the number of 7s
singleDice7s++;
}
roll3 = rand.nextInt( ) + 2;
// Simulate using 1 roll (2 12)
if (roll3 == 7) {
// Count the number of 7s
pairDice7s+;
}
}
// Show % of 7s for each simulation using:
(count/10,000) * 100)
System.out.println(% 1 roll = " +
doublesingleDice7s/100);
//16.67%
System.out.printf("% 2 dice = %d\n" , (double)pairDice7s/100); //
9.09%
}
}
2. Programming
Write a complete program that compares the number of vowels in names entered by a
user. Your program should first ask the user for the number of names to be entered, and
then repeatedly asks the user to enter names until that number is reached.
Each name entry will be of the form:
Your program should include a method named countVowels that receives a single String
parameter and returns the number of vowels found in that String.
When finished, the program should output the name with the most vowels and the
number of vowels in that name. If two names have this same maximum number of
vowels, only the first name entered will be displayed.
3.
Programming
Write a complete program that plays a single game of craps, displays the individual rolls
and reports when a game is “LOST” or “WON” and shows the number of rolls used by
the game.
The algorithm is as follows:
• Simulate the roll of 2 dice using a JAVA random number generator.
• Record the sum of this first roll as the game point and print out: Game point:
• (if) the game point is 7 or 11
print out: Game won in 1 roll
• (else) continue rolling the dice. After each roll, print:
•
a. (if) a roll is either 7 or 11
print out: Game lost in rolls. Sorry.
b. (else if) a roll equal the game point
print out: Game won in rolls. Congratulations!
c. Otherwise, repeat step 4
4.
Programming
Write a static method named favoriteLetter that accepts two parameters: a Scanner from
the console, and a favorite letter represented as a one-letter String. The method
repeatedly prompts the user until two consecutive words are entered that start with that
letter. The method then prints a message showing the last word typed.
You may assume that the user will type single-word responses to each prompt. Your
code should be case-sensitive, (if the favorite letter is a, you should not stop prompting
if the user types words that start with a capital A.
The following represents output from two calls to your method:
(User input is underlined.)
Scanner console = new
Scanner console
Call
Scanner(System.in);
favoriteLetter(console, "y");
Output Looking for two "y" words
in a row.
Type a word: hi
Type a word: bye
Type a word: yes
Type a word: what?
Type a word: yellow
Type a word: yippee
"y" is for "yippee"
5.
= new
Scanner(System.in);
favoriteLetter(console, "A");
Looking for two "A" words
in a row.
Type a word: I
Type a word: love
Type a word: COSC236!
Type a word: AND
Type a word: PROGRAMS
Type a word: are
Type a word: always
Type a word: Absolutely
Type a word: Awesome
"A" is for "Awesome
Programming
Write a static method named countEvenDigits that accepts an integer as its parameter
and returns the number of even-valued digits in that number. An even-valued digit is
either 0, 2, 4, 6, or 8.
For example, the number 8546587 has four even digits (the two 8s, the 4, and the 6), so
the call countEvenDigits(8346387) should return 4.
You may assume that the value passed to your method is non-negative. You may
NOT use a String to solve this problem.
Purchase answer to see full
attachment