Posts Tagged ‘loop’

Printing Triangles in Java

Featured, Java 23.11.2015 1 Comment

A common problem many new Java developers is to write a program that prints out a triangle. There a lots of variations on this problem but lets start with a simple case and then have a look at some possible variations. Probably the simplest case is a left aligned triangle of stars that looks like…

Read More

Bubble Sort

The Bubble Sort algorithm gets it’s name from the fact that on each pass, one element of the array ‘bubbles’ it’s way through to it’s correct (sorted) position in the array. Rather than me try and explain the details the following video gives a great visual demonstration of how the algorithm works. The source code…

Read More

Selection Sort

Java 19.6.2015 No Comments

When you are learning Java you are very likely going to be asked to write a program to sort values. We will start with one of the simpler sort algorithms, the Selection Sort (also known as an Exchange Sort). It’s not a particularly efficient algorithm and really only suitable for small lists.

Read More

How can I iterate through all dates in a range?

Java 23.11.2013 No Comments

Ever wanted to iterate through a range of Date’s? We can use Iterator’s for looping through the elements of a Collection, but for Date’s we need to implement the Date arithmetic required. The following class shows how we can create our own Iterator implementation to easily iterate over a range of Date’s, one day at…

Read More

Using a List instead of an array

Java 8.3.2012 2 Comments

Lists and arrays can both be used to store ordered collections of data. Both have their strengths and weakness which we shall discuss in a later post. Previously we showed you how to generate Pascals Triangle and in that Java example we used arrays to represent each row of the triangle. The following code shows…

Read More

High Low Guessing Game

Java 17.5.2011 5 Comments

The assignment here is to write a simple game where the user tries to guess a randomly selected number. After each guess the application tells the user if the guess is too high or too low. This is repeated until the user finally guesses the number. First thing we need is to generate a random…

Read More


Featured, Java 13.4.2011 1 Comment

A popular problem is how to determine if a String is a Palindrome or not, the following will also assume we are only dealing with Palindromes of single words ie. no spaces or punctuation. The way to determine this is to compare the characters on the left and right ends of the String. If they…

Read More

Money Change Breakdown

Java 28.3.2011 9 Comments
Money Change Breakdown

What is the smallest number of coins (or notes) that are required to give a specified amount of change? This is the next problem we will address. To solve this problem we use basically the same process a shop keeper would when giving change to customers. That is we look for the largest denomination coin/note…

Read More

Pascals Triangle

Java 13.2.2011 14 Comments

Write a Java application that prints the first 10 lines of Pascals Triangle. Each row of a Pascals Triangle can be calculated from the previous row so the core of the solution is a method that calculates a row based on the previous row which is passed as input. Once we have that it is…

Read More

Prime numbers

Java 21.1.2011 No Comments

The assignment here is to calculate all prime numbers less than 100. The solution provided uses the following to determine if a given number is prime. 2 is prime Any number divisible by 2 is not prime If the number is divisible by any odd number then it is not prime Otherwise it is prime…

Read More