Sunteți pe pagina 1din 50

1

SNO Chapter Page No

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

Programming in Java Lab

By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
2

Class : III IT & M Part : III Core Lab -5


Semester : V Semester Hours : 75
Subject Code : 15 UITP55 Credits : 04

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.

II. Using Java Concept Oriented Program:


1. Write a java program using Class and Object.
2. Write a java program to calculate area of square, rectangle, triangle using
Function Overloading.
3. Display the pay bill particulars and employee details using hybrid inheritance.
4. Write a java program for displaying Student details using interface concept.
5. Create a java program for package.
6. Create a java program to generate built in Exception (atleast 3).
7. Create a java program to throw user define Exception messages.
8. Create a java program for multi-thread using set priority and sleep method.
9. Create a java program for multi-thread using Suspend, Resume method.
10. Write a java program to create an array of objects in Java.

III. Program using Applet, Animation, Database.


1. Develop a java applet program and draw line, rectangle, circle, string using
Graphics class.

By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
3

2. Draw a home using Java Applet.


3. Design a car (or other picture) and run that object using Java Applet Thread.
4. Create a bank account and display the balance details using java concepts.
5. Write a java program to establish the database connection.

********************************

Lab Exam Pattern

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

II. External Exam

Practical Note Exam Project Total Marks

P1 P2
10 M 30 M 10 M 50 M
15 15

Note:

 Marks allotted for both writing and running the program in system.

 About the project :

 Based on the syllabus students had to do take their own concept and
apply practically by writing and running the program in system.

Different Versions of Java Runtime Environment from the beginning:

Release Year

JDK Beta 1995

By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
5

JDK 1.0 1996

JDK 1.1 1997

J2SE 1.2 1998

J2SE 1.3 2000

J2SE 1.4 2002

J2SE 5.0 2004

Java SE 6 2006

Java SE 7 2011

Java SE 8 2014

Installing the JDK & Setting the Path

1. Installing:

By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
6

Oracle corporation providing java different version Run Time Environment


(JDK) for normal (J2SE - Standard edition SDK), Mobile (J2ME-Mobile edition),
Web Development (J2EE – Enterprise edition).

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

2. Setting the Path:

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.

 D: Set path=”C:\Program Files (x86)\Java\jdk1.7.0\bin”;

II. Permanent Setting:

 Open my computer  open System Properties Open Advanced System


settings  click advanced in system properties dialog box click
Environment variable option.

 It will open Environment variable dialog box. Here click option and give

Variable name: path

Variable value: C:\Program Files (x86)\Java\jdk1.7.0\bin

Saving, Compiling and Running the Java File

1. We can use one of the following methods

a. Notepad - Typing the coding Manually

By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
7

b. IDE - Integrated Development Environment ( eg: Netbeans, Eclipse )

2. To save file in Notepad:

Filename.java [eg: sum.java]

3. To compile and Run the different concepts in Java

i. Normal Class and Object

compile : javac filename.java [eg: javac sum.java]

run : java filename [eg: java sum]

ii. Command Line Arguments

compile : javac filename.java [eg: javac sum.java]

run : java filename values for variables [eg: java sum 101 ravi

98 76 54]

iii. Package

compile : javac –d . filename.java [eg: javac –d . sum.java]

run : java package.filename [eg: java p1.sum]

iv. Applet

compile : javac filename.java [eg: javac app.java]

run : appletviewer filename.java [eg: appletviewer app.java]

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:

1. Import the header file io (input output stream).

2. Create a class name startriangle.

3. Write the Main function with IOException.

4. Declare variables i, j, k.

5. Process by using for loop.

6. Display the result.

7. End the Program.

Program:

Import java.io.*;
public class startriangle {

public static void main(String[] args) {

int i,j,k;

for(i=1; i<=5; i++) {

for(j=4; j>=i; j--) {

System.out.print(" ");

for(k=1; k<=(2*i-1); k++) {

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.

Ex No: 2 Matrix Multiplication

import java.util.Scanner;

class matrix{

int a[][]=new int[3][3];

By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
11

int b[][]=new int[3][3];

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

Scanner input=new Scanner(System.in);

void get() {

System.out.println("Enter the first matrix:");

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

for(int j=0;j<3;j++) {

a[i][j]=input.nextInt();

}}

System.out.println("Enter the second matrix:");

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

for(int j=0;j<3;j++) {

b[i][j]=input.nextInt();

}}}

void process(){

System.out.println("Matrix multiplication is as follows:");

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

System.out.println("A Matrix is :\n");

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");

System.out.println("B Matrix is :\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");

System.out.println("A*B Multiply Matrix is :\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{

public static void main(String args[]) {

matrix m=new matrix();

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

Enter the first matrix: 222222222

Enter the second matrix: 123456789

Matrix multiplication is as follows:

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;

public class fibonacci {

public static void main(String[] args) {

By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
14

int n, a = 0, b = 0, c = 1;

Scanner s = new Scanner(System.in);

System.out.print("Enter value of n:");

n = s.nextInt();

System.out.print("Fibonacci Series:");

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

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

Fibonacci Series : 011235

Ex No: 4 Palindrome Using Command Line Argument

class Palindrom{

public static void main(String args[]){

int num = Integer.parseInt(args[0]);

By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
15

int n = num; //used at last time check

int reverse=0,remainder;

while(num > 0){

remainder = num % 10;

reverse = reverse * 10 + remainder;

num = num / 10;

if(reverse == n)

System.out.println(n+" is a Palindrome Number");

else

System.out.println(n+" is not a Palindrome Number");

Output:

G:\ja>java Palindrom 121

121 is a Palindrome Number

G:\ja>java Palindrom 122

122 is not a Palindrome Number

Ex No: 5 String Handling

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;

Scanner s = new Scanner(System.in);

void sget() {

System.out.println("Enter String 1 & String 2 :");

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(" Your Given Strings are n");

System.out.println(" -----------------------------------------------------------------\n");

System.out.println("\tThe Given String1 : \t"+s1);

System.out.println("\tThe Given String2 : \t "+s2);

By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
17

System.out.println("\tThe String1 Coverted to Upper Case : \t"+s3);

System.out.println("\tThe Coverted Upper Case to Lower Case : \t"+s4);

System.out.println("\tThe Concat(addition) of Two Strings : \t"+s5);

System.out.println("\tThe Length of string2 is : \t"+s6);

System.out.println("\t Using Replace string2 is : \t"+s7);

System.out.println("\tChecking String1 and String 2 : \t"+s8);

System.out.println(" ------------------------------------------------------------------n");

class s1{

public static void main(String args[]) {

stringcheck stc=new stringcheck();

stc.sget();

stc.scalc();

stc.sdisplay();

Output:

I:\AAC june 2017\Java Prg>javac s1.java

I:\AAC june 2017\Java Prg>java s1

Enter String 1 & String 2 :

ARUL

ANANDAR

Your Given Strings are

By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
18

-------------------------------------------------------------------------------

The Given String1 : ARUL

The Given String2 : ANANDAR

The String1 Coverted to Upper Case : ARUL

The Coverted Upper Case to Lower Case : arul

The Concat(addition) of Two Strings : ARULANANDAR

The Length of string2 is : 7

Using Replace string2 is : LNLNDLR

Checking String1 and String 2 : false

--------------------------------------------------------------------------

Result:

The java program successfully compiled and manipulated by using String


Handling Methods.

III. Using Java Concept Oriented Program:

Ex No: 1 Class and Object

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:

1. Import the header file io (input output stream).

2. Create a class name Marks

3. Declare variables name, rno, m1, m2, tot, avg.

4. Declare the methods i) get() , ii) cal() & iii) dis()

5. Create DataInpuStream object to get the input at run time in get() Method.

6. Process the following calculation in Cal() Method.

i) tot=m1+m2

ii) avg=tot/2

7. Display the result in dis() method.

8. Create another Class name mark3 with Main function.

9. Create an object for above class marks with using ‘new’ keyword.

10. Call the method in marks class with the created object.

11. End the Program.

Program:

import java.io.*;

class marks{

String name;

int rno,m1,m2,tot;

float avg;

DataInputStream d1=new DataInputStream(System.in);

void get() throws IOException {

System.out.println("-------------Input-----------------");

System.out.print("Enter Your Name : ");

By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
20

name=d1.readLine();

System.out.print("Enter Your regno : ");

rno=Integer.parseInt(d1.readLine());

System.out.print("Enter Your Mark1 : ");

m1=Integer.parseInt(d1.readLine());

System.out.print("Enter Your Mark2 : ");

m2=Integer.parseInt(d1.readLine());

void cal() throws IOException {

tot=m1+m2;

avg=tot/2;

void dis() throws IOException {

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{

public static void main(String args[]) throws IOException {

marks m=new marks();

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

Enter Your Name : xxyy

Enter Your regno : 213

Enter Your Mark1 : 23

Enter Your Mark2 : 44

-------------Output-----------------

Name Rno M1 M2 Total Avg

xxyy 213 23 44 67 33.0

Result:

The java program created by using Class and Object concept which is
successfully compiled and displayed the result.

Ex No: 2 Method or Function Overloading

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 {

//Area Method for calulating Circle by radius

double area(double r)

{ return 3.14*r*r; }

//Area Method to calculate Area of Rectangle

int area(int l, int b)

{ return l*b; }

//Area Method to calculate Area of Triangle

double area(double b, double h)

{ return (0.5*b*h); }

class areaoverload1 {

public static void main(String args[]) throws IOException {

double a; int b,c,d,e,f;

Scanner s = new Scanner(System.in);

mo mod = new mo();

System.out.println("Enter Radius of Circle:");

a=s.nextDouble();

System.out.println("Area of circle with radius " +a+ " is : " + mod.area(a));


System.out.println("Area of rectangle with l*b is " + mod.area(10,10));

System.out.println("Area of circle with triangle is " + mod.area(10.0,10.0));

System.out.println("Area of circle with radius " +a+ " is " + mod.area(10));

Output:

By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
23

I:\AAC july 2016\Java Prg>javac areaoverload1.java

I:\AAC july 2016\Java Prg>java areaoverload1

Enter Radius of Circle: 5

Area of circle with radius 5.0 is : 78.5

Area of rectangle with length and breadth is 100

Area of circle with triangle is 50.0

Area of circle with radius 5.0 is 314.0

Ex No: 03 Implementing Multiple Inheritance

Aim:

Creating a java program to implement the multiple Inheritances using


Interface

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

Enter the Value of a, b:

10

20

The Product of a, b:200

The Difference of a, b:-10

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:

// Save the program as A2.java file

package p1;

import java.io.*;

public class A2 {

int a,b,c;

DataInputStream d1=new DataInputStream(System.in);

public void get() throws IOException {

System.out.println("Enter A & B value:");

a=Integer.parseInt(d1.readLine());

b=Integer.parseInt(d1.readLine());

c=a*b; }

public void dis() throws IOException {

System.out.println("The A value is:"+a);

System.out.println("The B value is:"+b);

System.out.println("The multiplication of C=A * B is :"+c);

By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
26

// Save the program as B1.java file

package p2;

import java.io.*;

public class B1 {

public static void main(String args[]) throws IOException {

p1.A2 obj = new p1.A2();

obj.get();

obj.dis();

Output:

G:\> javac -d . A2.java

G:\> javac -d . B1.java

G:\> java p2.B1

Enter A & B value: 100 10

The A value is : 100

The B value is : 10

The multiplication of C=A * B is : 1000

Ex No: 05 Exception types

Aim:

By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
27

Creating a java program to handle error by different types of exceptions.

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:

I:\AAC july 2016\Java Prg>javac ex1.java

I:\AAC july 2016\Java Prg>java ex1

enter value for a:

12

B value is=12

java.lang.NullPointerException

Ex No: 06 User Defined Exception

Program:

import java.io.*;

class exception2 extends Exception{

exception2()

System.out.println(" You had given A value is Less than B.


Please Correct error....");

By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
29

public static void main(String st[]){

int a; float b;

System.out.println("enter value for a:");

a=st[0];

try{

b=6;

if( b>a)

{throw new exception2();}

else

{System.out.println("b value is:"+b);}

catch(exception2 e){ }

Output:

I:\AAC july 2016\Java Prg>javac exception2.java

I:\AAC july 2016\Java Prg>java exception2

enter value for a:

By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
30

You had given A value is Less than B. Please Correct error....

I:\AAC july 2016\Java Prg>java exception2

enter value for a:

b value is:6.0

Ex No: 07 Multithread Priorities

Aim:

Creating a java program to decide which class or method to run first using
Multithread and Thread priorities.

Program:

class first extends Thread{


first() {
Thread t1=new Thread(this);
t1.start();
}
public void run() {
for(int i=1;i<=5;i++) {
System.out.println("First Thread");
}
}
}
class second extends Thread{

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:

G:\bcajavaprg\java Lab program\Thread Programs>javac second.java

G:\bcajavaprg\java Lab program\Thread Programs>java second

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

G:\bcajavaprg\java Lab program\Thread Programs>java second

First Thread

First Thread

First Thread

Second Thread

Second Thread

Second Thread

Second Thread

Second Thread

First Thread

First Thread

Ex No: 08 Multithread for Sleep, Suspend, Resume

Program:

class first extends Thread{


first() {

By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
33

Thread t1=new Thread(this);


t1.start();
}
public void run() {
for(int i=1;i<=5;i++) {
System.out.println("First Thread"+i);
suspend();
sleep(100);
resume(); }
}
}
class second extends Thread{
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();
}
}
Output:

G:\bcajavaprg\java Lab program\Thread Programs>javac second.java

By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
34

G:\bcajavaprg\java Lab program\Thread Programs>java second

First Thread1

Second Thread

Second Thread

Second Thread

Second Thread

Second Thread

First Thread2

First Thread3

First Thread4

First Thread5

III. Program using Applet, Animation, Database.

Ex No: 1 Simple Applet

Aim:

Creating a java Applet program to draw the shapes using paint method.

Program:

import java.io.*;

importjava.awt.*;

importjava.applet.Applet;

/*<applet code="app.class" width=450 height=560></applet>*/

By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
35

public class app extends Applet {

public void paint(Graphics g) {

setBackground(Color.pink);

g.setColor(Color.blue);

g.drawString("This is Applet Rectangle",20,40);

g.drawRect(50,50,90,65);

g.fillRect(50,50,90,65);

g.setColor(Color.yellow);

g.drawString("This is Applet Oval",20,200);

g.drawOval(10,220,150,120);

g.fillOval(10,220,150,120);

g.setColor(Color.red);

g.drawString("This is Applet line ",10,380);

g.drawLine(10,420,420,420);

} }

Output:

I:\AAC july 2016\Java Prg>javac app.java

I:\AAC july 2016\Java Prg>appletviewer app.java

By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
36

Ex No: 2 Design and getting input from Applet window

// Add two numbers using Applet


importjava.awt.*;
importjava.applet.*;
importjava.awt.event.*;
/* <applet code="asum.class" height=150 width =350></applet> */
public class asum extends Applet implements ActionListener{
TextField T1,T2,T3;
Button b1;

By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
37

public void init() {


T1 = new TextField(10);
T2 = new TextField(10);
T3= new TextField(10);
b1 = new Button("Sum");
add(T1);
add(T2);
add(T3);
add(b1);
T1.setText("0");
T2.setText("0");
b1.addActionListener(this);
}
public void actionPerformed(ActionEventee) {
inta,b,c;
a=Integer.parseInt(T1.getText());
b=Integer.parseInt(T2.getText());
c=a+b;
T3.setText(String.valueOf(c));
}
}
Output:

By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
38

Ex No: 3 Applet Animation

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:

Ex No: 4 Bank Account Program

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;

int accno,cdeposit, wdraw, balance, n;

DataInputStream d1=new DataInputStream(System.in);

void read() throws IOException {

System.out.println("Enter Customer Account No : ");

accno=Integer.parseInt(d1.readLine());

System.out.println("Enter Customer Name : ");

cname=d1.readLine();

System.out.println("Enter Deposit Amount : ");

cdeposit=Integer.parseInt(d1.readLine());

System.out.println("Enter 1-to withdraw, 2-check balance");

n=Integer.parseInt(d1.readLine());

System.out.println("Enter Withdrawal Amount : ");

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

System.out.println(" ----------------Welcome to AAC Bank------------------\n");

System.out.println("Min Balance Rs 500.U Can't Withdraw...\n");

else {

System.out.println(" -------------------Welcome to AAC Bank----------------\n");

System.out.println("Account No : "+accno);

System.out.println("Customer Name : "+cname);

System.out.println("Thank u for Withdrawing amt :"+wdraw);

break;

case 2:

if(balance>=500){

System.out.println(" ------------------Welcome to AAC Bank------------------\n");

System.out.println(" --------------------Balance Details--------------------------\n");

System.out.println("Account No : "+accno);

System.out.println("Customer Name : "+cname);

System.out.println(" -----------------------------------------------------------------\n");

System.out.println("\tDeposit Amt\tWithdraw Amt\tBalance Amt\t");

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 {

System.out.println("Minimum Balance is 500 ");

System.out.println("You Balance is Low and U Can't Withdraw amount ");

break;

default:

System.out.println(" -------------------Welcome to AAC Bank-----------------\n");

System.out.println(" Details Given are not match. Check U'r details---------\n");

break;

class acc1{

public static void main(String pgs[]) throws IOException {

getacc g=new getacc();

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

Enter Customer Account No : 9876

Enter Customer Name : zcvb

Enter Deposit Amount : 8600

Enter 1-to withdraw, 2-check balance :1

Enter Withdrawal Amount : 8700

------------------------Welcome to AAC Bank-----------------------

Min Balance Rs 500.U Can't Withdraw...

G:\javaaac>java acc1

Enter Customer Account No : 9876

Enter Customer Name : zcvb

Enter Deposit Amount : 8600

Enter 1-to withdraw, 2-check balance : 2

Enter Withdrawal Amount : 5000

------------------------Welcome to AAC Bank-----------------------

------------------------Balance Details--------------------------------

Account No : 9876

Customer Name : zcvb

By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
45

----------------------------------------------------------------------------

Deposit Amt Withdraw Amt Balance Amt

----------------------------------------------------------------------------

8600 5000 3600

----------------------------------------------------------------------------

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?

A class can be defined as a template/blue print that describes the


behaviors/states that object of its type support.

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?

Inheritance is a mechanism wherein a new class is derived from an existing


class.

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.

5. What is Called Subclass?


A class derived from another class is called a subclass. A subclass can have only
one 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?

An interface is a collection of abstract methods. A class implements an


interface, thereby inheriting the abstract methods of the interface.

An interface is not a class. Writing an interface is similar to writing a class,


but they are two different concepts. A class describes the attributes and behaviors of
an object. An interface contains behaviors that a class implements.

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.

In simple, it is a way of categorizing the classes and interfaces. When


developing applications in Java, hundreds of classes and interfaces will be written,
therefore categorizing these classes is a must as well as makes life much easier.

9. What is Applet?

By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
47

An applet is a small Internet-based program written in Java, a programming


language for the Web.

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.

10. Applet Life Cycle

I. Initialization

1. init() method - It is equivalent to born state of a thread

2. start() method - It is equivalent to inactive state of a thread

3. paint() method - This is equivalent to runnable state of thread.

II. Termination

4. stop () method - It is equivalent to the blocked state of the thread.

5. destroy () method - It is equivalent to the dead state of the thread.

11. What is Thread ?

A thread is a single sequential flow of control within a program.

12. What is MultiThread?

 Multithreading is a process of executing multiple threads


simultaneously.

 A multithreaded program contains two or more parts that can run


concurrently and each part can handle different task at the same time.

13. Thread Life-Cycle:

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.

14. Thread Piriorities

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)

15. What are the ways you can create Thread?

1. Creating by Extending Thread Class

2. Implementing Runnable Interface.

16. What is Java?

Java is a computer programming language that is concurrent, class-based,


object-oriented. It works on the principle Write Once Run Anywhere At Any Time
(WORAAT) that is program can create in one platform and run in any platform.

17. What is Complex Number?

By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai
49

A Complex Number is a combination of a Real Number and an Imaginary


Number.

Eg. A+Bi

18. What is String?

The Strings are sequence of characters.The Strings are objects. The String
class to create and manipulate strings.

19. What is Exception ?

An exception is an event, which occurs during the execution of a program,


that disrupts the normal flow of the program's instructions.

20. In which header file exception is available?

All exception classes are subtypes of the java.lang.Exception class. The


exception class is a subclass of the Throwable class.

21. Thread Methods

SN Methods with Description

public void suspend()


1 This method puts a thread in suspended state and can be resumed using resume()
method.

public void stop()


2
This method stops a thread completely.

public void resume()


3
This method resumes a thread which was suspended using suspend() method.

4 public void wait()

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().

public void notify()


5
Wakes up a single thread that is waiting on this object's monitor.

By Asst. Prof Albert Irudaya Raj J – IT & M Dept– Arul Anandar College – Karumathur - Madurai

S-ar putea să vă placă și