Tuesday, December 29, 2015

Static: what are the static variable and the static method?

We have a question here: what are the static variable and the static method? We will see what the static variable is and what the static method is in this post.

Definition


static variables: variables allocated for a class. Data stored in static variables is common for all the instances of that class.
static methods: methods allocated for a class. Static methods are common for all the instances of that class, so you don't need to make an instance to use that static method.

non-static variables:  variables allocated for an instance. Data stored in non-static variables is NOT common for all the instances of that class. The data stored changes its value depending instances even if they are made from a same class.

Static variables


At first, we will see an ordinal code without using static:

public class Test { public static void main(String args[]) { SubTest subtestone = new SubTest(); subtestone.a = 1; subtestone.b = 2; SubTest subtesttwo = new SubTest(); subtesttwo.a = 3; subtesttwo.b = 4; System.out.println(subtestone.a); System.out.println(subtestone.b); System.out.println(subtesttwo.a); System.out.println(subtesttwo.b); } } class SubTest{ int a; // these variables are not static int b; }
the result

The integer-variables "a" and "b" change its values by being assigned values. This is because the variables are allocated for the object, not for the class, thus, the each variable can have a different value every time we make a new object.

Now we will see how static variables work:

public class Test { public static void main(String args[]) { SubTest subtestone = new SubTest(); subtestone.a = 1; subtestone.b = 2; SubTest subtesttwo = new SubTest(); subtesttwo.a = 3; subtesttwo.b = 4; System.out.println(subtestone.a); System.out.println(subtestone.b); System.out.println(subtesttwo.a); System.out.println(subtesttwo.b); } } class SubTest{ static int a; //These variables are static static int b; }

the result

This time, all of the integer-variables "a" and "b" are treated as "3" and "4". This is because the variables are allocated for the class, not for the object, thus, the each variable can NOT have a different value every time we make a new object. Data stored in static variables is common for all the objects( or instances ) of that class. See the result above.

But, needless to say, if these static variables are allocated for the class which has the main method, the variables "a" and "b" change its values as follows:
public class Test { static int a; //These variables are static. static int b; public static void main(String args[]) { a = 1; b = 2; System.out.println(a); System.out.println(b); a = 3; b = 4; System.out.println(a); System.out.println(b); } }

the result


Static variables are allocated for the class, not for the object (or the instance), so even though they are static variables, they change its values depending on what value we assign to the variables.

Static methods

Static methods have same concept as static variables. Static methods are common for all the objects( or instances ) of that class.
At first, we will see an ordinal code without using static:

public class Test { public static void main(String args[]) { SubTest subtestone = new SubTest(); subtestone.showValue("My name is John."); SubTest subtesttwo = new SubTest(); subtesttwo.showValue("My name is Alice."); } } class SubTest{ void showValue(String word){ // This method is not static. System.out.println(word); } }

the result

The method is not static one, so the method is allocated for the object which we've created.

Now we will see how static methods work:
public class Test { public static void main(String args[]) { SubTest.showValue("My name is John."); SubTest.showValue("My name is Alice."); } } class SubTest{ static void showValue(String word){ // This method is static. System.out.println(word); } }

the result is same as the previous one.

The method is static one, so the method is allocated for the SubTest. Therefore, to use the static method, we don't need to make a new object. We can directly access to the static method of SubTest class.


Sunday, December 20, 2015

Constructor

How the constructor works? In C#, all steps inside a constructor are executed at first as you make an object of class.

We will see an example:
using System;

namespace Tester {
    public class Program {
        public static void Main(string[] args) {
            Console.WriteLine("Hello, world!");
            Calc calc = new Calc();
        }
    }

    public class Calc {
        int x;
        int y;
        int z;

        public Calc() {
           x = 5;
           y = 7;
           z = x * y;
           Console.WriteLine(z);
        }
    }
}

the result

We didn't use any method of Calc class in the main method to show the result of the calculation, but the result of this program shows "35" after Hello World.

This is because the Calc class has a constructor which calculates and show the result.

This constructor showed the result, so we didn't need to write any method for the calculation and showing the result. Everytime we make an object of the class, the constructor of the class is executed.

Without the constructor, this program would be as follows:
using System;

namespace Tester {
    public class Program {
        public static void Main(string[] args) {
            Console.WriteLine("Hello, world!");
            Calc calc = new Calc();
            calc.CalcA();
        }
    }

    public class Calc {
        int x;
        int y;
        int z;

        public void CalcA() {
           x = 5;
           y = 7;
           z = x * y;
           Console.WriteLine(z);
        }
    }
}

We can see, in the main method, calc.CalcA (CalcA method of Calc class) is being used.

We can compare how the constructor simplifies the program.

By the way, the constructor always have a same name as the class which contains the constructor. Plus, constructor is used as "void" (returns no value). On the other hand, the method doesn't have a same name as the class which contains the method. The method usually has a different name from the class.

Wednesday, December 16, 2015

break and continue

When you press "q", you can exit the infinite loop in this code:

using System;

class test
{
        static void Main(string[] args)
        {
                char myKey;
                for ( ; ; )
                {
                    // Read one character
                    myKey = (char) Console.Read();
                    if (myKey == 'q') break;
                }
        }
}


"continue" is used inside a loop to skip certain steps as follows:
using System;

class test
    {
        static void Main(string[] args)
        {
            int i = 1, n, sum;

            sum = 0;

            while (i <= 2) {
                n = int.Parse(Console.ReadLine());
                if (n < 0)
                continue;  //If n is less than 0, exit from loop
          
                sum = sum + n;
                i++;
            }
      
            Console.WriteLine("sum = " + sum);
            Console.ReadKey();
        }
    }

Saturday, December 12, 2015

The Product Over All Primes

The Product Over All Primes is  4π2 according to this paper.
http://link.springer.com/article/10.1007%2Fs00220-007-0350-z 
Abstract
We generalize the classical definition of zeta-regularization of an infinite product. The extension enjoys the same properties as the classical definition, and yields new infinite products. With this generalization we compute the product over all prime numbers answering a question of Ch. Soulé. The result is 4π2. This gives a new analytic proof, companion to Euler’s classical proof, that the set of prime numbers is infinite.

If you are interested in this kind of math, 1+2+3+4+.... Wikipedia.en is fun to read.

Multi-threaded programming

We will see how to use the multi-threaded programming in C#. See here to see what the multi-threading is.

the code:
using System;
using System.Threading;

namespace ThreadTest
{
    class Program
    {
        public static void Main() {
                Thread thrd1 = new Thread(new ThreadStart(MyThread.Thread1 ) );
                Thread thrd2 = new Thread(new ThreadStart(MyThread.Thread2 ) );

                thrd1.Start();
                thrd2.Start();
              
                Console.ReadKey(true);
        }
    }
  
    public class MyThread {

        public static void Thread1() {
                for (int i = 0; i < 10; i++) {
                        Console.WriteLine("Thread1 {0}", i);
                }
        }

        public static void Thread2() {
                for (int i = 0; i < 10; i++) {
                        Console.WriteLine("Thread2 {0}", i);
                }
        }
    }
}

To use the multi-threading, you have to make an object of class Thread. In this program, "thrd1" and "thrd2" are the objects that we have made. After making such thread's object, ".Start()" is used to start the thread. In this program, "thrd1.Start();" and "thrd2.Start()" are written to start the threads that we have made.

the result

The each thread works independently, so the result above shows the two threads that we have made are working simultaneously.

In the first program, the thread function (of MyThread class) was static function. But the thread function doesn't need to be static. If we will NOT use the thread function as static, we must make an object of Thread class and use it as follows:

using System;
using System.Threading;

namespace ThreadTest
{
    class Program
    {
        public static void Main() {
                MyThread thrdobj = new MyThread();
          
                Thread thrd1 = new Thread(new ThreadStart(thrdobj.Thread1 ) );
                Thread thrd2 = new Thread(new ThreadStart(thrdobj.Thread2 ) );

                thrd1.Start();
                thrd2.Start();
              
                Console.ReadKey(true);
        }
    }
  
    public class MyThread {

        public void Thread1() {
                for (int i = 0; i < 10; i++) {
                        Console.WriteLine("Thread1 {0}", i);
                }
        }

        public void Thread2() {
                for (int i = 0; i < 10; i++) {
                        Console.WriteLine("Thread2 {0}", i);
                }
        }
    }
}

References

"Multithreaded Programming Using C#", Code project, seen on 12th December 2015

Wednesday, December 2, 2015

Let's make life game 2

We already downloaded Swing designer in the last post. So now we can make an actual GUI application with SwingDesigner.

We will start making life game in this post. 

At first, we will make a display for the game. Start your Eclipse. 
Then make a Java project. Whatever is good for the name. I chose "LifeGame" for the project name.



Then click "Finish". We will see new project "LifeGame" was added to your project explorer.

We will make a new class for the main window. Click "File" from the menu bar and select "New", then "Others".



You will see the following display. Select "WindowBuilder".

Select the "Swing Designer", then"Application Window":

Whatever name is good for this. I wrote "MainWindow" for the name. Then click "OK".


You will see the following class was added to the project. Click "Design" now.

You will see an empty window. We will customize this empty window and make it LifeGame from now on.

Click the play button.


You can see the empty window which you've created is actually working.