Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone help me complete the generateSolution method where the input is put into ascending order? Test 2 5 48 10 5 266 97 100

Can someone help me complete the generateSolution method where the input is put into ascending order?

Test 2 5 48 10 5 266 97 100 2 5 46 20

Test 1

1 1 2

Test 3

10 1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2 1

Output for test 2

20 5 2 100 46

output for test 3

1 2 3 4 5 6 7 8 9 10

package hw1; // do not change this package name import java.io.*; import java.util.*; public class TestThorHelper { // Please do not touch this method! It is comparing the contents of two files. public static boolean areIdentical(String fileA, String fileB) throws IOException { File inputA = new File(fileA), inputB = new File(fileB); if( !inputA.exists() || !inputB.exists() ) { System.out.println("AO or EO or both cannot be found!"); return false; } Scanner readerA = new Scanner(inputA), readerB = new Scanner(inputB); String[] A = (readerA.nextLine()).split(" "); readerA.close(); String[] B = (readerB.nextLine()).split(" "); readerB.close(); return Arrays.equals(A, B); } // Please do not touch this method! public static boolean testSubmission( String input, String EOfile, String AOfile ) throws IOException{ ThorHelper T = new ThorHelper(input); T.writeSolutionTo(AOfile); return areIdentical(AOfile, EOfile); } public static void main(String[] args) throws IOException{ // Warning! other tests may also be used for grading your submission. // So, please make sure that your code is thoroughly tested before you submit. // EO stands for Expected Output // AO stands for Actual Output // A test passes if EO equals AO //===============================================================// if( testSubmission( "src/hw1/Inputs/HW1-test1.txt", "src/hw1/Outputs/HW1-test1-EO.txt", "src/hw1/Outputs/HW1-test1-AO.txt") ) System.out.println("Test passed."); else System.out.println("Test failed."); //===============================================================// if( testSubmission( "src/hw1/Inputs/HW1-test2.txt", "src/hw1/Outputs/HW1-test2-EO.txt", "src/hw1/Outputs/HW1-test2-AO.txt") ) System.out.println("Test passed."); else System.out.println("Test failed."); //===============================================================// if( testSubmission( "src/hw1/Inputs/HW1-test3.txt", "src/hw1/Outputs/HW1-test3-EO.txt", "src/hw1/Outputs/HW1-test3-AO.txt") ) System.out.println("Test passed."); else System.out.println("Test failed."); //===============================================================// } }

package hw1; // do not change this package name /* Full name (as appears in your Canvas): FIRSTNAME LASTNAME * Your N#: nXXXXXXXX */ import java.io.*; import java.util.*; public class ThorHelper { int n; int[] ellOne, ellTwo; // Please do not touch this method! It is reading the input from a file. public ThorHelper(String inputFileName) throws FileNotFoundException { File file = new File(inputFileName); if( !file.exists() ) throw new FileNotFoundException("Fatal error. File cannot be found!"); Scanner fileInput = new Scanner(file); n = Integer.parseInt(fileInput.nextLine()); ellOne = new int[n]; ellTwo = new int[n]; String line = fileInput.nextLine(); String[] integers = line.split(" "); for(int pos = 0; pos < integers.length; pos++) ellOne[pos] = Integer.parseInt(integers[pos]); line = fileInput.nextLine(); integers = line.split(" "); for(int pos = 0; pos < integers.length; pos++) ellTwo[pos] = Integer.parseInt(integers[pos]); fileInput.close(); } // Please do not touch this method! It is sending the output to a file. public void writeSolutionTo(String outputFile) throws IOException { int[] outputList = new int[n]; generateSolution(outputList); FileWriter output = new FileWriter(outputFile); for(int pos = 0; pos < n; pos++) output.write(outputList[pos] + " "); output.close(); } /** * Your only task is to complete this method. Please do not touch anything else! * Send the output to the array outputList. The two input lists are ellOne and ellTwo. * The outputList is already allocated from the method writeSolutionTo. */ private void generateSolution(int[] outputList) { int[] ellOneSorted = new int[n], ellTwoSorted = new int[n]; for(int i = 0; i < n; i++) { ellOneSorted[i] = ellOne[i]; ellTwoSorted[i] = ellTwo[i]; } // Complete the remaining part // For sorting arrays, use Arrays.sort() from java.util.* } }

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

Practical Azure SQL Database For Modern Developers Building Applications In The Microsoft Cloud

Authors: Davide Mauri, Silvano Coriani, Anna Hoffma, Sanjay Mishra, Jovan Popovic

1st Edition

1484263693, 978-1484263693

More Books

Students also viewed these Databases questions