JAVA: An Introduction (I)

1. A First Program

// file: First.java

class First

{

public static void main(String[] arg)

{

System.out.println("Weicome to Java!!");

}

}

//file: Welcome.java

import java.applet.Applet;

import java.awt.Graphics;

public class Welcome extends Applet

{

public void paint(Graphics g)

{

g.drawString("Hi, JAVA!!",25,25);

}

}

<!—file: Welcome.html 

<HTML>

<h1<center>First JAVA applet</center</h1>

<applet code="Welcome.class" width=300 height=300>

</applet>

</BODY>

</HTML>

// File: Welcome.java

import javax.swing.JOptionPane;

public class Welcome{

public static void main(String [] arg)

{

JOptionPane.showMessageDialog(null,"Welcome to JAVA");

System.exit(0);

}

}

2. JAVA Basics

//File: JavaBasic.java

class JavaBasic{

public int n1, n2;

public double d1,d2;

public int sum1(int n)

{

int ans=0;

while(n >=1)

{

ans=ans+n;

n=n-1;

}

return ans;

}

public int sum2(int n)

{

int ans=0;

for(int i=1; i <=n; i++)

{

ans=ans+i;

}

return ans;

}

public void logic(double s1, double s2)

{

if(s1 > s2)

{

System.out.println("s1 is greater than s2");

}

else if ( s1 < s2)

{

System.out.println("s2 is greater than s1");

}

else

{

System.out.println("s1 and s2 are equal");

}

}

public static void main(String [] arg)

{

int a1=5, a2=10;

double b1=15.5, b2=12.3;

JavaBasic jb1=new JavaBasic();

jb1.n1=jb1.sum1(a1);

jb1.n2=jb1.sum2(a2);

jb1.logic(b1,b2);

jb1.logic(b2,b1);

jb1.logic(b1,b1);

System.out.println(jb1.n1);

System.out.println(jb1.n2);

}

}

  1. Data types:

Primitive Type / Description
boolean / true or false
char / 16-bit Unicode character
byte / Signed 8-bit integer ( to )
short / Signed 16-bit integer ( to )
int / Signed 32-bit integer ( to )
long / Signed 64-bit integer ( to )
float / 32-bit floating point
double / 64-bit floating point
  1. Control flow:

The syntax:

If (condition 1){ expressions}

else if (condition 2){ expressions}

.

.

.

else{ last expressions}

Note:

Multiple conditions can be used within if statements. The syntax is

if (condition 1 & condition 2){ expressions }

if(condition 1 || condition 2){ expressions }

||: or, &: and, !: not

3. Loops: for, while

The syntax:

while (condition){expressions}

for(init; continue condition; update){expressions}

3. Mathematical Computations

Math is a class in the core java.lang package. Static methods in class Math perform basic mathematical computations such as maximum, minimum, absolute value and numeric operations including exponential, logarithm, square root and trigonometric functions.

Any numeric type:

Math.abs(number)

Math.max(number1,number2)

Math.min(number1,number2)

Floating-point methods:

Math.pow(double x, double y) /
Math.exp(double x) /
Math.ceil(double x) / Return an integer not less than x
Math.floor(double x) / Return an integer not greater than x
Math.log(double x) / (natural log)
Math.sqrt(double x) /
Math.round(float x) / Round to nearest int
Math.round(double x) / Round to nearest long
Math.random() / A random number between 0.0 and 1.0

Floating-point members:

Math.E / Base of the natural lograithms
Math.PI /

Trigonometric Functions:

Math.sin(double x) /
Math.cos(double x) /
Math.tan(double x) /
Math.asin(double x) /
Math.acos(double x) /
Math.atan(double x) /
Math.atan2(double x,double y) /

Package java.math:

Provides classes for performing arbitrary-precision integer arithmetic (BigInteger) and arbitrary-precision decimal arithmetic (BigDecimal).

## File: MyMath.java

class MyMath{

public static void main(String[] args)

{

int a=Math.abs(-3);

int b=Math.max(3,4);

double a1=Math.pow(2.0,3.0);

double b1=Math.exp(2.0);

double c1=Math.ceil(3.5);

double c2=Math.ceil(-3.4);

double d1=Math.floor(3.5);

double d2=Math.floor(-3.4);

double e=Math.log(1);

double f=Math.sqrt(9.0);

long g1=Math.round(3.4);

long g2=Math.round(3.5);

long g3=Math.round(3.6);

double h=Math.random();

double i=Math.sin(Math.PI/2);

double j=Math.cos(Math.PI);

double k=Math.tan(Math.PI/4);

double l=Math.asin(0.0);

double m=Math.acos(1.0);

double n=Math.atan(1.0);

double o=Math.atan2(2.0,2.0);

System.out.println("a is "+a);

System.out.println("b is "+b);

System.out.println("a1 is "+a1);

System.out.println("b1 is "+b1);

System.out.println("c1 is "+c1);

System.out.println("c2 is "+c2);

System.out.println("d1 is "+d1);

System.out.println("d2 is "+d2);

System.out.println("e is "+e);

System.out.println("f is "+f);

System.out.println("g1 is "+g1);

System.out.println("g2 is "+g2);

System.out.println("g3 is "+g3);

System.out.println("h is "+h);

System.out.println(Math.E);

System.out.println(Math.PI);

System.out.println("i is "+i);

System.out.println("j is "+j);

System.out.println("k is "+k);

System.out.println("l is "+l);

System.out.println("m is "+m);

System.out.println("n is "+n);

System.out.println("o is "+o);

}

}

4. String, Array and Matrix

### MyArray.java

class MyArray{

public static void main(String[] args)

{

String s1="I am";

String s2=" a pig";

String s3=s1+s2;

String s4=new String(s3);

int [] a1=new int[3];

for(int i=0; i<3; i++)

{

a1[i]=i;

}

int [] a2={4,5,6};

double [][] m1=new double[2][2];

for(int k=0; k<2;k++)

{

for(int j=0; j<2; j++)

{

m1[k][j]=1.0;

}

}

double [][] m2={{1.0,2.0,3.0},{3.0,4.0,5.0}};

System.out.println(s1+s2);

System.out.println(s4);

System.out.println(a2[0]);

System.out.println(a1[2]+a2[1]);

System.out.println(a1.length);

System.out.println(m1[0][0]*m1[0][1]);

System.out.println(m2.length);

System.out.println(m2[0].length);

System.out.println(m2[1][1]+a1[2]);

}

## File MyMatrix.java

class MyMatrix{

public double [][] add(double m1[][],double m2[][])

{

int nr=m1.length;

int nc=m1[0].length;

double [][] m=new double[nr][nc];

for(int i=0; i<nr; i++)

{

for(int j=0; j<nc; j++)

{

m[i][j]=m1[i][j]+m2[i][j];

}

}

return m;

}

public double [][] multiply(double m1[][],double m2[][])

{

int nr=m1.length;

int nc=m2[0].length;

int p=m1[0].length;

double [][] m=new double[nr][nc];

for(int i=0; i<nr; i++)

{

for(int j=0; j<nc; j++)

{

m[i][j]=0.0;

for(int k=0; k<p; k++)

{

m[i][j]+=m1[i][k]*m2[k][j];

}

}

}

return m;

}

public static void main(String[] args)

{

double [][] m1={{1.0,3.0},{2.0,4.0}};

double [][] m2={{1.0,2.0},{3.0,4.0}};

MyMatrix mm=new MyMatrix();

double [][] madd=mm.add(m1,m2);

double [][] mmul=mm.multiply(m1,m2);

System.out.println(madd[0][0]+" "+madd[0][1]);

System.out.println(madd[1][0]+" "+madd[1][1]);

System.out.println(mmul[0][0]+" "+mmul[0][1]);

System.out.println(mmul[1][0]+" "+mmul[1][1]);

}

}

5. Inheritance

// File: MyJavaBasic.java

class MyJavaBasic extends JavaBasic{

public int myn1, myn2;

public double d1,d2;

public int sum(int n1,int n2)

{

int ans;

ans=super.sum1(n1)+super.sum2(n2);

return ans;

}

public int sum(int n1,int n2,int n3)

{

int ans;

ans=super.sum1(n1)+super.sum1(n2)+super.sum1(n3);

return ans;

}

public void logic(double s1, double s2)

{

if(s1 >= s2)

{

System.out.println("s1 is greater or equal to s2");

}

else

{

System.out.println("s1 is smaller than s2");

}

}

public static void main(String [] arg)

{

int s1, s2;

int a1=5, a2=10, a3=5;

double b1=15.5, b2=12.3;

MyJavaBasic jb1=new MyJavaBasic();

JavaBasic jb2=new JavaBasic();

jb1.myn1=jb1.sum(a1,a2);

jb1.myn2=jb1.sum(a1,a2,a3);

s1=jb1.sum1(a1);

s2=jb1.sum2(a2);

jb2.logic(b1,b2);

jb1.logic(b1,b2);

jb1.logic(b2,b1);

System.out.println(jb1.myn1);

System.out.println(jb1.myn2);

}

}

Note:

extendsis the keyword for inheritance.

1