public class ZtomaszeA04i extends Object
Constructor and Description |
---|
ZtomaszeA04i() |
Modifier and Type | Method and Description |
---|---|
static int[] |
powersOfTwo(int[] array,
int startIndex)
Sets the cells within a section of the given array equal to 2^i, where i
is the index of the containing cell.
|
static String |
repeat(String str,
int times)
Returns a String containing the given String repeated the given number
of times.
|
static String |
row(int width)
Returns a String containing the given number of # characters.
|
static double |
sumOfSquares(int x)
Returns the sum of the square of each integer from 1 to x.
|
static String |
triangle(int size)
Returns a String of #s that form a right triangle of the given size.
|
public static String repeat(String str, int times)
Examples:
repeat("ho", 3) -> "hohoho" repeat("yes", 5) -> "yesyesyesyesyes" 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, the '\n' are 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[] powersOfTwo(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:
powersOfTwo(new int[5], 2) -> [0, 0, 4, 8, 16] int[] nums = {3, 5, 7, 9, 11}; powersOfTwo(nums, 6) -> [3, 5, 7, 9, 11] powersOfTwo(nums, 3) -> [3, 5, 7, 8, 16]
array
- The array of ints to changestartIndex
- The index within array at which to start writing
powers of 2public static double sumOfSquares(int x)
On the other hand, if x is negative, returns the sum of the square roots. For example, if x is -4, returns 1^(1/2) + 2^(1/2) + 3^(1/2) + 4^(1/2) = 1 + 1.4... + 1.7... + 2 = 6.14...
If x is 0, returns 0.
x
- The max square (or root) to add to the sum, starting at 1.