Java

TOPIC 31 – MODULAR PROGRAMMING

 

LESSON WORK

 

 

WE WILL DO THIS WORK AS A CLASS

 

QUESTION 1

 

In this question, we will start by coding simple functions and using them in increasingly complex ways. 

 

PART 1

 

Create two classes that are stored in the same folder.  One should be called BC.java (short for Building  Complexity) and the other should be called BCTester.java.

 

We will place all functions except the main function inside BC.java.  The main function will be inside BCTester.java.

 

PART 2

Inside BC.java, create the outputMulti function that has the following prototype:

 

                       public static void outputMulti(String s, int n)

 

This function should output to screen s for n times. Once done, the cursor should stay where it is on the same line.

 

If the function call was

 

                       BC.outputMulti("ha", 3)

then the output should be

 

                       hahaha

 

PART 3

 

Inside the BCTester.java class, add a main function.  Inside that function, include the function call as shown above and see if “hahaha” is outputted. 

 

Testing such as this is an important part of programming advanced applications.

 

PART 4

 

Back inside the BC.java class, create a function called outputStars that gets one parameter n and outputs to screen n stars (without moving the cursor to the next line).

 

Important note:  You already have a function outputMulti that outputs any string for a set amount of times.  Use it!

 

So the entire function will look like this:

 

public static void outputStars(int n)

{

   BC.outputMulti("*", n);

}

 

So we’ve now created more functionality for ourselves with this outputStars function without simply by using the outputMulti function.

 

PART 5

 

Test your new outputStars function.  Place your function call inside the main function that you already created.

 

PART 6

 

Do the same as in 4, but create an outputSpaces function that outputs n spaces instead of stars.  Test this function if you wish.

 

PART 7

 

Lets add a function called nl that will bring the cursor to the next line.  Here it is:

 

public static void nl()  //nl stands for next line

{

   System.out.println("");

}

 

Note – While this function isn’t really useful as it replaces a single line statement, it is nice because it is a short statement to call and it allows for consistency.

 

PART 8

 

Inside BC, add the function outputRectangle that gets two parameters (rows & cols) and outputs a rectangle of stars based on the parameters.

 

Make sure that you make use of the outputStars function!

 

PART 9

 

Inside BCTester, call the outputRectangle function to test it.

 

PART 10

 

Inside BC, add the function outputSquare that gets one parameter (size) and outputs a square of stars base on the parameter.

 

Make sure to use the outputRectangle function!

 

PART 11

 

In BCTester, test the outputSquare function.

 

PART 12

 

Inside BC, add a function called outputDiagonal that outputs a diagonal line of stars.  Make use of the outputStars and outputSpaces functions.

 

Here is some analysis of the problem that will help:

 

Current row

Spaces

Stars

1

0

1

2

1

1

3

2

1

4

3

1

5

4

1

6

5

1

 

By analyzing the data, you should see a simple enough pattern.  By using the previously created functions, creating a diagonal is fairly simple.

 

PART 13

 

Test the outputDiagonal function inside the main function of BCTester.