Question: JAVA CODING - Finish the find() method. Reading comments throughout the rest of the code will be helpful public class findit { private static int
JAVA CODING - Finish the find() method. Reading comments throughout the rest of the code will be helpful
public class findit { private static int coin[]; // NO modifications to scale() method // when we put coins with IDs ranging from firstID to lastID , // scale() method returns the weight of those coins // ex: scale(0, 7) returns the weight of eight coins (ID 0 to ID 7) private static int scale(int firstID, int lastID) { int sum = 0; for(int i = firstID; i <= lastID; ++i) sum += coin[i]; return sum; }
// DO: the method find(). DEFINING OTHER METHODS IS NOT ALLOWED // ** Requirements: // A. find() method MUST call itself in this method // B. find() method MUST call scale() method // ** Description: // find() method is used to find the defected coin (lesser weight) // YOU HAVE TO use divide & conquer technique to make the original problem smaller // and gradually decrease the range of coins that needs to bd analyzed // ** Return value: // it retures the ID of the defected coin public static int find(int firstID, int lastID) { }
// DONT modify the main() method public static void main(String[] args) { // all coins weigh the same coin = new int[128]; for(int i = 0; i < coin.length; ++i) coin[i] = 1; // except the defect coin, which is lighter than others int defectCoin = (int)(Math.random() * 128); coin[defectCoin] = 0; System.out.println("defect coin ID is " + defectCoin);
// use the method find() to find the defected coin using divide&conquer int foundit = find(0, coin.length - 1); System.out.println("the coin I found is ID " + foundit);
System.out.println(); if(defectCoin == foundit) System.out.println("Good"); else System.out.println("needs debugging"); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
