192k views
3 votes
Write a method named hopscotch that accepts an integer parameter for a number of "hops" and prints a hopscotch board of that many hops. A "hop" is defined as the split into two numbers and then back together again into one. For example, hopscotch(3); should print ________.

User Clfaster
by
4.9k points

1 Answer

5 votes

Answer:

Following are the program in the Java Programming Language.

public class Main // declared class

{

public void hopscotch(int x) // function definition

{

int count=0; // variable declaration

for (int j=0; j<x; j++) // iterating over the loop

{

System.out.println(" " + (++count)); // print the value of count

System.out.println((++count) + " " + (++count));

}

System.out.println(" " + (++count));

}

public static void main(String[] args) // main method

{

Main ob=new Main(); // creating object

ob.hopscotch(3); // calling hopscotch method

}

}

Output:

1

2 3

4

5 6

7

8 9

10

Step-by-step explanation:

Here, we define a class "Main" and inside the class, we define a function "hopscotch()" and pass an integer type argument in its parentheses and inside the function.

  • We set an integer variable "count" and assign value to 0.
  • Set the for loop which starts from 0 and end at "x" then, print space and value inside the loop, again print value then space then value, then repeat the 1st print space then value.
  • Finally, set the main function "main()" and inside the main function, we create the object of the class "ob" then, call the function " hopscotch()" through the object and pass the value 3 in its parentheses.
User Logus Graphics
by
5.4k points