Question
in java For this assignment, all code can be in main() that is methods and classes are not required. Task 1: perform the following array
in java
- For this assignment, all code can be in main() that is methods and classes are not required.
Task 1: perform the following array processing tasks:
- Generate and display two random numbers size1 and size2 - between 1 and 10.
- The values size1 and size2 will be used in step 2 as the size of each array.
- Generate size1 random integers between 1 and 25 and write them to the 1st array
- Generate size2 random integers between 1 and 25 and write them to the 2nd array
- Sort each array using the sort method in the Arrays class
- After each array is sorted, display the values in each array.
Task 2: perform the following file and array-based tasks:
- In your code create a file named assignment1.txt.
- Correct: File fileName = new File("assignment1.txt");
- Do not include any path names when creating the file.
- The file will be placed in your current working directory.
- Incorrect: File fileName = new File("C:/Dev/assignment1.txt");
- Correct: File fileName = new File("assignment1.txt");
- Open the file for writing.
- PrintWriter resultsFile = new PrintWriter (fileName);
- Write the values in the two arrays in sorted order to the file.
- See the output below where it shows Writing values to file for an example.
- Display each value as you write it to the file
- Close the file.
Task 3: perform the following file and array-based tasks:
- Reopen the file for reading.
- Scanner readFile = new Scanner (fileName);
- Read values from file and place into an array, removing duplicates as you read the values.
- Display the array containing the sorted list of values without duplicates.
Must Do and Tips
Must Do
- You must use an array, not an array list.
Must Not Do
- Do not include path names for the file. Use only the name assignment1.txt
- File fileName = new File("assignment1.txt");
- To create the file of sorted values, DO NOT move the values in the two arrays into another array, sort, then write to the file. That process requires little array manipulation.
- Instead, you must write code that walks through the arrays and writes one value at a time to the file that is manually combine the array values into the file.
- This process requires you to perform array manipulation.
- See the tips below for one way to write this code.
Tips
- The assignment1.txt file will be overwritten each time your code is run.
- Merging the two arrays manually into the file requires two steps:
- Step 1: write a while loop to take values from the arrays in sorted order and write them to the file.
- This while loop completes when all values in one of the arrays have been written to the file.
- Since the array sizes are randomly created, in most cases the arrays will be different sizes. This means, in most cases, one array will have all it values written to the file while the other array still contains values that need to be written to the file.
- Step 2: after the above while loop completes, determine which array still contains values and write those remaining values to the file.
while (more values in Array1 to write to file) {
// Write remaining values in array1 to file
}
while (more values in Array2 to write to file) {
// Write remaining values in array2 to file
}
Output
Your output should look like the following except with different random numbers.
Output Example
size1 = 4
size2 = 3
First array with 4 sorted random values
---------------------------------------
7
8
10
24
Second array with 3 sorted random values
---------------------------------------
12
12
25
Writing values to the file
--------------------------
Writing: 7
Writing: 8
Writing: 10
Writing: 12
Writing: 12
Writing: 24
Writing: 25
Array with no duplicate values
------------------------------
7
8
10
12
24
25
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