Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. JAVA Bob has a set, A, of n nuts and a set B of n bolts, such that each nut h

ID: 3862587 • Letter: 1

Question

1. JAVA

Bob has a set, A, of n nuts and a set B of n bolts, such that each nut has a unique matching bolt. Unfortunately, the nuts in A all look the same, and the bolts in B all look the same as well. The only comparison that Bob can make is to take a nut-bolt pair (a,b), such that a A and b B, and test if the threads of a are larger, smaller, or a perfect match with the threads of b.But the carpenter cannot compare two nuts or two bolts directly. Here are the requirements

1. Your algorithm must be no worse than O(nlogn). Implement this as a function sortNutsNBolts.

2. Create a test program NutsNBolts.java that takes two commandline arguments for filenames.

for ex NutsNBolts nuts.txt bolts.txt

The files nuts.txt and bolts.txt each contain n integers for the nuts and bolts respectively. Your program should read these files and call the function.

Explanation / Answer

Following program reads filenames from command line and sort nuts and bolts using sortNutsNBolts method

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

public class NutsNBolts {

   public static void main(String[] args) throws FileNotFoundException

   {

       BufferedReader readerNuts = new BufferedReader(new FileReader(new File(args[0])));

       BufferedReader readerBolts = new BufferedReader(new FileReader(new File(args[1])));

      

       List<Integer> nuts = new ArrayList<Integer>();

       List<Integer> bolts = new ArrayList<Integer>();

      

       sortNutsNBolts(nuts,bolts);

      

       System.out.print("Nuts: ");

       for(Integer n:nuts)

       {

           System.out.print(n+",");

       }

      

       System.out.println();

       System.out.print("Bolts: ");

       for(Integer n:bolts)

       {

           System.out.print(n+",");

       }

       System.out.println();

   }

  

   public static void sortNutsNBolts(List<Integer> nuts,List<Integer> bolts)

   {

       Collections.sort(nuts);

       Collections.sort(bolts);

      

   }

  

}