Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help! Sorting arrays and writing to file need Java code For this assignment, all code can be in main() that is methods and classes

Please help! Sorting arrays and writing to file need Java code

For this assignment, all code can be in main() that is methods and classes are not required. See the output section below for each tasks expected output.Task 1: Create an array and fill it with values:a.Use the following statement to create the array with these values:int[] array = {1, 18, 10, 2, 16, 8, 15, 9, 9, 17, 14, 18, 1, 19, 18, 2, 1};b. Display the values in the arrayc.Sort the array using the sort method in the Arrays classTask 2: Write values in the array to a file counting the number of even and odd values:a.In your code create a file named assignment1.txt. i.Correct: File fileName = new File("assignment1.txt");i. 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 thepath name when creating the file. b. Open the file for writing.i.PrintWriter outputFile = new PrintWriter (fileName);c.Write the values in the array to the filei.Count the number of even and odd values when writing values to the file.ii.Display the total numbers of values, even values, and odd values written to filed. Close the file.i.Make sure assignment1.txt appears on your hard drive in the workspace directoryii.See Must Do and Tips section for tip to help find file.Task 3: Create the even and odd arrays with no duplicatesa.Reopen the file for reading.i.Scanner readFile = new Scanner (fileName);b. Setup two new arrays based on number of even and odd values.c.Read value from file and place into even/odd array, removing duplicates when value is read i.After reading a value from the file, you must manually determine (this means write the code) if the value is already in the even or odd array (a duplicate). ii.Remember the values in the file were written in sorted order.d. Display the even and odd arrays containing the values without duplicates.i.At this point, the array size will be too big and zeros will appear at the end.

Must DoYou must use arrays, not an array list.Must Not DoDo not include path names for the file. Use only the name assignment1.txtoFile fileName = new File("assignment1.txt");When creating the non-duplicate arrays, you must use only the one even or odd array, that is:oDO NOT move the values from the file into an array then use a temporary array to locate and remove duplicates.oInstead, I want you perform array manipulation to strength your array processing skills. oYou must write code that walks through the array and determines if the file value is already in the array that is manually create the non-duplicate array. Tips: Finding is the file on your systemTo help find the file that is created on your hard drive, add the following code after you create the PrintWriter:System.out.println("File is in directory: " + fileName.getAbsolutePath());Tips: ExceptionsYou may have learned about exceptions and try/catch blocks in your Java class.Exceptions are important, but they add complexity to code. To eliminate the need for added complexity and the use of try/catch blocks, have main throw an IOExceptionpublicstaticvoid main(String[] args) throws IOException { ... }

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

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