Showing posts with label method. Show all posts
Showing posts with label method. Show all posts

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 >

Tuesday, 27 January 2015

Java Method Overloading

At this stage, I don’t know why you would want to do this, but Java allows method overloading. This means that you can have two or more methods with the same name:

Java > cat my_class.java
class my_class
  {
  public void multiply (int x, int y)
    {
    int a = x * y;
    System.out.println("a = " + a);
    }
  public void multiply (int x, int y, int z)
    {
    int b = x * y * z;
    System.out.println("b = " + b);
    }
  public void multiply (double x, double y)
    {
    double c = x * y;
    System.out.println("c = " + c);
    }
  }
Java > javac my_class.java
Java >

The only difference the outside world can see between these methods is in the number and type(s) of parameters they expect to receive. When you use an overloaded method, you let Java know which version to use by passing the parameters it requires:

Java > cat test_my_class.java
public class test_my_class
  {
  public static void main(String args[])
   {
    my_class my_class1 = new my_class();
    int i=2, j=3, k=4;
    double m=2.5, n=3;
    my_class1.multiply (i, j);
    my_class1.multiply (i, j, k);
    my_class1.multiply (m, n);
    }
  }
Java > javac test_my_class.java
Java > java test_my_class
a = 6
b = 24
c = 7.5
Java >

Monday, 26 January 2015

Primitive Data Types are Passed to Methods by Value in Java

I created a class called my_class with a method called times_two. The method accepts an integer and multiplies it by 2:

Java > cat my_class.java
public class my_class
  {
  public void times_two(int a)
    {
    a = a * 2;
    System.out.println("a = " + a);
    }
  }
Java > javac my_class.java
Java >

I created a program called test_my_class. This sets up an instance of my_class called b. It then creates an integer variable called x and gives it a values of 2. Next, it passes x as a parameter to the times_two method, which multiplies it by 2 to give 4. Finally, it displays the original value, which is still 2. This is because the value of x is passed to the method, not a reference to its location in memory.

Java > cat test_my_class.java
public class test_my_class
  {
  public static void main(String args[])
    {
    my_class b = new my_class();
    int x = 2;
    b.times_two(x);
    System.out.println("x = " + x);
    }
  }
Java > javac test_my_class.java
Java > java test_my_class
a = 4
x = 2
Java >

Saturday, 24 January 2015

How to Pass Parameters to a Java Method

I created a simple class called two_numbers with a method called show_greater. The method accepts two integer parameters and displays the larger:

andrew@UBUNTU:~/Java$ cat two_numbers.java
public class two_numbers
  {
  public void show_greater(int arg1, int arg2)
    {
    int greater = arg1;
    if (arg2 > arg1) greater = arg2;
    System.out.println(greater);
    }
  }
andrew@UBUNTU:~/Java$ javac two_numbers.java
andrew@UBUNTU:~/Java$


Then I created  a program called two_numbers_test. This declares an object called compare in the two_numbers class. It then calls the show_greater method three times, comparing two integers each time to see which one is bigger:

andrew@UBUNTU:~/Java$ cat two_numbers_test.java
public class two_numbers_test
  {
  public static void main(String args[])
    {
    two_numbers compare = new two_numbers();
    compare.show_greater(0,1);
    compare.show_greater(2,1);
    compare.show_greater(3,3);
    }
  }
andrew@UBUNTU:~/Java$ javac two_numbers_test.java
andrew@UBUNTU:~/Java$ java two_numbers_test
1
2
3
andrew@UBUNTU:~/Java$

Saturday, 17 January 2015

A Java Class with a Method

A Java class can have a method. This is a piece of code which can be used on members of the class. In the example below, a class called Square is created. The Square class has a method called display_area, which calculates and displays the area of the square: 

andrew@UBUNTU:~/Java$ cat Square.java
public class Square
  {
  double width;
  double area;
  public void display_area()
    {
    area = width * width;
    System.out.println("Area = " + area);
    }
  }
class SquareExample
  {
  public static void main(String args[])
    {
    Square my_square = new Square();
    my_square.width = 5;
    my_square.display_area();
    }
  }
andrew@UBUNTU:~/Java$ javac Square.java
andrew@UBUNTU:~/Java$ java SquareExample
Area = 25.0
andrew@UBUNTU:~/Java$