Starting from:

$30

Practice1 Solved

Question 1

Enter a value of long type and find the number of 0 in the binary representation of the value.

For Example,



input 0, which store in long type is :

00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
output 64



input -1, which store in long type is :

11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
output 0



input 1280, which store in long type is :

00000000
00000000
00000000
00000000
00000000
00000000
00000101
00000000
output 62





Question 2

You need to parse values from a byte buffer according to bit analysis. The order and bits of bit parsing are stored in an array lengthOfBits. The values can be stored in an int array.

For Example,

Byte buff: 128, 4

lengthOfBit: 4,4,8

First, elements in a buff can be represented in binary form as:

10000000 00000100

The First 4 bits are:

1000

The second 4 bits are:
0000

The last 8 bits are:

00000100

store the 3 values to an int array, output:

[4, 0, 8]


Question 3

Stack has the property LIFO (last in first out), In this question, you are required to implement Stack<E> structure. You should define the push and pop in the Stack class.

You have been provided with Stack.java, you should fill in the missing parts with appropriate code. You can search for “TODO” to find all unfinished parts you need to fill in. Please do not modify or remove any methods or fields that have been already defined.































Do the following operations:

Stack<Integer> stack = new Stack<>(5);

stack.push(1);

stack.push(2);

stack.push(3);

stack.push(4);

stack.push(5);

stack.push(6);
System.out.println(stack.pop());

System.out.println(stack.pop());

stack.push(10);

stack.showElements();


Output:

5

4

1

2

3

10


Evaluation Criteria:

The practice will be checked on this to the next next lab class(Mar.2) by teachers or

SAs. What will be tested:

    1. That you understand every line of your own code, not just copy from somewhere

    2. That your program compiles correctly (javac)

    3. Correctness of the program logic

    4. That the result is obtained in a reasonable time

This practice will contribute 1 mark to your overall grade. Late submissions within 2 week after the deadline ( Mar.2)will incur a 20% penalty, meaning that you can only get 80% of the score.

More products