0

Hi I've wrote a program to print out a Diamond for a CS class; I've been asked to use function decomposition (global variables I think?) and I have no idea how to do this; some insight would help me out; my code isn't really pretty; but it does the job.

   public class SquareDiamondMid
 {
    public static void main(String args[]){
    System.out.println("printing Diamond");
    int number = BIO.getInt();
    int dotsTop = (number / 2) - 1;
    int count = 0;
    int LineN = 1;
    int DotsMiddle = 0;
    int mainCount = (number / 2)+1;
    int count1 = 0;
    int dot = (number / 2) - 1;

    while(count1<=dot){
        System.out.print(".");
        count1 = count1 + 1;
    }
    System.out.print("*");
    count1 = 0;
    while(count1<=dot){
        System.out.print(".");
        count1 = count1 + 1;
    }
    System.out.println(""); 

    while(mainCount<number){
        while(count<dotsTop){
            System.out.print(".");
            count++;
        }
        System.out.print("*");
        while(DotsMiddle<LineN){
            System.out.print(".");
            DotsMiddle++;
        }
        System.out.print("*");
        count = 0;
        while(count<dotsTop){
            System.out.print(".");
            count++;
        }
        System.out.println("");
        LineN = LineN + 2;
        DotsMiddle = 0;
        dotsTop--;
        mainCount++;
        count=0;
    }
    //bottom
    int bottomLineN=1;
    int numberOfBottom = (number / 2) - 2;
    int bottomCount = 0;
    int mainBottomCount=0;
    int bottomMidCount = 0;
    int bottomMidDot = number - 4;
    int bottomCount2 = 0;

    while(mainBottomCount<=numberOfBottom){

        while(bottomCount<bottomLineN){
            System.out.print(".");
            bottomCount++;
        }
        System.out.print("*");
        while(bottomMidCount<bottomMidDot){
            System.out.print(".");
            bottomMidCount++;
        }
        System.out.print("*");

        while(bottomCount2<bottomLineN){
            System.out.print(".");
            bottomCount2++;
        }

        bottomCount2 = 0;
        bottomMidCount = 0;
        bottomMidDot = bottomMidDot - 2;
        System.out.println("");
        mainBottomCount++;
        bottomCount=0;
        bottomLineN = bottomLineN + 1;
    }

    int count3=0;
    while(count3<=dot){
        System.out.print(".");
        count3 = count3 + 1;
    }
    System.out.print("*");
    count3 = 0;
    while(count3<=dot){
        System.out.print(".");
        count3 = count3 + 1;
    }
    System.out.println(""); 
}

Thanks in advance`.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
EightSquared
  • 37
  • 2
  • 13

1 Answers1

2

Functional decomposition means breaking it down into a number of discrete steps that the process performs.

For example, making a cup of tea might involve:

  • Getting a cup
  • Putting teabag in cup
  • Put water in kettle
  • Boil water
  • Add water to the teabag-filled cup

Each of those steps could be encapsulated in a function, something that takes an input (let's say an empty cup) and returns a result (a cup with a teabag in it).

Now look through your code. What steps does it perform? Can they be expressed as something that takes an input or initial state of the world and does some work to produce an output?

Printing out some dots might qualify...what input does that need? What would the function need to do with that input?

Paolo
  • 22,188
  • 6
  • 42
  • 49