Saturday, November 26, 2016

Java and C


Java and C are two of the most common programming languages. When a person begins to learn computer science, he or she will learn either Java or C. We learn Java in CMSC 150, while one of my friends learns C++ in the introduction course of computer science of his university.

Both Java and C are imperative languages. The difference is that Java is object oriented, whereas C is function oriented. Unlike C, however, C++ supports object-oriented programming.

Compared with C, Java is more portable, as "write once, run anywhere". Different operating system can use JVM to interpret Java codes, but some C codes can only be executed in certain operating systems. This is because Java is interpreted language, and all codes will be transformed to bytecode and executed by JVM, whereas C is compiled language, and codes have to be translated by compiler to some codes that can be understood by the operating system first and then be executed.

Let’s compare Java and C specifically. See the following hello world codes from http://introcs.cs.princeton.edu/java/faq/c2java.html.
Java:
public class HelloWorld {
   public static void main(String[] args) {
       System.out.println("Hello");
   }
}

C:
#include<stdio.h>
int main(void) {
   printf("Hello\n");
   return 0;

}

---------------------------------------------------------------------------------

Writing References:
1.http://introcs.cs.princeton.edu/java/faq/c2java.html
2.http://durofy.com/10-major-differences-between-c-and-java/
Picture References:
1.http://code.rohitink.com/wp-content/uploads/2013/06/1301825696_183475245_1-Pictures-of-C-Java-for-10th-11th-12th-student-of-all-syllabus.jpg

Friday, November 18, 2016

ArrayList


When programming, we often need to use arrays. Besides creating an array, the most common method we use so far, we can also use ArrayList as an alternative way. The ArrayList class belongs to java.util, so we need to use import if we want to use ArrayList. The following are some advantages of using ArrayList instead of standard arrays.

To create an array, you should first know how many elements you would include in this array, because once the array is created, you cannot change its length without making a new reference. However, an ArrayList can either grows and shrinks as you need. Although you also need to create an ArrayList with an initial size, this size can easily increase as the number of elements exceeds the size, or decrease when you remove an element from the ArrayList. Using ArrayList, you won’t bother to keep track of the last slot in use, because the last slot in an ArrayList must be list.size()-1;



As what we learn in the class, ArrayList is generic. Thus, the parameter of ArrayList must be nonprimitive type. This means that if we create an ArrayList class, we should write code like this:
public class ArrayList<Integer> We cannot write such code as public class ArrayList<int>

---------------------------------------------------------------------------------

Writing References:
1.Barron's AP Computer Science
2.https://www.tutorialspoint.com/java/java_arraylist_class.htm
Picture References:
1.http://www.trustingeeks.com/wp-content/uploads/2014/12/Arraylist-example-in-java.png
2.http://www.codeproject.com/KB/vb/ArrayList2File/multiDimArrayList.png

Friday, November 11, 2016

Interface


Last week, I introduced abstract class in my blog post. This time, I will introduce interface, which is somewhat similar to abstract class and inheritance. An interface is a collection of related methods, but these methods do not have implementations. They only have return type, method name, and parameter. In addition, all of the methods in an interface are public and abstract, so you don’t have to include modifier.

Unlike inheritance, which defines is-a relationships, an interface can actually be implemented by classes that represent very different objects. However, these objects must share some capabilities or features that are expressed in the methods of the interface. For example, an interface called Runnable includes a method Run, which stimulates object’s running. Classes that can implement this interface may be Dog, Car, Person, etc. However, the class Bird cannot implement this interface because birds do not run but fly.

To be more clear, let’s see the following code.
public interface Runnable {
   void Run();
}

public class Dog implements Run {

   


So why do we use interface in programming? Interface provides a framework of behavior for any class. It also has the advantages suggested in the picture.

---------------------------------------------------------------------------------


Writing References:
1.Barron's AP Computer Science
Picture References:
1.https://www.prsblog.com/wp-content/uploads/2016/06/interface-in-java.png
2.http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/9a9e6f/interface-keyword-in-java/Images/interface%20in%20Java.jpg