CSE 142
-
Write a method called fractionSum that accepts an integer parameter
n
and returns as a double the sum of the firstn
terms of the sequence. In other words, the method should generate the following sequence:1 + (1/2) + (1/3) + (1/4) + (1/5) + ...
You may assume that the parametern
is non-negative. -
Write a method named
repl
that accepts aString
and a number of repetitions as parameters and returns theString
concatenated that many times. For example, the callrepl("hello", 3);
returns"hellohellohello"
. If the number of repetitions is 0 or less, an empty string is returned. -
Write a method named
evenSum
that prompts the user for a number of integers and prints the total even sum and maximum of the even numbers. You may assume that the user types at least one non-negative even integer.
how many integers? 4
next integer? 2
next integer? 9
next integer? 18
next integer? 4
even sum = 24
even max = 18
-
Write a method named
numUnique
that takes three integers as parameters and returns the number of unique integers among the three. For example, the callnumUnique(18, 3, 4);
should return3
because the parameters have 3 different values. By contrast, the callnumUnique(6, 7, 6);
would return2
because there are only 2 unique numbers among the three parameters: 6 and 7. -
A "perfect number" is a positive integer that is the sum of all its proper factors (that is, factors including 1 but not the number itself). The first two perfect numbers are 6 and 28, since 1+2+3=6 and 1+2+4+7+14=28. Write a static method
perfectNumbers
that takes an integer max as an argument and prints out all perfect numbers that are less than or equal to max.
Here is the console output from a call to perfectNumbers(6);
Perfect numbers up to 6: 6
Here is the console output from a call to perfectNumbers(500);
Perfect numbers up to 500: 6 28 496