Java Reference
In-Depth Information
should produce the following output:
34567
45673
56734
67345
73456
If the maximum passed is less than the minimum, the method produces no output.
5.
Write a method called
printGrid
that accepts two integers representing a number of rows and columns and prints a
grid of integers from
1
to (rows * columns) in column major order. For example, the call
printGrid(4, 6);
should produce the following output:
1 5 9 13 17 21
2 6 10 14 18 22
3 7 11 15 19 23
4 8 12 16 20 24
6.
Write a method called
largerAbsVal
that takes two integers as parameters and returns the larger of the two absolute
values. A call of
largerAbsVal(11, 2)
would return
11
, and a call of
largerAbsVal(4, -5)
would return
5
.
7.
Write a variation of the
largestAbsVal
method from the last exercise that takes three integers as parameters and
returns the largest of their three absolute values. For example, a call of
largestAbsVal(7, -2, -11)
would
return
11
, and a call of
largestAbsVal(
4, 5, 2)
would return
5
.
8.
Write a method called
quadratic
that solves quadratic equations and prints their roots. Recall that a quadratic equa-
tion is a polynomial equation in terms of a variable
x
of the form
ax
2
bx
c
0. The formula for solving a quad-
ratic equation is
b
2
-
b
; 2
-
4
ac
x
=
2
a
Here are some example equations and their roots:
x
2
-
7
x
+
12:
x
=
4,
x
=
3
x
2
+
3
x
+
2:
x
=-
2,
x
=-
1
Your method should accept the coefficients
a
,
b
, and
c
as parameters and should print the roots of the equation. You
may assume that the equation has two real roots, though mathematically this is not always the case.
9.
Write a method called
distance
that accepts four integer coordinates
x
1
,
y
1
,
x
2
, and
y
2
as parameters and computes
the distance between points (
x
1
,
y
1
) and (
x
2
,
y
2
) on the Cartesian plane. The equation for the distance is
x
1
)
2
y
1
)
2
d
= 2
(
x
2
-
+
(
y
2
-
For example, the call of
distance(1, 0, 4, 4)
would return
5.0
and the call of
distance(10, 2, 3, 5)
would return
14.7648230602334
.
10.
Write a method called
scientific
that accepts a real number base and an exponent as parameters and computes the
base times 10 to the exponent, as seen in scientific notation. For example, the call of
scientific(6.23, 5)
would
return
623000.0
and the call of
scientific(1.9, -2)
would return
0.019
.
Search WWH ::
Custom Search