Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Input is the array a of Nut and b of Bolt. Output is the integer array ind , svo a[i] and b[ind[i]] fit together. Use
Input is the array a of Nut and b of Bolt. Output is the integer array ind, svo a[i] and b[ind[i]] fit together. Use the following code bits to create a method:
2.3.15 Nuts and bolts. (G. J. E. Rawlins) You have a mixed pile of N nuts and N bolts and need to quickly find the corresponding pairs of nuts and bolts. Each nut matches exactly one bolt, and each bolt matches exactly one nut. By fitting a nut and bolt to- gether, you can see which is bigger, but it is not possible to directly compare two nuts or two bolts. Give an efficient method for solving the problem. Bolt.java public class Bolt extends NutBolt { public Bolt(int v) { super(v); } public int compareTo(NutBolt o) { try { Nut n = (Nut) o; return value - n.value; } catch (Exception e) { throw new ClassCastException(); } } } Nut.java public class Nut extends NutBolt { public Nut(int v) { super(v); } public int compareTo(NutBolt o) { try { Bolt b (Bolt) o; return b.value - value; } catch (Exception e) { throw new ClassCastException(); } } } NutBolt.java public class NutBolt implements ComparableStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started