Question

Exercise 4.b Answer the following questions for the method count () below: public static int count (List list, Object element
0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

import java.util.*;

public class CountElements{

public static void main(String[] args) {

List<Integer> list = new ArrayList<Integer>();

list.add(3);

list.add(3);

list.add(1);

System.out.println("Count is: "+count(list, 3));

List<Integer> list2 = new ArrayList<Integer>();

list2.add(1);

list2.add(7);

list2.add(5);

System.out.println("Count is: "+count(list2, 2));

}

public static int count(List<Integer> list, int search){

if(list==null)

throw new NullPointerException("List must not be empty");

else{

int count = 0;

for(Integer el: list){

if(el==search)

count++;

}

if(count==0)

return -1;

else

return count;

}

}

}

Add a comment
Know the answer?
Add Answer to:
Exercise 4.b Answer the following questions for the method count () below: public static int count...
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
  • Answer the following questions for the method intersection () below: public Set intersection (Set s1, Set s2) //Effects: If s1 or s2 is null throw NullPointerException /I else return a (non nul) Set...

    Answer the following questions for the method intersection () below: public Set intersection (Set s1, Set s2) //Effects: If s1 or s2 is null throw NullPointerException /I else return a (non nul) Set equal to the intersection // of Sets s1 and s2 Characteristic: Validity of s1 -s1 has at least one element Characteristic: Relation between s1 and s2 s1 and s2 represent the same set -$1 is a subset of s2 - s2 is a subset of s1 $1...

  • 1. Use the following characteristics and blocks for the questions below. Characteristics Block 1B...

    1. Use the following characteristics and blocks for the questions below. Characteristics Block 1Block 2Block 3 Block 4 Value 1 Value 2 Operation < 0 < O 0 0 S 0 S 0 (a) Give tests to satisfy the Each Choice criterion. b) Give tests to satisfy the Base Choice criterion. Assume base choices are Value->0, Value 2 >0, and Operation . (c) How many tests are needed to satisfy the All Combinations criterion? (Do not list all the tests!)...

  • 24) (3x2 marks) Consider the following method: public static int mysteryl (int a, int b) (...

    24) (3x2 marks) Consider the following method: public static int mysteryl (int a, int b) ( int result 0: if (a <b) ( else if (a b) else ( return result: result mystery2 (a) mystery2 (a)i result - mystery2 (b) result-ab; public static int mystery2 (int x) f int countx for (int i 0; іск; i++) count +1: return counti What are the values stored in the variable result after the following method calls? a) int result mysteryl(4,1): b) int...

  • b) Consider the following code. public static int f(int n) if (n == 1) return 0;...

    b) Consider the following code. public static int f(int n) if (n == 1) return 0; else if (n % 2 == 0). return g(n/2); else return g(n+1); public static int g(int n) int r = n % 3; if (r == 0) return f(n/3); else if (r == 1) return f(n+2); else return f(2 * n); // (HERE) public static void main(String[] args) { int x = 3; System.out.println(f(x)); (1) (5 points) Draw the call stack as it would...

  • You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement...

    You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement this method return new int[] {}; }    There is a utility method provided for you with the following signature. You may use this method to convert a list of integers into an array. public static int[] convertIntegers(List<Integer> integers) Provided code import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class MatrixSearch { // This method converts a list of integers to an array...

  • 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:...

  • 2) Consider the following C function: ----------------------------------------------------------------------------- int vdiv(double * a, double * b, double *c,...

    2) Consider the following C function: ----------------------------------------------------------------------------- int vdiv(double * a, double * b, double *c, int siz) {// divide vectors element-wise: c=a/b; // replaces divide by almost 0 with 1e99 and returns count int result=0; int i=0; 1: while(i<siz) 2: { if( abs(b[i])<1e-10 ){ // close to 0 3: c[i]=1e99; 4: result++; 5: }else{ 6: c[i]=a[i]/b[i]; 7: } 8: i++; 9: } 10: return result; } -------------------------------------------------------------------------- Executable lines are numbered. -Find all basic blocks, -draw control flow graph...

  • 4.The following questions refer to the skeletal C++ program shown below. a.Assume that static scoping is...

    4.The following questions refer to the skeletal C++ program shown below. a.Assume that static scoping is used. List all variables, along with the functions in which they are declared, that are visible at Line 1 in the program. b. Repeat part (a), but list the variables along with the functions in which they are declared that are visible at Line 2. c. Repeat part (a), but list the variables along with the functions in which they are declared that are...

  • 1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the...

    1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the number of leaf nodes in the tree. 2) Extend the Binary Search Tree ADT to include a public method singleParent-Count that returns the number of nodes in the tree that have only one child. 3) The Binary search tree ADT is extended to include a boolean method similarTrees that receives references to two binary trees and determines whether the shapes of the trees are...

  • 1) Write a public static method named printArray, that takes two arguments. The first argument is...

    1) Write a public static method named printArray, that takes two arguments. The first argument is an Array of int and the second argument is a String. The method should print out a list of the values in the array, each separated by the value of the second argument. For example, given the following Array declaration and instantiation: int[] myArray = {1, 22, 333, 400, 5005, 9}; printArray(myArray, ", ") will print out 1, 22, 333, 400, 5005, 9 printArray(myArray,...

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