Sunday, 15 February 2015

Java Program to Find Palindromes

A palindrome is a number which has the same value whether you read its digits forwards or backwards e.g. 8, 66, 525 etc. The program below shows how you can use Java to find them. It converts the number to a string, reverses the string then tests whether the reversed string is the same as the original:

andrew@UBUNTU:~/Java$ cat palindrome.java
class palindrome     
  {
  public static void main(String args[])
    {
    for (int a=1;a<1000;a++)
      {
      String s1 = String.valueOf(a);
      String s2 = new StringBuffer(s1).reverse().toString();
      if (s1.equals(s2))
        {
        System.out.println(a + " is a palindrome");
        }
      }
    }
  }
andrew@UBUNTU:~/Java$ javac palindrome.java

andrew@UBUNTU:~/Java$ java palindrome
1 is a palindrome
2 is a palindrome
3 is a palindrome
4 is a palindrome
5 is a palindrome
6 is a palindrome
7 is a palindrome
8 is a palindrome
9 is a palindrome
11 is a palindrome
22 is a palindrome
33 is a palindrome
44 is a palindrome
55 is a palindrome
66 is a palindrome
77 is a palindrome
88 is a palindrome
99 is a palindrome
101 is a palindrome
111 is a palindrome
121 is a palindrome
131 is a palindrome
141 is a palindrome
151 is a palindrome
161 is a palindrome
171 is a palindrome
181 is a palindrome
191 is a palindrome
202 is a palindrome
212 is a palindrome
222 is a palindrome
232 is a palindrome
242 is a palindrome
252 is a palindrome
262 is a palindrome
272 is a palindrome
282 is a palindrome
292 is a palindrome
303 is a palindrome
313 is a palindrome
323 is a palindrome
333 is a palindrome
343 is a palindrome
353 is a palindrome
363 is a palindrome
373 is a palindrome
383 is a palindrome
393 is a palindrome
404 is a palindrome
414 is a palindrome
424 is a palindrome
434 is a palindrome
444 is a palindrome
454 is a palindrome
464 is a palindrome
474 is a palindrome
484 is a palindrome
494 is a palindrome
505 is a palindrome
515 is a palindrome
525 is a palindrome
535 is a palindrome
545 is a palindrome
555 is a palindrome
565 is a palindrome
575 is a palindrome
585 is a palindrome
595 is a palindrome
606 is a palindrome
616 is a palindrome
626 is a palindrome
636 is a palindrome
646 is a palindrome
656 is a palindrome
666 is a palindrome
676 is a palindrome
686 is a palindrome
696 is a palindrome
707 is a palindrome
717 is a palindrome
727 is a palindrome
737 is a palindrome
747 is a palindrome
757 is a palindrome
767 is a palindrome
777 is a palindrome
787 is a palindrome
797 is a palindrome
808 is a palindrome
818 is a palindrome
828 is a palindrome
838 is a palindrome
848 is a palindrome
858 is a palindrome
868 is a palindrome
878 is a palindrome
888 is a palindrome
898 is a palindrome
909 is a palindrome
919 is a palindrome
929 is a palindrome
939 is a palindrome
949 is a palindrome
959 is a palindrome
969 is a palindrome
979 is a palindrome
989 is a palindrome
999 is a palindrome
andrew@UBUNTU:~/Java$

Saturday, 14 February 2015

Project Euler Problem 1

If you are starting to get to grips with Java and would like to use it to solve a mathematical problem, go to Project Euler I joined recently and have just solved problem 1. As usual, click on the image to enlarge it and bring it into focus if you need to:



Saturday, 7 February 2015

Java "this" Prefix

If you have a class and a constructor method with parameter(s), you might want to use the same names for the variables in the class as you use for the parameters passed to the method. To distinguish between the two, you can prefix the variables belonging to the class with the word this. In the example below, I create a class called Square. It has a variable called width. The constructor method accepts a parameter with the same name. It then displays the value of the parameter and the initial value of the class variable. After that, it assigns the parameter value to the class variable and redisplays the class variable to show that the assignment has worked: 

Java > cat Square.java
class Square
  {
  double width;
  Square (double width)
    {
    System.out.println("width = " + width);
    System.out.println("this.width = " + this.width);
    System.out.println("Assigning parameter value");
    this.width = width;
    System.out.println("this.width = " + this.width);
    }
  }
Java > javac Square.java
Java > cat SquareExample.java
public class SquareExample
  {
  public static void main(String args[])
    {
    Square s1 = new Square(5);
    }
  }
Java > javac SquareExample.java
Java > java SquareExample
width = 5.0
this.width = 0.0
Assigning parameter value
this.width = 5.0
Java >

Wednesday, 4 February 2015

Overloaded Java Constructors

The example below shows a class with an overloaded constructor:

Java > cat Rectangle.java
class Rectangle
  {
  double width;
  double height;
  Rectangle()
    {
    width = 2;
    height = 3;
    }
  Rectangle(double w, double h)
    {
    width = w;
    height = h;
    }
  }
Java > javac Rectangle.java
Java >

If you have a class like this, when you create members, you have a choice. You can accept the default settings or you can override them by supplying parameters. In the example below, r1 takes the default settings whereas r2 uses parameters to change the width to 4 and the height to 5:

Java > cat RectangleExample.java
public class RectangleExample
  {
  public static void main(String args[])
    {
    Rectangle r1 = new Rectangle();
    System.out.println("r1 width = " + r1.width);
    System.out.println("r1 height = " + r1.height);
    Rectangle r2 = new Rectangle(4,5);
    System.out.println("r2 width = " + r2.width);
    System.out.println("r2 height = " + r2.height);
    }
  }
Java > javac RectangleExample.java
Java > java RectangleExample
r1 width = 2.0
r1 height = 3.0
r2 width = 4.0
r2 height = 5.0
Java >

Tuesday, 3 February 2015

Java Constructors

If you have a class called, for example, Rectangle, you can add a method to it with the same name as the class. This method must have no return type, not even void. It is called a constructor. Here is an example:

Java > cat Rectangle.java
class Rectangle
  {
  double width;
  double height;
  Rectangle()
    {
    width = 2;
    height = 3;
    }
  }
Java > javac Rectangle.java
Java >

The purpose of a constructor is to give default values to a class member when it is created. You can see what I mean below, where an instance of Rectangle is created. It automatically has a width of 2 and a height of 3:

Java > cat RectangleExample.java
public class RectangleExample
  {
  public static void main(String args[])
    {
    Rectangle r1 = new Rectangle();
    System.out.println("width = " + r1.width);
    System.out.println("height = " + r1.height);
    }
  }
Java > javac RectangleExample.java
Java > java RectangleExample
width = 2.0
height = 3.0
Java >

Monday, 2 February 2015

Java Curly Braces

I wanted to produce the output below 5 times:

Flip
Flop

So I tried to do it like this:

andrew@UBUNTU:~/Java$ cat Curly_Braces1.java
class Curly_Braces1
  {
  public static void main(String args[])
    {
    for (int x=1; x<=5; x++)
      System.out.println("Flip");
      System.out.println("Flop");
    }
  }
andrew@UBUNTU:~/Java$ javac Curly_Braces1.java
andrew@UBUNTU:~/Java$ java Curly_Braces1
Flip
Flip
Flip
Flip
Flip
Flop
andrew@UBUNTU:~/Java$


Java only executes one line of code each time a for loop executes. The line displaying Flop was therefore only executed once, after the for loop had finished.

To achieve the desired effect, I started the code to be executed with a left-hand curly brace and finished it with a right-hand curly brace. Java then treated the two lines as a single block of code and executed them both for every iteration of the loop:

andrew@UBUNTU:~/Java$ cat Curly_Braces2.java
class Curly_Braces2
  {
  public static void main(String args[])
    {
    for (int x=1; x<=5; x++)
      {
      System.out.println("Flip");
      System.out.println("Flop");
      }
    }
  }
andrew@UBUNTU:~/Java$ javac Curly_Braces2.java
andrew@UBUNTU:~/Java$ java Curly_Braces2
Flip
Flop
Flip
Flop
Flip
Flop
Flip
Flop
Flip
Flop
andrew@UBUNTU:~/Java$

Sunday, 1 February 2015

Java is Case Sensitive

Java is case sensitive so you can have two different variables in a program, one called xyz and another called XYZ. You can see what I mean in the example below:

andrew@UBUNTU:~/Java$ cat Case_Sensitive.java
class Case_Sensitive   
  {
  public static void main(String args[])
    {
    int xyz = 123;
    int XYZ = 234;
    System.out.println("xyz = " + xyz);
    System.out.println("XYZ = " + XYZ);
    }
  }
andrew@UBUNTU:~/Java$ javac Case_Sensitive.java
andrew@UBUNTU:~/Java$ java Case_Sensitive
xyz = 123
XYZ = 234
andrew@UBUNTU:~/Java$