Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA help needed. Please help implement 3 remaining bolded //TODO methods with an explanation. ---------------------------------------------------------------------------- import java.io.*; import java.math.BigInteger; import java.util.ArrayList; import java.util.Comparator; import java.util.List;

JAVA help needed. Please help implement 3 remaining bolded //TODO methods with an explanation. ---------------------------------------------------------------------------- import java.io.*; import java.math.BigInteger; import java.util.ArrayList; import java.util.Comparator; import java.util.List; /** * This class represents a program that that determines the * largest number possible from the contents of a given integer array. * @author */ public class LargestNumberSolver { /** * This generic method sorts the input array using an insertion sort and the input Comparator object. * * @param  - Generic type * @param arr - this array. * @param cmp - what is being compared. */ public static  void insertionSort(T[] arr, Comparator cmp) { for (var i = 1; i < arr.length; i++) { T val = arr[i]; int j; for (j = i-1; j >=0 && cmp.compare(arr[j], val) > 0; j--) { arr[j+1] = arr[j]; } arr[j+1] = val; } } /** * This method returns the largest number that can be formed by arranging the * integers of the given array, in any order. If the array is empty, the largest * number that can be formed is 0. * * This method must not alter the given array and must call your insertionSort method * with a Comparator or lambda expression that you design. * * @param arr - this array. * @return largeNumber - new BigInteger toString. */ public static BigInteger findLargestNumber(Integer[] arr) { StringBuilder bigNumber = new StringBuilder(); if (arr != null) { insertionSort(arr, new largestNumber()); for (var i = arr.length - 1; i >= 0; i--) bigNumber.append(arr[i]); } else { bigNumber.append(0); } return new BigInteger(bigNumber.toString()); } protected static class largestNumber implements Comparator { public int compare(Integer o1, Integer o2) { String xy = o1.toString() + o2.toString(); String yx = o2.toString() + o1; return xy.compareTo(yx); } } /** * This method returns the largest int value that can be formed by arranging * the integers of the given array, in any order. * * Logic for solving the problem of determining the largest number should not * appear again in this method  call an existing public method or a helper method. * * This method must not alter the given array. * * @param arr - this array * @return int - the largestInt within the array. * @throws OutOfRangeException - if the largest number that can be formed is too large for the int data type. */ public static int findLargestInt(Integer[] arr) throws OutOfRangeException { BigInteger largestNumber = findLargestNumber(arr); if (largestNumber.bitLength() > 32) { throw new OutOfRangeException("The largest number that can be formed is too large for the int data type."); } else return largestNumber.intValue(); } /** * This method behaves the same as the previous method, but for data type long instead of data type int. * * @param arr - this array * @return long - same, but returns type long. * @throws OutOfRangeException - will throw OutOfRangeException if the number * is too large for data type long. */ public static long findLargestLong(Integer[] arr) throws OutOfRangeException { BigInteger largestNumber = findLargestNumber(arr); if (largestNumber.bitLength() > 64) { throw new OutOfRangeException("The largest number that can be formed is too large for the long data type."); } else return largestNumber.longValue(); } /** * This method sums the largest numbers that can be formed by each array in the given list. * * This method must not alter the given list. * * @param list - the list of Integer arrays. * @return sum - returns the sum of the largest numbers formed in each array. And is stored * in type BigInteger. */ public static BigInteger sum(List list) { //TODO IMPLEMENT } /** * This method determines the kth largest number that can be formed by each array in the given list. * * E.g., if k=0 returns the largest overall, if k=list.size()-1 returns the smallest overall. * * This method returns the original array that represents the kth largest number, not the kth largest number itself. * * This method must not alter the given list and must call your insertionSort method with a Comparator * or lambda expression that you design. * * @param list - list of each array. * @param k - representing all the largest numbers from each array in the list * sorted in descending order. * @return Integer [] - returns an Integer array of the largest numbers in the list. * @throws IllegalArgumentException - thrown if k is not a valid position in the list. */ public static Integer[] findKthLargest(List list, int k) throws IllegalArgumentException { //TODO IMPLEMENT } /** * This method generates list of integer arrays from an input file, * such that each line corresponds to one array of integers separated by blank spaces, * and returns an empty list if the file does not exist. * * @param filename * @return */ public static List readFile(String filename) { //TODO IMPLEMENT }
 public class OutOfRangeException extends RuntimeException { public OutOfRangeException(String dataTypeName) { super("The value is too large for the " + dataTypeName + " data type."); } } 

}

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

Data Management Databases And Organizations

Authors: Richard T. Watson

3rd Edition

0471418455, 978-0471418450

More Books

Students also viewed these Databases questions