Friday, May 10, 2019

Less Java, more Flutter and Dart

As you can see, I haven't updated this blog in quite some time!  Schools in South Africa have, for some reason, switched to using mostly Delphi and the Java space grew quiet.  As for my business, I moved from Java to Scala and Kotlin, especially for mobile development on Android.

Of late, I've been making use of Dart and Flutter, with a bit of Kotlin and Swift, to develop mobile applications. You can find some more information and programming tips at https://www.nofuss.co.za. As always, I'm happy to try and assist where I can!

Friday, September 28, 2012

Formatting blog code snippets

I use http://codeformatter.blogspot.com/ to format the code snippets. It's brilliant, do give it a try if you are posting code to your blog.

How to determine the next occurrence of a day of the week in Java

I recently ran into a question around how to determine the next occurrence of a particular day of the week in Java. For example, if today is Tuesday, how do I figure out when the next Saturday is? Or, if it's Saturday, when is the next Wednesday?

Sounds simple enough, right? Turns out, it's not that easy in Java! Here's my solution:


 public static Date getNextOccurenceOfDay(Date today, int dayOfWeek) {  
  Calendar cal = Calendar.getInstance();  
  cal.setTime(today);  
  int dow = cal.get(Calendar.DAY_OF_WEEK);  
  int numDays = 7 - ((dow - dayOfWeek) % 7 + 7) % 7;  
  cal.add(Calendar.DAY_OF_YEAR, numDays);  
  return cal.getTime();  
 }  

Turns out that the % (mod) in Java doesn't deal with negative numbers that well, but by using the trick of doing a double-mod, you get the right answer!

You simply call this method with the starting date, and then tell it to target, for example, Calendar. SATURDAY, and it will give you the date of the next Saturday!

Friday, September 21, 2012

Finding duplicate numbers with Binary Math in Java

I went for an interview once, and was asked the following question:

Given a sequence of numbers, from 1 to 1000, where only one number is duplicated, how would I proceed to find the duplicate number?

After I solved the problem using a basic loop that just checks if it's seen this number before using a hash table, the interviewer asked if I could improve my answer using XOR math. I didn't quite get it right, this is the solution they showed me :

int numbers[] = {4,2,3,4,5,6,7,8,1,10,11,12,13,14,9};

for (int pos = 1; pos < numbers.length; pos++)
{
  numbers[pos] = numbers[pos] ^ numbers[pos-1] ^ pos;
}

System.out.println("Duplicate is : " + numbers[numbers.length-1]);


This bit of Java code loops through the array and finds the duplicate number. Of course, this only works for positive integers, I've not tested it with negative numbers and I know it doesn't work with floats.

So, there you go, a good use for binary math in Java!

Tuesday, September 18, 2012

Learning Python with Umonya

It's time for some more Python fun!  Umonya is busy gearing up for another basic Python course aimed at high school pupils.

I quote from their website :

"Umonya will be having a course on 12-14 October 2012 where we will teach 100 High School children how to program in Python. It will be taking place during Cape Town's first ever Software week."

If you are interested, please visit their website at http://www.umonya.org/ to learn more.

Thursday, June 7, 2012

Counting words in a string

I had a question this morning from a student, asking when using a pattern matcher, if he could count the number of "hi" words in a string that didn't start with an "x".

Of course you can!


import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CountOccurences {
  
  // Our test string has 5 occurences.
  private static String input_string = "hi lo and xhi xhi hihi loxhihixhihi";
  
  public static void main(String args[]) {
    int count = 0;
    Pattern pattern = Pattern.compile("[^x]*hi");
    Matcher matcher = pattern.matcher(input_string);
    
    while (matcher.find()) {
      count++;
    }
    
    System.out.println("The final count is " + count);
  } 
}
  
Now I'll leave it as an exercise for you to go read up on Regular Expressions...

Friday, May 11, 2012

JavaK now has a GitHub repository. We will be uploading all of our source code there, as well as the various tutorials that we have released.

Find the GitHub entry for JavaK here - https://github.com/ewaldhorn/javak