Project 1 PRIME PROJECT:
Create a class called Primes. It will have the main method inside it as well as the methods described here. Since the main method is static, all of your methods should be static (more on what static means later).
Create a method called isPrime that determines “whether” an integer is a prime number (The number will be passed as a parameter)
Create a method called printNPrimes that will print the first N prime numbers. Use your method isPrime to do this. (i.e. if n = 5, it should print 2 3 5 7 11
)
Create a method called printPrimeFactors that will print the prime factors of a number.
(i.e. if n = 154, the method will print 2 7 11
Create a main method that will print the first 20 prime numbers and determine the prime factors of 546.
Project 2: Write a program that simulates flipping a coin a given number of times. Count the number of heads and tails that come up and output the results as counts. See the example below:
Sample Run of the Program (input and output values in bold):
int numFlips = 25; (Change this value several times to test)
Heads: 15
Tails: 10
Project 3: Each time Kevin re-reads his Java book (which happens every month), he learns 10% of whatever material he didn’t know before. He needs to score at least 95% on his comprehensive exam to become a certified Java developer. When Kevin started, he knew nothing about Java. Write a method that simulates Kevin’s learning progress and returns the number of months it will take him to get ready for the exam. Write a main method that displays the result (in years and months).
**You could do it all in the main method and have it print out the number of months.
Project 4: Write a program that receives 5 integer values from a user. Determine and display the highest and lowest integers input as well as the average value of the 5 integers. See the example below. This program requires no more than 5 variables (4 ints and 1 double).
Sample Run of the Program (input and output values in bold):
Enter integer #1: 5
Enter integer #2: 17
Enter integer #3: 34
Enter integer #4: 19
Enter integer #5: 29
Lowest Number: 5
Highest Number: 34
Average Value: 20.8
Project 9: Write a program to simulate rolling a 6-sided die 1000 times, counting how many 1's, 2's, 3's etc... that the "die" rolls. Display a table of results as shown. There is no input in this program.
Sample Run of the Program
1’s = 160
2’s = 182
3’s = 165
4’s = 177
5’s = 155
6’s = 161
Project 10: Write a program that asks the user for an integer, then displays a backwards count from that integer down to 1, finishing with the sum of all of those integers. See below to better understand what is being asked.
Sample Run of the Program (input and output values in bold):
Enter an integer: 6
6 + 5 + 4 + 3 + 2 + 1 = 21
Project 11: Write a program that will allow the user to play a guessing game with the computer. The computer will pick a random number from 1 to 100. The user then is asked to guess the computer’s number. If the user is right, say so, output the number of guesses it took and end the program. If the user is wrong, state whether the guess is TOO LOW or TOO HIGH and allow the user to guess again, until they get it right.
Sample Run of the Program (input values are in bold type):
Guess #1: 50
TOO HIGH. TRY AGAIN.
Guess #2: 25
TOO LOW. TRY AGAIN.
Guess #3: 37
TOO HIGH. TRY AGAIN.
Guess #4: 32
RIGHT ON! YOU GOT IT IN 4 GUESSES.
Project 12: Write a program that will allow you to play a game of Rock-Paper-Scissors against the computer. In the game, the computer randomly selects rock, paper or scissors. You also make a choice. If both choose the same, it is a tie and the game repeats. Otherwise, the winner is based on the following:
ROCK beats SCISSORS
SCISSORS beats PAPER
PAPER beats ROCK
Use a random number for the computer's choice. You will make your choice by entering a number from 1-3 based on the following (provide a menu):
1 = ROCK
2 = PAPER
3 = SCISSORS
Sample Run of the Program (input values are in bold type):
Your choice: 2
PAPER
Computer:
PAPER
Result: TIE
Your choice: 3
SCISSORS
Computer:
ROCK
Result: Computer Wins!
Project 13: When a player lands in jail in the game of Monopoly, he/she has the opportunity to “get out” by rolling doubles with the dice. The player must pay $50 if they are unsuccessful after 3 tries. Assume that they could roll as long as they wanted. How long, on average, would one expect to take to roll doubles? To answer this question, simulate this situation 10,000 times and find the average number of rolls needed per attempt to achieve doubles.
For example, if we ran three trials and they took 9, 5 and 6 rolls, the average would be 6.7 (20/3) rolls. To see this another way, look at it in table form:
Project 14: Write a program to determine the number of even and odd numbers as entered by the user. The program should ask how many integers to be entered, and then allow the user to enter the integers, using a loop. As the integers are entered, keep track of how many are even and how many are odd. At the end, display the results as shown in the example below. You may assume that the user will only enter positive integers.
Sample Run of the Program (input and output values in bold):
How many numbers will be entered: 4
Enter number 1: 10
Enter number 2: 11
Enter number 3: 18
Enter number 4: 20
Evens: 3
Odds: 1
Project 15: Write a method int addOdds(int n) that calculates and returns the sum of all odd integers from 1 to n. Your method should use exactly one for loop and no other iterative or if-else statements.
You can make the header: public static int addOdds(int n) so you can test the method in the same class as the main method.
Project 16: Write a program that uses nested loops to print the following multiplication table. All output MUST be generated using loops and must look EXACTLY as shown.
EXTRA CREDIT:
Project 17: Write a program to find the digital root of a given integer (maximum 4 digits). The digital root of a number can be found by adding its digits together until the result is a single digit. For example, to find the digital root of 347: 3+4+7=14 then 1+4=5. So the digital root of 347 is 5.
You MUST use a WHILE loop.
Sample Run of the Program (input and output values in bold):
Enter an integer up to 4 digits: 343
The digital root of 343 is 1
Here are a couple more to try:
The digital root of 3252 is 3
The digital root of 987 is 6
Project 18: Write a program to find the Greatest Common Divisor (GCD) for two positive integers. To find the GCD you will use the Euclidean algorithm, as follows:
1) Let x and y be positive integers
2) Let r = remainder of x divided by y
3) If r = 0, then y is the GCD. Otherwise, let x=y, y=r and go back to step 2
Sample Run of the Program (input and output values in bold):
Enter integer 1: 64
Enter integer 2: 44
The GCD of 64 and 44 is 4
Here are a couple more to try:
The GCD of 32 and 128 is 32
The GCD of 35 and 113 is 1
Project 19: Write a program to determine all possible ways to make change for a dollar using only quarters, dimes, nickels and pennies. The output must show the results, one line at a time, without showing zeros. Count the number of ways that the program finds to make change and print this result at the end of the program.
For example: 2 quarters, 5 dimes not 2 quarters, 5 dimes, 0 nickels, 0 pennies.
The total number of combinations is 242 (I think!)
Hint: This program requires 4 FOR LOOPS (one for each type of coin).
Project 20: Consider all fractions that have positive denominators between 1 and 100. Write a program that finds the two such fractions that are closest to 17/76; one from above and one from below.