Question: Programing Java package hw1; // do not change this package name import java.io.*; public class ThorHelper { int n; int[] ellOne, ellTwo; // Please do

Programing Java

Programing Java package hw1; // do not change this package name import

java.io.*; public class ThorHelper { int n; int[] ellOne, ellTwo; // Please

do not touch this method! It is reading the input from a

package hw1; // do not change this package name

import java.io.*;

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 IOException { FileReader input; try{ input = new FileReader(inputFileName); } catch(FileNotFoundException e) { System.out.println("Oops! the input file cannot be found!"); return; } BufferedReader reader = new BufferedReader(input); n = Integer.parseInt(reader.readLine()); ellOne = new int[n]; ellTwo = new int[n]; String line = reader.readLine(); String[] integers = line.split(" "); for(int pos = 0; pos

}

package hw1;

import java.io.*; import java.util.Arrays;

public class TestThorHelper { // Please do not touch this method! It is comparing the contents of two files. public static boolean areIndentical(String fileA, String fileB) throws IOException { FileReader inputA, inputB; try{ inputA = new FileReader(fileA); inputB = new FileReader(fileB); } catch(IOException e) { System.out.println("At least one of the 2 input files cannot be found!"); return false; } BufferedReader readerA = new BufferedReader(inputA); String[] A = (readerA.readLine()).split(" "); inputA.close();

BufferedReader readerB = new BufferedReader(inputB); String[] B = (readerB.readLine()).split(" "); inputB.close(); return (Arrays.equals(A, B) ) ? true : false; }

public static void main(String[] args) throws IOException{ // I may use several test inputs while grading. // So make sure that your code is thoroughly tested. // If you are using Eclipse, place the input .txt files in the directory // on your computer where you see the src and bin folders for this project. // Do this using your computer's file manager. // If the extension names are hidden on your system, it is a good idea to unhide it. // The output file will be created at the same place ThorHelper T1 = new ThorHelper("HW1-SampleTest1.txt"); T1.writeSolutionTo("output1.txt"); if( areIndentical( "output1.txt", "HW1-SampleOutput1.txt" ) ) System.out.println("Yay! Your code passed Test 1."); else System.out.println("Ooops! Your code did not pass Test 1. Please try again.");

ThorHelper T2 = new ThorHelper("HW1-SampleTest2.txt"); T2.writeSolutionTo("output2.txt"); if( areIndentical( "output2.txt", "HW1-SampleOutput2.txt" ) ) System.out.println("Yay! Your code passed Test 2."); else System.out.println("Ooops! Your code did not pass Test 2. Please try again.");

ThorHelper T3 = new ThorHelper("HW1-SampleTest3.txt"); T3.writeSolutionTo("output3.txt"); if( areIndentical( "output3.txt", "HW1-SampleOutput3.txt" ) ) System.out.println("Yay! Your code passed Test 3."); else System.out.println("Ooops! Your code did not pass Test 3. Please try again."); } }

sampletest1

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

sample output

1 2 3 4 5 6 7 8 9 10

The mighty Thanos was finally defeated by the Avengers. The Antman, now feeling pretty bored, decided to make fun of Thor, who still doubts if he is still worthy! The Antman has thus decided to give the following number-ordering problem to Thor since the Antman thinks that Thor still cannot keep everything in correct order. In the following, a list means just a sequence of positive integers. The input to this problem consists of two non-empty lists L1,L2 (not necessarily sorted), each containing n positive integers; assume that n > 1. Every integer in L has a friend integer in L2. The friendship is pretty simple. Assume that L, Lare sorted. Then for every 1 sisn, the ith item in the sorted L, and the ith item in the sorted L2 are friends. 1 Here is an example for you. Let L1 = (48, 10,5,266,97) and L2 = (100,2,5,46,20). After sorting them separately we obtain, L = (5,10,48,97,266) and L2 = (2,5,20,46, 100). The friends can now be found using a simple 1-1 correspondence between these two sorted lists: (5,2), (10,5), (48,20), (97,46), (266, 100). The objective is to keep the two given lists in correct order maintaining the friendship relationship, but in the input Antman has probably shuffled the list Lj. Thor must restore L2 in the same order as the first list, according to the friendship relationship, as described above. For example, given the two above lists, L2 must be reordered as (20,5,2,100, 46) to be in order with the list L. Write a program in Java to help out Thor. The input is taken from a text file. This file contains three lines. The first line contains n, the common length of the two lists. The second line contains the integers from Ly, separated by single spaces. Similarly, the third line contains the integers from L2. No list contains duplicates. The output should be sent to a text file. This file should contain a line containing all the integers from L2, arranged with respect to Lj. For more details, refer to the code outlines provided with this homework. For this assignment, you are not required to know how to read and/or write from files in Java. This is already done for you in the partial code given to you. Just knowing the format of the file, as described above, should be sufficient to solve this assignment. Your only task is to complete the method named generate Solution defined inside the class ThorHelper. You cannot use any data structure other than arrays. For sorting (if you need you may use Arrays.sort (if knowVw ro 12 of 3 wise@u may e any Osie sorting algorithm introduced in your previous programming courses such as Bubble Sort/Selection Sort/Insertion Sort. You cannot use any data structure other than arrays. For sorting (if you need) you may use Arrays.sort() if you know how to use it otherwise you may use any basic sorting algorithm introduced in your previous programming courses such as Bubble Sort/Selection Sort/Insertion Sort. 2 Sample tests 1. Input (given in a .txt file): 1 1 2 N Output (written to a .txt file): 2 2 2. Input (given in a .txt file): 5 48 10 5 266 97 100 2 5 46 20 Output (written to a .txt file): 205 2 100 46 3. Input (given in a .txt file): 10 12345678910 10987654321 Output (written to a .txt file): 1 2 3 4 5 6 7 8 9 10 Please think about more test cases, possibly with more integers and use them to find bugs in your code. A good programmer must also be good tester in this case. Your code may be graded with a different set of test cases. 3 What to deliver? Upload just the file ThorHelper.java to Canvas. Feel free to define additional helper methods wherever needed. No need to upload anything else. Commenting is highly encouraged. Please test thoroughly before submission. You will be given zero if your program does not compile or gives unnecessary warnings

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!