Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this assignment, there are 3 tasks. See output section below for each task s expected output. Task 1 : Creating and placing random values

For this assignment, there are 3 tasks. See output section below for each tasks expected output.
Task 1: Creating and placing random values into 2 arrays:
a. Randomly generate two random numbers size1 and size2- between 1 and 10, inclusive.
i. The values size1 and size2 will be used in c and d as the size of each array.
b. Display the random values in size1 and size2
c. Generate size1 random integers between 1 & 25(inclusive) and write them to the 1st array
d. Generate size2 random integers between 1 & 25(inclusive) and write them to the 2nd array
e. Sort each array using the Arrays.sort() method.
f. Display the values in each array in sorted order.
Task 2: Perform the following file and array-based tasks:
a. In your code create a file named assignment1.txt.
i. Correct: File fileName = new File("assignment1.txt");
i. Correct because the file will be created in your current working directory.
ii. Incorrect: File fileName = new File("C:/Dev/assignment1.txt");
i. Incorrect because it includes the path name when creating the file.
b. Open the file for writing.
i. PrintWriter resultsFile = new PrintWriter (fileName);
c. Write the values in the two arrays to the file in sorted order.
i. To create the file of sorted values, the sort MUST be done manually.
i. DO NOT move the values into a 3rd array, sort, then write to the file.
ii. See Must Do and Tips below for proper approach.
ii. Write one value on each line in the file.
iii. Display each value as you write it to the file.
iv. See the output below where it shows Writing values to file for an example.
d. Close the file.
i. Make sure assignment1.txt appears on your hard drive in the workspace directory.
ii. See the Must Do and Tips section for help with finding the file on your hard drive.
Task 3: Read the values in the file into a new array while removing any duplicates values:
a. Reopen the file for reading.
i. Scanner readFile = new Scanner (fileName);
b. Create a new array called noDuplicates.
c. Read each value in the file & if it is not a duplicate place it into the noDuplicates array.
d. Display the array containing the sorted list of values without duplicates.
i. Note, when removing duplicates from the file values, the array noDuplicates will not
be filled at the end so there will be zeros at the end.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

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

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions