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;
}
}
}
Exercise 4.b Answer the following questions for the method count () below: public static int count...
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 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) ( 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; 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 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, 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:...
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...
Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element stored at this node */ private int element; // reference to the element stored at this node /** A reference to the subsequent node in the list */ private Node next; // reference to the subsequent node in the list /** * Creates a node with the given element and next node. * * @param e the element to be stored * @param n...
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...
------------------------------------------------------------------------------------------------------------ CODE ALREADY HAVE BELOW--------------------------------------------------------------------------------------- public class LinkedQueue<T> implements QueueADT<T> { private int count; private LinearNode<T> head; private LinearNode<T> tail; public LinkedQueue() { count = 0; head = null; tail = null; } @Override public void enqueue(T element) { LinearNode<T> node = new LinearNode<T> (element); if(isEmpty()) head = node; else ...