public String countHello(int count) returns a string that contains Hello count t
ID: 3925027 • Letter: P
Question
public String countHello(int count) returns a string that contains Hello count times all on one line, separated by spaces. You must use a loop. The output for countHello(5) will look like this:
Hello Hello Hello Hello Hello
public int sumEveryThird(int limit) finds the sum of every third number that is less than a given number starting with 3 - EXCEPT those divisible by 5. Use loop. If the limit is 21, you would calculate
3 + 6 + 9 + 12 + 18 = 48
and the method would return 48
public String noXYZ(String s) returns a string in which all the letters x, y, and z have been removed. You must use a loop.
Explanation / Answer
//java code
public class Driver
{
public static String countHello(int count)
{
String result = "";
for (int i = 0; i < count ;i++ )
{
result = result + "Hello ";
}
return result;
}
public static int sumEveryThird(int limit)
{
int sum = 0;
for (int i = 3; i < limit ; i=i+3 )
{
if (i%5 != 0)
{
sum = sum + i;
}
}
return sum;
}
public static String noXYZ(String s)
{
s = s.replace("x", "");
s = s.replace("y", "");
s = s.replace("z", "");
s = s.replace("X", "");
s = s.replace("Y", "");
s = s.replace("Z", "");
return s;
}
public static void main(String[] args)
{
System.out.println(countHello(6));
// output: Hello Hello Hello Hello Hello Hello
System.out.println("Sum: " + sumEveryThird(21));
// output: Sum: 48
System.out.println(noXYZ("my name is xyz, I am from XYZ placeZZ"));
// output: m name is , I am from place
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.