Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Some JAVA question need help ?!!? 1. Complete Java program to produce the follow

ID: 3846286 • Letter: S

Question

Some JAVA question need help ?!!?

1. Complete Java program to produce the following output.

// Example of Method Overloading

class p3_4{

public static void main(String args[]){
p3_4 obj=new p3_4();
obj.sum(10.5,10.5);
obj.sum(20,20);
}
}

//output

21.0

40

2.Given the output stream A,B,C,D,E,F Write down the sequence of operations (Push for stack and Pop for unstack) which would produce the sequence C,B,D,E,F,A

3.What is the running time of each the method?

public int add100(int[] array) {

    if (array.length < 100) {

        return 0;

    }

    int sum = 0;

    for (int i = 0; i < 100; i++) {

        sum += array[i];

    }

    return sum;

}

public static void m4(int[] arr) {

for (int i=0; i<arr.length; i++) {   

    System.out.println(arr[i] * 10); }

for (int j=arr.length-1; j>=0; j--) {   

    System.out.println(arr[j] / 10); }

}

public static void m5(int[] arr) {

for (int i=0; i<15; i++) {  

    for (int j=0; j<arr.length; j++) {     

     System.out.println(Math.max(arr[i],    

     arr[j])); }}

}

4. Using big-O notation in terms of the parameter n, how much time does the following method take?

public static int mystery(int n) {

    int count = 0;

    int cur = 1;

    while (cur < n) {

        count++;

        cur = cur * 2;

    }

    return cur;

}

5. Each of the below methods determine m, n without using Math.pow. Using big-O notation in terms of n, how much time does each take?

a.

int pow(int m, int n) {

    int ret = 1;

    for (int i = 0; i < n; i++) {

        ret *= m;

    }

    return ret;

}

b.

int pow(int m, int n) {

    int ret = 1;

    int k = m;

    int i = n;

    while (i > 0) {

        if (i % 2 == 1) ret *= k;

        k *= k;

        i /= 2;

    }

    return ret;

}

6. Why is binary search O(log2 N) ?

7.Create min heap tree for {7, 3, 5, 2, 4, 6}

8.When the root is removed from the above heap tree, how do we alter the tree? Create a new heap tree step by step.

Explanation / Answer

As per Chegg policy, I am uploading solution of first 4 questions:

1.) Just paste the following code before the last curly braces (}):

public static void sum(double a, double b)

{

System.out.println(a+b);

}

public static void sum(int a, int b)

{

System.out.println(a+b);

}

2.) The following sequence of operations will give the desired sequence:

Push(A), Push(B), Push(C), Pop, Pop, Push(D), Pop, Push(E), Pop, Push(F), Pop, Pop.

3.) Since, loop in method add100 run for constant amount of time i.e., 100. Therefore, running time of this method is O(1).

Since, both loop in method m4 iterates for arr.length. Therefore, running time of this method is O(arr.length + arr.length) = O(arr.length).

In method m5, outer loop runs for constant amount of time i.e., 15 while inner loop runs arr.length times. Therefore, running time for method m5 is O(arr.length).

4.) Since, on each iteration value of n is decreasing by 2. Therefore, given method runs for log2 n times which gives O(log n).

Hope it helps, feel free to comment in case of any query.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote