Java codes I only need the codes not the program
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 (inti = 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 =
%dn” , (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: <first name>
<whitespace> <last name>
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:
<point><tab> - (if)
the game point is 7 or 11
print out: Game won in 1 roll - (else)
continue rolling the dice. After each roll, print:
<roll-value><tab>
- (if)
a roll is either 7 or 11
print out: Game lost in <roll-count> rolls. Sorry. - (else
if) a roll equal the game point
print out: Game won in <roll-count> rolls. Congratulations! - Otherwise,
repeat step 4
4- Write a static method named favoriteLetter that accepts two parameters: a Scanner from
the console, and afavorite 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.)
Call |
Scanner console = new favoriteLetter(console, |
Scanner console = new favoriteLetter(console, |
Output |
Looking for two “y” 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 |
Looking for two “A” 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 |
5. 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 aString
to solve this problem.