AAC Java Lab 2017 June - Oct
AAC Java Lab 2017 June - Oct
1 Syllabus 2
2 Exam Pattern 4
3 Versions of Java 5
4 Installing JDK & Setting the path 6
5 Compiling, Running and Saving Java programs 7
6 Program Contents
7 Mini Projects
8 Viva Question Model
Syllabus
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
2
I. Basic Programs:
1. Write a java program to print the value in triangle shape.
2. Write a java program for matrix Multiplication.
3. Write a java program to display Fibonacci Series.
4. Write a java program to Check the given string is palindrome or not.
5. Write a java program using String Handling Functions.
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
3
********************************
I. Internal Exam
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
4
Observation Lab
Exam Project Total Marks
Note Attendance
P1 P2
10 M 10 M 20 M 10 M 50 M
10 10
P1 P2
10 M 30 M 10 M 50 M
15 15
Note:
Marks allotted for both writing and running the program in system.
Based on the syllabus students had to do take their own concept and
apply practically by writing and running the program in system.
Release Year
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
5
Java SE 6 2006
Java SE 7 2011
Java SE 8 2014
1. Installing:
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
6
Here we have to install JDK version 1.7 based on our requirement. It’s a free
open source software (freeware) provided by Oracle version. The following is the
Website address to download the Link.
http://www.oracle.com/technetwork/java/javase/downloads/index.html
After installing setting the path to run the JDK. We can set any one of the
following methods.
I. Manual Setting:
Type or copy the address of JDK and Bin folder where it is installed.
It will open Environment variable dialog box. Here click option and give
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
7
run : java filename values for variables [eg: java sum 101 ravi
98 76 54]
iii. Package
iv. Applet
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
8
I. Basic Programs
Ex No: 1 Print the given value in triangle shape.
Aim:
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
9
Creating a simple program using Java to print the value in triangle shape.
Algorithm:
4. Declare variables i, j, k.
Program:
Import java.io.*;
public class startriangle {
int i,j,k;
System.out.print(" ");
System.out.print("*");
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
10
System.out.println("");
} Output:
G:\javaaac>javac startriangle.java
G:\javaaac>java startriangle
*
***
*****
*******
*********
Result:
The Simple java program created and successfully compiled and displayed
the result.
import java.util.Scanner;
class matrix{
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
11
void get() {
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
a[i][j]=input.nextInt();
}}
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
b[i][j]=input.nextInt();
}}}
void process(){
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];
}}}}
void display() {
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
12
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
System.out.print(a[i][j]+"\t"); }
System.out.println("\n"); }
System.out.println("\n");
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
System.out.print(b[i][j]+"\t"); }
System.out.println("\n"); }
System.out.println("\n");
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
System.out.print(c[i][j]+"\t");
}System.out.println("\n");
}}}
class mm2{
m.get();
m.process();
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
13
m.display();
}}
Output
D:\>javac mm2.java
D:\>java mm2
A Matrix is :
2 2 2
2 2 2
2 2 2
B Matrix is :
1 2 3
4 5 6
7 8 9
A*B Multiply Matrix is :
24 30 36
24 30 36
24 30 36
Ex No: 3 Fibonacci Series
import java.util.Scanner;
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
14
int n, a = 0, b = 0, c = 1;
n = s.nextInt();
System.out.print("Fibonacci Series:");
a = b;
b = c;
c = a + b;
System.out.print(a+" ");
G:\ja>javac fibonacci.java
G:\ja>java fibonacci
Enter value of n : 6
class Palindrom{
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
15
int reverse=0,remainder;
if(reverse == n)
else
Output:
Program:
import java.io.*;
import java.util.Scanner;
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
16
import java.lang.String;
class stringcheck {
String s1,s2,s3,s4,s5,s7;
boolean s8;
int s6;
void sget() {
s1=s.next();
s2=s.next();
void scalc() {
s3=s1.toUpperCase();
s4=s3.toLowerCase();
s5=s1.concat(s2);
s6=s2.length();
s7=s2.replace('j','l');
s8=s1.equals(s2);
void sdisplay() {
System.out.println(" -----------------------------------------------------------------\n");
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
17
System.out.println(" ------------------------------------------------------------------n");
class s1{
stc.sget();
stc.scalc();
stc.sdisplay();
Output:
ARUL
ANANDAR
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
18
-------------------------------------------------------------------------------
--------------------------------------------------------------------------
Result:
Aim:
Creating a java program using class and Object of Java OOPS Concept.
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
19
Algorithm:
5. Create DataInpuStream object to get the input at run time in get() Method.
i) tot=m1+m2
ii) avg=tot/2
9. Create an object for above class marks with using ‘new’ keyword.
10. Call the method in marks class with the created object.
Program:
import java.io.*;
class marks{
String name;
int rno,m1,m2,tot;
float avg;
System.out.println("-------------Input-----------------");
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
20
name=d1.readLine();
rno=Integer.parseInt(d1.readLine());
m1=Integer.parseInt(d1.readLine());
m2=Integer.parseInt(d1.readLine());
tot=m1+m2;
avg=tot/2;
System.out.println("-------------Output-----------------");
System.out.println("\tName\tRno\tM1\tM2\tTotal\tAvg");
System.out.println( "\t"+name+"\t"+rno+"\t"+m1+"\t"+m2+"\t"+tot+"\t"+avg);
class mark3{
m.get();
m.cal();
m.dis();
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
21
Output:
G:\javaaac>javac mark3.java
G:\javaaac>java mark3
-------------Input-----------------
-------------Output-----------------
Result:
The java program created by using Class and Object concept which is
successfully compiled and displayed the result.
Program:
import java.io.*;
import java.util.Scanner;
import java.lang.*;
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
22
class mo {
double area(double r)
{ return 3.14*r*r; }
{ return l*b; }
{ return (0.5*b*h); }
class areaoverload1 {
a=s.nextDouble();
Output:
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
23
Aim:
Program:
import java.io.*;
interface int1{
public void s1(); }
interface int2 extends int1{
public void s2(); }
class sum implements int2 {
DataInputStream sp=new DataInputStream(System.in);
int a,b,c;
public void get() throws IOException{
System.out.println("Enter the Value of a, b:");
a=Integer.parseInt(sp.readLine());
b=Integer.parseInt(sp.readLine());
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
24
}
public void s1() {
c=a*b;
System.out.println("The Product of a, b:"+c);
}
public void s2() {
c=a-b;
System.out.println("The Difference of a, b:"+c); }
public static void main(String mm[]) throws IOException{
sum s=new sum();
s.get();
s.s1();
s.s2();
}
}
Output:
G:\>java sum
10
20
Ex No: 04 Package
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
25
Aim:
Creating a java program to get the input in one file under a package and
process the value in another package file.
Program:
package p1;
import java.io.*;
public class A2 {
int a,b,c;
a=Integer.parseInt(d1.readLine());
b=Integer.parseInt(d1.readLine());
c=a*b; }
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
26
package p2;
import java.io.*;
public class B1 {
obj.get();
obj.dis();
Output:
The B value is : 10
Aim:
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
27
Program:
import java.io.*;
public class ex1 {
public static void main(String[] args) throws IOException{
DataInputStream S1=new DataInputStream(System.in);
int a,b,d[]=new int[3];
ex1 emp=null;
try{
System.out.println("enter value for a:");
a=Integer.parseInt(S1.readLine());
//int b=a/0;
b=a;
System.out.println("B value is="+b);
System.out.println(emp.getClass());
for(int i=1;i<=3;i++) {
d[i]=Integer.parseInt(S1.readLine());
}
System.out.println("d="+d);
}
catch(ArithmeticException ae)
{System.out.println(ae);}
catch(NullPointerException np)
{System.out.println(np);}
catch(ArrayIndexOutOfBoundsException ne)
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
28
{ System.out.println(ne);}
catch(Exception e)
{System.out.println(e);}
}
}
Output:
12
B value is=12
java.lang.NullPointerException
Program:
import java.io.*;
exception2()
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
29
int a; float b;
a=st[0];
try{
b=6;
if( b>a)
else
catch(exception2 e){ }
Output:
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
30
b value is:6.0
Aim:
Creating a java program to decide which class or method to run first using
Multithread and Thread priorities.
Program:
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
31
second() {
Thread t2=new Thread(this);
t2.start();
}
public void run(){
for(int i=1;i<=5;i++){
System.out.println("Second Thread");
}
}
public static void main(String args[]) {
first f1=new first();
second s1=new second();
f1.setPriority(5);
s1.setPriority(1);
}
}
Output:
First Thread
First Thread
First Thread
First Thread
First Thread
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
32
Second Thread
Second Thread
Second Thread
Second Thread
Second Thread
First Thread
First Thread
First Thread
Second Thread
Second Thread
Second Thread
Second Thread
Second Thread
First Thread
First Thread
Program:
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
33
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
34
First Thread1
Second Thread
Second Thread
Second Thread
Second Thread
Second Thread
First Thread2
First Thread3
First Thread4
First Thread5
Aim:
Creating a java Applet program to draw the shapes using paint method.
Program:
import java.io.*;
importjava.awt.*;
importjava.applet.Applet;
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
35
setBackground(Color.pink);
g.setColor(Color.blue);
g.drawRect(50,50,90,65);
g.fillRect(50,50,90,65);
g.setColor(Color.yellow);
g.drawOval(10,220,150,120);
g.fillOval(10,220,150,120);
g.setColor(Color.red);
g.drawLine(10,420,420,420);
} }
Output:
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
36
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
37
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
38
Aim:
Creating a java Applet program for animation using Thread with Applet.
Program:
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
39
importjava.awt.*;
importjava.applet.*;
importjava.util.*;
/* <APPLET CODE="House1" WIDTH=600 HEIGHT=600></APPLET>*/
public class House1 extends Applet implements Runnable{
Thread t;
int x1=20;
public void start() {
t=new Thread(this,"New Thread");
t.start();
}
public void run() {
Thread t1=Thread.currentThread();
while(t==t1) {
repaint();
try{
Thread.sleep(150); }
catch(Exception e){}
}
}
public void paint(Graphics g) {
g.setColor(Color.red);
g.drawString("Xyz Street, Dindigul ....",250,320);
// drawing Road
g.setColor(Color.BLACK);
g.drawLine(20,260,600,260);
g.drawLine(20,300,600,300);
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
40
x1=(x1+16)%600;
g.fillRoundRect(x1,280,20,10,2,2);
}
}
Output:
Aim:
Creating a java program using Switch case in class and Object of Java OOPS
Concept.
Program:
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
41
import java.io.*;
class getacc{
String cname;
accno=Integer.parseInt(d1.readLine());
cname=d1.readLine();
cdeposit=Integer.parseInt(d1.readLine());
n=Integer.parseInt(d1.readLine());
wdraw=Integer.parseInt(d1.readLine());
balance=cdeposit-wdraw;
switch(n){
case 1:
if(balance<=500) {
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
42
else {
System.out.println("Account No : "+accno);
break;
case 2:
if(balance>=500){
System.out.println("Account No : "+accno);
System.out.println(" -----------------------------------------------------------------\n");
System.out.println(" -----------------------------------------------------------------\n");
System.out.println("\t\t"+cdeposit+"\t\t"+wdraw+"\t\t"+balance);
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
43
System.out.println(" -----------------------------------------------------------------\n");
else {
break;
default:
break;
class acc1{
g.read();
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
44
Output:
G:\javaaac>javac acc1.java
G:\javaaac>java acc1
G:\javaaac>java acc1
------------------------Balance Details--------------------------------
Account No : 9876
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
45
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
Result:
The java program created the Bank Account using Switch case in Class and
Object concept which is successfully compiled and display the balance details.
1. What is Class?
2. What is Object?
Objects have states and behaviors. Example: A dog has states - color, name,
breed as well as behaviors -wagging, barking, eating. An object is an instance of a
class.
3. What is Inheritance?
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
46
4. Which Keyword used to inherit the properties from one class to another in
inheritance?
The keyword “extends” is used to derive a subclass from the superclass.
6. What is SuperClass?
The class from which a subclass is derived is called a superclass. A superclass
may have one or more subclasses
7. What is interface?
8. What is Package?
Package can be defined as a grouping of related types(classes, interfaces,
enumerations and annotations ) providing access protection and name space
management.
9. What is Applet?
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
47
The applet is also able to run in HTML. The applet is usually embedded in an
HTML page on a Web site and can be executed from within a browser.
I. Initialization
II. Termination
1. New
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
48
2. Runnable
3. Running
4. Time Waiting(Blocked)
5. Terminated.
Constant Description
The maximum priority of
Thread.MIN_PRIORITY any thread (an int value
of 10)
The minimum priority of
Thread.MAX_PRIORITY any thread (an int value
of 1)
The normal priority of
Thread.NORM_PRIORITY any thread (an int value
of 5)
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
49
Eg. A+Bi
The Strings are sequence of characters.The Strings are objects. The String
class to create and manipulate strings.
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
50
Causes the current thread to wait until another thread invokes the notify().
By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai