Is Java easy?

Java isn’t easy. It’s often more complex than needed. But being easy for beginners shouldn’t be a design goal with high priority.

Should a programming language be easy?

I don’t know why so many educational establishments use Java to teach programming. They often start with Java in the first semester. Even the Oracle tutorials expect the reader to already know some basics.  Java was designed for programmers who already know C/C++ as seen in this example.

The main method is similar to the main function in C and C++; it’s the entry point for your application and will subsequently invoke all the other methods required by your program. [Lesson: A Closer Look at the “Hello World!” Application]

Continue reading “Is Java easy?”

Misconceptions about Dates and Time

Was 1900 a leap year and does every minute have 60 seconds?

In Java we have a new API for Date and Time since Java SE 8. Before that, there was already an API with mutable dates and fore some reason there was a date just for SQL.
Since many were not happy with it there was Joda-Time, which later became the basis for JSR-310 and is now found in the package “java.time”.

Continue reading “Misconceptions about Dates and Time”

Now free of ads

I have my own blog on my own domain now. No more ads.

I have moved the blog from wordpress.com to my own domain. It’s on a managed server on which I run some other sites. So there are no more ads.

The new URL of this blog is:
https://humanoid-readable.claude-martin.ch

This used to be on humanoidreadable.wordpress.com. All old URLs redirect to the new blog as long as I pay them for this service. 

I still have to fix some of the posts. Somehow the format of wordpress keeps changing. Now they have “blocks”, which is nice. But I have to fix all posts that contain code (that’s most of them). Until then the code will contain &lt; instead of < and so on. But at least I now have full control and you don’t get any annoying ads.

Claude

Stalin-Sort

Stalin-Sort using divide and conquer.

I just love this idea:

I     came up with a single pass O(n) sort algorithm I call Stalin-Sort. U iterate   down the list of elements checking if they are in order. Any element which is  out of order is eliminated. At the end u have a sorted list

But when you have an input such as [42,1,2,3,4,5,6] you just get
[42] instead of [1,2,3,4,5,6]. You can get a better result if you use divide and conquer. And that is something Stalin would do, don’t you think? So I implemented a recursive solution in Java. It tries all possible results, which is not a single pass and therefore defeats the purpose of having a fast O(n) algorithm. And my implementation isn’t even in-place. A new data structure has to be created. That’s why I also added an implementation using a linked list, which is what Mathew describes.

The code is on pastebin: https://pastebin.com/rEmvZSiA

Recursive FizzBuzz

FizzBuzz in two lines of code.

The only import you need is one for System.out:

import static java.lang.System.out;

Simple two-liner:

static void f(int a, int z) {
	out.println(a % 15 < 1 ? "FizzBuzz" : a % 3 < 1 ? "Fizz" : a % 5 < 1 ? "Buzz" : a);
	if (a < z) f(1 + a, z);
}

Without using the literal “FizzBuzz”:

static void g(int a, int z) {
  int x = 0;
  if (a % 3 < 1) { out.print("Fizz"); ++x; }
  if (a % 5 < 1) { out.print("Buzz"); ++x; }
  if (x < 1) out.print(a);
  out.println();
  if (a < z) g(1 + a, z);
}

Note that I use x<1 instead of x==0 to save a single character.