public class ZtomaszeA03r extends Object
Constructor and Description |
---|
ZtomaszeA03r() |
Modifier and Type | Method and Description |
---|---|
static int |
gcd(int a,
int b)
Uses the Euclidean algorithm to compute the greatest common divisor
of the given two integers.
|
static String |
interpose(String str,
int times)
Returns a String containing the given String fragment repeated the given
number of times with a decreasing count placed between the fragments.
|
static void |
main(String[] args)
Tests the methods in this class, printing the results to the screen.
|
static String |
row(int width)
Returns a String containing the given number of * characters.
|
static int[] |
squares(int[] array,
int startIndex)
Sets the cells within a section of the given array equal to i*i, where i
is the index of the containing cell.
|
static String |
triangle(int size)
Returns a String of *s that form a right triangle of the given size.
|
public static void main(String[] args)
args
- Ignored.public static String interpose(String str, int times)
If times is 0 or less, returns an empty string "".
Examples:
repeat("ho", 3) -> "ho2ho1ho" repeat("yes", 5) -> "yes4yes3yes2yes1yes" repeat("x", 1) -> "x" repeat("", 4) -> 321 repeat("raise", -1) -> ""
str
- The string to repeattimes
- How many times to repeat itpublic static String row(int width)
If width is 0 or more, the returned String will end with a newline ('\n') character. If width is negative, returns the empty string "".
Examples:
row(3) -> "***\n" row(5) -> "*****\n" row(0) -> "\n" row(-2) -> ""
width
- How many *s should be in the generated rowpublic static String triangle(int size)
If size is 0 or less, returns the empty String "".
Examples:
triangle(6) -> "*\n**\n***\n****\n*****\n******\n" triangle(3) -> "*\n**\n***\n" triangle(1) -> "*\n" triangle(0) -> "" triangle(-2) -> ""
In these examples, '\n' is a single newline characters. If these strings were printed, they would result in multiple lines of output.
size
- The width and height of a right triangle of *spublic static int[] squares(int[] array, int startIndex)
Changes the given array, but also returns a reference to it. If start index is outside the bounds of the array, this method does nothing, returning the array unchanged.
Examples:
squares(new int[5], 2) -> [0, 0, 4, 9, 16] int[] nums = {2, 4, 6, 8, 10}; squares(nums, 6) -> [2, 4, 6, 8, 10] squares(nums, 3) -> [2, 4, 6, 9, 16]
array
- The array of ints to changestartIndex
- The index within array at which to start writing
squarespublic static int gcd(int a, int b)
gcd(a, 0) == a, and gcd(a, b) == gcd(b, a - (b * (a/b)))
When calling this method, the parameter a does not need to be >= b because this method will appropriately reorder the parameters' values if necessary. The returned value will always be >= 0.
Examples:
gcd(48, 32) -> 16 gcd(32, 48) -> 16 gcd(32, 32) -> 32 gcd(0, 32) -> 32 gcd(9, 17) -> 1 gcd(18, -6) -> 6 gcd(-6, -18) -> 6
a
- One of the two values to find the GCD ofb
- The other of two values to find the GCD of