Sunteți pe pagina 1din 2

Static Functions :-

2. Write a static method odd() that takes three boolean inputs


and returns true if an odd number of inputs are true,
and false otherwise.
3. Write a static method majority() that takes
three boolean arguments and returns true if at least two of the
arguments have the value true, and falseotherwise. Do not
use an if statement. Solution: here are two solutions: the first is
concise; the second strictly adheres to the rules.
4. Write a static method eq() that takes two arrays of integers as
arguments and returns true if they contain the same number of
elements and all corresponding pairs of elements are equal,
and false otherwise.

9. --Give the function call trace for java Newton 4.0 9.0.
10. Write a static method lg() that takes a double value N as
argument and returns the base 2 logarithm of N. You may use
Java's Math library.
11. --Write a static method lg() that takes an int value N as
argument and returns the largest int not larger than the base-2
logarithm of N. Do not use Math.

14. Consider the static method cube() below.


public static void cube(int i) {
i = i * i * i;
}

How many times is the following for loop iterated?


for (int i = 0; i < 1000; i++)
cube(i);

1000 times
15. The following checksum formula is widely used by banks and
credit card companies to validate legal account numbers:

d0 + f(d1) + d2 + f(d3) + d4 + f(d5) + d6 + ... = 0


(mod 10)

The di are the decimal digits of the account number and f(d) is the
sum of the decimal digits of 2d (for example, f(7) = 5 because 2 * 7 =
14 and 1 + 4 = 5). For example, 17327 is valid because 1 + 5 + 3 + 4
+ 7 = 20, which is a multiple of 10. Implement the function f and write
a program to take a 10-digit integer as a command-line argument and
print a valid 11-digit number with the given integer as its first 10 digits
and the checksum as the last digit.

15. Write a method that takes an array of double values as


argument and rescales the array so that each element is between 0
and 1 (by subtracting the minimum value from each element and then
dividing each element by the difference between the minimum and
maximum values). Use the max() method defined in the table in the
text, and write and use a matching min() method.