One- Dimensional Arrays
Project 1: Write a program that generates 10 random integers from 1 to 100 and stores them in a single array. Then display a list of the odd and even integers, with results shown as shown below.
Sample Output
Odds: 11 75 43 89
Evens: 26 32 68 52 84 40
Project 2: Write a program that tests the randomness of the computer’s random number generator. Generate 10,000 integers in the range 0 to 9 and display a list of the counts for each number at the end. Since there is a 1 in 10 chance for each number, there should be about 1000 of each when done.
Sample Output
0’s: 1007
1’s: 1024
2’s: 989
etc...
Project 3: Roll a pair of dice and find their sum 250 times. Count the frequency of each sum in an array and then display a bar graph of the results, like that shown below:
2: ********
3: **************
4: ********************
5: *******************************
6: **************************************************
7: **********************************************************
8: ********************************************
9: **************************
10: ******************
11: ************
12: ******
Project 4: A teacher wants to select up to 10 students from a class of size N (N>=20, as chosen by the user) to take part in a survey. In order to eliminate bias on the part of the teacher, a computer program will be used to randomly select the students. Each student will be assigned a unique integer from 1 to N and the program will generate the numbers of the students randomly. Each student may be chosen only once (i.e., no repeats) and a list of the students’ numbers will be printed to the screen. Begin the program by asking the teacher how many students are in the class. Then ask for the number of students to generate (must be from 1 to 10). Finally, display the random student numbers.
Example Run of the program:
How many students are in the class? 32
How many students to randomly select? 5
Students selected are: 13 25 7 8 30
2D Arrays
Project 5: Write a program to generate a random playing field for the game Minesweeper. Begin the program by asking what the dimensions of the field should be (max: 10 x 10). Then ask for the number of mines to be placed (error-check to make sure that there are enough spaces for the specified number of mines). Next, randomly place the mines on the field. Then display the field to the screen showing:
X's for squares containing mines
A number corresponding the number of adjacent mines for each box that is adjacent to at least one mine
0’s for squares that are not adjacent to mines
Here is an example for a 5x5 field with 6 mines:
Project 6: Write a program to simulate the random movement of an ant walking on a coordinate grid with dimensions 9x9. Position the ant in cell (5,5) to begin. Then allow the ant to move randomly up, down, left or right. The ant stops when it falls off of the grid (goes to 0 or 10 in either direction). In the example below, the ant falls off the grid to the South, noted by the X (you don’t have to show the x it is just for illustration purposes). As the ant is moving, you need to keep track of the following information:
The number of moves before falling off
The number of times the ant visited each cell (displayed in the matrix)
Which direction the ant fell off the grid (North, South, East or West)
Note: You must update and redisplay the matrix after each movement
Project 7: GET IN LINE – Write a program that uses an ArrayList to store a list of names for people who are standing in a line. Initially, the list should contain (in the order given): Andrew, Sarah, Will, Evelyn and David. The program should then carry out the following activities:
· Steven joins the end of the line
· Evelyn gets out of the line
· Samantha cuts in line in and gets in position #1 (positions start with 0)
· Display the current position # for Will*
· Jessica joins the end of the line
· Will gets out of the line and rejoins it at the end, behind Jessica
· Display the name of the person currently in the next to last position in line**
· The first and last people in line switch places
· Display a message declaring whether or not a person named David is in the line.***
· The user enters the name of a new person (use “Chris”) and a position for them to enter the line (use 3). Be sure to error-check the position #, as the line may not contain enough people to make that position possible.
· All people whose names start with the letter “S” get out of line
· Display the final order of the people in the line.****
The displayed results should be as follows (use the *’s for reference):
* 3
**Jessica
*** YES or TRUE
**** [Will, Chris, David, Jessica, Andrew].
NOTE: You will likely use every ArrayList method at least once in this program.
Project 8: Create a method that accepts an ArrayList of Strings (assumed to be in alphabetical order) called animals and another String animal. A new ArrayList should be returned with animal inserted in the correct position. To test your method, you will have to create an ArrayList of animal names that are in alphabetical order. Then try to send several different other animals to the list to put them in alphabetical order.
Try each of the following words to test your program: lion, aardvark, zebra
Project 9: Write a method that takes an ArrayList<String> and returns a new ArrayList<String> in which elements are stored in reverse order.
Project 10: Write a method called scramble that accepts a String. You should assign each letter to an element to create a String array consisting of single letters. Then scramble the letters and return a String that is the original String scrambled.