Let's Create a Rectangle with Java

Welcome to mil-notes. In this post, we are going to fun with Java again. But, now we will play with something fundamental called "algorithm". What is algorithm?. Based on Google, Algorithm (ˈalɡəˌriT͟Həm | Noun)  can be defined as a process or set of rules to be followed in calculations or other problem-solving operations, especially by a computer. So, in this post we will creating something fun but also require an algorithm. What is it?

CREATING A RECTANGLE
Source: Wikipedia


Now, what is Rectangle?. Back to Google again, rectangle can defined as a plane figure with four straight sides and four right angles, especially one with unequal adjacent sides, in contrast to a square. So, how does it look with Java?. Ok, let's take a look for result example.



Interesting right? ok, now let's begin. Because we will also talk about algorithm too, so first of all we must create the algorithm for this program.

ALGORITHM OF JAVA RECTANGLE
  1. Program start
  2. Define two variables (e.g. Right, and Down) to determine the length and height of the rectangle.
  3. Create a loop inside a loop. Outer loop to form rectangle height meanwhile inner loop to form rectangle length.
  4. Inside inner loop, print a "*" character every loop to form the length.
  5. Inside outer loop, print a new line to form the height.
  6. The program ends.
After we done with algorithm, so it is time to transform it in to code.

So, begin with program start. Type these code.

Continue to define two variables.

Next, Create first loop

Continue with create second loop

Then, print a "*" character in inner loop

Continue with print newline in outer loop

Program end.
Don't forget to save it!

COMPILE AND RUN!
So, let's compile it and run it!. I Assume here You understand how to operate a CLI or terminal. If you still don't understand, learn how to use it first!. To compile and run, see the screenshot below.

If you want to change the size, you can edit this line:
Let's take a look for another size:
right = 10; down = 10;

right = 10; down = 15;

right = 15; down = 5;
It easy right? now you can modify it. Maybe with input? so you don't need to change the code every you want to change the size.

Well, I think this is the end for this post. Wait for my next post...!

- FIN -

[ SOURCE CODE ]
  1. Rectangle.java
[ RESOURCE ]
Google, and any other wiki.

Comments