Power to Build
Advertisements

Java 9 update…


Java 9 is (still) coming! Here is another post from the author that gave us "Ultimate Guide to Java 9" recently.

https://www.sitepoint.com/inside-java-9-part-i/

Here are some features that caught my attention in Java 9:

Multi-version jar – it actually contains class files for different versions in a single Jar. Interesting. If you ever tried to run newer classes with older JVM, you would run into problem. With this, you can create class files for different versions and keep them separate.

The more I look at it, Java is slowly absorbing the "features" it refused to include from C/C++, in the first place. Wouldn’t a preprocessor + JIT better to handle this?? In those days, the programs had to have small foot print both in memory and disk space. With both available cheap, we seem to be able to splurge a little. I cannot imagine running Java on the same machine I ran my small C programs!

Do you really have to call it java9?? The Java runtime executable seem to be called that. Sounds like we are moving more and more like PowerBuilder, where you need to have the exact version of PBVM to corresponding version code. Every version of PB had to have its own executable PB10, PB125 etc.

Here is another blog that lists 5 important features in Java 9.

Here is a complete list of features in Java 9 in the Open JDK release:
http://openjdk.java.net/projects/jdk9/

There are mentions about security, logging, graphics etc. There is a Jshell, to run/test Java commands interactively – I think it’s getting a facelift from an earlier version. I see these are new features, but nothing sounds earth shattering or change the way I would program in Java. Hope to see some more before the final release comes out in 2017?

According to this site, we have another 264 days before we see the final release. Lot could change in the time we wait for it. This blog post talks about why Java 9 is delayed again?! Did you know Java 9 project is called Project Jigsaw?

Advertisements

Gotcha: Java JScrollPane


So, you are working with Swing. You build a small window with a Text box (JTextArea or JTextPane) like listed here. Everything works fine. You show text nicely wrapped in the text box.

Suddenly one day, the text pasted on to the Text box is longer than what’s in the view (View port). Sure, you say, I have seen this in other platforms. Just scroll, right?. Wait, there is no Scroll bar. Oops! Can you scroll with a up/down arrow key like in Windows? Nope! No luck! If you are coming from other IDE environment like me (PowerBuilder, Visual Studio – VB6/VB.Net or even C#) this will be a shocker.

In PowerBuilder, I would slap on Multi-line Edit on to a Window. Check the checkboxes for turning on Horizontal and Vertical Scrollbars. Set the text into the box. Bingo!

In PB and other such platforms, you don’t normally see the scrollbars, but the
Scrollbars appear magically, when the text is longer than the viewport, if you opted for it. You can also set Autoscroll instead of scrollbars, where user can scroll endlessly using the arrow key,until the memory runs out.

For e.g.,

In PowerBuilder, the code will look like this:

type mle_1 from multilineedit within w_sheet
 ....
 ....
 boolean init_hscrollbar = true
 boolean init_vscrollbar = true

end type

In C#, the scrollbars are added by default. If you want only Vertical bar only, you can set it like this:

      this.richTextBox1.ScrollBars =
             System.Windows.Forms.RichTextBoxScrollBars.Vertical;

This wraps text in the text box horizontally.

As you can see, scrollbars are just properties of the Text box cotrol. But then all the languages I mentioned are tightly coupled with the OS and the OS API has a lot of System functions that can be called as seen in C# code above.

None of those here in Java. Java tries to be everything for everyone. It has to mimic a lot OS level functions within, so it can run anywhere. In Java (here Swing) everything is available, but you have to go get it.Often times, you get a lot of options available to do the same thing, you have to find and use the right solution for you. And the Java solution is always like, if I can do this for this object, can I also do it for that other object? Thus, they try to extract the Scrolling functionality on its own and make it available for any component like Text box, or a JTable. You can even embed a pane (JPanel) onto a Scroll Pane. This approach of “decorating” any Visual object with Scroll Pane follows the Decorator Pattern. Here is a nice course material explaining how this is done.

Let’s see how the scrollbars can be added to a text box in Java:

To create a window, you create a JFrame or some kind of container (Equivalent to a Window in Windows environment). You plop a JPanel on to it, so you can contain all the controls together. Then you add a JTextArea on to the JPanel for your text box.

Wait, did you want scrolling capability? That’s a different pane; just add a JScrollPane to the JPanel, then add the JTextArea inside the JScrollPane. It is very hierarchical and very Object oriented. You can easily replace JTextArea with a Jtable or Jpanel to get scrollbar capability elsewhere.

Now, for the Gotcha part. Did I say, add the JTextArea to the JScrollPane? Wrong thing to do. This is what you do, typically add a component to a container/component using add method, but not so with JScrollPane. Here is why:

JScrollPane has a component called “View Port” already added to it. We can show a single component inside this “View Port”. So, essentially you will have to put the JTextArea in the View. Here is a diagram that may help you:

JScrollPane-1.gif

To do this in a Java way, you simply pass your component to the Constructor of JScrollPane. If you didn’t, then you will have to set the component to the View Port using, you guessed it,

scrollPane.setViewPortView(component)

or you can do it round about using

scrollPane.getViewPort.SetView(component)

But then, you have surely seen an add method in the Javadocs for JScrollPane?! Sure, it does have one, as it inherited it from JContainer. Like I said, JScrollPane already has a component inside it – that’s the ViewPort component. So, if you use the add method, you are replacing the Viewport with your component. That’s why if you added your component directly (by mistake) to the JScrollPane, your view port is gone and it will be grayed out.

So, here is the finished Java sample for a Text box with (only) Vertical Scrollbar:

import java.awt.*;
import javax.swing.*;

public class TextWithScrollbars
{
   public static void main(String[] args)
   {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(400, 600);

      JTextArea area = new JTextArea(40, 60); // 40 rows, 60 columns
      JScrollPane scrollPane = new JScrollPane(area);
      scrollPane.setHorizontalScrollBar(null);

      Container contentPane = frame.getContentPane();
      contentPane.add(scrollPane, BorderLayout.CENTER);

      frame.setVisible(true);
   }
}

The line that reads,

scrollPane.setHorizontalScrollBar(null);

turns off the HorizontalScrollBar by setting it to Null. Otherwise, you would see that as well. Without horizontal scroll bar, it will automatically wrap text within the View port.

Online Educational resources – 2


I am constantly seeking opportunities to learn, update my skills and if possible for free. There are plenty of resources available online including this blog (shameless plug :).

Don’t get me wrong about free, I do pay if the courses are required and good. But, when I am casually dabbling in some new technology, I want to get my feet wet with free resources if and when possible. For all I know, I may not even revisit :). Often times, free could mean “cheap” and sometimes even useless. So, you have to sift through the web. I am happy to share my experiences here.

I have posted about some of these online educational sites before. Recently, I found out about courses on IBM Developerworks. I have used their site before and their Redbooks. You can find a lot of mainframe as well as client/server topics there.

Their Developerworks learn page  is full of quality courses that are free. Here is the page with their Java courses. And they also sponsored the Big Data University  which offers several courses that are free. Here is their official pitch:

Learn Data Science With Our Data Science Fundamentals Learning Path

bigdatauniversity.com

Want to learn Data Science? Take our free Data Science Fundamentals learning path. You’ll learn data science fundamentals, tools, and even R programming.

 

As I mentioned earlier, Coursera, Edx, Udacity and Udemy are some of the other sites I use. These are called Moocs and reputed universities around the world offer highly technical courses on those sites. (For e.g., Udacity offers this nano-degree course on AI). Most of these offer the courses free to audit and fee for certificate option. So, you can try out the course before you fully invest in it.

And if you didn’t know, anyone who has premium membership on Linkedin, already have “Learning” option available to them. This is free. Linkedin aquired Lynda.com, an online training site and they seem to be integrating courses from Lynda into their Learning page. You can read more about it here:

http://www.pcworld.com/article/3123118/it-skills-training/linkedin-learning-finally-puts-lyndacoms-tech-training-courses-to-work.html

These continuing education sites are a boon to professionals working full time. These courses often cover complete topics but small enough to finish successfully. For e.g., a typical University level course may be broken down into 2 or 3 courses here. And you can take your course at your convenience, and if you miss the deadline for the classes, you can simply roll over to the next session that may start the following month. Remember the goal of these classes is to help you learn not to get a degree or put undue pressure.

I hope you will find these resources useful in adding value to your career path.

From C# To Java: Events


Java programming language tries to be everything for everybody and thus becomes a huge bag of goodies. But it could be confusing if not daunting, even for small tasks, to folks coming from other platforms.

Here the author talks vividly about how the Event Model in Java almost got him.

Scattered Code

I have been developing in C# for the last 3-4 years, and before that I was primarily a php coder. During one semester of my freshman year of college I did a little Java, but not much, and I have not done any since. I have decided to work on an N-Tier application in Java to give me a good project under my belt and show that I can use Java (and not just say to people that I can learn Java).

The problem with going from C# to Java is that the language is very similar. This is a problem because while I can figure out the language itself pretty easily, most “getting started” guideand tutorials are too beginner level to really be helpful. However, jumping straight into something such as a Spring framework tutorial or many other more high level tutorials can be pretty overwhelming, as they all…

View original post 496 more words

Regular Expressions (Regex or Regexp)


I use Regular Expressions (or Regex or Regexp as they are fondly called by programmers) a lot in my line of work as a programmer. This makes a lot of mundane editing tasks much  easier and even interesting. But, most of the programmers I know, either don’t know it or even if they heard of it, they are afraid to use it. So, here is my attempt at explaining this to everyone, whether you are a programmer or otherwise (user).

Real world Example 1
You all know the vowels. Remember?

A E I O U*

And remember the articles? There is a rule in English grammar to use articles while referring to nouns – one of a, an, the. General rule is, we have to use “an” when a noun begins with one of the above 5 vowels*. Remember that?

* I will ignore H in hour or honor for simplicity sake

For a non-native speaker of English, this may become confusing. That too, if their native languages don’t have the concept of using articles (they may just use “0ne” or just drop it altogether).

Imagine someone with less experience in articles wrote this flyer you asked for:

“A home insurance program is for the          regular joe like you and me. This home insurance program is a boon to the middle            class family                        to protect ­a asset. It’s a umbrella coverage that can cover a item or the complete home.

The insurance agency is upstairs. You take a elevator          and go up. You can talk to the advisor and get it all setup quickly. It is a operation, that is                  simple and easy to use, why wouldn’t you?”

All is well, you are about to print 100’s of this, but then you realize formatting is off. Look at the load of spaces in the middle. And yes, as a native speaker, those a’s in bold are so glaring to you (Flyer didn’t have the bold, let’s say the reader marked with thick pen). Now, you can go back to the computer and correct each entry there individually. On top of that, what if the company name is actually “Affordable Home Insurance” not just Home Insurance. You have to carefully change the “a”s there too.

This is only a few lines, so all is well. But, what if you are proof reading an author made this mistake randomly in the draft of his “first book” in English? Will you go back and look for it and correct one by one?

If you are a patterns kind of guy (or girl) you might think… Hmm. if only there is a way to tell the editor (or word processor) that you are looking for all the words that begin in a vowel and make sure any “a” before, be changed to “an”. And wherever there is a load of space, just replace with a single space. Bingo!! With couple of replacements, your entire document is proof-read.

You, my friend, just did a regular expression on me! This is possible and available in most modern editors.

(For the “an” problem you would have said a[ ]+([aeiouAEIOU])(.*). For finding multiple spaces, you could simply say [ ]+. And they could easily be replaced with An $1$2 and ” “ (single space) on regexr.com. More on these later).

Real world Example 2
For a sightly more complicated example, imagine you are working on RSVPs for a birthday party, you are arranging. You sort of created a simple questionnaire and sent out emails to people, like this:

Name:
Will you attend:
Will you be here for lunch:
How many:

And you got a lot of replies, say 100. You somehow managed to copy those into the below text file.

RSVP 1:

Name: Sam Varadarajan
Will you attend: Yes
Will you be here for lunch: Yes
How many: may be 2 or 3

RSVP 2:
Name: John Doe
Will you attend: X
Will you be here for lunch: –
How many: 1

RSVP 3:
Name: Jane Doe
Will you attend: YES
Will you be here for lunch:
How many: 2+

RSVP 4:
Name: Roboto Monstero
Will you attend: NO
Will you be here for lunch:
How many: 0

ETC

Then you realized, oh oh. It would have been nice to have them all listed out in a table? Like Excel?

# Name Coming Lunch? Howmany?
1 Sam Varadarajan Yes Yes 2 or 3
2 John Doe X 1
3 Jane Doe YES 2+
4 Roboto Monstero NO 0
Total (minimum) 5

You can go ahead sit there at your computer and copy each entry to the corresponding cell in Excel. Eventually, you will get there. What if you are doing this for a community project, say a marathon registration and there are literally 100s of people responding? (Of course, the questionnaire will be larger too).

You pattern matching girl will immediately think, if only I can tell the computer (editor or Word processing software you are using) to copy each RSVP (starts from RSVP and runs through How many: …) and collapse it to one line each. In doing so, pick up only numbers for some responses, consider YES/yes/Y/X as same. This is doable.

In all such scenarios, where you are dealing some (fuzzy?) text patterns and you want a tool/technique to blindly find and replace so as to standardize it, you are looking at Regular expression.

Or more formally, Regular expressions are a set of search patterns used to identify some text without specifically looking for it. Here is what Wikipedia has to say about them:

In theoretical computer science and formal language theory, a regular expression (sometimes called a rational expression)[1][2] is a sequence of characters that define a search pattern, mainly for use in pattern matchingwith strings, or string matching, i.e. “find and replace”-like operations.

If you have used PCs (or Unix) for a long while, you surely heard Star-dot-Star (*.*)? This means all the files. So, in its rudimentary form * represents all the characters in a string and thus is a regular expression. This pattern (*.*) then matches files like MyFirstDocument.doc and UsefulProgram.exe. Another character you might have come across would be question mark “?” which points to any single character.

There you have it. Armed with ? and *, you can start matching up words.

?_*.???
A_beautifulbook.doc

*+*=*
2+2=4

Note: * = all and ? = 1 char is only applicable in some commands, particularly in OS command prompt. As you enter the Regex world, forget these meanings. Here a dot (“.”) Represents single (any) character and dot-star (“.*”) represents many such characters.

All the above examples are somewhat of a real life (user level?) situation. But, we programmers face this type of question/challenge every day. We get files in one format, we need to reformat it, so the users could consume it in a more presentable format. Or we parse a log to make some sense out of it. One could use Regex for all such scenarios. In fact, almost all programming languages have some constructs to support this, with Perl, Python on one end, with fully integrated Regex capabilities to Java, C#, Javascript having libraries to support it.

With that you are now entering the exciting (or scary!)  world of Regular Expressions! Sort of an innocent looking English expression, but very powerful tool indeed. We will continue in the next blog post.

Contd…

to Part 2

Query function in Google Spreadsheets


anandexcels

Most versatile effective function and unique to Google Spreadsheets. I am documenting this function to understand it properly and for those who do not have programming background. I do not have previous experience with query. i will try to illustrate with examples.

If you have confusion and stuck some where please post your questions Google Docs forum at the following link https://productforums.google.com/forum/#!categories/docs/spreadsheets. There are experts in this area waiting for your questions.

Google Spreadsheet query is designed to be similar to SQL  with few exceptions. it is a subset of  SQL with a few feature of its own. if you are familiar with SQL it will be easy to learn.

The Syntax of the function is as follow:

QUERY(data, query, headers)

DATA: it can be columns(A:C open ranges) of data you want to query, range of cells such as A1:C10, result of function such as importrange, index etc.,

View original post 741 more words

Excel: Anchoring Cell


Excel is a great tool for analyzing and presenting data. You can create fancy and complex formulas to make it look nice and even create fancy reports. Like I posted in my last post a Doctor used it* to create a nice patient tracking system.

I create a lot of spreadsheets in my work as a developer everyday. I am not using it for business purposes, but more for analyzing data, sometimes presenting data in a fancier reports for users to see. This involves lot of formulas used in coloring, highlighting data.

If you are like me, you will type the formula once and copy and paste that on all cells. Here is an example:

2016-09-26-15_45_10-microsoft-excel-book1

Now, if you look at the formulas behind those cells (using Ctrl + Backtick – see here), you will see what I mean:

2016-09-26-15_50_23-microsoft-excel-book1

I typed the formula into D2, then I just copied and pasted the formula into D3, D4. Then Summed it up in D5.

This is fine for regular calculations where you have each line calculates based on the data in the same row.

What if you have a common cell that needs to be used in all calculations? Consider the below example, where the transactions are in a different currency and you want to calculate in USD. Instead of hard coding the Currency exchange rate, you put that in a Cell, so you can use it in various lines or even sheets.

2016-09-26-15_56_56-microsoft-excel-book1

Let’s look at the formulas behind (again by pressing control + back tick(`)):

2016-09-26-15_58_47-microsoft-excel-book1

 

What happened? I did the same thing like I did before, enter formula in C2 and copied and pasted into C3, C5. But the values there are 0. Why?

Hmm.. Now I see the problem. My conversion rate is in Cell B8. I correctly used this in my formula on the first line (C2). When I copied and pasted in to other cells, Excel naturally shifted the row numbers. Thus, my second value in the expression, in C3, C5, became B9, B11… which happen to be empty, thus the result = 0.

How do we avoid this? I typically go back and correct the formula, but that tends to be a tedious task, if you have a lot of such formulas on your spreadsheet. This is where anchoring comes in handy:

In Excel you can anchor a cell used in a formula by adding $ to it. Here is the modified formula and pasted values in other cells:

2016-09-26-16_02_28-microsoft-excel-book1

 

See that? When I added $ to B8 ($B$8), it anchored it. When I copied and pasted into other cells (C3, C5) it maintained that. And that did the job:

2016-09-26 16_05_10-Microsoft Excel - Book1.png

Voila! Oh yeah, to add anchor to a cell, you can type $ sign each time, or simply press F4 when you type a cell reference into a formula, as mentioned here. I love it when there is already a solution for a problem you run into. Don’t you?

Before closing, here are some terminologies:

Anchoring a cell reference gives you Absolute Reference – same reference is used anywhere on the spreadsheet. If you don’t anchor it, it gives you a Relative Reference – relative to the cell you are in. These are typically valid when used in a formula.


(*) I know the Doctor used Google Sheet there, but for practical purposes it works the same way Excel does.