Assignment 4 CSS 105
Assignment 4 CSS 105
Assignment 4
1) (1 point)
You are given a 2D 0-indexed integer array dimension.
For all indices i, 0 <= i < dimensions.length, dimensions[i][0] represents the length and
dimensions[i][1] represents the width of the rectangle i.e
Return the area of the rectangle having the longest diagonal. If there are multiple rectangles
with the longest diagonal, return the area of the rectangle having the maximum area.
Explanation:
For index = 0, length = 9 and width = 3. Diagonal length = sqrt(9 * 9 + 3 * 3) = sqrt(90) ≈ 9.487.
For index = 1, length = 8 and width = 6. Diagonal length = sqrt(8 * 8 + 6 * 6) = sqrt(100) = 10.
So, the rectangle at index 1 has a greater diagonal length therefore we return area = 8 * 6 = 48.
2) (1 point)
You are given a 0-indexed 2D integer matrix grid of size n * n with values in the range [1, n2].
Each integer appears exactly once except a which appears twice and b which is missing. The
task is to find the repeating and missing numbers a and b.
Return a 0-indexed integer array ans of size 2 where ans[0] equals to a and ans[1] equals to b.
Example 1:
Input: grid = [[1,3],[2,2]]
Output: [2,4]
3)(0.2 point) Write a method in Java to calculate the sum of elements in an array.
4)(1 point) Suppose you write a program that displays the calendar for a given month of the
year. The program prompts the user to enter the year and the month, then displays the entire
calendar for the month, as shown in the following sample run.
5)(1 point) Write a Java method to display the first 50 pentagonal numbers.
Note: A pentagonal number is a figurate number that extends the concept of triangular and
square numbers to the pentagon, but, unlike the first two, the patterns involved in the
construction of pentagonal numbers are not rotationally symmetrical.
Expected Output:
1 5 12 22 35 51 70 92 117 145
176 210 247 287 330 376 425 477 532 590
651 715 782 852 925 1001 1080 1162 1247 1335
1426 1520 1617 1717 1820 1926 2035 2147 2262 2380
2501 2625 2752 2882 3015 3151 3290 3432 3577 3725
6) (0.8 point) Write a Java method to compute the future investment value at a given interest
rate for a specified number of years.
Sample data (Monthly compounded) and Output:
Input the investment amount: 1000
Input the rate of interest: 10
Input number of years: 5
Expected Output:
Years FutureValue
1 1104.71
2 1220.39
3 1348.18
4 1489.35
5 1645.31