Question
For this assignment USE JAVA programming and Program 1 uses arrays and does a simple sort. Program 2: a simple two dimension array program. Assignment-Arrays
For this assignment USE JAVA programming and
Program 1 uses arrays and does a simple sort.
Program 2: a simple two dimension array program.
Assignment-Arrays
Program 1: Generating data for an array via Random class and sorting
Make a program that will ask for a size of an array, then allocate and integer array called values of that size.
Use the Random class to get numbers from 0 to 99 and store them into the array. Use a for loop to get each random number, store each into the array, and print each value.
Then use the bubble sort to sort the array, and print out the stored array. The bubble sort code is as follows:
boolean swap;
do
{
swap = false;
int temp;
for (int count = 0; count < values.length - 1; count++)
if (values[count] > values[count+1])
{
temp = values[count];
values[count] = values[count+1];
values[count+1] = temp;
swap = true;
}
} while (swap);
Make the bubble sort into a method and have the array defined and filled in main then pass it to the method. Try a run with 15 numbers.
Random statements needed are: import java.util.Random; Random randomNum = new Random(); and num = randomNum.nextInt(100);).
Program 2: Using two dimensions
Declare a two dimensional array of 10 by 10 of characters. Go through each and every element (use nested for loops) and use a random value from 0 to 14. If the value is 0, put in a *, if it is 1 or 2, put in a ., otherwise put in a blank. You will get something like
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
0 |
|
|
|
| . | . |
|
|
| * |
1 |
| . | * |
|
|
|
|
|
|
|
2 |
|
|
|
|
| . |
|
|
|
|
3 |
|
|
| * |
|
|
|
| . |
|
4 | . |
|
|
|
|
|
|
|
|
|
5 |
|
| . |
|
|
|
| . |
|
|
6 |
|
|
|
|
| . |
|
|
|
|
7 | . |
|
| . |
|
|
|
|
|
|
8 |
|
| . |
|
|
|
| * |
|
|
9 |
|
|
|
|
|
|
|
|
|
|
That will be a starry night!
Have the program then ask where you want to place your satellite (via row and column). Verify the row and column is in the 0 to 9 range. If there is a star, state Cannot place there and ask for another location. If it is blank, put in a @.
Now print Starry night as a title then print your starry night.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started