Answered step by step
Verified Expert Solution
Link Copied!

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

image text in transcribed

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:

image text in transcribed

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 Comparable { protected int value; protected NutBolt(int v) { this.value = v; } public int compareTo(NutBolt o) { throw new ClassCastException(); } } NutBoltMatch.java public class NutBoltMatch { public static int[] match(Nut[] a, Bolt[] b) { return new int[0]; } }

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

Repairing And Querying Databases Under Aggregate Constraints

Authors: Sergio Flesca ,Filippo Furfaro ,Francesco Parisi

2011th Edition

146141640X, 978-1461416401

More Books

Students also viewed these Databases questions