Showing posts sorted by relevance for query Java. Sort by date Show all posts
Showing posts sorted by relevance for query Java. Sort by date Show all posts

Wednesday, January 7, 2015

Top 18 design patterns interview questions in Java to know

This is (almost) verbatim from the Java67 blog (to which the appropriate credit must be given), with some links in addition to Java 8.

Design pattern interview question are integral part of any good list of core Java interview questions. Java is a popular Object oriented programming language and have lots of design pattern and design principles, contributed by many developers and open source framework. As a Java programmer it is expected from you to know OOPS concept like Abstraction, Encapsulation and polymorphism, what is design pattern in Java, some popular Java design pattern and, most importantly, when to use those design pattern in Java application. Purpose of asking design pattern interview question in Java is to check whether a Java programmer is familiar to those essential design patterns or not. Design patterns in Java interviews are as important as multi-threading, collection and programming questions. If you are senior or experienced Java programmer than expect more complex and tough design pattern in Java interview, e.g. Chain of responsibility design pattern, and solving real time software design questions.



Top Java design pattern questions and answers

Here is my list of top 10 design pattern interview question in Java. I have also provided answer of those Java design pattern question as link. no matter which level of Javainterview are you going e.g. programmer, software engineer, senior software engineer inJava, you can expect few question from Java design pattern.

1. When to use Strategy Design Pattern in Java?

Strategy pattern in quite useful for implementing set of related algorithms e.g. compressionalgorithms, filtering strategies etc. Strategy design pattern allows you to create Context classes, which uses Strategy implementation classes for applying business rules. This pattern follow open closed design principle and quite useful in Java. One example of Strategy pattern from JDK itself is a Collections.sort() method and Comparator interface, which is a strategy interface and defines strategy for comparing objects. Because of this pattern, we don't need to modify sort()method (closed for modification) to compare any object, at same time we can implement Comparator interface to define new comparing strategy (open for extension).


A more detailed review of the Strategy pattern, in the same blog,  is here. However, Java 8, changes the scenario. Here and here some examples of using the strategy pattern with lambdas.

2. What is Observer design pattern in Java? When do you use Observer pattern in Java?

This is one of the most common Java design pattern interview question. Observer pattern is based upon notification, there are two kinds of object Subject and Observer. Whenever there is change on subject's state observer will receive notification. See What is Observer design pattern in Java with real life example for more details.


3. Difference between Strategy and State design Pattern in Java?

This is an interesting Java design pattern interview questions as both Strategy and State pattern has same structure. If you look at UML class diagram for both pattern they look exactly same, but there intent is totally different. State design pattern is used to define and mange state of object, while Strategy pattern is used to define a set of interchangeable algorithm and let's client to choose one of them. So Strategy pattern is a client driven pattern while Object can manage there state itself.

4. What is Decorator pattern in Java? Can you give an example of Decorator pattern?

Decorator pattern is another popular java design pattern question which is common because of its heavy usage in java.io package. BufferedReader and BufferedWriter are good example of decorator pattern in Java. See How to use Decorator pattern in Java fore more details.

5. When to use Composite design Pattern in Java? Have you used previously in your project?

This design pattern question is asked on Java interview not just to check familiarity with Composite pattern but also, whether candidate has real life experience or not. Composite pattern is also a core Java design pattern, which allows you to treat both whole and part object to treat in similar way. Client code, which deals with Composite or individual object doesn't differentiate on them, it is possible because Composite class also implement same interface as there individual part. One of the good example of Composite pattern from JDK is JPanel class, which is both Component and Container. When paint() method is called on JPanel, it internally called paint() method of individual components and let them draw themselves. On second part of this design pattern interview question, be truthful, if you have used then say yes, otherwise say that you are familiar with concept and used it by your own. By the way always remember, giving an example from your project creates better impression.

6. What is Singleton pattern in Java?

Singleton pattern in Java is a pattern which allows only one instance of Singleton class available in whole application. java.lang.Runtime is good example of Singleton pattern in Java. There are lot's of follow up questions on Singleton pattern see 10 Java singleton interview question answers for those followups

7. Can you write thread-safe Singleton in Java?

There are multiple ways to write thread-safe singleton in Java e.g by writing singleton using double checked locking, by using static Singleton instance initialized during class loading. By the way using Java enum to create thread-safe singleton is most simple way. See Why Enum singleton is better in Java for more details.

8. When to use Template method design Pattern in Java?

Template pattern is another popular core Java design pattern interview question. I have seen it appear many times in real life project itself. Template pattern outlines an algorithm in form of template method and let subclass implement individual steps. Key point to mention, while answering this question is that template method should be final, so that subclass can not override and change steps of algorithm, but same time individual step should be abstract, so that child classes can implement them.

9. What is Factory pattern in Java? What is advantage of using static factory method to create object?
Factory pattern in Java is a creation Java design pattern and favorite on many Javainterviews.Factory pattern used to create object by providing static factory methods. There are many advantage of providing factory methods e.g. caching immutable objects, easy to introduce new objects etc. See What is Factory pattern in Java and benefits for more details.

10. Difference between Decorator and Proxy pattern in Java?

Another tricky Java design pattern question and trick here is that both Decorator and Proxy implements interface of the object they decorate or encapsulate. As I said, many Java design pattern can have similar or exactly same structure but they differ in there intent. Decorator pattern is used to implement functionality on already created object, while Proxy pattern is used for controlling access to object. One more difference between Decorator and Proxy design pattern is that, Decorator doesn't create object, instead it get object in it's constructor, while Proxy actually creates objects.

11. When to use Setter and Constructor Injection in Dependency Injection pattern?

Use Setter injection to provide optional dependencies of an object, while use Constructor injection to provide mandatory dependency of an object, without which it can not work. This question is related to Dependency Injection design pattern and mostly asked in context of Spring framework, which is now become an standard for developing Java application. Since Spring provides IOC container, it also gives you way to specify dependencies either by using setter methods or constructors. You can also take a look my previous post on same topic.

12. What is difference between Factory and Abstract factory in Java

see here to answer this Java design pattern interview question.

13. When to use Adapter pattern in Java? Have you used it before in your project?

Use Adapter pattern when you need to make two class work with incompatible interfaces. Adapter pattern can also be used to encapsulate third party code, so that your application only depends upon Adapter, which can adapt itself when third party code changes or you moved to a different third party library. By the way this Java design pattern question can also be asked by providing actual scenario.

14. Can you write code to implement Producer Consumer design pattern in Java?

Producer consumer design pattern is a concurrency design pattern in Java which can be implemented using multiple way. if you are working in Java 5 then its better to use Concurrency util to implement producer consumer pattern instead of plain old wait and notify in Java. Here is a good example of implementing producer consumer problem using BlockingQueue in Java.

15. What is Open closed design principle in Java?

Open closed design principle is one of the SOLID principle defined by Robert C. Martin, popularly known as Uncle Bob. This principle advices that a code should be open for extension but close for modification. At first this may look conflicting but once you explore power of polymorphism, you will start finding patterns which can provide stability and flexibility of this principle. One of the key example of this is State and Strategy design pattern, where Context class is closed for modification and new functionality is provided by writing new code by implementing new state of strategy. Seethis article to know more about Open closed principle.

16. What is Builder design pattern in Java? When do you use Builder pattern ?

Builder pattern in Java is another creational design pattern in Java and often asked in Java interviews because of its specific use when you need to build an object which requires multiple properties some optional and some mandatory. See When to use Builder pattern in Java for more details

17. Can you give an example of SOLID design principles in Java?

There are lots of SOLID design pattern which forms acronym SOLID, read this list of SOLID design principles for Java programmer to answer this Java interview question.

18. What is difference between Abstraction and Encapsulation in Java?

I have already covered answer of this Java interview question in my previous post as Difference between encapsulation and abstraction in Java. See there to answer this question.

This was my list of 10 popular design pattern interview question in Java. I have not included MVC (Model View Controller) design pattern because that is more specific to J2EE and Servlet JSP interview, but if you are going for any Java interview which demands experience in J2EE than you must prepare MVC design pattern. That's all on Java design pattern interview question and answers. Please let us know if you have any other interesting question on Java design pattern.

P.S. - Here a synthetic discussion on how Java 8 can change the design pattern implementation, which can be completed by this stackoverflow post.  A discussion of some IBM authors of the same topic  can be found here. Here , instead an example of the "Commander" (observer-like) pattern, and, in the second part of the post, a new example of the strategy pattern in Java 8. 

Saturday, December 22, 2012

A Java Library (of books) for beginners, and a little path to self instruction for geophysicists


It is quite a few years that I insist for students moving to program in Java. Java is not anymore Java 1, it has evolved towards a mature language and now it is at its version 7 (Java 8, now and Java 9 soon) Java  13. Times ago I talked about doing matrix algebra in Java. Most of that post is still valid.

However, many would realize that for learning Java the right way s/he has to start from the beginning, and possibly, start to learn also the concepts of object oriented programming (the real reason to use Java).

My personal training began with the videos (in Italian) of one of my Trento university colleague Marco Ronchetti. For others, MIT Courseware could be a good starting, but also these less academical clips can help.

Programming is more than just knowing a language. This concept is well covered in

Introduction to programming using Java by David J.Eck
Java Precisely by P. Sestoft (suggested by Joshua Bloch, see below)

and nothing can substitute the reading of a good book. More specialised classics on Java are also:

Thinking in Java 4th edition

Thinking in Java annotated solutions book

the two by Bruce Eckel

The design pattern Java companion, by J. W. Cooper

These books sum up to a lot of pages to read and working them out is not a thing you do in a day.
People who teach Java, usually rely on command line. However, I would prefer to learn how to use an IDE. Our choice is, since many years, Eclipse. Learning to use it is easy but is made easier by reading Lars Vogel tutorials and material.

Obviously Oracle itself has a series of very good tutorials.

Going to more specialised book, for scientists, I also recommend

Mak, R. (2010). Prentice Hall - Java Number Cruncher. The Java Programmer's Guide to Numerical Computing (pp. 1–493).

Besset, D.H., Object-Oriented Implementation of Numerical Methods: An Introduction with Java & Smalltalk , 2000. (~70 Mb) (His code can be found here).

Nikishkov, G. (2010). Programming Finite Elements in Java (pp. 1–394). The preface of his book can give a good rational for using Java instead of other languages.

A general collection of Algorithms in Java is Algorithms by Kevin Wayne and Robert Sedgewick (also addressed here).

You can also give a look to old C Numerical Recipes (for algorithms) and use this free Java project for their application. (Do not support NR license policies).

Having got a clear idea of what Java is, you probably wants to write clean code. The right book is Clean Code by Robert Martin.

Now, you are probably ready to understand the subtleties of the Java Matrix Libraries I indicated in my older post.

If one wants to move to geosciences and GIS, the best choice is to give a look to the GEOtools site and to al the documentation s/he can find there. At that point s/he will be probably also ready for programming the Jgrasstools.  Programming OMS3 components would be easier, indeed, if you do not care of geographic features.  For GIS and working with us, one should consider the introduction by Andrea Antonello.

Eventually you can probably go back and make reacher your library (of books) by also reading and applying

Martin Fowler's UML Distilled: A Brief Guide to the Standard Object Modeling Language

Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides: Design Patterns: Elements of Reusable Object-Oriented Software (but the easiest book, from the opinion of many is Head First Design Patterns by Eric Freeman and Elisabeth Robson.

and  the book that many consider the best one:

Effective Java Second Edition, by Joshua Bloch  by many considered the best book about Java.
Here it is the third Edition ;-).
Here there is also a video about.

For mastering Java generics and collections, I suggest:

Java Generics and collections by Maurcie Naftalin & Philip Wadler, by O'Reilly.

Obviously Java is one of the most used languages, and there is a lot of material on the web. Starting from the Oracles itself main site.

However, programming is about writing code, not just reading about it. Wishing I would have time to do it! 

Monday, December 31, 2012

Open Java resources for Hydrologists

Next question about Java, after having introduced it, and talked about options for doing numerics, is to see if there are effective resources on which hydrologists can build something useful, incrementally, and as a part of a community.
Also OpenMI uses Java, but, at the moment, Java there is just the second choice, well behind their C#. A second framework which we are looking to is:
  • Openda. It is a framework for data assimilation and calibration of models promoted by Deltares. 

There are out there probably other Java frameworks, and I will add them to the list, whenever they will be brought to my attention.
The field of more intense Java activity is actually the GIS one. There are at least three/four major open source efforts in Java:
In fact, it is not very clear to me, but also the
initiative seems having parts based on Java.
They are based on various type of resources among those in Java:
Both GeoTools and Sextante use 
  • JTS, the Java topology suite for several elementary operations.
An interesting,  old but not outdated initiative to give a third dimension to data visualisation is 
  • Visad, a library to visualise scientific datasets
from which sprout
  • the IDV  virtual globes family to visualize 3D geophysical data
IDV is not the unique Java Virtual globe. Another one is:
  • the Nasa WorldWind which gives (as open source) what Google Earth gives to the general public. Various prototype implementations of Nasa WorldWind in uDig (and in OMS3) were already made, and possibly sooner or later we will have a full mainstream implementation of it.
Last but not least an important resources is 
  • the NetCDF data format (of which IDV, but also ncBrowse,  is a viewer) has also an implementation in Java.
Even if my post is more concerned about the development of Hydrological resources and models, the above frameworks/programs/applications/models or part of them can just be used (and just not developed) with profit.  Starting from OMS3 there are at least three major modelling efforts that use it:
Not to forget is the whole Jgrasstools available for DEM manipulation (and further modelling) inside (and outside) uDig. All of them have more resources in a post dedicated to OMS 3 resources.
The Sextante library itself comes with more than 300 tools for manipulation of DEM and images. Also IDV and NasaWorldWind can be just used for the purposes of any research.
Another full-fledged Java Catchment model is

by Ricardo Mantilla. Its source code can be obtained following the instructions in the user manual.

A recent addition to the resources is


Why use Java, instead of  R ?

In one of my previous post, I talked about R software, and in fact, my group of people use both.  R is much more for some higher level operations, and is much less customisable than the resources developed in Java, and,  usually, less efficient.  My former students, using Java capabilities were able to built professional/industrial applications on the basis of Java (the GIS are a proof), that using just R would have been impossible.
However, the new incoming version of OMS3, should include the way to call R from the OMS console. Please contact the OMS version 3 developers for more information.

Why do not use Python ?

Python (or many other languages) could have been a good choice. Never say never, however I chose Java and to be successful one has to consistently invest his/her own limited resources in one direction.

There exist material/documentation on the above material ?

One introduction to programming GIS and essentially all I would require to a collaborator to know  can be found here by Andrea Antonello

Tuesday, April 18, 2023

Using ChapGPT 4 for Learning Java

 Currently, there's a lot of hype surrounding ChatGPT v4. I was curious to explore its capabilities, so I used OpenAccessGPT to bypass current Italian law and access the BOT.



I tested ChatGPT v4 by asking it to search for literature on a specific topic - the stomata action under the effect of decreasing tension in xylem. While the results were interesting, they weren't necessarily the best. In fact, when I tested the BOT on more specialized topics, its performance seemed immature.

To improve my experience, I decided to seek help in programming and discovered that ChatGPT v4 could be a valuable resource. I soon realized that my idea wasn't entirely original, as there are many resources available online, such as this YouTube video. Perhaps ChatGPT v4 could have even provided me with this information.

This is what ChapGPT tells on the topic:

"ChatGPT can be a valuable resource for learning Java in several ways:

  1. Asking Questions: You can ask ChatGPT any question you have about Java, such as how to use a specific feature or how to solve a particular problem. ChatGPT can provide you with a detailed explanation or point you in the right direction to find the information you need.
  2. Practice Exercises: ChatGPT can provide you with practice exercises to help you improve your Java skills. These exercises can range from simple coding challenges to more complex projects.
  3. Code Examples: ChatGPT can provide you with Java code examples that illustrate how to use various features and solve different problems. These examples can help you understand Java concepts more easily.
  4. Tutorials and Resources: ChatGPT can recommend Java tutorials and resources that can help you learn more about the language. These resources can include online courses, books, and websites.

Overall, ChatGPT can be a valuable tool for anyone looking to learn Java. It can provide you with personalized assistance, practice exercises, and resources to help you improve your skills and become a proficient Java programmer. "

Browsing the YouTube class, I realized that the approach to Java was good but not the way I desired. So I asked: "Please help me in learning Java but not following the traditional way. I would like that you introduce me early on Java design patterns and topics related to numerical computation. I need also you to introduce me to the use of generics. I would say introduce me to modern Java".  Here below the result

Sure, I can design a ten-hour class on the topics of Java design patterns, numerical computation, and generics. Here's a rough outline:

Day 1 (2 hours):
1. Introduction to Java design patterns
2. Singleton pattern
3. Factory pattern
4. Observer pattern
5. Hands-on exercise

Day 2 (2 hours):
1. Introduction to numerical computation in Java
2. Overview of Apache Commons Math and JScience libraries
3. Basic mathematical operations (addition, subtraction, multiplication, division)
4. Hands-on exercise

Day 3 (2 hours):
1. Statistical analysis using Apache Commons Math
2. Descriptive statistics (mean, median, mode, variance, standard deviation)
3. Hypothesis testing (t-tests, ANOVA)
4. Hands-on exercise

Day 4 (2 hours):
1. Linear algebra using JScience
2. Matrices and vectors
3. Matrix operations (addition, subtraction, multiplication)
4. Eigenvalues and eigenvectors
5. Hands-on exercise

Day 5 (2 hours):
1. Introduction to generics in Java
2. Defining and using generic classes
3. Defining and using generic interfaces
4. Defining and using generic methods
5. Hands-on exercise

Each day will consist of a mixture of lecture-style teaching and hands-on exercises, to ensure that students have a chance to practice what they've learned. The hands-on exercises will involve writing code and solving problems related to the topics covered in class.

By the end of the ten-hour class, students should have a good understanding of Java design patterns, numerical computation, and generics, and be able to apply these concepts to real-world programming problems.

Not sure that I am really satisfied but my further experiments with more detailed questions went better. Therefore, up to a point, the user has also to lear how to interact with the BOT.  When you go on more detailed questions the performance are less generic. For instance, I asked:

Can do you do examples on how define and using generics classes in Java ?

The answer was:

public class Box<T> {
    private T contents;

    public void setContents(T contents) {
        this.contents = contents;
    }

    public T getContents() {
        return contents;
    }
}

This answer is correct by a little obscure to a newbie. So I refined my request: "How can I use the previous class in a main() ?  And I have got:

public class Main {
    public static void main(String[] args) {
        // Create a Box that holds strings
        Box<String> stringBox = new Box<>();

        // Set the contents of the box to a string
        stringBox.setContents("Hello, world!");

        // Get the contents of the box and print it to the console
        String contents = stringBox.getContents();
        System.out.println(contents);
    }
}


with the comment:

"In this example, we create a Box that holds strings using the syntax Box<String>. We then set the contents of the box to the string "Hello, world!" using the setContents method, and retrieve the contents using the getContents method. Finally, we print the contents to the console using System.out.println".

Overall (there were also some other example, this was quite satisfying.  Next step: making ChapGPT learn about our own staff and being able to support, for instance, people in using GEOframe. 

Tuesday, July 16, 2013

Java for Hydrologists 101

There are a few postings on Java in this blog. Since I want to teach it to my students, I am quietly starting to populate this page with presentations which, eventually, will constitute the core of an informal class (;-)) the Java for Hydrologsts 101.
The focus of the JfH-101 is not simply to gain Java knowledge from the scratch (or so), but to address those topics and issues that have to do with my experience of hydrologist. So, I will try to cover Java as well as OMS3, and at the right time Geotools, and jgrasstools, not forgetting the tools of the tool (Ant, Maven, Git)^*, but in the meanwhile I will try to address the topics related to object oriented programming.  Programming is actually very much not talking about  that but doing it, so many of the slides will actually recall to actions.

Topics

0 - Getting Started (mostly things to read -or start to read- before the start) (YouTube 2018 video)
1 - Your first program (You Tube Video 2018)
2 - Solving a linear equation

3 -  A few diversions
4 - Reading  data from the system's console
5 - Reading data from a File

6 - Working with Git
-----Not yet implemented: ----

7 - Programming the heat diffusion equation
8 - Making the heat diffusion an OMS3 components
9 - Building Java projects with Ant, Maven and GRADLE
8 - GEOtools essentials
9 - Commenting the programming of the GEOframe-ET
10 - The Java REPL
11 - A little on Java Modules in Java 9
12 - Setting the continuous integration in GIT (using Travis)

The source code is available for download to from GitHub.

References

Please go to this blogpost.


^* - From the links you can quite understand the I rely very much on Lars Vogel site for the basic stuff. It is not obviously the only good resource available (stackoverflow is another one, for instance, and many others will be addressed).

Thursday, November 9, 2023

Java for Hydrologists 101

There are a few postings on Java in this blog. Since I want to teach it to my students, I am quietly starting to populate this page with presentations which, eventually, will constitute the core of an informal class (;-)) the Java for Hydrologsts 101. The first version of this blog dated back to more than 10 years ago and this gives the idea on how slow thing could go. The text presented here is at present the same but I am progressively modifying it. 


The primary aim of JfH-101 is not merely to impart basic Java knowledge, but to delve into topics and issues that align with my hydrologist experience. In collaboration with my colleagues and friends from GEOframes, we plan to cover not only Java, but also OMS3, and when appropriate, Geotools and Horton Machine (former JGrasstools). We won't overlook Ant, Maven, Gradle, Giteither. Meanwhile, we'll address topics pertinent to object oriented programming. 

Programming is less about discussing theory and more about practical application. Therefore, many of our slides will prompt you to take action and apply what you've learned.

Various (overwhelming) material can be found at the Java for Hydrologist OSF repository The new video will be added to the Java 101 for hydrologists Vimeo Showcase.  Here below, you'll find all the material in a ordered form: 

Topics

0 - Getting Started (mostly things to read -or start to read- before the start) (Vimeo2023)

Oldies but Goldies

* -  A few diversions
* - Reading  data from the system's console
* - Reading data from a File

* - Working with Git
The more challenging course is here


References

Please go to this blogpost where you can find links to books and various material. 


^* - From the links you can quite understand the I rely very much on Lars Vogel site for the basic stuff. It is not obviously the only good resource available (stackoverflow is another one, for instance, and many others will be addressed). Now you can also take the code and splice into  a LLM and have further information and help. 

Thursday, September 27, 2018

Java 11 trap

As you know we use Java (R and python).  Version 11 is a major release because it has long-term support (LTS). But it comes with some traps. Here below, please find  "verbatim" a post from Stephen Colebourne's blog which inform about some problems that Oracles's new licensing strategy can cause. (From the same blog, also interesting the suggestions on how to move from Java 8 to Java 11).

" ... It is a But Oracle have also set it up to be a trap (either deliberately or accidentally).

The trap

For 23 years, developers have downloaded the JDK from Oracle and used it for $free. Type "JDK" into your favourite search engine, and the top link will be to an Oracle Java SE download page (I'm deliberately not providing a link). But that search and that link is now a trap.
Oracle JDK, the one all web searches take you to, is now commercial not $free.
The key part of the terms is as follows:

You may not: use the Programs for any data processing or any commercial, production, or internal business purposes other than developing, testing, prototyping, and demonstrating your Application;

The trap is as follows:
  • Download Oracle JDK (because that is what you've always done, and it is what the web-search tells you). 
  • Use it in production (because you didn't realise the license changed)
  • Get a nasty phone call from Oracle's license enforcement teams demanding lots of money
In other words, Oracle can rely on inertia from Java developers to cause them to download the wrong (commercial) release of Java. Unless you read the text/warnings/legalese very carefully you might not even realise Oracle JDK is now commercial, and that you are therefore liable to pay Oracle for Java.
Is this trap malicious behaviour on the part of Oracle? Readers will have their own opinions. I do suggest bearing in mind that Oracle invests huge amounts in developing Java, so it is reasonable to have a commercial plan available for those that want it. And they do provide a $free alternative...
The solution

The solution is simple!

Use an OpenJDK build.
As well as their commercial JDK, Oracle produce an OpenJDK build. It is free, zero-cot and GPL licensed (with Classpath exception, so safe for commercial use).

Download $free Java here.

Right now, the OpenJDK build from Oracle is the only one available. But more will be available soon such as from AdoptOpenJDK (probably later this week). And the OpenJDK build from groups other than Oracle will the the ones to use for Java 11 longer-term support. See Couleborne's post on zero-cost Java and the wide variety of OpenJDK builds for more details.


Summary

Do NOT download or use the Oracle JDK unless you intend to pay for it.
For Java 11, download and use an OpenJDK build, currently from here. (No comments on this post. There are plenty of other places to express opinions.)"

Wednesday, August 30, 2017

OMS 3 essentials

OMS3 stands for Object Modelling System version 3 (see also this page here). Now it has also a server-side companion called Cloud Service Integration platform, CSIP. You can find information about OMS3 here:
A version of OMS3 source code is here.

For more general information you can browse the various dedicated posts in this blog.


https://contemplatingmontessori.wordpress.com/2011/11/14/platonic-solids/

For running it, you have to perform various installations. If can go to to the easiest path provided by 
Otherwise, if you prefer to go through all the details, give a look  to the short notes below. 

  • Java. GEOframe and OMS are written in Java and they require to have installed Java on your computer. Here you can find instructions to install Java on your computer. OMS need Java 8 JDK. Please note that you need the Java Development Toolkit (JDK) installed not the Java Runtime Environment (JRE). The official Oracle's pages are here but we now want to use AdoptOpenJDK (see why here). Recently, for simplify these installations,  and the companion installations of the system GEOFrame built upon OMS3, we have used the Anaconda Environment, here. 
There are various ways to use OMS. The most important two are using Docker or using the OMSConsole (the latter is not really up-to-date). Please notice that the OMS console still works for Java 8, while the Docker version can work with the most recent Java version (this does not implies that your OMS components work with it, if they contain some dependence on old Java versions).

References

To have a recent overview of the subject jointly with our GEOframe stuff, one can also give a look to 
Further information

Our development regarding the Net3 (Francesco Serafin's work): https://alm.engr.colostate.edu/cb/repository/24720 

Hopefully they will merge soon in an official release. All the "alm" repo at present require a password that can be asked to Olaf David (odavid <at> colostate. edu) col.

Saturday, December 8, 2018

Material for the Winter School on the GEOframe System - Installations

The Winter School on the GEOframe system is approaching.  In this webpost you will find all the preparatory material of the school indexed.

Installations

You are not assumed to know Python or Java to participate to the School. However programs runs on Java.. We will not do any Java programming though.  Input/Output of models will be treated by using some scripting in Python. We will communicate the appropriate notions during the classes. However, for the interested there are plenty of courses on the web (for instance this comes from SciPy 2018. Other from SciPy here.)
  • Java. GEOframe and OMS are written in Java and they require to have installed Java on your computer. Here you can find instructions to install Java on your computer.  OMS need Java 8 JDK.  Please note that you need the Java Development Toolkit (JDK) installed not the Java Runtime Environment (JRE).  The official Oracle's pages are here. You can watch several videos for Windows: here. Googling you can get videos for your platform.
  • We are going to use Docker for our modelling. Therefore you need to install it (here for Linuxes) on your computer. To quickly understand what Docker is, you can see here. Or read this tutorial. This video could be useful too. The knowledge of Docker required will be extremely elementary. You will not be required to do Docker applications. You simply use one and, at the end, is just matter of executing a command. 
  • During the School we will use Jupyter and Python 3 for data management and visualisation. It would be great if you could arrive
    • The post at this link contains all the information needed. For installation of the software, we suggest Anaconda. For any problem contact us through the mailing list. 
    • To understand what a Jupyter notebook is about, please see its manual. However, we will use Jupyterlab.  (You can see a YouTube video about here).  One can think that Jupyter was heavily based on the look-and-feel of Mathematica notebooks: but Jupyterlab is a step ahead to something different. Installing Anaconda, you also have already installed Jupyter notebook. So you have just to try it issuing the command: 
      • Jupyter notebook
  • You still do not know what to do with it, but we will lear step by step during the Winter School
  • Finally, at least for MacOs user, due to a bug either in the Docker or in GEOtools, it is necessary to use the OMS Console.  You can download the beta 3.5.62 release from here.
  • If you want to further with Jupiter and Jupiterlab, you can  install BeakerX. For its installation, please follow verbatim the instructions here. The instructions given at the main page of BeakerX are incomplete ;-).

Monday, February 3, 2020

4 Researchers Who Want to Develop Hydrological Models Without Reinventing the Wheel

Maybe you can save efforts and you do not need to reinvent the wheel. Therefore I wrote about our project that lasts since almost fifteen years. We use an open infrastructure, the Object Modelling System v. 3 and developed an open system, called GEOframe that has unmatched capabilities to be expanded while not wasting the old work done and maintaining your independence as developer.

The best way to understand OMS and see how it works is probably to give a look to the material of our last winter School on our system GEOframe. Specifically, this is the page of introduction to OMS. But also starting from installations of the material can give insights.
There you can find links to the OMS v3 pages at CSU and original material. Our choice of OMS and Java was long meditated. This was my assessment almost ten years ago. This is instead the assessment made by NIWA six years ago.

Here  you can find some thinking about the choice of programming  languages. We use, in practice, a mix of Python and Java. Python for treating and visualizing data. Java for writing our models.
When properly used Java is not so slow (twice slower of well written C++) and was favored by having an incredible suite of tools helping proper programming, project building and code maintenance that make the experience to develop in C++ a poor one. Python (specifically some of its libraries) and the Jupyter lab are instead a very nice experience for treating data and, besides, is supported by a vibrant community of developers, even among hydrologists, who really make the experience better and better everyday. Python is also becoming a “lingua franca” among scientists and this makes less difficult and steep to share results. I have a couple of pages to introduce Python to hydrologists that you can access starting from here. For what regards of Java one can start from here.
In any case Java is not mandatory for the use of OMS. Also FORTRAN and C++ codes can be used: NIWA did it with FORTRAN, but we never tried it.

OMS code is distributed with the MIT Open Source license. It is not available on a public repository though, but Olaf David, its chief architect, gave us access to it and does not have problem to give it to others. I recognize that this is a limitation but with more users, it would not be so difficult to get a branch of the source code in a public repository, I guess.
Our own source code is at the GEOframe components Github repository. The software is under GPL3 and the repository is open to anyone willing to collaborate.

The structure of the OMS allows to easily add components with any type of licence, the one more suited to you, and does not oblige you to release the software under the GPLv3 license.
Worth to mention is that OMS has a server side companion, CSIP of which you can find any information here.

Once Olaf David mentioned to me that the overall cost of OMS was closely 10 million of dollars. From my side I guess i invested around 1 million and more on developing GEOframe and its ancestors, Just to give a measure of the investment needed for such an enterprise. But building on top of them is now much less expensive and much more immediate.
Our current task are finishing the 1D-2D-3D solving of Richards equation (extended to treat groundwater and surface waters) and its coupling with the energy budget, evaporation and vegetation dynamics. This would replace our older GEOtop in two years, except for snow modelling for which we do not have started anything so far. We have also ongoing work for hillslope stability and a well established set of tools for doing lumped modelling, including the estimation of travel times and tracers/pollutants concentrations. Some other developments, more on the side of informatics were done by a former students of mine and are reported in his Ph.D. Dissertation. It contains also some work on neural networks.
For the future, an effort should be made to bring OMS to Java 9, which, I think, could make the code more understandable. Classes are a good thing but understanding the connection between them is what matters. Java 9 Modules should help to get the code more clean, and Gradle should do the rest for external libraries.  A few years go I started to dig into the code. I could not complete the task for my limited time but I believe one Java programmer can master OMS3 code in six intense months or so.  I also envision that, having a good project, OMS could be completely embedded in Jupyter lab making it usable through it making its use more familiar to a multitude of users. 

Obviously, we are willing to give any help, if you join us. I hope this information can help you to make your decisions.

Monday, June 6, 2016

Java, Python, C/C++ or FORTRAN in scientific programming ?

I found this simple post by Michael Scharf on Quora  that discusses part of the question. It is easy to read, clear, and probably true. So I publish it verbatim (with some comment of mine, especially for FORTRAN)

"I have used  (Java, Python, C/C++ - my note) each of them for 15-20+ years. There is no best. They have different strengths and weaknesses.
  • C and C++ require a lot of discipline because you have to do memory management yourself.
  • C++ is extremely powerful but also very complex.
  • C and C++ are "dangerous" because, if you are not careful, your program can access and modify data that it is not supposed to touch.
  • Python is elegant and designed to be easy to use and read. It has the least distractions when it comes to syntax.
  • The syntax of C, C++ and Java look somewhat similar. Python looks different, it  uses indentation instead of {} to group code
  • Java has the best IDE support (e.g. eclipse or IntelliJ)
  • C and C++ are also statically typed, but the preprocessor can add a level of complexity that can make it difficult to be sure what actually happens.
  • In terms of speed C/C++ are fastest, but for most problems Java is very close in speed. Python can be slow, but if needed critical parts can be written in C. On modern execution speed is rarely the limit - cache behaviour, memory and disc access are the limits.
If you want to learn programming, I would learn Python first, then Java, then C and finally C++.
I personally would not recommend C++ because of its complexity. However, if your are disciplined and have a strict set of rules for a project, C++ can be fantastic.
I would use C only for low level stuff, like writing device drivers.

Java is good for large projects, provided you write good APIs and you are carefully modularizing your software.

Python is good for small projects. If the team and the software gets bigger, it can become hard to maintain unless you have a very good test coverage."

There is no mention of FORTRAN  because since many years, it is just a language that regards the niche of users that do numerics (of any type).  Here a discussion in StackOverflow about this language compared (more or less) to the other languages mentioned above. 
I never really used FORTRAN, so I am not the best person to  talk about it.  I actually moved out of it in 1988 when I decided to use a language with dynamic memory allocation, and I never went back to it.  Generically I believe its velocity is overestimated, especially considering that the average scientific programmer is a bad programmer, and a bad program in FORTRAN can be much slower than a program in any of the above languages.

I saw a lot of tools made in FORTRAN and some large models. But I am not sure it is the best choice for a large project.  Anyway, FORTRAN plus Python is the choice of many in these days. Still I believe it is a sub-optimal choice for managing software projects that go beyond programming a few algorithms, and personally I do not like its syntax and its convoluted object orientation. But I will never personally test if this is actually the truth. 

Moreover, it is true that "It's (FORTRAN's) array handling is nice, with succinct array operations on both whole arrays and on slices, comparable with matlab or numpy but super fast. The language is carefully designed to make it very difficult to accidentally write slow code -- pointers are restricted in such a way that it's immediately obvious if there might be aliasing, as the standard example -- and so the optimizer can go to town on your code. Current incarnations have things like coarray fortran, and do concurrent and forall built into the language, allowing distributed memory and shared memory parallelism, and vectorization. (cit. from Stack Overflow)". However,  it is this attitude that favors such a certain laziness in software design that I do not appreciate. 

For most people, however, is just matter, of what they were taught, i.e. a matter of legacy. They just have a lot of material to which they got used to, and do not want (or have the time) to change. So think that your initial choice can stick with you for most of your life, because what Peter Norvig says, has a lot of foundations (Teach yourself programming in then years).  On the other hand,  consider that, if your activity has to do with programming, during your life you will embrace  more than one programming language (but one will remain your main one). Not because it is mandatory (and for some jobs it is), but because you are this type of guy (gal) that likes this sort of things. 

So what would be my choice for students ? Well, for students in hydrology, I will choose Python and R. But for real life applications, I would chose Java first (as I did).  Eventually, my recent reflections tend to support the idea that the language (with its syntax and attitudes) is just one part of the problem, and that the real focus should be (without making of it an idol) the design of the code and its proper documentation (which has some peculiarities when the concern is a scientific activity).

Saturday, December 6, 2014

Imagine to be a hydrologist and you want to learn Java

Dear * to learn Java: 

you can use my lectures (still being produced): http://abouthydrology.blogspot.it/2013/07/java-for-hydrologists-101.html

Read the books I list in my blog: http://abouthydrology.blogspot.it/2012/12/a-little-java-library-for-beginners.html. Many of them are specifically dedicated to Numerics and Scientific Computation.

Learning Java is very much programming, not just reading. So You have to choose a task and try to perform it. Good experiments should be, creating reusable classes for:
  • Reading and writing data to a file
  • For a generic function
  • For solving an Ordinary differential equation (or a set of them: Lorenz' chaotic equations would be a good exercise indeed

If you do math, sooner or later you to use matrixes. You do not need to reinvent the wheel, even if a standard choice has not been yet emerged. See here.

In general all the Java resources I came across (including this one, are at):  http://abouthydrology.blogspot.it/search/label/Java

Francesco Serafin, master thesis, introduces to various tools and methods that can be used to integrate partial differential equations: http://abouthydrology.blogspot.it/2014/07/patterns-for-application-of-modern.html

The subsequent step is to learn OMS: maybe this can be a little more complicated. Anyway, I way to do this is to start with the 2013 summer school:

or you can go and work out the examples you can find at the original OMS site: http://abouthydrology.blogspot.it/search/label/OMS3


I will be improving the resources all the time. So check them once in a while !

Friday, May 6, 2011

Programming and Doing Matrix Algebra in Java

Java is a great programming language. I really appreciate the enormous efforts that were made in making programmer life better, and the elegance of the Object Oriented programming, as compared to the procedural way of thinking I was used to.
Java is special for that being incredibly large the number of things that should be managed for its professional use. A possible workflow for learning it, is presented here (pay attentions to looping links).

At the very end  most of the core work I have to do, is to do linear algebra. Recently the Mathematical Libraries for Java grew many and interesting.   Information can be found in Wikipedia at the List of Numerical Libraries (but most of the links do not work, you have to google the names).


.
Among the libraries cited there, recently we used the Parallel Colt in the work by Francesco Serafin. The merit of the page Parallel Colt was also to bring my attention on the theory of what I was looking for, and especially to the CSparse book by Tim Davis, and to the Golub work, and book.

A comprehensive overview of Java Matrix Packages is given at the Java-Matrix.org.
A comparison of Matrix packages performance was made here but a test with our tools will be necessary, sooner or later.  UJMP is the only one of all the packages that also has a companion paper that tries to describe the design of the library. For me a great add-value.  Also la4j seems promising. Both la4j and UJMP seems to be currently updated, while other efforts are more or less quiescent (including Parallel Colt).

I also observe that a possible use of new features of Java 8 could be of some interest for matrix multiplication. However the most informed answers suggested to cope with ready-made work.

Before doing anything, remeber that for many problem Apache Commons, as well as the more specialised Math Commons and the Google Guava library already have a solution.

A lot to learn indeed.