Java Programming Lab Exercises
Java Programming Lab Exercises
LAB
Under the Guidance of
Sri [Link],
ASSOCIATE PROFESSOR,
DEPT. OF CSE,
GIT , GITAM UNIVERSITY.
2/4 [Link] (B4), CSE DEPARTMENT
GIT, GITAM UNIVERSITY
INDEX
Name: Regd no:
Section: Branch:
S. No Name of the program Page No Signature
*1
Temparature Conversion 1 - 2
*2
Bank Account Class 3 - 6
*3
String Palindrome 7 - 8
4
2D String Array 9 -11
5
Commandline & Static 12 - 13
6
Abstract classes & methods 14 - 16
7
Access Controls 17 - 18
8
Method Overloading 19 - 21
9
Constructor Overloading 22 - 23
10
Multilevel Inheritance 24 - 25
11
Hierarchial Inheritance 26 - 27
*12
Multiple
Inheritance(Interface)
28 - 30
13
Method Over-riding 31 - 34
14
Prevention using "Final" 35 - 36
15
System defined exceptions 37 - 38
16
User defined exceptions 39 - 40
*17
Threads with "Runnable" 41 - 42
18
Threads with priority 43 - 44
19
Flow Layout 45 - 47
20
Border Layout 48 - 49
21
Grid Layout 50 - 51
22
Label 52 - 53
23
Button 54 - 56
24
Checkbox 57 -59
25
Checkbox Group 60 - 62
26
Choice 63 - 65
INDEX
Name: Regd no:
Section: Branch:
S. No Name of the program Page No Signature
27
List 66 - 68
28
Text Field 69 - 71
29
Text Area 72 - 73
*30
User inputs in applet 74 - 76
*31
Future Investment 77 - 79
*32
Frames and Panels 80 - 81
*33
Mouse pressed location 82 - 83
*34
Mouse events 84 - 86
*35
Drawing Shapes 87 - 88
*36
Barcharts with passing
parameters
89 - 90
*37
Barcharts with different
inputs
91 - 93
38
39
40
* : Lab cycle programs
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 1
PROGRAM 1
AIM:
To develop a program to convert the given temparature in
Fahrenheit to Celcius.
SOURCE CODE:
import [Link].*;
public class TempConvert
{
public static void main( String ar[] )throws IOException
{
DataInputStream d =new DataInputStream( [Link] );
String s1[] =new String[10];
String s2;
float a[] =new float[10];
float b[] =new float[10];
[Link]("Enter the [Link] temparature
readings:");
s2 =[Link]();
int n =[Link]( s2 );
[Link]("Enter the " +n +" readings in
Fahrenheit scale:");
for( int i =0 ; i <n ; i++)
{
s1[i] =[Link]();
a[i] =[Link]( s1[i] );
b[i] =( ( a[i] - 32 ) / (float)1.8 );
}
[Link]("The converted data is:");
[Link]("FAHRENHEIT\t\tCELCIUS");
[Link]("--------------------------------");
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 2
for( int i =0 ; i <n ; i++)
{
[Link]( " " +a[i] +"\t\t" +b[i] );
}
}
}
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 3
PROGRAM 2
AIM:
Develop a program to design a class that represents a bank
account,including the following members:
DATA MEMBERS:
[Link] of the depositor
[Link] number
[Link] of account
[Link]
METHODS:
[Link] assign initial values
[Link] deposit an amount
[Link] withdraw an amount after checking balance
[Link] display the name,account number and
balance
SOURCE CODE:
import [Link].*;
class BankRep
{
double bal , wthd , dep;
int ty;
DataInputStream d =new DataInputStream( [Link] );
String name , s3 , s4 , s5 , s6 , acc;
public void init()throws IOException
{
[Link]("Enter the name of account-holder:");
name =[Link]();
[Link]("Enter the account number:");
acc =[Link]();
[Link]("Types of account:\[Link]\[Link]\[Link]
DEPOSIT\nEnter choice:");
s3 =[Link]();
ty =[Link]( s3 );
switch( ty )
{
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 4
case 1: s3 ="CURRENT";
break;
case 2: s3 ="SAVINGS";
break;
case 3: s3 ="FIXED DEPOSIT";
break;
default:[Link]("Illegal entry!!");
}
[Link]("Type of account entered:" +s3);
[Link]("Enter the amount present in account:");
s4 =[Link]();
bal =[Link]( s4 );
}
public void deposit()throws IOException
{
[Link]("Enter the amount to deposit:");
s5 =[Link]();
dep =[Link]( s5 );
bal =bal +dep;
}
public void withd()throws IOException
{
[Link]("Enter the amount to withdraw:");
s6 =[Link]();
wthd =[Link]( s6 );
bal =bal - wthd;
}
public void enq()
{
[Link]("Balance is:" +bal);
}
public void display()
{
[Link]("The updated details of account are:\nNAME:" +name +
"\nAccount Number:" +acc +"\nBalance in account after last transaction:" +bal);
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 5
}
}
public class Class
{
public static void main( String ar[] )throws IOException
{
BankRep b =new BankRep();
[Link]();
int ch;
String s;
DataInputStream k =new DataInputStream( [Link] );
do
{
[Link]("Type of
Transaction:\[Link]\[Link]\[Link] ENQUIRY\[Link]\nEnter choice:");
s =[Link]();
ch =[Link]( s );
switch( ch )
{
case 1: [Link]();
break;
case 2: [Link]();
break;
case 3: [Link]();
break;
case 4: [Link]();
[Link](0);
default:[Link]("Illegal choice!!");
}
}while( ch !=5 );
}
}
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 6
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 7
PROGRAM 3
AIM:
Develop a program that takes a string from the commandline
arguments and checks whether it is a palindrome or not.
SOURCE CODE:
public class StrPalind
{
public static void main( String ar[] )
{
String s=ar[0];
[Link]("The given string is:" +ar[0] );
int l=[Link]();
int count=0;
for(int i=0;i<l;i++)
{
if([Link](i)!=[Link](l-i-1))
{
count=1;
}
}
if(count==1)
{
[Link]("The given string '" +ar[0] +"' is not a palindrome!");
}
else
{
[Link]("The given string '" +ar[0] +"' is a palindrome!");
}
}
}
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 8
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 9
PROGRAM 4
AIM:
Develop a program that appends two 2D string arrays using string
handling functions.
SOURCE CODE:
import [Link].*;
class string2D
{
public static void main( String ar[] )throws IOException
{
DataInputStream d =new DataInputStream( [Link] );
String a[][] =new String[20][20];
String b[][] =new String[20][20];
String c[][] =new String[20][20];
String s1 , s2 , s3 , s4;
[Link]("Enter the values of rows and columns of first matrix:");
s1 =[Link]();
s2 =[Link]();
[Link]("Enter the values of rows and columns of second matrix:");
s3 =[Link]();
s4 =[Link]();
int m , n , p , q , i , j;
m =[Link]( s1 );
n =[Link]( s2 );
p =[Link]( s3 );
q =[Link]( s4 );
[Link]("Enter the elements of first matrix:");
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 10
for( i =0 ; i <m ; i++)
for( j =0 ; j <n ; j++)
{
a[i][j] =[Link]();
}
[Link]("Enter the elements of second matrix:");
for( i =0 ; i <p ; i++)
for( j =0 ; j <q ; j++)
{
b[i][j] =[Link]();
}
[Link]("The entered first matrix:");
for( i =0 ; i <m ; i++)
{
for( j =0 ; j <n ; j++)
{
[Link]( a[i][j] +"\t" );
}
[Link]("\n");
}
[Link]("The entered second matrix:");
for( i =0 ; i <p ; i++)
{
for( j =0 ; j <q ; j++)
{
[Link]( b[i][j] +"\t" );
}
[Link]("\n");
}
if( ( m ==p ) && ( n ==q ) )
{
for( i =0 ; i <p ; i++)
for( j =0 ; j <q ; j++)
{
c[i][j] =a[i][j].concat( b[i][j] );
}
}
else
[Link]("Addition is not possible as the matrices orders are
unequal");
[Link]("The result matrix:");
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 11
for( i =0 ; i <p ; i++)
{
for( j =0 ; j <q ; j++)
{
[Link]( c[i][j] +"\t" );
}
[Link]("\n");
}
}
}
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 12
PROGRAM 5
AIM:
Develop a program to demonstrate both commandline arguments
and static members.
SOURCE CODE:
class CommandLine
{
public static void main(String args[])
{
for(int i=0; i<[Link]; i++)
[Link]("args[" +i +"]: " +
args[i]);
UseStatic ob =new UseStatic();
[Link](42);
}
}
class UseStatic
{
static int a =3;
static int b;
static void meth(int x)
{
[Link]("x =" +x);
[Link]("a =" +a);
[Link]("b =" +b);
}
static
{
[Link]("Static block initialized.");
b =a * 4;
}
}
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 13
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 14
PROGRAM 6
AIM:
Develop a program to demonstrate abstract classes and methods .
SOURCE CODE:
import [Link].*;
abstract class Figure
{
double dim1;
double dim2;
Figure( ) throws IOException
{
String s;
DataInputStream g =new DataInputStream( [Link] );
[Link]("Enter the dimensions:");
s =[Link]();
dim1 =[Link]( s );
s =[Link]();
dim2 =[Link]( s );
}
abstract double area( );
}
class Rectangle extends Figure
{
Rectangle( ) throws IOException
{
super();
}
// override area for rectangle
double area( )
{
[Link]("Inside Area for Rectangle:");
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 15
return dim1 * dim2;
}
}
class Triangle extends Figure
{
Triangle()throws IOException
{
super();
}
// override area for right triangle
double area()
{
[Link]("Inside Area for Triangle:");
return dim1 * dim2 / 2;
}
}
class AbstractAreas
{
public static void main( String ar[] )throws IOException
{
int ch;
String s;
Figure figref;
DataInputStream d =new DataInputStream( [Link] );
OverloadDemo ob =new OverloadDemo();
do
{
[Link]("\nMENU\[Link] area of rectangle\[Link] area of
triangle");
[Link]("[Link]\nEnter youir choice:");
s =[Link]();
ch =[Link]( s );
switch( ch )
{
case 1: Rectangle r =new Rectangle();
figref =r;
[Link]( "Area is " +[Link]() );
break;
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 16
case 2: Triangle t =new Triangle();
figref =t;
[Link]( "Area is " +[Link]() );
break;
case 3: [Link]( 0 );
break;
default:[Link]("Invalid choice!!!");
}
}while( ch !=3 );
}
}
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 17
PROGRAM 7
AIM:
Develop a program to demonstrate the access specifiers .
SOURCE CODE:
class Test
{
int a;
public int b;
private int c;
void setc(int i)
{
c =i;
}
int getc()
{
return c;
}
}
class AccessTest
{
public static void main(String args[])
{
Test ob =new Test();
ob.a =10;
ob.b =20;
[Link](100);
[Link]("a, b, and c: " +ob.a +" " +ob.b +" " +[Link]());
}
}
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 18
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 19
PROGRAM 8
AIM:
Develop a program to demonstrate the method overloading .
SOURCE CODE:
import [Link].*;
class OverloadDemo
{
void test()
{
[Link]("No parameters");
}
void test(int a)
{
[Link]("a: " +a);
}
void test(int a, int b)
{
[Link]("a and b: " +a +" " +b);
}
double test(double a)
{
[Link]("double a: " +a);
return a*a;
}
}
class Test
{
public static void main( String ar[] )throws IOException
{
int ch ;
String s;
double result;
DataInputStream d =new DataInputStream( [Link] );
OverloadDemo ob =new OverloadDemo();
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 20
do
{
[Link]("MENU\[Link] method with no parameters\[Link]
method with one integer parameters");
[Link]("[Link] method with two integer parameters\[Link]
method with a double parameter");
[Link]("[Link]\nEnter youir choice:");
s =[Link]();
ch =[Link]( s );
switch( ch )
{
case 1: [Link]();
break;
case 2: [Link]( 10 );
break;
case 3: [Link]( 10 , 20 );
break;
case 4: result =[Link]( 11.3654 );
[Link]("Result =" +result );
break;
case 5: [Link]( 0 );
break;
default:[Link]("Invalid choice!!!");
}
}while( ch !=5 );
}
}
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 21
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 22
PROGRAM 9
AIM:
Develop a program to demonstrate the constructor overloading .
SOURCE CODE:
import [Link].*;
class Box
{
double width;
double height;
double depth;
Box(double w, double h, double d)
{
width =w;
height =h;
depth =d;
}
Box()
{
width =5; // use -1 to indicate
height =5; // an uninitialized
depth =5; // box
}
Box(double len)
{
width =height =depth =len;
}
double volume()
{
return width * height * depth;
}
}
class OverloadCons
{
public static void main( String ar[] )
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 23
{
Box mybox1 =new Box( 100 , 200 , 150 );
Box mybox2 =new Box();
Box mycube =new Box( 70 );
double vol;
vol =[Link]();
[Link]("Volume of mybox1 is " +vol);
vol =[Link]();
[Link]("Volume of mybox2 is " +vol);
vol =[Link]();
[Link]("Volume of mycube is " +vol);
}
}
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 24
PROGRAM 10
AIM:
Develop a program to demonstrate multilevel inheritance.
SOURCE CODE:
import [Link].*;
class student
{
int roll_no;
public void getnumber(int m)
{
roll_no =m;
}
}
class marks extends student
{
int marks1,marks2;
public void getmarks(int m,int n)
{
marks1 =m;
marks2 =n;
}
}
class results extends marks
{
void putdetails()
{
int total=marks1+marks2;
[Link]("\nStudent roll number : "+roll_no);
[Link]("\nMarks Obtained:");
[Link]("Marks1 ="+marks1);
[Link]("Marks2 ="+marks2);
[Link]("\nTotal ="+total);
}
}
class exam
{
public static void main(String args[])throws IOException
{
DataInputStream d =new DataInputStream([Link]);
String s1,s2,s3;
int a,b,c;
[Link]("Enter roll-number : ");
s1=[Link]();
[Link]("Enter Marks : ");
s2=[Link]();
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 25
s3=[Link]();
a=[Link](s1);
b=[Link](s2);
c=[Link](s3);
results ob =new results();
[Link](a);
[Link](b,c);
[Link]();
}
}
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 26
PROGRAM 11
AIM:
Develop a program to demonstrate hierarchial inheritance.
SOURCE CODE:
import [Link].*;
class numbers
{
int num1,num2;
public void getnumbers(int m,int n)
{
num1 =m;
num2 =n;
}
}
class addition extends numbers
{
int sum;
void add()
{
sum =num1+num2;
}
void putdata()
{
[Link]("\nSum of "+num1+" and "+num2+" ="+sum);
}
}
class subtraction extends numbers
{
int diff;
void sub()
{
diff =num1-num2;
}
void putdata()
{
[Link]("\nDifference between "+num1+" and "+num2+" ="+diff);
}
}
class operation
{
public static void main(String args[])throws IOException
{
DataInputStream d =new DataInputStream( [Link] );
String s1,s2;
int a,b;
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 27
[Link]("Enter the numbers:" );
s1 =[Link]();
s2 =[Link]();
a =[Link](s1);
b =[Link](s2);
addition ob1 =new addition();
[Link](a,b);
[Link]();
[Link]();
subtraction ob2 =new subtraction();
[Link](a,b);
[Link]();
[Link]();
}
}
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 28
PROGRAM 12
AIM:
Develop a program to demonstrate multiple inheritance using
interfaces.
SOURCE CODE:
import [Link].*;
class Student
{
int roll;
String name;
void getData()throws IOException
{
String s;
DataInputStream d =new DataInputStream( [Link] );
[Link]("Enter the name of the student:");
name =[Link]();
[Link]("Enter the roll number of the student:");
s =[Link]();
roll =[Link]( s );
}
void putData()
{
[Link]("Name:" +name +"\nRoll no:" +roll );
}
}
class Test extends Student
{
float m1 , m2;
void getMarks()throws IOException
{
String s;
DataInputStream d =new DataInputStream( [Link] );
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 29
[Link]("Enter the inernal marks of the student:");
s =[Link]();
m1 =[Link]( s );
[Link]("Enter the external marks of the student:");
s =[Link]();
m2 =[Link]( s );
}
void putMarks()
{
[Link]("Marks obtained:\nInternal:" +m1 +"\nExternal:" +m2 );
}
}
interface Sports
{
float sprt =6.5F;
void putWt();
}
class Results extends Test implements Sports
{
float tot;
public void putWt()
{
[Link]("Sport Weight:" +sprt );
}
void display()
{
tot =m1 +m2 +sprt;
putData();
putMarks();
putWt();
[Link]("Total Score:" +tot );
}
}
class Multiple
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 30
{
public static void main( String ar[] )throws IOException
{
Results r =new Results();
[Link]();
[Link]();
[Link]();
}
}
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 31
PROGRAM 13
AIM:
Develop a program to demonstrate method over-riding.
SOURCE CODE:
import [Link].*;
class Figure
{
double dim1;
double dim2;
Figure()throws IOException
{
String s;
DataInputStream g =new DataInputStream( [Link] );
[Link]("Enter the dimensions:");
s =[Link]();
dim1 =[Link]( s );
s =[Link]();
dim2 =[Link]( s );
}
double area()
{
[Link]("Area for Figure is undefined,as we don't know the shape!");
return 0;
}
}
class Rectangle extends Figure
{
Rectangle()throws IOException
{
super();
}
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 32
// override area for rectangle
double area()
{
[Link]("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
class Triangle extends Figure
{
Triangle()throws IOException
{
super();
}
// override area for right triangle
double area()
{
[Link]("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}
}
class FindAreas
{
public static void main( String ar[] )throws IOException
{
int ch;
String s;
Figure figref;
DataInputStream d =new DataInputStream( [Link] );
OverloadDemo ob =new OverloadDemo();
do
{
[Link]("\nMENU\[Link] area of rectangle\[Link] area of
triangle");
[Link]("[Link] area of figure by super class method");
[Link]("[Link]\nEnter youir choice:");
s =[Link]();
ch =[Link]( s );
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 33
switch( ch )
{
case 1: Rectangle r =new Rectangle();
figref =r;
[Link]( "Area is " +[Link]() );
break;
case 2: Triangle t =new Triangle();
figref =t;
[Link]( "Area is " +[Link]() );
break;
case 3: Figure f =new Figure();
figref =f;
[Link]("Area is " +[Link]());
break;
case 4: [Link]( 0 );
break;
default:[Link]("Invalid choice!!!");
}
}while( ch !=4 );
}
}
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 34
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 35
PROGRAM 14
AIM:
Develop a program to demonstrate how to prevent method over-
riding using "final" keyword.
SOURCE CODE:
//Program to prevent method overloading using final keyword:#1
class A
{
final void meth()
{
[Link]("This is a final method.");
}
}
class B extends A
{
void meth()
{
// ERROR! Can't override.
[Link]("Illegal!");
}
}
class TestFinal
{
public static void main( String ar[] )
{
B b =new B();
[Link]();
}
}
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 36
//Program to prevent method overloading using final keyword:#2
class A
{
final void meth()
{
[Link]("This is a final method.");
}
}
class B extends A {}
class TestFinal
{
public static void main( String ar[] )
{
B b =new B();
[Link]();
}
}
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 37
PROGRAM 15
AIM:
Develop a program to demonstrate how to handle system defined
exceptions.
SOURCE CODE:
class Multicatch
{
public static void main(String args[])
{
int a[]={5 , 10 };
int b =5;
try
{
int z =a[2] / b - a[1];
}
catch( ArithmeticException e )
{
[Link]("Division by zero");
}
catch( ArrayIndexOutOfBoundsException e )
{
[Link]("Array out of bounds");
}
catch( NullPointerException e )
{
[Link]("Null exception");
}
finally
{
[Link]("System defined exception is caught!");
int y =a[1] / a[0];
[Link]( "Value =" +y );
}
}
}
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 38
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 39
PROGRAM 16
AIM:
Develop a program to demonstrate how to handle user defined
exceptions.
SOURCE CODE:
import [Link].*;
class MyException extends Exception
{
private float detail;
MyException(float a)
{
detail =a;
}
public String toString()
{
return "MyException[" +detail +"]";
}
}
class ExceptionDemo
{
static void compute(float a) throws MyException
{
[Link]("Called compute(" +a +")");
if(a <1)
throw new MyException(a);
[Link]("Normal exit");
}
public static void main( String ar[] )throws IOException
{
int a , b;
float r;
DataInputStream d =new DataInputStream( [Link] );
[Link]("Enter the values of a and b:");
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 40
String s =[Link]();
a =[Link]( s );
s =[Link]();
b =[Link]( s );
try
{
r =( float ) a / ( float ) b ;
compute(r);
}
catch ( MyException e )
{
[Link]("Caught " +e);
}
}
}
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 41
PROGRAM 17
AIM:
Develop a program to demonstrate multithreading concept using
"Runnable " interface.
SOURCE CODE:
class Runnabletest implements Runnable
{
int i;
String str;
Runnabletest( String s )
{
str=s;
}
public void run()
{
for(int i=0;i<8;i++)
{
[Link](str);
}
}
}
class Runnabledemo
{
public static void main(String args[])
{
Runnabletest x=new Runnabletest("First thread");
Runnabletest y=new Runnabletest("second thread");
Thread t1=new Thread(x);
Thread t2=new Thread(y);
[Link]();
[Link]();
}
}
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 42
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 43
PROGRAM 18
AIM:
Develop a program to demonstrate multithreading concept
implementing priorities.
SOURCE CODE:
import [Link].*;
class A extends Thread
{
publc void run( ) throws IOException
{
DataInputStream d =new DataInputStream( [Link] );
for(int j=1;j<=5;j++)
{
[Link]("\nEnter the value for thread A:");
String s =[Link]( );
}
[Link]("\t Exit from A");
}
}
class B extends Thread
{
public void run( )throws IOException
{
for(int j=1;j<=5;j++)
{
[Link]("\nEnter the value for thread B:");
String s =[Link]( );
}
[Link]("\t Exit from B");
}
}
class C extends Thread
{
public void run( )throws IOException
{
for(int k=1;k<=5;k++)
{
[Link]("\nEnter the value for thread C:");
String s =[Link]( );
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 44
}
[Link]("\t Exit from C");
}
}
class Test
{
public static void main(String args[])
{
A ob1 =new A( );
B ob2 =new B( );
C ob3 =new C( );
[Link]( Thread.MAX_PRIORITY );
[Link]( [Link]( ) +1 );
[Link]( Thread.MIN_PRIORITY );
[Link]( "Start Thread A" );
[Link]( );
[Link]( "Start Thread B" );
[Link]( );
[Link]( "Start Thread C" );
[Link]( );
[Link]( "End of Main Thread" );
}
}
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 45
PROGRAM 19
AIM:
Develop a program to demonstrate flow layout.
SOURCE CODE:
import [Link].*;
import [Link].*;
import [Link].*;
/*
<applet code ="FlowLayoutDemo"
width =400
height =200>
</applet>
*/
public class FlowLayoutDemo extends Applet implements ItemListener
{
String msg =" ";
Checkbox win98 , winNT , solaris , mac;
public void init()
{
setLayout( new FlowLayout( [Link] ) );
win98 =new Checkbox("Windows 98/XP",null,true);
winNT =new Checkbox("Windows NT");
solaris =new Checkbox("Solaris");
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 46
mac =new Checkbox("Apple's Mac");
add( win98 );
add( winNT );
add( solaris );
add( mac );
[Link]( this );
[Link]( this );
[Link]( this );
[Link]( this );
}
public void itemStateChanged( ItemEvent ie )
{
repaint( );
}
public void paint( Graphics g )
{
msg ="Current State:";
[Link]( msg , 6 , 80 );
msg ="Windows 98/XP:"+[Link]( );
[Link]( msg , 6 , 100 );
msg ="Windows NT:"+[Link]( );
[Link]( msg , 6 , 120 );
msg ="Solaris:"+[Link]( );
[Link]( msg , 6 , 140 );
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 47
msg ="Apple's Macintosh:"+[Link]( );
[Link]( msg , 6 , 160 );
}
}
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 48
PROGRAM 20
AIM:
Develop a program to demonstrate border layout.
SOURCE CODE:
import [Link].*;
import [Link].*;
public class ShowBorderLayout extends Frame
{
public ShowBorderLayout( )
{
setTitle("ShowBorderLayout");
setLayout( new BorderLayout( 5 , 10 ) );
add("East", new Button("India on the East") );
add("South", new Button("Australia on the South") );
add("West", new Button("America on the West") );
add("North", new Button("Britain on the North") );
add("Center", new Button("Africa on the Center") );
}
public static void main( String ar[ ] )
{
Frame f =new ShowBorderLayout();
[Link]( 400 , 300 );
[Link]( true );
[Link]( new WindowAdapter()
{
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 49
public void windowClosing( WindowEvent evt )
{
[Link]( 0 );
}
});
}
}
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 50
PROGRAM 21
AIM:
Develop a program to demonstrate grid layout.
SOURCE CODE:
import [Link].*;
import [Link].*;
public class ShowGridLayout extends Frame
{
public ShowGridLayout( )
{
setTitle("Grid Layout");
setLayout( new GridLayout( 4 , 3 , 5 , 5 ) );
for( int i =1 ; i <=10 ; i++)
{
add( new Button("Component#"+i) );
}
}
public static void main( String ar[ ] )
{
Frame f =new ShowGridLayout( );
[Link]( 400 , 300 );
[Link]( true );
[Link]( new WindowAdapter( )
{
public void WindowClosing( WindowEvent evt )
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 51
{
[Link]( 0 );
}
});
}
}
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 52
PROGRAM 22
AIM:
Develop a program to demonstrate GUI component "Label".
SOURCE CODE:
import [Link].*;
import [Link].*;
/*
<applet code="LabelDemo" width=100 height=100>
</applet>
*/
public class LabelDemo extends Applet
{
public void init()
{
Label one =new Label("My name");
Label two =new Label("is");
Label three =new Label("Namburi VK");
add(one);
add(two);
add(three);
}
}
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 53
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 54
PROGRAM 23
AIM:
Develop a program to demonstrate GUI component "Button".
SOURCE CODE:
import [Link].*;
import [Link].*;
import [Link].*;
/*
<applet code="ButtonDemo"
width =250
height=150>
</applet>
*/
public class ButtonDemo extends Applet implements ActionListener
{
String msg =" ";
Button yes,no,maybe;
public void init()
{
yes=new Button("Yes");
no=new Button("No");
maybe=new Button("Undecided");
add(yes);
add(no);
add(maybe);
[Link](this);
[Link](this);
[Link](this);
}
public void actionPerformed(ActionEvent ae)
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 55
{
String str =[Link]();
if([Link]("Yes"))
{
msg="You pressed Yes.";
}
else if ([Link]("No"))
{
msg="You pressed No.";
}
else
{
msg="You pressed Undecided.";
}
repaint();
}
public void paint(Graphics g)
{
[Link](msg,6,100);
}
}
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 56
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 57
PROGRAM 24
AIM:
Develop a program to demonstrate GUI component "Checkbox".
SOURCE CODE:
import [Link].*;
import [Link].*;
import [Link].*;
/*
<applet code ="CheckboxDemo"
width=250
height=200 >
</applet>
*/
public class CheckboxDemo extends Applet implements ItemListener
{
String msg =" ";
Checkbox Win98, winNT,solaris,mac;
public void init()
{
Win98 =new Checkbox("Windows 98/XP",null,true);
winNT =new Checkbox("Windows NT/2000");
solaris =new Checkbox("Solaris");
mac =new Checkbox("Apple'sMac");
add(Win98);
add(winNT);
add(solaris);
add(mac);
[Link](this);
[Link](this);
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 58
[Link](this);
[Link](this);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
//Display current state of the check boxes.
public void paint(Graphics g)
{
msg=" Current state :";
[Link](msg,6,80);
msg=" Windows 98/XP:"+[Link]();
[Link](msg,6,100);
msg=" Windows NT/2000:"+[Link]();
[Link](msg,6,120);
msg=" Solaris :"+[Link]();
[Link](msg,6,140);
msg=" Apple's Macintosh: "+[Link]();
[Link](msg,6,160);
}
}
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 59
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 60
PROGRAM 25
AIM:
Develop a program to demonstrate GUI component
"CheckboxGroup".
SOURCE CODE:
import [Link].*;
import [Link].*;
import [Link].*;
/* <applet code ="CheckboxGroupDemo"
width=250
height=200>
</applet>
*/
public class CheckboxGroupDemo extends Applet implements ItemListener
{
String msg =" ";
Checkbox Win98,winNT,solaris,mac;
CheckboxGroup cbg;
public void init()
{
cbg=new CheckboxGroup();
Win98=new Checkbox("Windows 98/XP",cbg,true);
winNT=new Checkbox("Windows NT/2000",cbg,false);
solaris=new Checkbox("Solaris",cbg,false);
mac=new Checkbox("MacOS",cbg,false);
add(Win98);
add(winNT);
add(solaris);
add(mac);
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 61
[Link](this);
[Link](this);
[Link](this);
[Link](this);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
msg="Current selection: ";
msg+=[Link]().getLabel();
[Link](msg,6,100);
}
}
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 62
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 63
PROGRAM 26
AIM:
Develop a program to demonstrate GUI component "Choice".
SOURCE CODE:
import [Link].*;
import [Link].*;
import [Link].*;
/*
<applet code ="ChoiceDemo"
width=300
height=180 >
</applet>
*/
public class ChoiceDemo extends Applet implements ItemListener
{
Choice os,browser;
String msg=" ";
public void init()
{
os=new Choice();
browser=new Choice();
//add items to os list
[Link]("Windows 98/XP");
[Link]("Windows NT/2000");
[Link]("Solaris");
[Link]("MacOS");
//add items to browser list
[Link]("Internet Explorer");
[Link]("Firefox");
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 64
[Link]("Opera");
//add choice lists to window
add(os);
add(browser);
//register to receive item events
[Link](this);
[Link](this);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
msg="Current OS:";
msg+=[Link]();
[Link](msg,6,120);
msg="Current Browser: ";
msg+=[Link]();
[Link](msg,6,140);
}
}
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 65
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 66
PROGRAM 27
AIM:
Develop a program to demonstrate GUI component "List".
SOURCE CODE:
import [Link].*;
import [Link].*;
import [Link].*;
/*
<applet code ="ListDemo"
width =500
height =300 >
</applet>
*/
public class ListDemo extends Applet implements ActionListener
{
List br , prg;
Label l1 , l2;
String msg ="";
public void init()
{
prg =new List( 2 , true );
br =new List( 5 , false );
l1 =new Label("Select a program:");
l2 =new Label("Select a branch:");
[Link]("[Link]");
[Link]("[Link]");
[Link]("C.S.E");
[Link]("I.T");
[Link]("E.C.E");
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 67
[Link]("E.I.E");
[Link]("E.E.E");
[Link]( 1 );
add( l1 );
add( prg );
add( l2 );
add( br );
[Link]( this );
[Link]( this );
}
public void actionPerformed( ActionEvent ae )
{
repaint();
}
public void paint( Graphics g )
{
int n[];
msg ="Program selected:";
n =[Link]();
for( int i =0 ; i <[Link] ; i++)
{
msg +=[Link]( n[i] ) +" ";
}
[Link]( msg , 6 , 120 );
msg ="Branch selected:";
msg +=[Link]();
[Link]( msg , 6 , 140 );
}
}
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 68
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 69
PROGRAM 28
AIM:
Develop a program to demonstrate GUI component "TextField".
SOURCE CODE:
//program to calculate future value of an investment
import [Link].*;
import [Link].*;
import [Link].*;
/*<applet code ="TextFieldDemo"
width =250
height =250 >
</applet>
*/
public class TextFieldDemo extends Applet implements ActionListener
{
TextField intrate , intamnt;
Label l1 , l2;
public void init( )
{
intrate =new TextField(15);
intamnt =new TextField(10);
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 70
l1 =new Label("NAME:",[Link]);
l2 =new Label("PASSWORD:",[Link]);
[Link]('*');
add(l1);
add(intrate);
add(l2);
add(intamnt);
[Link]( this );
[Link]( this );
}
public void actionPerformed( ActionEvent ae )
{
repaint( );
}
public void paint( Graphics g )
{
[Link]("Name:" +[Link]( ) , 6 , 90 );
[Link]("Selected text in name:" +[Link]( ) , 6 , 110 );
[Link]("Password:" +[Link]( ) , 6 , 130 );
} }
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 71
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 72
PROGRAM 29
AIM:
Develop a program to demonstrate GUI component "TextArea".
SOURCE CODE:
import [Link].*;
import [Link].*;
import [Link].*;
/*
<applet code ="TextAreaDemo"
width =500
height =400 >
</applet>
*/
public class TextAreaDemo extends Applet implements ActionListener
{
TextArea a;
Button b;
Label l1;
String msg ="";
public void init()
{
l1 =new Label("Write about yourself in less than 250 characters:");
b =new Button("SUBMIT");
a =new TextArea();
add( l1 );
add( a );
add( b );
[Link](this);
}
public void actionPerformed( ActionEvent ae )
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 73
{
String str =[Link]();
if( [Link]("SUBMIT") )
{
msg ="Your answer is submitted!";
}
repaint();
}
public void paint( Graphics g )
{
[Link]( msg , 180 , 250 );
}
}
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 74
PROGRAM 30
AIM:
Develop an applet that receives three numeric values as input
from the user and then display the largest of the three.
SOURCE CODE:
import [Link].*;
import [Link].*;
/*<applet code ="Input3"
width =800
height =200>
</applet>*/
public class Input3 extends Applet
{
TextField txt1 , txt2 , txt3;
public void init()
{
txt1 =new TextField( 10 );
txt2 =new TextField( 10 );
txt3 =new TextField( 10 );
Label one =new Label("Enter the three input numbers:");
add( one );
add( txt1 );
add( txt2 );
add( txt3 );
/*[Link]("0");
[Link]("0");
[Link]("0");*/
}
public void paint( Graphics g )
{
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 75
int x =0 , y =0 , z =0 , l ,l1 , l2;
String s1 , s2 , s3 ,s;
try
{
s1 =[Link]();//if u don't give this code in try,u can remove
initializations of x , y , z and enter the comments of setText
x =[Link]( s1 );
s2 =[Link]();
y =[Link]( s2 );
s3 =[Link]();
z =[Link]( s3 );
}
catch( Exception e ){}
l1 =x >y ? x : y;
l2 =x >z ? x : z;
l =l1 >l2 ? l1 : l2;
s =[Link]( l );
[Link]("The largest of the three numbers entered is:" , 20 , 120 );
[Link]( s , 275 , 120 );
}
public boolean action( Event event , Object object )
{
repaint();
return true;
}
}
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 76
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 77
PROGRAM 31
AIM:
Develop program to calculate the future value of an investment at
a given interest rate for a specified number of years. The formula for the
calculat ion is as follows:
Future value =Investment Amount (1+Interest Rate) years.
Use text fields for interest rate, investment amount and years. Display the
future amount in a textfield when clicking the calculate button, or
choosing calculate form the operation menu.
SOURCE CODE:
import [Link].*;
import [Link].*;
import [Link].*;
/*<applet code ="FtrInvst"
width =250
height =250 >
</applet>*/
public class FtrInvst extends Applet implements ActionListener
{
TextField intrate , intamnt , yrs , futamnt;
Button cal;
Label l1 , l2 , l3 , l4;
public void init()
{
intrate =new TextField(5);
intamnt =new TextField(10);
yrs =new TextField(3);
futamnt =new TextField(15);
cal =new Button("CALCULATE");
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 78
l1 =new Label("Interest Rate:",[Link]);
l2 =new Label("Investment Amount:",[Link]);
l3 =new Label("Number of years:",[Link]);
l4 =new Label("Future value of investment:",[Link]);
add(l1);
add(intrate);
add(l2);
add(intamnt);
add(l3);
add(yrs);
add(cal);
add(l4);
add(futamnt);
[Link]( this );
}
public void actionPerformed( ActionEvent ae )
{
float x =0 , y =0;
int z =0;
double f =0;
String s1 , s2 , s3 , s;
try
{
s1 =[Link]();
x =[Link]( s1 );
s2 =[Link]();
y =[Link]( s2 );
s3 =[Link]();
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 79
z =[Link]( s3 );
}
catch( Exception e ){}
String str =[Link]();
if( [Link]("CALCULATE") )
{
f =y * ( 1 +x ) * z;
}
s =[Link]( f );
[Link]( s );
repaint();
}
}
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 80
PROGRAM 32
AIM:
Develop a program to meet the following requirements
(a) Create a frame with flow layout
(b) Create two panels and add the panels to the frame
(c) Each panel contains three buttons. The panel uses flow layout.
SOURCE CODE:
import [Link].*;
public class PanFrReq extends Frame
{
public void init()
{
FlowLayout x=new FlowLayout();
setLayout(x);
Panel p1=new Panel();
[Link](100,100);
Panel p2=new Panel();
[Link](100,100);
Button btn1=new Button("B1");
Button btn2=new Button("B2");
Button btn3=new Button("B3");
Button btn4=new Button("B4");
Button btn5=new Button("B5");
Button btn6=new Button("B6");
[Link](btn1);
[Link](btn2);
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 81
[Link](btn3);
[Link](btn4);
[Link](btn5);
[Link](btn6);
add(p1);
add(p2);
[Link]( [Link] );
[Link]( [Link] );
setSize(500,500);
setVisible(true);
}
public static void main(String args[])
{
PanFrReq ob=new PanFrReq();
[Link]();
}
}
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 82
PROGRAM 33
AIM:
Develop a program to display the mouse position when the mouse
is pressed.
SOURCE CODE:
import [Link].*;
import [Link].*;
import [Link].*;
/* <applet code ="MouseLoc"
width =400
height =200> </applet>*/
public class MouseLoc extends Applet
{
int x=0;
int y=0;
public void init()
{
addMouseListener(new mymouselistener());
}
public void start()
{
}
public void paint(Graphics g)
{
[Link](x,y,x,y);
[Link](x +","+y, x,y);
}
public class mymouselistener extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
x =[Link]();
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 83
y =[Link]();
repaint();
}
}
}
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 84
PROGRAM 34
AIM:
Develop a program to handle mouse events.
SOURCE CODE:
import [Link].*;
import [Link];
import [Link].*;
import [Link].*;
/*<applet code="Mouse"
width =400
height =300>
</applet>*/
public class Mouse extends Applet implements MouseListener,MouseMotionListener
{
String txt="";
int x=10,y=30;
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me)
{
txt="Mouse Clicked";
setForeground([Link]);
repaint();
}
public void mouseEntered(MouseEvent me)
{
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 85
txt="Mouse Entered";
repaint();
}
public void mouseExited(MouseEvent me)
{
txt="Mouse Exited";
setForeground([Link]);
repaint();
}
public void mousePressed(MouseEvent me)
{
txt="Mouse Pressed";
setForeground([Link]);
repaint();
}
public void mouseMoved(MouseEvent me)
{
txt="Mouse Moved";
setForeground([Link]);
repaint();
}
public void mouseDragged(MouseEvent me)
{
txt="Mouse Dragged";
setForeground([Link]);
repaint();
}
public void mouseReleased(MouseEvent me)
{
txt="Mouse Released";
setForeground([Link]);
repaint();
}
public void paint(Graphics g)
{
[Link](txt,200,150);
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 86
showStatus("Mouse events Handling");
}
}
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 87
PROGRAM 35
AIM:
Develop an applet to draw the following shapes.
[Link]
[Link]
[Link]
[Link] inside a circle
[Link] inside a square
SOURCE CODE:
import [Link].*;
import [Link].*;
/*<applet code=shapes
width=300
height=300>
</applet>*/
public class shapes extends Applet
{
public void paint(Graphics g)
{
[Link](30,200,80,40);
[Link](30,220,70,50);
[Link](110,220,70,50);
[Link]("cone",50,260);
[Link](200,200,80,40);
[Link](200,50,80,40);
[Link](200,70,200,220);
[Link](280,70,280,220);
[Link]("cylinder",210,260);
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 88
[Link](360,50,80,80);
[Link](400,90,80,80);
[Link](360,50,400,90);
[Link](440,50,480,90);
[Link](360,130,400,170);
[Link](440,130,480,170);
[Link]("cube",420,200);
[Link](30,350,100,100);
[Link](30,350,100,100);
[Link]("circle in square",30,470);
[Link](220,350,100,100);
[Link](235,365,70,70);
[Link]("square in circle",220,470);
}
}
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 89
PROGRAM 36
AIM:
Develop an applet to display a bar chart by passing parameters to
applet.
SOURCE CODE:
/* <applet code="exBarChart"
height=300
width=350>
<param name ="totcol" value="4">
<param name ="c1" value ="110">
<param name ="c2" value ="150">
<param name ="c3" value ="100">
<param name ="c4" value ="170">
<param name ="lbl1" value ="2010">
<param name ="lbl2" value ="2011">
<param name ="lbl3" value ="2012">
<param name ="lbl4" value ="2013">
</applet>*/
import [Link].*;
import [Link].*;
public class exBarChart extends Applet
{
int n=0;
String label[];
int value[];
public void init()
{
try
{
n =[Link](getParameter("totcol"));
label =new String[n];
value =new int[n];
label[0] =getParameter("lbl1");
label[1] =getParameter("lbl2");
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 90
label[2] =getParameter("lbl3");
label[3] =getParameter("lbl4");
value[0] =[Link](getParameter("c1"));
value[1] =[Link](getParameter("c2"));
value[2] =[Link](getParameter("c3"));
value[3] =[Link](getParameter("c4"));
}
catch(Exception e) {}
}
public void paint(Graphics g)
{
for (int i=0;i<n ;i++)
{
[Link]([Link]);
[Link](label[i],20,(i*50)+30);
[Link]([Link]);
[Link](50,(i*50)+10,value[i],40);
[Link](" " +value[i],value[i]+50,(i*50)+30);
}
}
}
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 91
PROGRAM 37
AIM:
Develop an applet to display different bar charts by taking
different inputs.
SOURCE CODE:
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link];
import [Link].*;
/*
<applet code="Drawchart"
width=500
height=500 >
</applet>
*/
public class Drawchart extends Applet
{
int x[]=new int[10];
String y[]=new String[10];
int n;
public void init()
{
[Link]("Enter the [Link] bars in chart:");
Scanner a=new Scanner([Link]);
n=[Link]();
[Link]("Enter titles of "+n+" bars:");
for(int j=0;j<=n;j++)
{
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 92
y[j]=[Link]();
}
for(int i=1;i<=n;i++)
{
[Link]("Enter the value of bar" +i +":");
x[i]=[Link]();
}
}
public void paint(Graphics g)
{
[Link]("The depicted bar graph is:",50,50);
for(int i=1;i<=n;i++)
{
[Link]( [Link] );
[Link](150+(i*50),150-x[i],40,x[i]);
[Link]( [Link] );
[Link](y[i],157+(i*50),170+30);
}
}
}
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 93
OUTPUT:
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 94
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 95
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 96
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 97
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 98
JAVA RECORD - 2/ 4 B4
CSE,GIT,GITAM University Page 99