Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey!

I'm a complete beginner learning Java so I am sorry for the simple question! The following program outputs a pyramid. I am wondering how the last three methods know what value to assign to their parameters. Where do they get this value?

Java
import java.util.Scanner;

public class StaticPyramidProg
{
   public final static int MARGIN = 10;

   public static char brickCharacter;
   public static int height;
   
   public static void main(String[] args)
   {
      Scanner scan = new Scanner(System.in);
      System.out.print("\n\tEnter the number of lines for the pyramid: ");
      height = scan.nextInt();
      System.out.print("\tEnter the character from which the pyramid should be made: ");
      brickCharacter = scan.next().charAt(0);
      
      System.out.println(pyramidString());
   }
   
   //-----------------------------------------------------------------
   //  Returns a string containing a representation of the pyramid.
   //-----------------------------------------------------------------
   public static String pyramidString()
   {
      String pattern = "\n";
      for (int lineCount = 1; lineCount <= height; lineCount++)
      {
         pattern += pyramidLine(lineCount);
      }
      return pattern;
   }
   
   //-----------------------------------------------------------------
   //  Returns a string containing a representation of 
   //  a given line of the pyramid.
   //-----------------------------------------------------------------
   private static String pyramidLine(int lineNumber)
   {
      String line = "";
      line += spacesForPyramidLine(lineNumber);
      line += symbolsForPyramidLine(lineNumber);
      line += "\n";
      return line;
   }
   
   //-----------------------------------------------------------------
   //  Returns a string of spaces for the given line of the pyramid.
   //-----------------------------------------------------------------
   private static String spacesForPyramidLine(int lineNum)
   {
      String lineSpaces = "";
      for (int spacesCount = 1; spacesCount <= (MARGIN + height + 1 - lineNum); spacesCount++)
      {
         lineSpaces += " ";
      }
      return lineSpaces;
   }
   
   //-----------------------------------------------------------------
   //  Returns a string of symbols for the given line of the pyramid.
   //-----------------------------------------------------------------
   private static String symbolsForPyramidLine(int lineNum)
   {
      String lineSymbols = "";
      for (int symbolsCount = 1; symbolsCount <= ((lineNum * 2) - 1); symbolsCount++)
      {
         lineSymbols += brickCharacter;
      }
      return lineSymbols;
   }
}


Thank you so much for your help!!

What I have tried:

This is more of a question than a troubleshoot
Posted
Updated 29-Oct-17 2:31am

1 solution

You pass them to the function when you call it.
When you define a function, you specify what parameters it needs to be given:
Java
private static string pyramidLine(int lineNumber)
{
   ...
}
Says "when you call pyramidLine, you must give it an integer, and it will return a string".
Within the body of the function, the parameter value is stored in a variable - in this case called lineNumber - which the code of the function body can work with. This is unrelated to any other variables of that name: the function parameter is local to that function and that function only.
So when you call the function, you just pass it an integer value:
Java
String s;
s = pyramidLine(666);
Or:
Java
String s;
int lines = 666;
s = pyramidLine(lines);
Or even:
Java
String s;
int lines = 666;
s = pyramidLine(lines * 10);
The value is evaluated, and passed to the function.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900