Learntofish's Blog

A blog about math, physics and computer science

Interview with Linus Torvalds

Posted by Ed on October 6, 2018

Check out this fascinating interview with Linus Torvalds, the inventor of Linux and Git:

Posted in programming | Tagged: , , | Leave a Comment »

Clean code posts by Anas Shekhamis

Posted by Ed on October 1, 2018

Check out these posts on Clean Code by Anas Shekhamis.

Posted in programming | Tagged: | Leave a Comment »

Data Science and Artificial Intelligence with Python

Posted by Ed on September 30, 2018

Check out Chris Albon’s website on data science and artificial intelligence containing many examples in Python.

Posted in programming | Tagged: , , | Leave a Comment »

Python style guide

Posted by Ed on September 30, 2018

Check out this style guide for Python from The Hitchhiker’s guide to Python. It recommends installing pycodestyle and autopep8 to format your code.

Posted in programming | Tagged: , | Leave a Comment »

Java static nested class vs inner class

Posted by Ed on September 30, 2018

In Java there are two types of nested classes:

  1. If the nested class is declared static, then it is referred to as static nested class.
  2. If the nested class is declared non static, then it is referred to as inner class.

The following code shows a class OuterClass that contains both a static nested class and an inner class:

public class OuterClass {
	private String outerClassAttribute = "Hello from outer class.";
	
	static class StaticNestedClass {
		public void printHello() {
			System.out.println("Hello from the static nested class.");
		}
	}

	class InnerClass {
		public void printHello() {
			System.out.println("Hello from the inner class.");
		}
		
		public void printOuterClassAttribute() {
			System.out.println(outerClassAttribute);
		}
	}

}

Here is an example that illustrates how to instantiate objects of the respective classes. Notice that the inner class has access to the member variable outerClassAttribute of the outer class.

public class OuterClassExample {
	public static void main(String[] args) {
		staticNestedClassDemo();
		innerClassDemo();
	}

	public static void staticNestedClassDemo() {
		OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
		nestedObject.printHello();
		System.out.println();
	}

	public static void innerClassDemo() {
		OuterClass outerObject = new OuterClass();
		OuterClass.InnerClass innerObject = outerObject.new InnerClass();
		innerObject.printHello();
		innerObject.printOuterClassAttribute();
	}

}

And here is the output:

Hello from the static nested class.

Hello from the inner class.
Hello from outer class.

References

  1. Nested classes (from the official Java tutorial by Oracle)
  2. Java inner class and static nested class (StackOverflow discussion)

Posted in programming | Tagged: , , | Leave a Comment »

Endure boredom and increase attention span

Posted by Ed on September 30, 2018

Here is an interesting video by Derek from his channel Veritasium on the scientific benefits of boredom.

And here is a video by CollegeHumor that I found via the reddit thread How can I get my attention span back? When watching it you may feel the urge to jump to different website just to saturate the hunger for new information.

I’m guilty of this. Last week I caught myself having 50 tabs open in the browser! After realizing this problem I have deleted several reddit subscriptions.

Posted in health | Leave a Comment »

Java Eclipse IDE shortcuts

Posted by Ed on September 29, 2018

Here are some shortcuts for the Eclipse IDE that I use when programming in Java:

Ctrl + Space : Auto-Complete, e.g. type Hash and hit Ctrl + Space. This will open a box with suggestions you can choose from to auto complete.

Ctrl + Shift + T : Search for a type / class. Example: You want check the code for the class SimpleDateFormat. Hit the shortcut and enter SimpleDateFormat. The box then returns suggestions for classes with this name (there could be multiple classes with the same name, but in different packages). It even suffices to enter the capital letters in SimpleDateFormat, i.e. you search for SDF.

Ctrl + Alt + H : Display call hierarchy. Mark a method or class and hit the shortcut. This will display the places where your method or class is used.

Ctrl + D : Deletes the line that your cursor is in.

Alt + Shift + A : Activates multi line editing. Hit the shortcut, hold shift and use the directional keys to mark the lines you want to edit. Hit the shortcut again to get out of the multi line editing mode.

Ctrl + 1 : Displays suggestions. Example: you want to get the size of a list called myList and store it in the variable n.

final List myList = new ArrayList();
myList.add("Alice");
myList.add("Bob");
myList.add("Eve");

If you now type

n = myList.size();

and hit the shortcut Ctrl + 1, then it will suggest the correct type for n.

Ctrl + Shift + O : Organizes the imports. Example: You get an error because of a missing import. Then you can use this shortcut to use the required imports. You can also remove unused imports with this shortcut.

Ctrl + Shift + F : Formats the code.

Ctrl + O : Displays methods and attributes of the current class.

Ctrl + L : Go to line.

F3 : Mark a variable or class name and hit this shortcut to jump to its declaration.

References

Vogella Eclipse PDF cheatsheet

Youtube video by Team MAST

Java Eclipse Shortcuts: Tips and Tricks for the Eclipse Java IDE

Quora article

Posted in programming | Tagged: , , | Leave a Comment »

Eclipse auto suggestion without ctrl-space

Posted by Ed on September 23, 2018

You can make the Eclipse IDE auto suggest without hitting ctrl + space as follows:

Go to

1. Window -> Preferences
2. Java -> Editor -> Content Assist
3. Under the section auto-activation enter the following for the Auto activation triggers for Java:

._abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

References
https://stackoverflow.com/a/1959967/4019402
http://ananthchellathurai.blogspot.com/2013/10/how-to-make-eclipse-autocomplete-to.html

Posted in programming | Tagged: , , | Leave a Comment »

Untrack files in git

Posted by Ed on September 23, 2018

Files in git can either be

1. tracked
2. untracked.

Tracked means the file has been committed and is known to git. A file is untracked if git sees it for the first time.

In Egit (Git for the Eclipse IDE) the status of a file is shown with different icons.

Now, suppose you committed a file, but you later realize that you shouldn’t have done it, and that instead you want to ignore the file (by adding it to the gitignore file).

In git you cannot ignore a file that has been tracked. You first have to untrack it.

Once the file has been untracked you can then ignore it.

References

Pro Git book, written by Scott Chacon and Ben Straub.

Icons in Egit (Git for Eclipse) show status of file.

Posted in programming | Tagged: , | Leave a Comment »

Initialize ArrayList in Java

Posted by Ed on September 22, 2018

Initialize array

Here is an example on how to initialize an array:

String[] names = new String[] {"Alice", "Bob", "Eve"};

How can we initialize an ArrayList in a similar manner?

Initialize an ArrayList

We first create an array, and then convert it to an ArrayList as follows:

1. We create an array.
2. Pass the array to the function Array.asList() to get a list of fixed size.
3. Pass the resulting list to the ArrayList constructor

Here is an example:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test {

	public static void main(String[] args) {
		final String[] names = {"Alice", "Bob", "Eve"};
		
		final List namesList = new ArrayList(Arrays.asList(names)); 
		
		// alternatively use
		// final List namesList = new ArrayList(Arrays.asList("Alice", "Bob", "Eve"));
		
		System.out.println(namesList);
		
	}
	
}

You can also add elements one by one.

Posted in programming | Tagged: , | Leave a Comment »