Array as parameters

Hope you already gone thru basic of arrays.
An array can be sent as parameter to a constructor or a method. Also an array can be returned by a method.
Below example shows the same.


class Student{
	int marks[];
	
	Student(int m[])
	{
		marks = m;
	}
	
	private int getTotal(){
		int sum = 0;
		for(int i=0;i

Below is an example showing a method returning an array.

public class StudentProgram {
	public static void main(String arg[])
	{
		int gmp[] = getArray();
		
		for(int item:gmp)
		{
			System.out.println(item);
		}
	}
	
	
	private static int[] getArray()
	{
		int smp[] = {45,64,53,26};
		
		return smp;
	}
}

You may also like to read:
array example
how to sort an array in java

Package and interface example

Assignment Problem:
Create a class named Circle which can take radius as constructor parameter, and has methods which calculates and returns diameter, circumference and surface area. Write another class named MainCircle, which has main method, and creates an object of Circle, and invokes all methods of Circle class, and print the return values?

Solution:

package p2;

public interface ICircle {
public final float pi=3.14f;
   public float getDiameter();
   public float getcircumference();
   public  float getarea();    
}
package p1;
import p2.ICircle;
public class CircleImpl implements ICircle{
        float radius;
    public CircleImpl(float radius){
        this.radius=radius;
    }
    public float getDiameter(){
        float diameter=2*radius;
        System.out.println("The diameter is: "+diameter);
        return diameter;
    }
    public float getcircumference(){
        float circumference=2*pi*radius;
        System.out.println("The circumference is: "+circumference);
        return circumference;
    }
    public float getarea(){
        float area=pi*radius*radius;
        System.out.println("The area is: "+area);
        return area;
    }
}

Below is main method, which creates object of CircleImpl class, and invokes its methods

package p3;
import p1.CircleImpl;
public class MainCircle {
    public static void main(String[]args){
CircleImpl obj=new CircleImpl(3f);
obj.getDiameter();
obj.getcircumference();
obj.getarea();
    }
}

Click here to download this code, on Eclipse
You may also like to read:
What is a package
What are access specifiers