23-24_31015_Java Assignment_2 (1)
23-24_31015_Java Assignment_2 (1)
1. Write a method isMultiple that determines, for a pair of integers, whether the second integer is a
multiple of the first. The method should take two integer arguments and return true if the second is a
multiple of the first and false otherwise. [Hint: Use the remainder operator.] Incorporate this method
into an application that inputs a series of pairs of integers (one pair at a time) and determines whether
the second value in each pair is a multiple of the first.
2. Write a method isEven that uses the remainder operator (%) to determine whether an integer is even.
The method should take an integer argument and return true if the integer is even and false otherwise.
Incorporate this method into an application that inputs a sequence of integers (one at a time) and
determines whether each is even or odd.
3. Write a method minimum3 that returns the smallest of three floating-point numbers. Use the
Math.min method to implement minimum3. Incorporate the method into an application that reads
three values from the user, determines the smallest value and displays the result.
4. Write a Java program that demonstrates the difference between passing a reference to an entire array
and passing an individual array element by value to a method. Declare and initialize an array of
integers. Print the values of the array before any modification. Call a method modifyArray(int[]
array) that multiplies each element of the array by 2, and then print the array after modification to
show the changes. Call a method modifyElement(int element) that multiplies a single element by 2
and prints its value, but ensure the original value in the array remains unchanged. After calling
modifyElement(int element), print the value of the specific element from the array again to
demonstrate that it hasn’t been changed by the method.
Sample Output:
1 2 3 4 5
2 4 6 8 10
Effects of passing array element value:
5. Create a java program demonstrates initializing two-dimensional arrays with array initializes and
using nested for loops to traverse the arrays (i.e., manipulate every element of each array). Class
InitArray’s main declares two arrays. The declaration of array1 uses nested array initializers of the
same length to initialize the first row to the values 1, 2 and 3, and the second row to the values 4, 5
and 6. The declaration of array2 uses nested initializers of different lengths. In this case, the first row
is initialized to two elements with the values 1 and 2, respectively. The second row is initialized to
one element with the value 3. The third row is initialized to three elements with the values 4, 5 and 6,
respectively.
6. The Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21, … begins with the terms 0 and 1 and has the property
that each succeeding term is the sum of the two preceding terms. Write a method fibonacci(n) with
array as an input parameter that provide the series of the nth Fibonacci number. Incorporate this
method into an application that enables the user to enter the value of n.
*************************End of Questions*************************