tag:blogger.com,1999:blog-25978418152809984182024-03-12T06:45:03.420+01:00This Thread<i>Tutorial-like examples and chats while coding in Java, Python, C, C++, ...</i>Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.comBlogger833125tag:blogger.com,1999:blog-2597841815280998418.post-9688107760300315622021-06-28T16:23:00.000+02:002021-06-28T16:23:13.042+02:00HackerRank Fraudulent Activity NotificationsDon't be too impressed by its name, this problem boils down to calculating the median for a moving window in a list of integers. It is not conceptually difficult, it just asks some attention in choosing the algorithm, since they are doing some check on the resulting performances. I do suggest you to try it yourself going to Sorting - Fraudulent Activity Notifications on HackerRank and then Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com5tag:blogger.com,1999:blog-2597841815280998418.post-81370433561705742992021-03-05T12:39:00.002+01:002021-03-05T12:40:11.795+01:00Heroku Web App Basic settingShopping list for setting up a Java EE Web App to be deployed on Heroku.
Assuming Maven, Java 11, Tomcat 9 as web server.
pom.xml: execution
Add to the build - plugins - plugin for maven-dependency-plugin, in the executions
<execution>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-54207727269541985352021-02-21T11:27:00.001+01:002021-02-21T11:36:00.609+01:00HackerRank Array ManipulationThink of an array of 64-bit integers initialized to zero. It could be quite large, up to ten million elements. We change it applying up to 20 thousand times a series of simple operation: add to all its elements in a specified range an increase that could be in the order of billions. At the end, we want to know the max value in the array.
Please, try it on HackerRank before checking the Java Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-68532287768685374092020-12-24T12:10:00.002+01:002020-12-24T12:10:53.999+01:00Conditionally disabling/enabling JUnit 5 Jupiter testI have an application that uses JDBC for database connectivity, and I want it configurable for running on Oracle DB, MySql, or SQLite.
As we are well aware, any DBMS is entitled of its own approach to SQL . Testing features in this situation could become complex. Luckly, the new (JUnit 5.7) @DisabledIf and @EnabledIf annotations are here to help us.
Let's see what we can use when we deal with Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-87108276552038512632020-07-06T11:04:00.001+02:002020-07-06T11:15:48.387+02:00Creating an executable JAR with MavenI have a very simple mavenized Java project that results in a JAR, both in its "slim" and "fat" flavor. Here I want to make the result executable and, along the way, get rid of the "slim" one.
The expected result is a JAR file that I could run like this
java -jar myApp.jar
To to that, I have to provide a manifest in the JAR, pointing out to the class that should be run. This is a task that is Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-21930981729017925072020-05-03T17:46:00.001+02:002020-07-06T10:03:36.936+02:00Test on build with MavenLet's add some testing to the tiny mavenized Java app seen in the previous couple of posts.
Actually, the app is so tiny that there is that there's nothing to test. This is easily solved, adding a method to the Main class
public static String getLoggerClassName() {
return LOG.getClass().getName();
}
I plan to use JUnit 5 (aka Jupiter) as testing framework, asserting via Hamcrest, so I add Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-36387671058369742302020-05-03T15:00:00.000+02:002020-05-03T15:35:21.144+02:00Jar with dependencies through MavenI'm creating a Java desktop app, a very simple one, see the previous post if you don't believe me, using Maven as build manager. The second step here is adding a dependency, and let Maven build a "fat" jar including them, to ease its deployment.
Say that I just want to use Logback classic in my application. I search for it on MvnRepository, I stay away from the alpha version so I go for 1.2.3.
Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-14693308615995646092020-05-03T11:24:00.000+02:002020-05-03T11:32:00.477+02:00Simple Maven Eclipse ProjectWhat I'm about to do here is creating a simple desktop app using Apache Maven as build automation tool.
To follow me, ensure you have on your machine:
Java JDK - I use version 11, the currently latest available LTS, but the app is so simple that virtually any JDK will do
Eclipse IDE - Actually, on my machine I have Spring Tool Suite (STS) 4.6, but again, the job is so basic, that almost any Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-23582154710234325992019-06-29T12:27:00.001+02:002019-06-29T12:27:11.860+02:00At least one JAR was scanned for TLDs yet contained no TLDsAs soon as I added a jar to my Tomcat base web application, I got this new info log message.
INFO [main] org.apache.jasper.servlet.TldScanner.scanJars
At least one JAR was scanned for TLDs yet contained no TLDs
It makes sense, since it was a JDBC driver, and I would have been very surprised if it contained a TLD.
Let's see how to get rid of it.
Firstly, I read the rest of the logging message
Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-82423180390888602132019-06-11T12:32:00.000+02:002019-06-11T12:32:10.931+02:00Testing Spring Boot with JUnit5I decided that is time to move my tests in Spring app from JUnit 4 to Jupiter. It is not too complicated, given that Spring is already supporting JUnit 5, even though the default is still the previous version. However it requires being careful in a couple of steps.
Firstly, I have changed the POM in this way:
<dependency>
<groupId>org.springframework.boot</groupId>
<Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-88381362663681231152018-11-19T13:03:00.000+01:002018-11-19T13:03:16.080+01:00Another interesting MOOC for Java developerI've just completed the EdX MOOC Data Structures and Software Design by University of Pennsylvania. I would suggest it to someone with a high-beginner or lower-intermediate experience as Java software developer.
I think that the lessons would sound reasonably interesting even if your level is higher, however you could be get a bit bored by the exercises - kind of too simple. This is why I didn'tMannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-21237728985050729212018-11-09T11:35:00.000+01:002018-11-09T11:35:32.041+01:00A nice Java testing MOOCI've just finished the first week of the MOOC "Automated Software Testing: Practical Skills for Java Developers" provided by TU Delft on EdX. It looks to me they did a good job. Maybe, if you are a native English speaker, the speakers' accent could sometimes sound a bit weird. However, as continental European, I kind of enjoy it (and I know that my Italian accent sounds even funnier).
The courseMannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com1tag:blogger.com,1999:blog-2597841815280998418.post-7575533348295945492018-09-28T15:12:00.000+02:002018-09-28T15:18:43.874+02:00Autowired, Resource, InjectIn chapter three, Introducing IoC and DI in Spring, of Pro Spring 5, I have read the example about Using Setter Injection in the annotation flavor, where for the "renderer" service the Autowired annotation was used in its setter to inject the "provider" bean in its parameter.
A note hints that other annotations, Resource and Inject, could be used, but no example is provided. I thought it was Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-86777926469222182712018-09-24T14:30:00.000+02:002018-09-24T15:55:58.429+02:00HackerRank Array ManipulationWe have an array of integers of a given size, that could be in the order of tenth of millions. It is initialized with all zero elements.
We change it a few times, up to tenth of thousands, adding to given subintervals each time a positive number.
At the end, we want to know which is the highest values stored in the array.
This is a Data Structures HackerRank problem. Here below I show you a Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com1tag:blogger.com,1999:blog-2597841815280998418.post-26981110215604732612018-09-18T10:58:00.000+02:002018-09-18T11:10:28.523+02:00How to close a Spring ApplicationContextI've just finished reading chapter 2 Getting Started of Pro Spring 5. Nice and smooth introduction to Spring, and to Dependency Injection motivation.
Going through the provided examples, I had just a minor on the code provided. In a couple of cases, the created Spring contexts is stored in plain ApplicationContexts reference. This is normally considered a good way of writing code. We don't wantMannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com1tag:blogger.com,1999:blog-2597841815280998418.post-21370401884713680732018-09-15T13:14:00.000+02:002018-09-17T15:33:06.246+02:00The Gradle nature of Pro Spring 5I've started reading Pro Spring 5: An In-Depth Guide to the Spring Framework and Its Tools. It looks fun and interesting. The first issue I had was on having its code from GitHub working in my STS IDE. Here are a few notes on what I did to solve it, if someone else stumble in the same place.
First step was easy. After navigating through the File menu in this way ...
File
Import...
Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-14914457859683566332018-07-18T10:24:00.000+02:002018-07-18T10:42:28.586+02:00A thread-safe DateFormatter via ThreadLocalThe exercise 2-b at the end of chapter two from the book Java 8 Lambdas by Richard Warburton asks to wrap a DateFormatter in a ThreadLocal so to make it thread safe.
We have spotted in legacy code, designed to work in a single thread context, something like:
DateFormatter formatter = // ...
// ...
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 1970);
cal.set(Calendar.MONTH, Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-51423281411510732742018-07-02T14:20:00.001+02:002021-02-13T15:53:52.769+01:00HackerRank Java DequeueGiven n integers in input, consider each window in it sized m, where m is less or equal to n, and get the number of unique elements in each window. Return the maximum value among them.
This HackerRank Java Challenges problem is named Java Dequeue, a clear hint that a Deque would helpful to solve it elegantly.
The HackerRank settings implies that data are passed us by System.in. To develop a Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-75470333228758256022018-06-30T12:25:00.001+02:002021-02-22T20:35:09.018+01:00HackerRank Java SortWe have a Student class, having an int, a String (the name) and a double data member. We want read a bunch of Students from a data stream, sort them by the double - decreasing, then string, and then int. In the end we'll output just the student names.
This apparently boring little HackerRank problem shows how much more fun is Java since functional programming has been introduced in it.
I won't Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-87971605204411836462018-06-28T13:09:00.003+02:002021-02-23T19:32:16.735+01:00HackerRank Java 1D Array (Part 2)Don't be misled by its puzzling name, this fun little HackerRank problem has its main interest not in its implementation language nor in the use of the array data structure, but in the algorithm you apply to solve it.
There is a one dimensional board. Each cell in it could be set to zero (meaning free) or one. At startup the pawn is placed on the first cell to the left, and its job is going out Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-86004267167038937252018-06-21T11:27:00.000+02:002018-06-21T11:27:17.556+02:00Partitioning Souvenirs (patched)Given a list of integers, we want to know if there is a way to split it evenly in three parts, so that the sum of each part is the same than the other ones.
Problem given in week six of the edX MOOC Algs200x Algorithmic Design and Techniques by the UC San Diego.
My first solution, as you can see in a previous post (please check it out for more background information), was accepted in the MOOC Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com7tag:blogger.com,1999:blog-2597841815280998418.post-53585191725966970102018-05-10T13:22:00.000+02:002018-05-10T13:27:38.007+02:00HackerRank Deque-STLGiven an array of integers, find the max value for each contiguous subarray in it sized k. This HackerRank problem is meant to be solved in C++ and, as its name suggests, using a deque.
For instance, if we are given the array {3, 4, 6, 3, 4} and k is 2, we have to consider four subarrays sized:
{3,4} {4,6} {6,3} {3,4} And the expected solution is
{4, 6, 6, 4}An adapter
The original HackerRank Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-10323506813744119682018-04-13T12:16:00.003+02:002018-04-13T12:20:36.146+02:00HackerRank EqualWe have a list of integers, and we want to know in how many rounds we could make them all equal. In each round we add to all the items in the list but one the same number chosen among 1, 2, and 5. Pay special attention when you solve this problem on HackerRank. Currently (April 2018) its enunciation says each increase should be of one, three, or five. However, this is not what the solution is Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com2tag:blogger.com,1999:blog-2597841815280998418.post-1576078439559554982018-04-11T10:03:00.000+02:002018-04-11T10:08:01.993+02:00HackerRank Roads and LibrariesWe are given n nodes and a (possibly huge) number of edges. We are also given the cost of building a library in a city (i.e. a node) and a road (i.e. an edge). Based on these data we want to minimize the cost of creating a forest of graphs from the given nodes and edges, with the requirement that each graph should have a library on one of its nodes. This is a HackerRank problem on Graph Theory Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0tag:blogger.com,1999:blog-2597841815280998418.post-34258172775880735002018-04-10T13:44:00.000+02:002018-04-10T13:48:36.361+02:00Union FindI have a (possibly huge) bunch of edges describing a forest of graphs, and I'd like to know how many components it actually has. This problem has a simple solution if we shovel the edges in a union-find data structure, and then just ask it for that piece of information.
Besides the number of components, our structure keeps track of the id associated to each node, and the size of each component. Mannyhttp://www.blogger.com/profile/07393063644320426727noreply@blogger.com0