Suppose that you want to make the following pattern on the screen:
*
**
***
****
*****
Build it up from small pieces. Start with what you know, build slowly.
1. You notice that the number of stars on each line makes the following pattern:
| 1 | * |
| 2 | ** |
| 3 | *** |
| 4 | **** |
| 5 | ***** |
2. You start with a basic program, labelling the end } so you can track them:
public class mathArt
{
public static void main (String args [])
{ new mathArt ();
} //main
public mathArt ()
{
} //constructor
} //class
3. You add a loop to print out the numbers 1 to 5.
public class mathArt
{
public static void main (String args [])
{
new mathArt ();
} //main
public mathArt ()
{
int counter = 1;
while (counter < 6)
{
System.out.println (counter);
counter++;
}//while outer
} //constructor
} //class
4. You change the loop to have another loop inside that prints out 1 of 1, 2 of 2 etc. For example: 122333444455555. The outer loop handles the variables moving from 1 to 2 to 3 to 4 to 5. The inner loop handles the printing of one 1, two 2s, three 3s etc.
public class mathArt
{
public static void main (String args [])
{
new mathArt ();
} //main
public mathArt ()
{
int counter = 1;
while (counter < 6)
{
int counter2 = 0;
while (counter2 < counter)
{
System.out.print (counter);
counter2++;
} //while inner
counter++;
} //while outer
} //constructor
} //class
5. Your next step is to switch the System.out.println(counter) to print a star. Then, you make an enter print after every line ie in the outer loop.
public class mathArt
{
public static void main (String args [])
{
new mathArt ();
} //main
public mathArt ()
{
int counter = 1;
while (counter < 6)
{
int counter2 = 0;
while (counter2 < counter)
{
System.out.print ("*");
counter2++;
} //while inner
System.out.println();
counter++;
} //while outer
} //constructor
} //class
You're finished.
import java.applet.Applet; import java.awt.*;
public class MathArt extends Applet
{
public void paint (Graphics g)
{
int x = 15;
int y = 15;
int count = 1;
while (count < 26)
{
g.drawRect (100, 100, x, y);
x += 5;
y += 5;
count++;
} //while
} //paint
} //class
|
1. print out 5 boxes |
public void paint (Graphics g)
{
int x = 15;
int y = 15;
int count = 1;
while (count < 6)
{
g.drawRect (x, y, 25, 25);
x += 30;
count++;
} //paint |
|
2. print out 5 X 6 boxes |
public void paint (Graphics g)
{
int x = 15;
int y = 15;
int count1 = 1;
int count = 1;
while (count1 < 5)
{
while (count < 6)
{
g.drawRect (x, y, 25, 25);
x += 30;
count++;
} //while inner x = 15;
y += 30;
count1++;
count = 1;
} //while outer
} //paint |
|
3. fills in 5 X 6 boxes |
public void paint (Graphics g)
{
int x = 15;
int y = 15;
int count1 = 1;
int count = 1;
while (count1 < 5)
{
while (count < 6)
{
g.setColor (new Color (0, 0, 255));
g.fillRect (x, y, 25, 25);
x += 30;
count++;
} //while inner
x = 15;
y += 30;
count1++;
count = 1;
} //while outer |
|
4. varies the colour |
public void paint (Graphics g)
{
int x = 15;
int y = 15;
int count1 = 1;
int count = 1;
int blue = 255;
while (count1 < 5)
{
while (count < 6)
{
g.setColor (new Color (0, 0, blue));
g.fillRect (x, y, 25, 25);
x += 30;
count++;
blue -= 10;
} //while inner x = 15;
y += 30;
count1++;
count = 1;
} //while outer
} //paint |