Question

Test 11 Spring 2018 NYIT Programming CSCI-125-MO3 Prof. F. Fischman Name: 1. Consider the method do Task defined below. publi

0 0
Add a comment Improve this question Transcribed image text
Answer #1

✔Solution (1)

Output is -> 2

Reason:-.>This function searches the most frequent number in a sequence like in the following list of numbers.

the most frequent number found in the list of numbers is 2 finds seven times which is the most frequent number in a sequence.

public class Main {
public static void main(String[] args)
{
int[] arr ={1,2,3,3,3,3,4,2,2,2,2,2,2,2,2,5,6,6,6,6,6,6,4,7,8};
System.out.println(doTask(arr));
}

/**
* This function search the
* most frequent number
* like in
* the following list of numbers
* @param a
* @return most frequent number found in the
* list of numbers
* like 2 finds seven times which is most
* frequent number
*/
public static int doTask(int [] a)
{
int index=0;
int soFar=1;
int count=1;
for (int k = 1; k <a.length; k++) {
/**
* If number is equal
* to previous number
*/
if(a[k]==a[k-1])
{
//update counter
count++;
/**
* if count is greater than
* soFar
*/
if(count>soFar)
{
//soFar equals to count
//assign how many duplicates find in
// a sequence
soFar = count;
//Update index to
//index of most frequent
// value
index = k;
}
}
/**
* If number is not equal
* to previous number
* initialize count to 1
*/
else
{
count=1;
}
}
return a[index];
}
}

✔Solution (2)->

//Simple Print

public class Main1 {
public static void main(String[] args)
{
int[] A = {1,2,3,4,5,6,7,8};
int [] B ={4,5,6,7,8};
//call function
printContents(A);
printContents(B);
}
public static void printContents(int[] arr)
{

for (int i = 0; i <arr.length ; i++) {
System.out.print(arr[i]+", ");

}

System.out.print("\n");
}
}

//Output

| 1, 2, 3,4,5,6,7,8, 4,5,6,7,8,

//Java Code to Print in Brackets

public class Main1 {
public static void main(String[] args)
{
int[] A = {1,2,3,4,5,6,7,8};
int [] B ={4,5,6,7,8};
//call function
printContents(A);
printContents(B);
}
public static void printContents(int[] arr)
{
System.out.print("[");
for (int i = 0; i <arr.length ; i++) {
System.out.print(arr[i]);
if((i+1)<arr.length)
System.out.print(", ");
}
System.out.print("]\n");

}
}

//Output

C:\Program Files\Java\jdk1.8.0_221\bin [1, 2, 3, 4, 5, 6, 7, 8] [4, 5, 6, 7, 8]

//If you need any help regarding this solution......... please leave a comment...... thanks

Add a comment
Know the answer?
Add Answer to:
Test 11 Spring 2018 NYIT Programming CSCI-125-MO3 Prof. F. Fischman Name: 1. Consider the method do...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the following code segment. ArrayList<String> towns = new ArrayList<String> (); towns.add("Baltimore"); towns.add("Bethesda"); towns.add("Wheaton"); towns.add("Greenbelt"); towns.add("Rockville");...

    Consider the following code segment. ArrayList<String> towns = new ArrayList<String> (); towns.add("Baltimore"); towns.add("Bethesda"); towns.add("Wheaton"); towns.add("Greenbelt"); towns.add("Rockville"); for (int k = 1; k< towns.size(); k ++) System.out.print(towns.get(k) + " "); What is printed as a result of executing the code segment? Baltimore Bethesda Wheaton Greenbelt IndexOutOfBoundsException message Baltimore Bethesda Wheaton Greenbelt Rockville Bethesda Wheaton Greenbelt Rockville Refer to the doSomething method: public static void doSomething (ArrayList list, int i, intj) SomeType temp = list.get(i); list.set(i, list.get()); list.set(j, temp); Which best describes...

  • Consider the following method. public static ArrayList<Integer mystery(int n) ArrayList<Integer seg - new ArrayList<IntegerO; for (int...

    Consider the following method. public static ArrayList<Integer mystery(int n) ArrayList<Integer seg - new ArrayList<IntegerO; for (int k = n; k > 0; k--) seq.add(new Integer(k+3)); return seq What is the final output of the following Java statement? System.out.println(mystery(3)); a. [1,2,4,5) b. [2,3,4,5) c. [6,5,4,3] d. [7, 7, 8, 8] e. [7,8,9, 8) O Consider the following method: public static int mystery(int[] arr, int k) ifk-0) return 0; }else{ return arr[k - 1] + mystery(arr, k-1):) The code segment below is...

  • Given the following code for a binary search, how many times this method have to be...

    Given the following code for a binary search, how many times this method have to be executed in order to find the number 5? A) 1 time B) 2 times C) 3 times D) 4 times E) more than 5 times public class BinarySearch{ public static void main(String []args){ if (right >= left) { int middle = left + (right - left) / 2; if (arr[middle] == num) return middle; if (arr[mid] > num) return binarySearch(arr, left, middle - 1,...

  • Consider the following mergeSortHelper method, which is part of an algorithm to recursively sort an array...

    Consider the following mergeSortHelper method, which is part of an algorithm to recursively sort an array of integers. /**  Precondition: (arr.length == 0 or 0 <= from <= to <= arr.length) * arr.length == temp.length */ public static void mergeSortHelper(int[] arr, int from, int to, int[] temp) { if (from < to) { int middle = (from + to) / 2; mergeSortHelper(arr, from, middle, temp); mergeSortHelper(arr, middle + 1, to, temp); merge(arr, from, middle, to, temp); } } The merge method...

  • Consider the following code segment. int[]arr={1, 2, 3, 4, 5, 6, 7, 8}; for(int k=3; k<arr.length-1;...

    Consider the following code segment. int[]arr={1, 2, 3, 4, 5, 6, 7, 8}; for(int k=3; k<arr.length-1; R++ arr[k]-arr[k+1]; What are the contents of arr as a result of executing the code segment? a. {1, 2, 3, 5, 6, 7, 8, 8) b. {2, 2, 4, 5, 6, 7, 8, 8} C. {2, 4, 6, 5, 6, 7, 8,8} d. {4, 2, 4, 4, 6, 7, 8, 8) e. {6, 6, 4, 5, 6, 7, 8, 8} int) arr={7, 2.5, 3.0,...

  • import java.util.Arrays; public class lab {    public static void main(String args[])    {    int...

    import java.util.Arrays; public class lab {    public static void main(String args[])    {    int arr[] = {10, 7, 8, 9, 1, 5,6,7};    int arr2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};    int arr3[] = {1, 3, 5, 3, 2, 6, 20};    quicksort(arr,0,arr.length-1);    quicksort(arr2,0,arr2.length-1);    quicksort(arr3,0,arr3.length-1);    System.out.println(Arrays.toString(arr));    System.out.println(Arrays.toString(arr2));    System.out.println(Arrays.toString(arr3));       }    private static int partition(int[] items,int low, int high)    {    int i=0;    int j=0;...

  • 8.         R-4.8 Order the following functions by asymptotic growth rate. 4nlogn + 2n 2^10    2^logn 3n...

    8.         R-4.8 Order the following functions by asymptotic growth rate. 4nlogn + 2n 2^10    2^logn 3n + 100logn      4n     2^n n^2 + 10n        n^3       nlogn 9.         R-4.9 Give a big-Oh characterization, in terms of n, of the running time of the example 1 method shown in Code Fragment 4.12. 10.       R-4.10 Give a big-Oh characterization, in terms of n, of the running time of the example 2 method shown in Code Fragment 4.12. 11.       R-4.11 Give a big-Oh characterization, in...

  • Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr,...

    Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr, int count, int key ) { 1: find the index of the first occurance of key. By first occurance we mean lowest index that contains this value. hint: copy the indexOf() method from Lab#3 into the bottom of this project file and call it from inside this remove method. The you will have the index of the value to remove from the array 2:...

  • please check first question before you do this one Java please it's data sturcture classs using...

    please check first question before you do this one Java please it's data sturcture classs using java editor netbeans 2.   3. public static int indexOfLargest(int[] arr) – if arr is null then return -1; otherwise return the index of the largest value. If the largest value appears more than one time, then return the left-most index. Example: If arr = {9, -2, 3, 50, 4, 99, 11, 4, 5, 99, 3, 4, -6}; then the method returns 5 4. public...

  • Question 3 Searching Algorithms [12 marks (4 marks) Define a method that when passed an array...

    Question 3 Searching Algorithms [12 marks (4 marks) Define a method that when passed an array of integers arr and another integer target, returns the last index in arr where target exists and -1 if target isn't found in arr a. b. (4 marks) Provide a trace, in the form of a logic table, of running the following method for 1, 4, 4, 4, 5, 7, 8, 12, 12, 15, 16, 17, 35, 35, 35, 40 arr = target 14...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT