You are on page 1of 41

CS 8392

OBJECT ORIENTED
PROGRAMMING
Vasantha Kumar V ,AP/CSE
Arrays

• An array sequential collection of elements of the same data


type.
• Arrays are used to store multiple values in a single variable,
instead of declaring separate variables for each value.
• To declare an array, define the variable type with square
brackets.
Define an Array
Syntax of declaring array variables is:
datatype[] identifier; //preferred way
or
datatype identifier[];
Example :
char[] refVar;
int[] refVar;
String[] cars;
Initialize an Array

int age[5] = {22,25,30,32,35};


Initialize an Array

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.

int age[5] = {22,25,30,32,35};


System.out.println(age.length);
Accessing Array Element

int age[5] = {22,25,30,32,35};


for (int i = 0; i < age.length; i++)
{
System.out.println(age[i]);
}
Multidimensional Arrays

Data is stored in row and column-based index.


• dataType[][] arrayRefVar;
(or)
• dataType arrayRefVar[][];
(or)
• dataType []arrayRefVar[];
Initialize a Multidimensional Arrays

int[][] arr=new int[3][3];//3 row and 3 column

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 TestJaggedArray for (int i=0; i<arr.length; i++)


{ {
public static void main(String[] args) for (int j=0; j<arr[i].length; j++)
{ {
int arr[][] = new int[3][]; System.out.print(arr[i][j]+" ");
arr[0] = new int[3]; }
arr[1] = new int[4]; System.out.println();
arr[2] = new int[2]; }
} }
int count = 0;
for (int i=0; i<arr.length; i++)
for(int j=0; j<arr[i].length; j++)
arr[i][j] = count++;
Copying an Array
arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

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}};

int c[][]=new int[2][3];

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

There are two types of methods in Java:


• Predefined Method
• User-defined Method
Predefined Method - Example

public class Demo


{
public static void main(String[] args)
{
System.out.print("The maximum number is: " + Math.max(9,7));
}
}
User-defined Method - Example

public static void findEvenOdd(int num)


{
if(num%2==0)
System.out.println(num+" is even");
else
System.out.println(num+" is odd");
}
Import a 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

public class Addition public static int add(int n1, int n


{ 2)
public static void main(String[] args) {
{ int s;
int a = 19; s=n1+n2;
int b = 5; return s;
int c = add(a, b); } }
System.out.println("The sum of a and b is= " +
c);
}
Instance Method

public class IME {


public static void main(String [] args) {
IME obj = new IME();
System.out.println("The sum is: "+obj.add(12, 13));
}
int s;
public int add(int a, int b) {
s = a+b;
return s;
}}
Java Package

• A java package is a group of similar types of classes,


interfaces and sub-packages.
• Package in java can be categorized in two form, built-in
package and user-defined package.
• There are many built-in packages such as java, lang, awt,
javax, swing, net, io, util, sql etc.
Java
Package
Advantage of Package

• 1) Java package is used to categorize the classes and


interfaces so that they can be easily maintained.
• 2) Java package provides access protection.
• 3) Java package removes naming collision
Simple example of package

The package keyword is used to create a package in java.


package mypack;
public class Simple{
public static void main(String args[]){
System.out.println("Welcome to package");
} }
Compile :
javac -d directory javafilename
How to
compile For example
and run
java javac -d . Simple.java
package Run :
java mypack.Simple
Example – 1 – Creating a package

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

• Javadoc Comments are specific to the Java language and


provide a means for a programmer to fully document his / her
source code as well as providing a means to generate an
Application Programmer Interface (API) for the code using the
javadoc tool that is bundled with the JDK.
• These comments have a special format which we will discuss in
this section and then in the following section we will look at
how to use the javadoc tool to generate an API.
Running The Javadoc Tool

The format for running the tool is:


javadoc [options] [packagenames] [sourcefiles] [classnames] [@files]

Example :
javadoc MyClass.java
General Order of Tags

The general order in which the tags occur is as follows:


• @author
• @version
• @param
• @return
• @throws
• @see
• @since
• @deprecated
Javadoc Comments
Author Tag
• Form: @author name
• Used Where: Interface and Class comments.
• Used For: Giving the names of the authors of the source code. You should use the
full name of the author or "unascribed" when the author is unknown. Authors are
listed in chronological order, with the creator of the class or interface being listed
first.
Since Tag
• Form: @since version
• Used Where: Interface and Class comments.
• Used For: Indicates the version of the source code that this item was introduced. It is
usually just a version umber but may also contain a specific date.
Javadoc Comments
Version Tag
• Form: @version description
• Used Where: Interface and Class comments.
• Used For: Describes the current version number of the source code. This is
often simply a version number including only the major and minor number
and not build number. Some instances also include a date.
Deprecated tag
• Form: @deprecated
• Used Where: Interface, class and method comments.
• Used For: Used to indicated that an item is a member of the deprecated API.
Deprecated items should not be used and are merely included for
backwards compatibility.
Javadoc Comments
Parameter Tag
• Form: @param name description
• Used Where: Method comments.
• Used For: Describes a method parameter. The name should be the formal
parameter name. the description should be a brief one line description of the
parameter.
Return Tag
• Form: @return description
• Used Where: Method comments.
• Used For: Describe the return value from a method with the exception of void
methods and con tructors.
Javadoc Comments

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

See Class Member Tag


Form: @see classname#member
Used Where: Any item being commented.
Used For: If another class's members may provide additional
clarity this tag can be used to link to that class's member.
Specifying Visibility
By default, the Javadoc tool will document both public and protected members of an API. Sometimes
you might want to show the private members as well or only show public members. In order to do this
you can tell the Javadoc tool which member accessibility level to document.
There are actually four types of visibility that you can specify:
• public - Will naturally only document public members, private and protected members are not shown.
• protected - Documents both public and protected members and not private members. This is the
default.
• package - Similar as protected but also shows package classes and members.
• private - Displays all members whether they are private or not.

An example of using this method:


javadoc -private MyClass.java
THANK YOU

You might also like