Professional Documents
Culture Documents
OBJECT ORIENTED
PROGRAMMING
Vasantha Kumar V ,AP/CSE
Arrays
int [] age;
age = new int[3];
Assign Value :
age [0] = 10;
age [1] = 20;
age [2] = 5;
Array Length
To find out how many elements an array has, use the length property.
Assign Value :
• arr[0][0]=1; • arr[1][2]=6;
• arr[0][1]=2; • arr[2][0]=7;
• arr[0][2]=3; • arr[2][1]=8;
• arr[1][0]=4; • arr[2][2]=9;
• arr[1][1]=5;
Example of Multidimensional Array
class Testarray3 {
public static void main(String args[]) {
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
} }}
Jagged Array
An array of arrays with different number of columns.
class TestArrayCopyDemo {
public static void main(String[] args) {
char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e', 'i', 'n', 'a', 't', 'e', 'd' };
char[] copyTo = new char[7];
System.arraycopy(copyFrom, 2, copyTo, 0, 7);
System.out.println(new String(copyTo));
}}
Cloning an Array
class Testarray1 System.out.println("Printing clone of the array:");
{ int carr[]=arr.clone();
public static void main(String args[]) for(int i:carr)
{ System.out.println(i);
int arr[]={33,3,4,5};
System.out.println("Printing original array:"); System.out.println("Are both equal?");
System.out.println(arr==carr);
for(int i:arr) }}
System.out.println(i);
Addition of 2 Matrices
class Testarray5{
public static void main(String args[]){
int a[][]={{1,3,4},{3,4,5}};
int b[][]={{1,3,4},{3,4,5}};
for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
c[i][j]=a[i][j]+b[i][j];
System.out.print(c[i][j]+" ");
}
System.out.println();
} }}
Multiplication of 2 Matrices
public class MatrixMultiplicationExample{
public static void main(String args[]){
int a[][]={{1,1,1},{2,2,2},{3,3,3}};
int b[][]={{1,1,1},{2,2,2},{3,3,3}};
int c[][]=new int[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=0;
for(int k=0;k<3;k++) {
c[i][j]+=a[i][k]*b[k][j];
}
System.out.print(c[i][j]+" ");
}
System.out.println();
} }}
Method
Access Specifier
Access Specifier: Access specifier or modifier is the access type of the method. It
specifies the visibility of the method. Java provides four types of access specifier:
• Public: The method is accessible by all classes when we use public specifier in our
application.
• Private: When we use a private access specifier, the method is accessible only in the
classes in which it is defined.
• Protected: When we use protected access specifier, the method is accessible within
the same package or subclasses in a different package.
• Default: When we do not use any access specifier in the method declaration, Java
uses default access specifier by default. It is visible only from the same package only.
Types of Method
import java.util.Scanner;
public class EvenOdd
{
public static void main (String args[])
{
Scanner scan=new Scanner(System.in);
System.out.print("Enter the number: ");
int num=scan.nextInt();
findEvenOdd(num);
}
Adding 2 numbers
package pack;
public class A {
public static void main(String[] args) {
msg();
}
public static void msg()
{
System.out.println("Hai");
}}
Example – 2 – importing a package in
another package
package mypack;
import pack.*;
public class B{
public static void main(String[] args){
A obj = new A();
obj.msg();
}}
Example – 3 – importing a package
import pack.A;
class C
{
public static void main(String[] args){
A ob = new A();
ob.msg();
}}
Javadoc Comments
Example :
javadoc MyClass.java
General Order of Tags
Exception Tag
Form: @throws exception description
Used Where: Method comments.
Used For: Indicates any exceptions that the method might throw and the
possible reasons for this exception occurring.
See Class Tag
Form: @see classname
Used Where: Any item being commented.
Used For: If another class may help provide clarity this tag may be used to
provide a link to that class.
Javadoc Comments