Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

  1. 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:

  1. Generate and display two random numbers size1 and size2 - between 1 and 10.
    1. The values size1 and size2 will be used in step 2 as the size of each array.
  2. Generate size1 random integers between 1 and 25 and write them to the 1st array
  3. Generate size2 random integers between 1 and 25 and write them to the 2nd array
  4. Sort each array using the sort method in the Arrays class
  5. After each array is sorted, display the values in each array.

Task 2: perform the following file and array-based tasks:

  1. In your code create a file named assignment1.txt.
    1. Correct: File fileName = new File("assignment1.txt");
      1. Do not include any path names when creating the file.
      2. The file will be placed in your current working directory.
    2. Incorrect: File fileName = new File("C:/Dev/assignment1.txt");
  2. Open the file for writing.
    1. PrintWriter resultsFile = new PrintWriter (fileName);
  3. Write the values in the two arrays in sorted order to the file.
    1. See the output below where it shows Writing values to file for an example.
    2. Display each value as you write it to the file
  4. Close the file.

Task 3: perform the following file and array-based tasks:

  1. Reopen the file for reading.
    1. Scanner readFile = new Scanner (fileName);
  2. Read values from file and place into an array, removing duplicates as you read the values.
  3. 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Structured Search For Big Data From Keywords To Key-objects

Authors: Mikhail Gilula

1st Edition

012804652X, 9780128046524

More Books

Students also viewed these Databases questions

Question

What is operatiing system?

Answered: 1 week ago

Question

=+j Explain the relationship between unions and MNEs.

Answered: 1 week ago