Byte code, Machine code, Source code

Hello, how are you doing? well i am fine indeed.Last time we talked about OOP or Object Oriented Programming, we see how OOP can benefit us. We talk about it’s definition, it’s example, it’s advantages and stuff.Now we’ll talk about a compiled programming language, since Java is a compiled programming language. First I apologize if there is any mistake, any help or knowledge would be nice. Lets be done with the chit-chat and lets begin.

Probably you already know that java is an OOP and compiled programming language, but what is the meaning of compiled programming language, what is it benefitial? well basically a compiled programming language is a programming language that compiles instruction that the operators write into bytecode or machine language with the help of an IDE.Another explanation is, A compiled language is a programming language that its implementations are typically based to be compilers (translators that will generate machine code from the source code that operators write), and not interpreters (step-by-step instruction executors of the source code itself, where there are no pre-runtime translation that takes place).


Now you might wonder, what the heck is bytecode, machine code and source code is? good question, and as for that I will answer. What is bytecode? Bytecode, also known as p-code or a portable code, is a form of instruction that is set and designed for efficient execution by a software interpreter, that is its general definition, but… as for the Java bytecode, bytecode is a set of instruction that is created by the Java virtual machine. Each bytecode is composed from one or two bytes that represents the instruction (p-code).

Not to much of a difficult things right? well then… let’s move to machine code. What is a machine code? let’s just divide it into 2 part: first is the machine, the machine represents our computers or any machine that run codes, second… code is a Code!!. Well as for the explanation is, a computer programming language that uses binary or hexadecimal insturction that consist of one and zero(bits) that the computer can responds to directly using it’s central processing unit (cpu). Phewww… easy right? now that you understand what machine code is you could get a deeper meaning in coding.

Last one is our Source code, what is a source code? here is my explanation, source code is a list of text that contains commands or instruction created by operators in a read-able computer language that needs to be compiled to a machine code then will an executable computer program.The source code of a computer program is specially designed to be a facilites for the work of computer programmers, who then… will specify the actions to be performed by a computer mostly by writing source code.

Now that you understand those 3 parts that consist in OOP, I hope you will be better at coding using OOP programming language, since now you understands its meaning and part.

Conclusion

  • A compiled programming language is a programming language that compiles instruction that the operators write into bytecode or machine language with the help of an IDE.
  •  Bytecode, also known as p-code or a portable code, is a form of instruction that is set and designed for efficient execution by a software interpreter.
  • Java bytecode, bytecode is a set of instruction that is created by the Java virtual machine. Each bytecode is composed from one or two bytes that represents the instruction (p-code).
  • a computer programming language that uses binary or hexadecimal insturction that consist of one and zero(bits) that the computer can responds to directly using it’s central processing unit (cpu)
  • source code is a list of text that contains commands or instruction created by operators in a read-able computer language that needs to be compiled to a machine code then will an executable computer program. 

OOP or Object Oriented Programming

Hello, how are you doing? well i am fine indeed.Last time we talked about each of Java and Javascript pros and cons, both of them had their own advantages and disadvantages, but they’re basically the same.Now we’re going to talk about OOP or Object Oriented Programming, what is its definition, example and stuff. First I apologize if there is any mistake, any help or knowledge would be nice. Lets be done with the chit-chat and lets begin.

What is an Object Oriented Programming? (or we’ll call it OOP just for a shorter instance) OOP is just a programming language that often uses object as its coding bases. If you wanted to be more specific, Object Oriented Programming is a programming language model that is organized around “objects” rather than logic manipulating, OOP takes the view that what we really care is the object that manipulate itself rather than the logic required to manipulate them. It may sounds confusing, so you could just uses the first definition for your beliefs.

What programming language that uses OOP as its base, can you guess? i’ll give a clue, we talk about it in more or less about 3 post, we make a program with it using an IDE. Can you remember now? okay i’ll tell it to you now, it’s Java!! yes, Java is an Object Oriented Programming, just think that we need to make a ball object, a function, method etc… and I’d just tell you in my last post, Java vs Javascript, in there I explained that Java is an Object Oriented Programming language.

What is the basics of Object Oriented Programming or OOP ? well there is actually 10 basics of OOP that I knows, here are the list :

  • Abstraction
  • Class
  • Encapsulation
  • Information hiding
  • Inheritance
  • Interface
  • Messaging
  • Object
  • Polymorphism
  • Procedure

 Let’s talk about 3 of them, which is : Class, Inheritance and Object itself.

First… Class, what is a class?, not that kind of class you go everyday, a Class in Coding, well here is my definition, a Class is a category of objects, a class will contain all of the common properties that belongs to any objects wether it’s different or not.We can also say it in another way, a class is an extensible program-code-template that used to create objects, and could also implements behavior.

Second… an Inheritance, what is an enhiritance?, Just imagine like our world, that every object has something in commong, just for example, a monkey and a human, both of them are a living things, both of them has hands, legs, eyes, ears and mouth, but there is something in someway that seperate a monkey and a human, well it’s called an Inheritance, an Inheritance are the way of some objects will shares similarities like characters but in one if them has some difference, but still inherit something from another object. Well that’s pretty easy right? now let’s move to the last one.

Third… an Object, what does an object means in programming? is it the same definition with the one’s that are in real life? well… an object in a programming language is a particular instance to a “Class” which the object itself can be combination of variables, function and data structures.

What is the advantages that people sees in OOP, One of the most common advantages of object-oriented programming techniques rather than using normal programming techniques is that OOP would allow the programmers to create a somewhat a modules that do not need to be changed over and over again, when a new type of object is added to the program. A programmer can simply create a new object that inherits many of its features or functionality or properties, from existing objects. This makes object-oriented programs easier to modify.

Conclusions :

  •  OOP is a programming language that often uses object as its coding bases or Object Oriented Programming is a programming language model that is organized around “objects” rather than logic manipulating, OOP takes the view that what we really care is the object that manipulate itself rather than the logic required to manipulate them.
  •  Java is an Object Oriented Programming.
  • a Class is a category of objects, a class will contain all of the common properties that belongs to any objects wether it’s different or not
  •  an Inheritance are the way of some objects will shares similarities like characters but in one if them has some difference, but still inherit something from another object.
  • an object in a programming language is a particular instance to a “Class” which the object itself can be combination of variables, function and data structures.
  • The advantages of OOP, A programmer can simply create a new object that inherits many of its features or functionality or properties, from existing objects

Java vs Javascript

Hello, how are you doing? well i am fine indeed.On my last post we just made ourself another version of movingBall program, it’s the same movingBall program but written in different programming language. We made ourself 2 movingBall program, first one in Java and second one in Javascript, well now it’s time for us to take a look at pros and cons from both Java and Javascript. First I apologize if there is any mistake, any help or knowledge would be nice. Lets be done with the chit-chat and lets begin.

First let’s get the definition of both Java and Javascript, Java is an Object Oriented Programming language while Javascript is an Object Oriented Programming Scripting Language. Both of them are an OOP language but there is a difference, Javascript is a OOP scripting language and what does that means? a scripting language is a programming language that supporst script, program written for a special run-time environment that automate the execution of somekind a task that alternatively can be executed one by one with human operator. An environments that can be automated through scripting is, software applications, web pages within browser, and some numerous numbers of games.


Java is a compiled programming language, it is typically written in an Integrated Development Environment (IDE) and compiled into a bytecode, bytecode is not readable for human language and any Java Virtual Machine could run it. Javascript is a programming language that is Interpreted rather than Compiled, an Interpreted programming language is a programming language that execute the instruction directly, without previously compiling it into a machine-language. Javascript may be easier to create because it doesn’t need to be compiled and deployed like Java, Javascript sometimes can be just created by using a simple text editor like notepad++

Both of java and javascript can be runned inside a browser, in modern browser they obviously take advantages to enhance’s the users experience.Java applet can also be runned in browser, but it have been declining in it’s popularity for various of reasons, first it’s compatibility issues even to security reasons. It is been widely common for a website visitors that it’s javascript is enabled and it is also widely common for a website visitors that it’s Java applet is disabled, especially in mobile browsers.

In javascript you need to send it to the internet first or to your browser first to get the program works, but Java can create a standalone application that doesn’t need any other intermediary to run it, it works on it own. The standalone application is called a Java “applet” (so-called like that because it is a little application) that is a fully contained program. While javascript can only be runned in Javascript with the help of Html and CSS.

Each of them had their own pros and cons, you should pick one programming language that suites you heart, the one that you get comfortable with. Well you could learn both of them but after that choose one that you’ll go deeper with.

Conclusions:

  • Java is an Object Oriented Programming language
  • Javascript is an Object Oriented Programming Scripting Language
  • a scripting language is a programming language that supporst script, program written for a special run-time environment that automate the execution of somekind a task that alternatively can be executed one by one with human operator.
  • Java is a compiled programming language
  • Javascript is a programming language that is Interpreted
  • A compiled programming language is a language that compiles codes into machine-language
  • Both of java and javascript can be runned inside a browser
  • Java can create a standalone application that doesn’t need any other intermediary to run it

MovingBall in Javascript

Hello, how are you doing? well i am fine indeed.In my last post we get our brain and finger exercised on Javascript, we did our exercise in Chrome development console well it’s just for an exercise, but today we’ll do some real Javascript coding, we’ll simply make a Movingball program just like in Java.The way to do this is we need to make a Html file that contains javascript, so let’s do that right now.  first I apologize if there is any mistake, any help or knowledge would be nice. Lets be done with the chit-chat and lets begin.

First we need to make an HTML file, you can do this in the notepad++ it can be freely downloaded, now let’s make the structure of the Html, since we don’t really talk about Html much in this blog i’m just going to give you the important Html code structure which looks like this, the canvas is just how big that we want  the background of the painting will be, so don’t worry:


<!DOCTYPE html>
<head> 
<title>MovingBall</title>
</head>

<body>
    <canvas id=”canvas” width =”700″ height=”700″></canvas>
    <script> 

                  /*the section that
                 
we’ll write some Javascript in it*/

    </script>
</body>
</html>

 you just need to write this inside the notepad++ and save as Ball.html (you could name it whatever you want, as long it ends with .html) now after you save that as Ball.html  you will see some changes that is applied to your code, now it has colors right it’s just a helping technique that will help programmer write their code more efficient and more easy.

Now after we finished with the html, let’s work some javascript! the structure prototype and function of this MovingBall is a lot similar with the one that we did with Java, but this one is going to be more easy because it doesn’t have variable typing and class that needs to be created etc… so let’s write it inside the
<script></script>:
first we’re going to make the ball object or ball prototype :
  var Ball = function() {
    this.x = 100;
    this.y = 100;
    this.xSpeed = -5;
    this.ySpeed = 6;
    this.border = 10;
    };

after this we’re going to create a function that will draw a circle whenever it’s called or executed, this is how we write it:
  var circle = function(x, y, radius, fillCircle){
        ctx.beginPath();
        ctx.arc(x, y, radius, 0, Math.PI * 2, false);
        if (fillCircle) {
            ctx.fill();
        } else {
            ctx.stroke();
        }
    };

good now we have created a circle function, now let’s add some more function for our ball, just like draw, move and checkCollisions :
    Ball.prototype.draw = function(){
        circle(this.x, this.y, 20, true);
    };
   
    Ball.prototype.move = function(){
        this.x += this.xSpeed;
        this.y += this.ySpeed;
    };
   
    Ball.prototype.checkCollision = function() {
        if (this.x < 0 + this.border || this.x > 700 – this.border) {
        this.xSpeed = -this.xSpeed;
    }
        if (this.y < 0 + this.border || this.y > 700 – this.border) {
        this.ySpeed = -this.ySpeed;
    }
    }

you see it’s not that different than what we write in our Java coding right? so this will be pretty much easy. Do you have any clue for what we need to write next? we’ll since we’re in javascript environtment inside html inside a browser (yeah right! we’re going to run it in a browser) we need to call a chunk of code that will get the 2d context for the program and this is how we do it:
    var canvas = document.getElementById(“canvas”);
    var ctx = canvas.getContext(“2d”);

 after we create the canvas and context environtment now we’re going to call the ball object or ball prototype and after that we paint or call everybit of code in this program repeatedly over and over, so we write it like this:
 var ball = new Ball(); //we call the ball object or prototype
   
    setInterval(function () {
        ctx.clearRect(0, 0, 700, 700); //we clear the background
        
        ball.draw();
        ball.move();
        ball.checkCollision();
      
        ctx.strokeRect(0, 0, 700, 700); //we create a stroke for the background
        ctx.
        ctx.fillRect(350, 350, 40, 40); //we create a white background
    }, 30);

And you need to remember that you need to write this inside the <script></script> tags or it will never work.
Now, the way you run it is you go to the location of you saved Ball.html, now right click on Ball.html and select open with>(you can choose any browser, but i recommend using firefox or chrome) then… tada!!!! you just make a moving bouncing ball in javascript inside html inside a browser, fun right this may seems much easier for you to understand rather than Java that is a strong typed language, well javascript is easier but each of them had their own advantage and disadvantages just find which one you’re comfortable with.

Javascript Warm Up

Hello, how are you doing? well i am fine indeed.In my last post we talk about Javascript, its history, the guy who made it, what is its used for, “var” and more. We also use Javascript to make our web browsing more unique by implementing animation and rendering, which is very cool and we also could do things like that if we learn Javascript. Now, we’re going to exercise JS or JavaScript, first I apologize if there is any mistake, any help or knowledge would be nice. Lets be done with the chit-chat and lets begin.

First we need to find a place to learn our javascript, and luckily Chrome serves us right! Chrome implemented a development console in the browser to do JS stuff, well firefox also did implement development console but lets just use chrome for the sake facility. Now, open your chrome and go to a blank page, if you don’t know how you just type about:blank in the address bar, well this is only optional but it’s easier to just see blank page first when writing our code, now let’s get to writing part!.


Now we got that sorted out let’s write some javascript! now you need to open the development console in the chrome, if you don’t know how this is how you do it, first open the google chrome control menu –> hover you mouse to more tools –> click development tools. That’s the long way to open the development  console, for simplicity sake just use Ctrl+Shift+I, got you! (haha). Now for a warm up, let’s just write the most simplest code that we’ll write today, just type 1 + 1 you don’t need to put an equal( = ) sign, just do that and hit ENTER and… that’s it you just write a succesful piece of code(By the way it should output the number 2). You may try to even billions of number calculating but the computer will always answer as long as you put the syntax right.


Remember when I say javascript is an Untyped programming language, it means that we could use “var” variables for any type of data we want, we could put number inside it, text, commas, special character etc… so if you want to make a position of an object that is keep changing you could use the “var” variable, for example ” var x1 = 200 ” as position 1 and we need to make it move 100 pixels to the right and we make a variable for that too ” var x2 = 200 ” and what we need to do is add those 2 variables together, so we do this “ x1 + x2 ” and it will output 400, it’s just the same when we calculate just the number only, like 200 + 200 but in this case we use the variable to keep track of the numbers that is keep changing.

Well it’s quite easy isn’t it?! it’s just a tini tiny part of code that you can write in javascript, now let’s move to a bit more advanced javascript coding, let’s output your name into the console of javascript world, first you make a variable called “name” and inside that variable you put your name in a string type, it should look like this ” var myName = “you’re name”; ” well it wouldn’t output anything at the moment but it’s just the shell for our next code. After that lets output our name to the world, type this in the console  myName + ” is awesome”; ” (you could put anything inside that string), here what it would output if I do it with my own name :
Rafif is awesome
tada! we made it isn’t it fun, (tell me how you did it with your own name).

Now let’s get to a more complex piece of code, we’ll output our name to the console 10 times automatically without human help, what do I mean without human help? it means that when we execute the code the computer will automatically outpute 10 of the requested output, so lets do that right now!. first you make your name variable just like before, second we’ll make a function that will output the information of our variable for 10 times, here is how we create the function:

var myName = function (howMany) {
for (var i = 0; i < howMany; i++) {
console.log( i + name + ” is awesome”);
}
};

after we create the function it’s time to execute or call the function!:

myName(10);

tada! you just make another piece of working code, by the way this is how my output looks like :

0Rafif is awesome
1Rafif is awesome
2Rafif is awesome
3Rafif is awesome
4Rafif is awesome
5Rafif is awesome
6Rafif is awesome
7Rafif is awesome
8Rafif is awesome
9Rafif is awesome

In javascript you could make such many other things, not only number calculating but you can make an animation or even a game! wooww! cool right? you could do such things like that if you learn it and understand it, and by it I mean javascript, so keep learning!.

Javascript

Hello, how are you doing? well i am fine indeed.In my last post we talk about an IDE, what is its definition, what is its used for, example for an IDE and more. We also use an IDE to make our MovingBall program, which is very cool and its very helpful for new coders. Now, we’re going to talk about JS or JavaScript, first i apologize if there is any mistake, any help or knowledge would be nice. Lets be done with the chit-chat and lets begin.

(The history of Javascript, Javascript was created in 10 days in May 1995 by Brendan Eich, you know… that Javascript wasn’t the first name that is used for this language, first time it was mocha and  in September of 1995 the name was changed to LiveScript, then in December of the same year, upon receiving a trademark license from Sun, the name JavaScript was adopted. This was somekind of a marketing move at the time, and because that Java being very popular programming language around then.) 

Since we talk a lot about Java, Javascript feeling a little bit lonely, but now the time has come for us to know Javascript.First, what is Javascript? for the most simple explanation, javascript is a programming language, just like java. Now for the long explanation, my understanding of javascript is, “a high-level, untyped programming language that is widely used for major web browser“alongside with the HTML and CSS, it is one of the three core programming language that is used for the technologies of world wide web.

Now let me explain this little by little, first javascript is a high-level programming language just like Java so what does this means? a high-level programming language is a programming language that is more closer to human language and further from the machine language so this makes the coder jobs easier because it is using a  language that is more understandable by human brain. Next one is the “untyped“, what does untyped means? of course it doesn’t means that you don’t need to type it to the computer but it means that, not like Java… Javascript does not use typing for its language, perhaps the word typing is familiar to you, well I already told you in my previous post (Java Variables) that Java is a strongly typed language this means that any data needs to be stored in the right variables, like Integer for number, String for words, and double for numbers that contains decimal.Well javascript doesn’t use that kind of thing, it only uses one variable for all kind of data which is “var” variable, it is a    for variables, right? did you catch that before me? so that is what it means to be an untyped programming language.

Javascript is widely used by the web, because it is the only scripting language that is supported by every major web browser approved by  ECMA(Internet Explorer, Firefox, Chrome, Netscape, Safari, Opera, Camino, etc). Moving on to the next thing, why do I say HTML, CSS and Javascript is the three core for world wide web? just see that everything you see in this page in you browser is made by those 3 programming language, can you guess what language do what? if you don’t let me tell you, first… can you see text inside the pages you see (of course right? or maybe the text is readed to you) well the text that you see is made by the HTML language, HTML is used to make any kind of text, second… can you see color or text style in this page? well CSS is the one that made such a thing, CSS is used to style a page, and now…. do you see a scrollbar in your page?(if you didn’t see it in this page try to go to a page that contains a lot of text)can you scroll it up and down?of course, and that thing uses Javascript to make it, Javascript is used to render or animate things.

You see those three programming language to be the structure to make web pages. Without one of those programming language a web page would be boring and not interesting, so if you want to make a web page make sure that you have all those 3 programming language implemented in your web page.

Conclusions

  • Javascript is “a high-level, untyped programming language that is widely used for major web browser”.
  • HTML, CSS and Javascript are the three core of world wide web.
  • A high-level programming language is a programming language that is more closer to human language and further from the machine language.
  • Javascript only uses one variable for all kind of data which is “var” variable so its considered to be an untyped programming language.
  • Javascript is the only scripting language that is supported by every major web browser approved by  ECMA

What is an IDE ?

Hello, how are you doing? well i am fine indeed.In my last post we make our way to create a program which is called a MovingBall, which is a program that makes a ball moves accross the screen and it could bounce off the side of the program and that is very cool and to make that program we use an IDE the IDE is called netbeans. Now, we’re going to talk about IDE, first i apologize if there is any mistake, any help or knowledge would be nice. Lets be done with the chit-chat and lets begin.

First, we need to know what an IDE is, and why is it created. well in my understanding, an IDE is a software application that provides the computer with facilities for computer programming or software development, it contains a code editor or a compiler that the user acces through a single GUI (graphical user interface). It is created for computer programmers and software developers because it has a compiler or code editor that helps coding syntax, what do I mean by help? well simply it could add a sugestion on what you’re going to type and this is very helpful for a beginner coder because we don’t need to search the internet or read books and do other complicated things, it will automatically add sugestion on what we’re suppose to type or what we want to type, an IDE will serves developing environment on one or multiple coding language.

You might ask, what is the functionality of an IDE? Just like I say before it has a compiler of code editor that helps coding syntax and there are many common features that is created in an IDE, such as debugging code or program and data structure browser etc… this is  very helpful for its user because it can make a user quickly execute an action without needing to go to another application so that help maximize the productivity of coding with the help of its GUI (graphical user interface) and UI (user interface).

what is an example of an IDE? there are 2 IDEs that i know, first one is Netbeans and the second one is Java Eclipse. each of them serves the developing environment with their own way but you’ll find it very similar in some way. if you want to check it out here is the link : Netbeans and Java Eclipse. You may try both of them at first, but as you feel more comfortable with an IDE you should choose one of them to avoid complicated problem when you switch from one to another ID, you should choose the one that you thinks more comfortable and you feel good with it.

here are 2 footages of both Netbeans and Java Eclipse:

Netbeans
Java Eclipse

  

 conclusions

  • an IDE is a software application that provides the computer with facilities for computer programming or software development, it contains a code editor or a compiler that the user acces through a single GUI (graphical user interface).
  • an IDE has a compiler or code editor that helps coding syntax.
  • an IDE will serves developing environment on one or multiple coding language.
  • common features in an IDE : debugging code or program and data structure browser etc…
  • an IDE makes a developer quickly execute an action without needing to go to another application so that help maximize the productivity of coding with the help of its GUI and UI.

Make a MovingBall in Java, Part 2

Hello, how are you doing? well i am fine indeed. Today we’re going to continue our exercise making a ball move in Java, well on the last post we manage to make a frame for the ball to move, today we’re going to make the ball “move”. before we begin, first i apologize if there is any mistake, any help or knowledge would be nice. Lets be done with the chit-chat and lets begin!


So, first you need to have netbeans if you don’t you can download it here: Netbeans. and you need to install a minimum of JDK 1.7.0, you can download it here: JDK. If everything is set to go, now you open your netbeans. lets continue from our last post which is this:

Make a method that paints an object in the program
Paint method

now the most important thing on this program is to have a ball moving on the screen righ? because we’re making MovingBall program, so to make a ball moving on the screen we need to make a method, a method is a piece or a chunk of code that will execute when you call upon it, now you put ball.moveBall(); before the “ball.repaint();” the reason that we put ball. before the method because it’s for the ball object. at first it’s going to give you an error, you see that little lightbulb thing with red warning that means it is an error, why?? it’s because you haven’t created the method yet, what you did is you call the method without making it first, understand?? now let’s make the method, you need to write this :
public void moveBall()
{
       

} 

you write it before the paint method, and it should look like this:

now the most importand thing is to draw the ball, we need to draw the ball in a method, can you guess what method we should use to draw the ball? correct, the paint method. Now you might ask “why didn’t we use the moveBall method instead?” well the moveBall method is a method to make the ball “move” and not to draw the ball, instead paint method is a method to draw an object, since logically “paint”, duhh!.

here is the all the code you need to write to draw a ball : public class MovingBall extends JPanel{
   
    int x = 0;
    int y = 0;
   

    int diameter = 30;   

    public void moveBall()
    {
   
    }
   
    @Override
    public void paint(Graphics g)
    {
        super.paint(g);
       
        g.fillOval(x, y, diameter, diameter);
    }

 and when you run it is should look like this:

congratulations, you just draw a ball in a screen, cool!!.
now let me explain the steps that is needed to draw a ball like that.First we need to make a variable that contains the positions of the ball and the diameter of the ball, second we draw the ball.Now, I already explain what is variable in my other post, chek it out: Variables. Specifically we need to make an integer variable, for what you might ask, well it’s to determine the ball position.
“well why didn’t we determine the position with whole number when we draw the ball in this line  “g.fillOval(x, y, 30, 30);” ” you could use a whole number like 0, 1, or 2 to determine the position, but consider this, what program are you making? does the ball move or it doesn’t? if the ball moves we need to make a variable to store the position of the ball, because!! the position keeps changing automatically, there is no way you could do it without a variable that store the position of the ball. Now that the problem is settled, lets continue, now we need to make the ball move. Just like I say “the position keeps changing automatically”, we need to make the ball move on itself, the way to do is pretty easy its just two line of code which is:     int x = 0;
    int y = 0;
    int diameter = 30;


    public void moveBall()
    {
        x++;//this line
        y++;
//and this line

    }
   
    @Override
    public void paint(Graphics g)
    {
        super.paint(g);
       
        g.fillOval(x, y, diameter, diameter);
    }

can’t do images because the ball wouldn’t move, lol.

then you run it! well done *clap, clap* you just make a ball move! great right? now there is one problem, can you tell? the ball move through the side of the program! Grrahhh. we really really need to fix this problem!. now we need to create a barier, we write this in the code. We somehow need to calculate, when the ball hit a side of the program it should come back with different velocity. we can do it like this:  int x = 0;
    int y = 0;
  
    int diameter = 30;
  
    boolean moveDown = true;
//if we didn’t create this the ball will never bounce back because the
                                                  previous velocity will still be applied
     boolean moveRight = true;//if we didn’t create this the ball will never bounce back because the
                                                   previous velocity will still be applied
  
    public void moveBall()
    {
        if(x == getWidth() – diameter)
        {

              moveRight = false;  //if the x of the ball hit the right side of the program then moveRight
                                                 is false this will make it move to the left
        }
        else if(y == getHeight() – diameter)
        {
              moveDown = false;
//if the y of the ball hit the right side of the program then moveDown
                                                 is false this will make it move Up
        }

        }
      
        if(x == 0)
        {
        
          moveRight = true; 
//it’s just the same with the one above but it’s flipped to it’s original situation

        }
        else if(y == 0)
        {
              moveDown = true; 
//it’s just the same with the one above but it’s flipped to it’s original situation 
        }
      
        if(moveDown)
        {
         y++;
//this situation is if moveDown is true, then, y position will be moved 1 pixels each frame/time;
        }
        else if(!moveDown)
        {
         y–;
//this situation is if moveDown is false, then, y position will be moved1 pixels backwards each
                  frame/time
        }
      
        if(moveRight)
        {
         x++; 
//this situation is if moveRight is true, then, x position will be moved 1 pixels each frame/time; 
        }
        else if(!moveRight)
        {
         x–;
//this situation is if moveRight is false, then, x position will be moved 1 pixels backwards each
                 frame/time
 }

    }

after you write that on you program codes, you run ! BOOM!! you have a fully working moving, bouncing ball program in Java, congrats!.
After this exercise I hope you will get more comfortable and will more understand the Java world of Coding We have finished our exercise, so see you in another post!.

See part 1

Make a MovingBall in Java, Part 1

Hello, how are you doing? well i am fine of course!. Now lets get to the fun part of Coding, making a Program, now we’re going to make a moving ball in java. I know that program like that is small and doesn’t contain anything much spesial but we need to get an exercise at Coding. For this “tutorial” i will tell you step by step on how to make a ball moving in your program, now I will use netbeans for the IDE. Does netbeans and IDE sound familliar for you? maybe some of you are, but for you who don’t netbeans is an IDE, an IDE is a development environment for programming, we talk about it in my last post which is Coding Syntax. first if there is any mistake i apologize and any knowledge or help would be nice. now forget the chit-chat and lets begin!


So, first you need to have netbeans if you don’t you can download it here: Netbeans. and you need to install a minimum of JDK 1.7.0, you can download it here: JDK. If everything is set to go, now you open your netbeans, after you open netbans it should look like this :
and because you don’t have any project yet you will have empty netbeans like this.                              

netbeans without any projects or empty netbeans
The netbeans appearance

after you open it you need to make a new project, which is a java project the way to open it is you click “file->New Project” after that you choose the folder “Java” and choose “Java Application” after that click next and now there will be a box that you need to fill, now you fill it with your project name, I will use “Moving Ball” you can name it whatever you want, the project location and the project folder.
After you fill that box you click finish and it should look like this :

empty project whitout any extra code
Netbeans with project inside it

Now we code! first we need to make the programe frame, it’s called JFrame because it creates the program frame. We type this code in the                                                   public static void main(String[] args)
{

}
you write a code like this:
 JFrame jframe = new JFrame(“moveBall”);
you can name it whatever you want inside the (“”) I’m going to use moveBall. And now you need to import it, if you see a little lightbuld click it and an option should apear and you should choose the “add import for javax.swing.JFrame”

import the JFrame by clicking the Add import for javax.swing.JFrame
Import JFrame

After we create the frame now we set the properties of the frame like, size, visibility and default close operations and we also need to make a ball object from MovingBall but it’s not going to contain anything yet, so it should be like this:
        jframe.setSize(500, 500);
        jframe.setVisible(true);
        jframe.setDefaultCloseOperation(jframe.EXIT_ON_CLOSE);

        MovingBall ball = new MovingBall();
  
after you type those codes you can run the program by clicking “run->run project (MovingBall)” or you could just press “f6”. the program should run and a frame should pop up like this:

An empty program window with 500 by 500 size
Program Window

well it’s empty but now we’ll write the real program codes, now we need to extends the MovingBall class to a JPanel. After we extends the MovingBall class to a JPanel next we need to make a function that draw the objects and a while loop that keeps repainting the object and background no matter what, wel also write a thread and sleep for 10 ms and many other things and we write it like this:
public class MovingBall extends JPanel{
    public void paint(Graphics g)
    {
        super.paint(g);
    } 

    public static void main(String[] args) throws InterruptedException {
    JFrame jframe = new JFrame(“moveBall”);
      
        jframe.setSize(500, 500);
        jframe.setVisible(true);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
        MovingBall ball = new MovingBall();

        jframe.add(ball);
      
        while(true)
        {
            ball.repaint();

            thread.sleep
        }

    }

Make a method that paints an object in the program
Paint method

 and you need to import the “Graphics” just like you import JFrame the only thing different is you need to choose the option “add import for java.awt.Graphics”.
Now in the next part well create the method to make the ball “Move” and finish our program.

see part 2

Coding Algorithm

Hello, how are you? i’m well fine in this sunday, first i apologize if there is any mistake any help or knowledge would be nice. From our last journey in the world of Coding syntax we take a little peak about syntax and it’s example we learn that variables are also part of the Coding syntax, and we also have IDEs that help us to start coding easily with it’s sytax helper. Now we’re about to talk the #3 thing which is the algorithm of Coding.

Have any of you here has heard about algorithm? if yes, give comment about how you define algorithm yourself. In the real world we do stuff like cycling to home, eating a cheese bread, travelling  etc… do you know or do you realise that what you’re doing does contain algorithm in it? well maybe some of you does realise, but for you that don’t here is my explanation,
algorithm is a set of steps or any problem solving to accomplish somekind of task.

Now let’s get back to one of our life routines, the cycling routines, cycling does contain algorithm in it can you guess how? here is how:

  1. get a bicycle
  2. start cycling
  3. cycle along the road that you know (or if you have a map than this is only optional)
  4. arrive at home

those 4 step for cycling to home is an algorithm, how? algorithm  is a set of steps to accomplish a task well those 4 are the steps to accomplish the task, we could make question to determine whether it is an algorithm or not. 1st what is the task? cycling to home, 2nd what is the steps? those 4 steps above. Now how do you feel, do you get more comfortable at algorithm or more confused? I hope you get more comfortable in this.

Well in Coding, it’s just the same but with a little change, here how it goes: algorithm in Coding or programming is a set of steps or any problem solving for a computer program to accomplish somekind of task. You see, it has the same definition ( without the “computer program” sentence), easy right? algorithm also puts the “Science” in “Computer Science” because it is rather considered science in computer. Now let’s make one of our own algorithm in solving something in Coding, let’s say that we want to make a game, what would be the algorithm, let’s just make it like this :

  1. create a name for the game = pong
  2. gather the idea for the game
  3. start writing codes
  4. (if there is an error in the codes, bonus algorithym) :
    1. find out what is the error
    2. find out the way to fix the error
    3. fix the error

  5. finish game and start playing or sell it 

Just like the cycling, we have task the steps and the problem solving, so we can say it’s an algorithm

    There are often many other different ways for a computer program algorithm to accomplish a task, each of the algorithm has an advantages and disadvantages to solve a problem. One of the most used algoritm is Sorting, you might wonder what is Sorting? here is a quick definition of Sorting in the wikipedia

    Sorting is any process of arranging items systematically, and has two common, yet distinct meanings:

    1. ordering: arranging items in a sequence ordered by some criterion;
    2. categorizing: grouping items with similar properties. “

    those definition is obtained from wikipedia

      Now i’m going to tell you 4 types of Sorting:

      • Bin sort
      • Merge sort
      • Bubble sort
      • Shell sort
      • Quicksort

      Now just imagine that you have 1.5 million integer between 1 and 15 and you need to sort those integer, well the right sorting algorithm to use is Bin sort, if you would have 2 million book titles for the best algorithms it goes with the Quicksort.

      Conclusion 

      • algorithm is a set of steps or any problem solving to accomplish somekind of task.
      • algorithm in Coding or programming is a set of steps or any problem solving for a computer program to accomplish somekind of task
      • example of algorithm: Sorting
      • 5 example of sorting:

        • Bin sort
        • Merge sort
        • Bubble sort
        • Shell sort
        • Quicksort

      Give comment, help you and me grow our knowledge together. Tips, Help and Knowledge would really be appreciated. Thank you, see you on another Article.

      😀