Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

in java, i posted this question before but the person that answered tried all this fancy stuff and it wouldnt compile and i could not

in java, i posted this question before but the person that answered tried all this fancy stuff and it wouldnt compile and i could not figure out the errors. image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
II 5. For this assignment, all code can be in main()- that is - methods and classes are not required. See the output section below for each task's 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, 19, 2, 16, 8, 15, 9, 9, 17, 14, 18, 1, 19, 18, 2, 1); b. Display the values in the array c. Sort the array using the sort method in the Arrays class Task 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 assignmenti.txt i. Correct: File fileName = new File("assignment1.txt"); 1. The file will be created in your current working directory ii. Incorrect: File fileName = new File("C:/dev/assignment2.txt"); i. Incorrect because it includes the path name when creating the file. b. Open the file for writing 1. Printwriter outputFile = new Printwriter (fileName); c. Write the values in the array to the file 1. 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 file d. Close the file 1. Make sure assignment1.txt appears on your hard drive in the workspace directory ii. See Must Do and Tips section for tip to help find file. Task 3: Create the even and odd arrays with no duplicates a. Reopen the file for reading i. Scanner readFile = new Scanner (fiteame); 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 1. 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). i. Remember the values in the file were written in sorted order. 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. 1. At this point, the array size will be too big and zeros will appear at the end Even array with no duplicates and extra zeros even (0) - 2 even[1] = 8 Array is too big at this point. even [2] = 10 The array size was based on the even[3] = 14 the number of even values but that even (4) = 16 number included the duplicates. even (5) = 18 Since duplicates are removed when even[6] = 0 values are read from the file, the even (7) = 0 array could be too big if there are even (8) - 8 duplicate values. e. Move the even and odd arrays into properly sized arrays f. Display the properly sized arrays. Must Do and Tips Must Do . You must use arrays, 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("assignmenti.txt"); When creating the non-duplicate arrays, you must use only the one even or odd array, that is: DO NOT move the values from the file into an array then use a temporary array to locate and remove duplicates. o Instead, I want you perform array manipulation to strength your array processing skills, You 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 system To help find the file that is created on your hard drive, add the following code arter you create the printWriter 11:50 Tips: Finding is the file on your system To 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: Exceptions You 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 IOException public static void main(String[] args) throws IOException {...} Output Your output should look like the following: Output - Example Array Values array(0) = 1 array[1=18 array[2] = 10 These are the original values on assignment sheet array(3) = 2 array[4] = 16 array(5) = 8 array[6] = 15 array(71 - 9 array[8] = 9 array[9] = 17 array(10) = 14 array(11) = 18 array(12) = 1 array(13) = 19 array(14) = 18 array(15) = 2 array(16) = 1 Where the file is located on my Values written to file: 17 hard drive, see Tips for code Even values written to file: 9 Odd values written to file: 8 File is in directory: C:\Dev eclipse_workspace\C51458\assignment2.txt Even array with no duplicates and extra zeros hard Values written to file: 17 drive, see Tips for code Even values written to file: 9 Odd values written to file: 8 File is in directory: C:\Devleclipse_workspace\C51456\assignment2.txt Even array with no duplicates and extra zeros even (0) = 2 even(i) = 8 Array is too big at this point. even (2) = 10 The array size was based on the even (3) = 14 the number of even values but that even (4) = 16 number included the duplicates. even (5) = 18 Since duplicates are removed when even (6) = 0 values are read from the file, the even (7) = 0 array could be too big if there are even [8] - duplicate values. Odd array with no duplicates and extra zeros odd(e) = 1 odd [1] = 9 odd(21 = 15 odd [3] = 17 odd [4] = 19 odd [5] = 0 odd[6] = odd [7] = 0 Even array with no duplicates even (0) - 2 even (1) = 8 Array is now correct size even (2) - 10 even[3] = 14 even (4) = 16 even (5) = 18 Odd array with no duplicates odd[0] = 1 odd [1] = 9 odd [2] = 15 odd(31 = 17 odd [4) - 19

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

Recommended Textbook for

More Books

Students also viewed these Databases questions