203k views
0 votes
Write the definition of a method dashedLine, with one parameter, an int. If the parameter is negative or zero, the method does nothing. Otherwise it prints a complete line terminated by a newline to standard output consisting of dashes (hyphens) with the parameter's value determining the number of dashes. The method returns nothing

1 Answer

7 votes

Answer:

The following are the program in the Java Programming Language.

//define a function

public void dashedLine (int x)

{

//check the variable 'a' is greater than 0

if (x>0)

{

//declare integer data type variable

int i;

//set the for loop that iterates from 1 to variable 'a'

for (i=1;i<=x;i=i+1)

{

//print the following dashes

System.out.print("-");

}

//for empty space

System.out.println("");

}

}

Step-by-step explanation:

The following are the description of the program.

  • Define a function 'dashedLine()' and pass an argument 'x' in the function's parameter.
  • Then, set the if conditional statement that check the argument x is greater than 0 or not.
  • Then, declare the variable 'i' and set the for loop statement that iterates from 1 to the variable x then, print the dashes and print the empty spaces after every loop statement.
User Sebastian Nielsen
by
5.2k points