Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Thursday, March 6, 2014

MOOC Review: MongoDB for Java Developers

* This review is my personal review on the course and is not sponsored by MongoDB, Inc. 

Course Name:
M101J: MongoDB for Java Developers
Link:
https://education.mongodb.com/courses/10gen/M101J/2014_March/about
Institution:
MongoDB
Instructors:
Andrew Erlichson, Jeff Yemin

The most difficult part of the course, in my opinion, was the first week, in which I had to install things on Windows, which was not too easy. This was partially because the videos were mainly for... (I forgot the OS used in the demo, but it should be the like of Linux.) Still, with the discussion forum and other online resources, I was still able to install and use things on Windows.

Although something later in the course still varied from OS to OS, the cases were handled easily and well by some text explanations. This is why I did not encounter many issues after the first week.

I usually learn better by reading than by watching videos, so it scared me in the first week that the course was mainly composed of videos. However, the instructors' voice was clear, and subtitles were available. Therefore, I got used to the mode as the course progressed.

The videos introduced MongoDB and its Java driver. The non-driver part is almost the same as that for two other courses offered by MongoDB University - M101JS: MongoDB for Node.js Developers and M101P: MongoDB for Developers as far as what I completed (I did not complete the other two). The Java driver part can be learned more easily with the API documentation.

Most videos were short, and it was not difficult for me to pay attention for the duration of a video.

Sometimes comparisons between MongoDB and relational database were introduced. It might be interesting to those who know relational database as I do.

Most videos were followed by quizzes, which helped understand what was covered by the videos and did not count toward the certificate. They were helpful in my opinion. The types included multiple choice and scripting, and the problems were similar to some of the weekly assignment problems.

The assignments included multiple choice, scripting, and programming problems. The scripting ones were done either on the browser or on MongoDB Shell, depending on the problem. These helped me get familiar with commands on MongoDB Shell. The programming ones typically involved using the Java driver and building a blog (the user interface design was provided, so one did not have to make one).

The blog project was interesting to me in that it increased experience working on something complicated.

The final exam was like the assignments, but the problems required more thinking to deal with the data.

The last week of the course contained the final exam and case studies. The case studies were interviews with Jon Hoffman from Foursquare and Ryan Bubinksi from Codecademy. It was interesting to learn more about these two websites.

In general, the course was organized, and the quality of the materials was good. The difficulty was okay (except for the first week for Windows users who are not familiar with IT stuff). There was also help on the discussion forum from the staff and other participants. The course should serve as a good introduction to MongoDB.

Tuesday, March 4, 2014

Ceiling of Square Root of BigInteger in Java

I was working on the programming assignment* of a MOOC dealing with big integers. I chose Java as the language and used BigInteger to handle the giant integers. In the assignment, I had to find the ceiling of the square root of a big integer (to be used somewhere else in the assignment), so I tried to find a built-in function for this. As some people I saw on online discussion forums, I could not find such a built-in function. As a result, I had to either use one coded by someone or implement one myself. The later was easier for me because by doing that, I did not have to worry about possible lack of background knowledge to understand a decent but difficult algorithm. 

(* Because the assignment is not about finding the ceiling of the square root of an integer, the code in this article does not violate any code of conduct. In fact, finding the ceiling of the square root of an integer for this assignment can be done by programming in another language using that language's libraries.)

Because what I wanted was the ceiling, which is also an integer, a simple algorithm for the approximation of the square root can lead to such a ceiling. I wrote a function for this for positive integers (for the assignment's purpose, many checks were unnecessary and therefore omitted): 

    /***
     * Finds the ceiling of the square root of an integer
     * @param N: the integer whose square root to find
     * @return the ceiling of the square root of N
     */
    public static BigInteger biSqrt(BigInteger N) {
        BigInteger o = new BigInteger("1");
        BigInteger t = new BigInteger("2");
        
        BigInteger u = N; // upper bound of search region
        // initial search value = ceil(0.5*N)
        BigInteger r = N.mod(t).compareTo(o) == 0 ? N.divide(t).add(o) : N.divide(t);
        BigInteger b = new BigInteger("0"); // lower bound of search region
        
        // new search value is ceiling of mid of upper and lower bounds
        while (true) {
            BigInteger r2 = r.pow(2); // square of search value
            
            if (r2.compareTo(N) > 0) {
                // too large, lower search upper bound
                u = r;
                BigInteger s = u.add(b);
                r = s.mod(t).compareTo(o) == 0 ? s.divide(t).add(o) : s.divide(t);
            } else if (r2.compareTo(N) < 0) {
                // too small, increase search lower bound
                b = r;
                BigInteger s = u.add(b);
                r = s.mod(t).compareTo(o) == 0 ? s.divide(t).add(o) : s.divide(t);
            } else {
                // find the value
                break;
            }
            
            // handle non-integer square root, 
            // whose ceiling = final upper bound
            if (u.compareTo(r) == 0) {
                break;
            }
        }
        
        return r;
    }

If executing these: 

        BigInteger z = new BigInteger("0");
        BigInteger o = new BigInteger("1");
        BigInteger t0 = new BigInteger("9");
        BigInteger t1 = new BigInteger("11");
        BigInteger t2 = new BigInteger("1099511627776"); // 2^40
        BigInteger t3 = new BigInteger("1099511627777"); // 2^40 + 1

The results are: 

0
1
3
4
1048576
1048577

The code was not reviewed by anyone other than myself, so it might have issues. Also, there are better algorithms for the same task. However, this should suffice for completing the assignment in Java. 

Any constructive feedback will be appreciated.