Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

need help with writing the driver class as above of methods.java //Java code import java.util.*; public class Methods { // Second largest method public static

need help with writing the driver class as above of methods.java

//Java code

import java.util.*; public class Methods { // Second largest method public static int secondLargest(int[] arr) { int largest = arr[0], secondLargest = arr[1]; if (largest < secondLargest) { int temp = largest; largest = secondLargest; secondLargest = temp; } for (int i = 2; i < arr.length; i++) { if (arr[i] > largest) { secondLargest = largest; largest = arr[i]; } else if (arr[i] > secondLargest) { secondLargest = arr[i]; } } return secondLargest; } // A method that prints array elements 10 elements per line. public static void printArray(int[] arr) { for(int i = 0; i < arr.length; i++) { System.out.print(" " + arr[i]); if((i+1) % 10 == 0) System.out.print(" "); } } //A mergesort method that mergesorts array elements in descending order. public static int[] merge_sort(int[] array) { if (array.length <= 1) { return array; } int midPoint = array.length / 2; int[] left = new int[midPoint]; int[] right = new int[array.length - midPoint]; int result[] = new int[array.length]; for (int i = 0; i < midPoint; i++) { left[i] = array[i]; } int x = 0; for (int j = midPoint; j < array.length; j++) { if (x < right.length) { right[x] = array[j]; x++; } } left = merge_sort(left); right = merge_sort(right); result = merge(left, right); return result; } public static int[] merge(int[] left, int[] right) { int lengthResult = left.length + right.length; int indexL = 0; int indexR = 0; int resultIndex = 0; int[] result = new int[lengthResult]; while (indexL < left.length || indexR < right.length) { if (indexL < left.length && indexR < right.length) { if (left[indexL] >= right[indexR]) { result[resultIndex] = left[indexL]; indexL++; resultIndex++; } else { result[resultIndex] = right[indexR]; indexR++; resultIndex++; } } else if (indexL < left.length) { result[resultIndex] = left[indexL]; indexL++; resultIndex++; } else if (indexR < right.length) { result[resultIndex] = right[indexR]; indexR++; resultIndex++; } } return result; } // Method for search public static int binarySearch(int arr[], int x) { int l = 0, r = arr.length - 1; while (l <= r) { int m = l + (r - l) / 2; if (arr[m] == x) return m; else if (arr[m] < x) l = m + 1; else r = m - 1; } return -1; } }

//===========================

Your main method should be as follow:

public static void main(String args[]) throws exception{

prt(" Program to: find 2nd largest, mergesort and binary search);

prt( Prepared by Last: , First: and Student ID: .");

prt(" Date: ");

try{

Scanner inf = new Scanner (System.in);

n = inf.nextInt(); // read no. of input

mp1 p1 = new mp1 (n);

read n integers and store them in array arr and print arr(formatted) 10 elements per line. (40 pts)

find 2nd largest and print it. (40 pts)

mergesort array arr and print arr(formatted) 10 elements per line. (80 pts)

k = inf.nextInt(); // read no. of elements to search

read k keys to search using binary search and print their position in array arr. (40 pts)

}catch (Exception e) {prt(" Exception " + e + " ");}

}// end main method

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part 3 Lnai 8726

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448440, 978-3662448441

More Books

Students also viewed these Databases questions

Question

What is the role of the Joint Commission in health care?

Answered: 1 week ago