Answered step by step
Verified Expert Solution
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 tasks. See output section below for each tasks expected output.
Task : Creating and placing random values into arrays:
a Randomly generate two random numbers size and size between and inclusive.
i The values size and size will be used in c and d as the size of each array.
b Display the random values in size and size
c Generate size random integers between & inclusive and write them to the st array
d Generate size random integers between & inclusive and write them to the nd array
e Sort each array using the Arrays.sort method.
f Display the values in each array in sorted order.
Task : Perform the following file and arraybased tasks:
a In your code create a file named assignmenttxt
i Correct: File fileName new Fileassignmenttxt;
i Correct because the file will be created in your current working directory.
ii Incorrect: File fileName new FileC:Devassignmenttxt;
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 rd 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 assignmenttxt 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 : 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
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