CompSci
  • Home
  •  Unit 1
    Output Exercises
     Unit 2
    Variables Math Modules Exercises
     Unit 3
    Conditionals Exercises
     Unit 4
    Iteration / Loops Exercises
     Unit 5
    Strings Exercises
     Unit 6
    Lists Exercises
     Unit 7
    Functions Exercises
  •  Unit 1
    Java Introduction Java Syntax Errors & Coding Conventions Format Method Exercises
     Unit 2
    Variable Introduction Math / Arithmetic Operators Obtaining a Value Formatting Numeric Output Concatenation & Constants Division & Type Casting Exercises
     Unit 3
    Conditionals Exercises
     Unit 4
    while Loops for Loops Exercises
     Unit 5
    Strings Exercises
     Unit 6
    Methods Method Parameters Method Return Types Exercises
  • Muddy City
  • Github Classroom
  •  Assignments
    Add an Assignment Edit Assignment Search Program Code
     Forum
    Ask a Question Search the Forum
     New Account
    New Account


Account Mgmt
Reset Password

Java - Iteration (Loops)


Exercises

Investment

Difficulty: Low

Title: Investment

Description:
Create an Investment application that calculates how many years it will take for an initial investment to be worth at least x amount of money if compounded annually at 7.5%.

  • Prompt the user for the initial investment.
  • Prompt the user for the amount of money you want to earn.
  • Every iteration / loop is equal to 1 year
  • Output the dollar amount of the investment as it grows each year
  • Output how many years (loops) it took to reach the amount you wanted to earn

Output:
Initial Investment: 2500
Amount to Earn: 5000
Year 1: $2,687.50
Year 2: $2,889.06
Year 3: $3,105.74
Year 4: $3,338.67
Year 5: $3,589.07
Year 6: $3,858.25
Year 7: $4,147.62
Year 8: $4,458.69
Year 9: $4,793.10
Year 10: $5,152.58
Number of years: 10


Digit Sum

Difficulty: Low

Title: Digit Sum

Description:
Create a DigitsSum application that prompts the user for a non-negative integer and then displays the sum of the digits.

  • This program builds upon the DigitsDisplay and Digits program, but the output is the sum of the number entered (ex. 8 + 9 + 2 = 19).
  • You need to catch in your program if a user enters a value that is not a non-negative integer. If they do enter an incorrect value you need to prompt them of this and have them re-enter their value.

Output:
Enter a positive integer: 892
The sum of the digits is: 19


Prime Number

Difficulty: Medium

Title: Prime Number

Description:
A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example 2, 3, 5, and 7 are prime numbers, but 4, 6 , 8, and 9 are not. The secret is modulus!!!

  • The only even prime number is 2. All other even numbers can be divided by 2 and are not prime numbers. For example 4 MOD 2 equals 0, which is your hint how you can use modulus to determine if a number is even or odd.
  • If the sum of a number's digits is a multiple of 3, that number can be divided by 3 and is not a prime number. This means that not all odd numbers are automatically prime numbers. Your hint once again is modulus. For example 9 MOD 3 equals 0, which means it divides evenly by three and is not a prime number.
  • Any number divisible by 5 and does not have a remainder is not a prime number (HINT: It is the same hint as above).
  • Any number divisible by 7 and does not have a remainder is not a prime number (HINT: It is the same hint as above).
  • Any number divisible by 11 and does not have a remainder is not a prime number (HINT: It is the same hint as above).
  • Any number divisible by 13 and does not have a remainder is not a prime number (HINT: It is the same hint as above).
Here is a list of prime number from 2 to 97 to help you check your work:

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Create a PrimeNumber application that has two modes:
  • Prompts the user for a number and the displays a message indicating whether the number is prime or not. Write out "Prime" or "Not Prime" to pass the tests.
  • Prompts the user for two numbers and then displays the prime numbers between those numbers (HINT: This is where you will need to use a loop).

Output:
1. Single Number
2. List all Prime Numbers
Mode: 1
Number: 47
Prime


1. Single Number
2. List all Prime Numbers
Mode: 1
Number: 15
Not Prime
1. Single Number
2. List all Prime Numbers
Mode: 2
Start: 2
End: 100
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97


Random Walk

Difficulty: High

Title: Random Walk

Description:
In the “random walk” problem, a person is placed at the center of a 7 meter long bridge. Each step the person moves 1 meter either forward or backward at random.

X

Create a RandomWalk application that determines how many steps the person will walk before taking a step off the bridge.
  • Have the application run 50 trials
  • At the end of the 50th run display the average
  • At the end of the 50th run display the maximum number of steps

Hint: Generate a random number between 0 and 1, with 0 meaning to go forward and 1 meaning to go backward.
NOTE: My average steps ranged between 15 to 20. What does it mean if you have some very large average number of steps? What did you not account for in your code?

Output:
Example output with just 10 trials (I showed the number of steps per trial to show you the logic for average and maximum number of steps):

Fell off after 8 steps
Fell off after 26 steps
Fell off after 12 steps
Fell off after 4 steps
Fell off after 50 steps
Fell off after 14 steps
Fell off after 4 steps
Fell off after 16 steps
Fell off after 8 steps
Fell off after 30 steps
Average Steps 17
Maximum Steps 50



Computer Science