Write a method called ten2one(), that takes no input parameters, and returns not
ID: 3563958 • Letter: W
Question
Write a method
called ten2one(), that takes no input parameters, and returns nothing (void).
The body of the method must include a loop that counts from 10 down to 1, printing the
count value each time in the loop. The output should look like (notice the spacing):
10 9 8 7 6 5 4 3 2 1
Call this function from main().
2.Write a method called hundredByFives() that takes no input arguments and returns nothing.
The body of the function must include a loop that counts from 5 to 100 by fives, printing the count ineach iteration. The output should look like:
5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100
Notice the commas.
Notice that there is no comma at the beginning or end.
That means that you must do something special either at the beginning or at the end.
A good solution should not need an if statement inside the loop.
3.
Write a function called countDown(int value) that takes an integer parameter as input but
returns nothing.
(
Explanation / Answer
public class Loop
{
public void ten2one()
{
int i;
for ( i = 10; i >1; i--) {
System.out.print(i+" ");
}
System.out.println(i);
}
public void hundredByFives()
{
int i;
for ( i = 1; i <20; i++) {
System.out.print(5*i+",");
}
System.out.println(5*i);
}
public void countDown(int value)
{
int i;
for (i = value; i >0; i--)
System.out.print(i+" ");
System.out.println(i);
}
public void countUp(int value)
{
int i;
for (i = 1; i<value; i++)
System.out.print(i+" ");
System.out.println(i);
}
}
// MAIN CLASS
public class Project
{
public static void main(String[] args)
{
int value;
Loop loop=new Loop();
loop.ten2one();
loop.hundredByFives();
value=10; loop.countDown(value);
value=20; loop.countDown(value);
value=30; loop.countDown(value);
value=10;loop.countUp(value);
value=20;loop.countUp(value);
value=30;loop.countUp(value);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.