Certificate: Date of Submission: - No. of Practical Certified
Certificate: Date of Submission: - No. of Practical Certified
College Seal
1
CGPIT/CE/SEM-5/Java Technologies
CHHOTUBHAI GOPALBHAI PATEL INSTITUTE OF TECHNOLOGY
Computer Engineering
Java Technologies (030090502)
Index
Sr. Date of
Title Sign
No. Practical
1. a) Write a program to print Hello CGPIT on the console.
b) Write a program to print inputs given from command line
arguments on the console. 30-07-2020
c) Write a program to check whether given number is Armstrong or
not.
2. a) Writea program using switch which takes two command line
arguments. First argument is a string (odd, even, avg, sum) specifies the
operation and second argument (number N) is the upper limit of the
range starting from 0.
Ex. 1) Arguments:even 10; 06-08-2020
Output: 0 2 4 6 8 10.
2) Arguments: sum 5;
Output: 15
b) Write a program to sort an array of N integer.
3. Design a class to represent a Bank account.
Fields:
1.Name of account holder
2.Account number
3.Balance in the account
20-08-2020
Methods:
1.To deposit amount
2. To withdraw amount after checking for balance
3. To display the details of account
Write a program to create account for two customers.
4. a) Write a program to remove duplicate characters from a string. 27-08-2020
b) Write a program to insert character at specific location of a string.
5. Create class Box which has members height, width, length and method
volume() to calculate the volume of Box.
03-09-2020
Write a program to display volume of two boxes. Use default
constructor and parameterized constructor to initialize data members.
6. Create a class student which contains data members enrollment 10-09-2020
number, marks1, marks2 and marks3. It contains methods set() and
get() to take input and print the values of attributes. Create another
class result by inheriting class student.Result class contains the field
2
CGPIT/CE/SEM-5/Java Technologies
total_marks and method dis_res() which displays the percentage of a
student. Write a program to display the results of two students.
7. a) Write a Java class ShapePerimeter which calculates the perimeter of
a square, rectangle, and circle. Write a program to print the
perimeter of square, rectangle and circle. Demonstrate the concept
of method overloading.
b) Create class Bank with member function getInterestRate(). Create
another three classes SBI, ICICI and AXIS by inheriting class Bank.
These classes overrides member function getInterestRate() of class 17-09-2020
Bank. In case of Bank class getInterestRate() method will return
value 9.5 for interest rate, in case of SBI class this method will
return value 7.5 for interest rate, in case of ICICI class this method
will return value 8.5 for interest rate, in case of AXIS class this
method will return value 8.25 for interest rate. Write a program to
find the interest amount for selected bank using the concept of
method overriding and dynamic method dispatch.
8. a) Write a program to demonstrate uses of final keyword.
b) Write a program to demonstrate uses of static keyword.
c) Create a base class Vehicle with member variable speed. Create
another class Bike which inherits class Vehicle and has member
variable speed along with member variable name. Use a 24-09-2020
constructor to assign values to member variables. Along with data
members provide one method in both the classes named display()
to show values of class variables. Write a program to demonstrate
the uses of the super keyword.
9. a) Create abstract class department with data members university,
college, method display() and abstract method subject_list(). Create
subclasses CE and EE which implements the method subject_list().
Write a program to display subjects offered by CE and EE department. 01-10-2020
b) Define an interface bird having the methods food() and voice().
Define two classes sparrow and peacock to implement the interface
bird. Write a program to test sparrow and peacock classes.
10. a) Write a program to handle following exceptions:
1. StringIndexOutOfBoundsException
2. NullPointerException 21-10-2020
3. NumberFormatException
b) Write a program to generate and handle a custom exception to
validate the entered phone number.
11. Write an application that executes two threads. One thread Displays
“I’m Thread 1“every 1,000 milliseconds. And the other displays “I’m
Thread 2 “every 3,000 milliseconds.
21-10-2020
Create the threads by
a) implementing the Runnable interface &
b) extending Thread class.
12. Write a program in which user takes Celsius from file:"celsius.txt" and
convert it into Fahrenheit and put down generated output in different 28-10-2020
file "fahrenheit.txt".
3
CGPIT/CE/SEM-5/Java Technologies
13. Write a Java program using socket programming in which client send
string of 5 numbers to server, and server arrange all numbers in
04-11-2020
descending order sorting and send back that string of 5 numbers to
client to display result at client side.
14. Create mini project using concepts of Java like multithreading, applet,
04-11-2020
socket programming, file handling etc.
15. Write a program to create arithmetic calculator using applet. 19-11-2020
16. Create the class SumOfSquare class.It contains method findSum() and
isEqualSum().
26-11-2020
Write a java program to find sum of two different types of array are
equal or not using generic programming.
4
CGPIT/CE/SEM-5/Java Technologies
Practical - 1
Aim:
A) Write a Java program to print Hello CGPIT on the console.
Code:
public class Main
{
public static void main(String[] args) {
System.out.println(“Hello CGPIT”;);
}
}
Output:
Code:
import java.util.*;
class Hexa_Binary
{
Scanner scan;
int num;
void getVal()
{
scan = new Scanner(System.in);
5
CGPIT/CE/SEM-5/Java Technologies
num = Integer.parseInt(scan.nextLine(), 16);
}
void convert()
{
String binary = Integer.toBinaryString(num);
System.out.println(binary);
}
}
class Main
{
public static void main(String args[])
{
Hexa_Binary obj = new Hexa_Binary();
obj.getVal();
obj.convert();
}
}
Output:
Code:
import java.util.*;
public class Main
{
static int ar(int n, int r)
{
int c, p = 1;
for (c = 1; c <= r; c++)
6
CGPIT/CE/SEM-5/Java Technologies
p = p*n;
return p;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
Main ob = new Main();
System.out.println(“Enter number:”);
int n = sc.nextInt();
int a, arm=0, r,digits=0;
a = n;
while (a>0)
{
digits++;
a = a/10;
}
a = n;
while(a>0)
{
r = a%10;
arm = arm + ob.ar(r,digits) ;
a = a/10;
}
if(arm == n)
System.out.print(n+”is an Armstrong number”);
else
System.out.print(n+”is not an Armstrong number”);
}
}
7
CGPIT/CE/SEM-5/Java Technologies
Output:
Practical –2
Aim:
A)Write a program using switch which takes two inputs from user. Firstinput
is a string (odd, even, avg, sum) specifies the operation andsecond input
(number N) is the upper limit of the range starting from 0.
Code:
import java.util.*;
class Main
{
public static void main(String[] args)
{
scanner sc = new Scanner(System.in);
String s = sc.next();
int x = sc.nextInt();
int n=0;
int sum =0;
switch(s)
{
case “odd” :
for(int i=0;i<=x;i++)
if(i%2!=0)
System.out.println(i);
break;
case “even” :
for(int i=0;i<=x/2;i++)
8
CGPIT/CE/SEM-5/Java Technologies
System.out.println(2*i);
break;
case "sum" :
for(int i=1;i<=x;i++)
sum+=i;
System.out.println(sum);
break;
case "avg" :
for(int i=1;i<=x;i++)
sum+=i;
System.out.println((float)sum/x);
break;
default:
System.out.println("no match");
}
}
}
Output:
9
CGPIT/CE/SEM-5/Java Technologies
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter total number of values:");
int n = sc.nextInt(),temp;
int[] num = new int[n];
System.out.println("Enter Data:");
for(int i=0;i<n;i++)
num[i] = sc.nextInt();
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(num[i]>num[j])
{
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
for(int i=0;i<n;i++)
System.out.println(num[i]);
}
}
Output:
10
CGPIT/CE/SEM-5/Java Technologies
Practical –3
Aim:Design a class to represent a Bank account. Fields: 1. Name of account
holder 2. Account number 3. Balance in the account
Methods: 1. To deposit amount 2. To withdraw amount after checking for
balance 3. To display the details of account
Write a program to create account for two customers.
Code:
import java.util.Scanner;
import java.io.*;
class bank
{
String name = "";
int accountNumber = 0;
double balance = 0;
void deposit(double amount)
{
balance += amount;
}
void withdraw(double amount)
{
balance -= amount;
}
void details()
{
System.out.println("Name :" + name);
System.out.println("Account number : " + accountNumber);
System.out.println("Balance : " + balance);
11
CGPIT/CE/SEM-5/Java Technologies
}
}
public class Main extends bank
{
public static void main(String[] args)
{
bank[] b = new bank[2];
int accountNumber, choice = 0, amount;
Scanner sc = new Scanner(System.in);
String[] cust = {"first", "second"};
for(int i = 0; i < 2; i++)
b[i] = new bank();
for(int i = 0; i < 2; i++)
{
System.out.println("Enter name of " + cust[i] + " customer");
b[i].name = sc.next();
System.out.println("Enter account number of " + cust[i] + " customer");
b[i].accountNumber = sc.nextInt();
System.out.println("Enter balance of " + cust[i] + " customer");
b[i].balance = sc.nextDouble();
}
do{
System.out.println("Enter account number of deposit and withdraw process");
accountNumber = sc.nextInt();
for(int i = 0; i < 2; i++)
if(accountNumber == b[i].accountNumber)
{
System.out.println("Enter 1 for deposit and 2 for withdrawal for "
+ cust[i] +"customer");
12
CGPIT/CE/SEM-5/Java Technologies
choice = sc.nextInt();
if(choice == 1)
{
System.out.println("Enter amount to be deposit for " + cust[i] + " customer");
amount = sc.nextInt();
b[i].deposit(amount);
System.out.println("Account " + accountNumber + "
is having balance of: " + b[i].balance);
}
else if(choice == 2)
{
System.out.println("Enter amount to be withdraw for "
+ cust[i] + " customer");
amount = sc.nextInt();
b[i].withdraw(amount);
System.out.println("Account " + accountNumber + "
is having balance of: " + b[i].balance);
}
System.out.println("Do you want to continue?
Press 1 for Yes Press 2 for No:");
choice = sc.nextInt();
}
}while(choice == 1);
}
}
Output:
Practical –4
Aim:
13
CGPIT/CE/SEM-5/Java Technologies
A) Write a program to remove duplicate characters from a string.
Code:
import java.lang.*;
import java.util.*;
class Main
{
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
String str = "";
int tmp = 0;
String s = sc.nextLine();
for (int i=0; i<s.length(); i++)
{
if(i == 0)
str += s.charAt(i);
for(int j=0;j<str.length();j++)
if(s.charAt(i) == str.charAt(j))
{
tmp = 1;
break;
}
else
tmp = 0;
if(tmp == 0)
str += s.charAt(i);
}
System.out.println(str);
14
CGPIT/CE/SEM-5/Java Technologies
}
}
Output:
B) Write a program to insert character at specific location of a string.
Code:
import java.io.*;
import java.util.*;
class Main
{
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter String:");
String s = sc.nextLine();
String sec = "";
System.out.println("Enter Location:");
int loc = sc.nextInt();
System.out.println("Enter new character:");
String c = sc.next();
sec = s.substring( loc, s.length() );
s = s.substring(0,loc);
s += c;
System.out.println(s+sec);
}
}
Output:
Practical –5
15
CGPIT/CE/SEM-5/Java Technologies
Aim: Create class Box which has members height, width, length, and
method volume() to calculate the volume of Box. Write a program to
display the volume of two boxes. Use default constructor and
parameterized constructor to initialize data members.
Code:
import java.util.*;
class Box
{
int h,w,l;
Box()
{
h=3;
w=4;
l=5;
}
Box(int a,int b,int c)
{
h=a;
w=b;
l=c;
}
void volume()
{
System.out.println("Volume of box is: "+h*w*l);
}
};
class Main
{
public static void main (String[] args)
16
CGPIT/CE/SEM-5/Java Technologies
{
Scanner s=new Scanner(System.in);
Box a = new Box();
System.out.println("Enter height: ");
int p=s.nextInt();
System.out.println("Enter width: ");
int q=s.nextInt();
System.out.println("Enter length: ");
int r=s.nextInt();
Box b = new Box(p,q,r);
System.out.println("Answer for Default constructor");
a.volume();
System.out.println("Answer for Parameterized constructor");
b.volume();
}
}
Output:
Practical –6
17
CGPIT/CE/SEM-5/Java Technologies
Aim: Create a class student that contains private data members'
enrollment number, marks1, marks2, and marks3. It contains methods for
all private data members as set() and get() to take input and print the
values of attributes. Create another class result by inheriting class students.
The Result class contains the field total_marks and method dis_res() which
displays the percentage of a student. Write a program to display the results
of two students.
Code:
import java.util.*;
class student
{
private int en=0;
public int marks=0;
private int[] mar = new int[3];
public void set(int en, int[] mar)
{
this.en = en;
for(int i = 0; i<3;i++)
{
this.mar[i] = mar[i];
marks += mar[i];
}
}
public void get(int en)
{
System.out.println("Enrollment Number : " + en);
for(int i=0;i<3;i++)
System.out.println("Marks "+ (i+1) +" : " + mar[i]);
}
public int getEn()
18
CGPIT/CE/SEM-5/Java Technologies
{
return en;
}
}
class result extends student
{
int total_marks;
public void dis_res()
{
total_marks = marks;
System.out.println("Student " + getEn() + " havingpercentage:"+
(float)total_marks/3);
}
}
class Main
{
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
result[] r = new result[2];
for(int i=0;i<2;i++)
r[i] = new result();
int en=0;
int[] mar = new int[3];
for(int i = 0; i < 2; i++)
{
System.out.println("Enter enrollmentNumber of student" + (i+1)+":");
en = sc.nextInt();
for(int j = 0; j < 3; j++)
19
CGPIT/CE/SEM-5/Java Technologies
{
System.out.println("Enter marks " + (j+1) +" of student" +
(i+1)+":");
mar[j] = sc.nextInt();
}
r[i].set(en,mar);
}
for(int i=0;i<2;i++)
r[i].dis_res();
}
}
Output:
Practical –7
Aim:
20
CGPIT/CE/SEM-5/Java Technologies
A) Write a Java class ShapePerimeter which calculates the perimeter of a
square, rectangle, and circle. Write a program to print the perimeter of
square, rectangle and circle. Demonstrate the concept of method
overloading.
Code:
import java.util.*;
class ShapePerimeter
{
void perimeter(int a)
{
System.out.println(4*a);
}
void perimeter(float a)
{
System.out.println(2*3.14*a);
}
void perimeter(int a,int b)
{
System.out.println(2*(a+b));
}
}
class Main
{
public static void main (String[] args)
{
Scanner s = new Scanner(System.in);
ShapePerimeter p1 = new ShapePerimeter();
System.out.println("Enter for Square ");
int a=s.nextInt();
21
CGPIT/CE/SEM-5/Java Technologies
System.out.println("Enter for Circle");
float b=s.nextFloat();
System.out.println("Enter for Rectangle");
int c=s.nextInt();
int d=s.nextInt();
p1.perimeter(a);
p1.perimeter(b);
p1.perimeter(c,d);
}
}
Output:
Code:
import java.util.*;
class Bank
{
double i=9.5;
double getInterestRate()
{
return i;
}
22
CGPIT/CE/SEM-5/Java Technologies
}
class SBI extends Bank
{
double i=7.5;
double getInterestRate()
{
return this.i;
}
}
class ICICI extends Bank
{
double i=8.5;
double getInterestRate()
{
return this.i;
}
}
class AXIS extends Bank
{
double i=8.25;
double getInterestRate()
{
return this.i;
}
}
class Main
{
public static void main (String[] args)
{
23
CGPIT/CE/SEM-5/Java Technologies
Scanner s = new Scanner(System.in);
SBI a1 = new SBI();
ICICI a2 = new ICICI();
AXIS a3 = new AXIS();
Bank a4 = new Bank();
System.out.println("Enter principal amount:");
int p=s.nextInt();
System.out.println("Enter no. of year:");
int n=s.nextInt();
System.out.println("Enter name of bank:");
s.nextLine();
String b=s.nextLine();
switch(b)
{
case "SBI":
double in=(p*n*(a1.getInterestRate()))/100;
System.out.println("Interest Amount "+in);
break;
case "ICICI":
in=(p*n*(a2.getInterestRate()))/100;
System.out.println("Interest Amount "+in);
break;
case "AXIS":
in=(p*n*(a3.getInterestRate()))/100;
System.out.println("Interest Amount "+in);
break;
default:
in=p*n*(a4.getInterestRate())/100;
System.out.println("Interest Amount "+in);
24
CGPIT/CE/SEM-5/Java Technologies
}
}
}
Output:
Practical –8
Aim:
25
CGPIT/CE/SEM-5/Java Technologies
A) Write a program to demonstrate uses of final keyword.
Code:
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner s1 = new Scanner(System.in);
final double PIE = 3.14;//declare final variable PIE = 3.14 that can't be change.
int r;
System.out.print("Enter radius: ");
r=s1.nextInt();
double a = PIE*r*r;
System.out.println("Area of Circle: " +a);
}
}
Output:
B)Write a program to demonstrate uses of static keyword.
Code:
class Demo
{
static int count=0;
void inc()
{
count++;
System.out.println(count);
26
CGPIT/CE/SEM-5/Java Technologies
}
}
class Main
{
public static void main(String args[])
{
Demo d1[] = new Demo[5];
for(int i=0;i<5;i++)
{
d1[i] = new Demo();
d1[i].inc();
}
}
}
Output:
C) Create a base class Vehicle with member variable speed. Create another
class Bike which inherits class Vehicle and has member variable speed
along with member variable name. Use a constructor to assign values to
member variables. Along with data members provide one method in both
the classes named display() to show values of class variables. Write a
program to demonstrate the uses of the super keyword.
Code:
import java.util.Scanner;
class Vehicle
{
int speed;
void display()
{
27
CGPIT/CE/SEM-5/Java Technologies
System.out.println("Speed: "+speed);
}
}
class Bike extends Vehicle
{
int speed;
String name;
Bike(int s,String n)
{
super.speed = s;
speed = s;
name = n;
}
void display()
{
System.out.println("Name: "+name);
super.display();
}
}
class Main
{
public static void main (String[] args)
{
int s;
String n;
Scanner s1 = new Scanner(System.in);
System.out.print("Enter name of bike: ");
n = s1.next();
System.out.print("Enter speed of bike: ");
28
CGPIT/CE/SEM-5/Java Technologies
s = s1.nextInt();
Bike b1 = new Bike(s,n);
b1.display();
}
}
Output:
Practical –9
Aim:
29
CGPIT/CE/SEM-5/Java Technologies
A) Create abstract class department with data members university,
college, method display() and abstract method subject_list(). Create
subclasses CE and EE which implements the method subject_list(). Write a
program to display subjects offered by CE and EE department.
Code:
abstract class department
{
String uni,coll;
department(String uni,String coll)
{
this.uni=uni;
this.coll=coll;
}
abstract void subject_list();
void display()
{
System.out.println("University name is: "+uni);
System.out.println("College name is: "+coll);
}
}
class CE extends department
{
CE(String a,String b)
{
super(a,b);
}
void subject_list()
{
display();
30
CGPIT/CE/SEM-5/Java Technologies
System.out.println("subjects for CE department are: ");
System.out.println("java\n wt\n ape\n cn\n");
}
}
class EE extends department
{
EE(String a,String b)
{
super(a,b);
}
void subject_list()
{
display();
System.out.println("subjects for EE department are: ");
System.out.println("ac\n msd\n dld\n");
}
}
class Main
{
public static void main(String[] args)
{
CE c = new CE("UTU","CGPIT");
c.subject_list();
EE e = new EE("GTU","SCET");
e.subject_list();
}
}
Output:
31
CGPIT/CE/SEM-5/Java Technologies
B) Define an interface bird having the methods food() and voice(). Define
two classes sparrow and peacock to implement the interface bird. Write a
program to test sparrow and peacock classes.
Code:
nterface bird
{
void food();
void voice();
}
class sparrow implements bird
{
public void food()
{
System.out.println("food : penuts");
}
public void voice()
{
System.out.println("voice : chi.. chi..");
}
}
class peacock implements bird
{
public void food()
{
System.out.println("food : nuts");
}
public void voice()
{
System.out.println("voice : koo... koo..");
32
CGPIT/CE/SEM-5/Java Technologies
}
}
class Main
{
public static void main(String[] args)
{
sparrow s = new sparrow();
s.food();
s.voice();
peacock p = new peacock();
p.food();
p.voice();
}
}
Output:
Practical – 10
Aim:
A)Write a program to handle following exceptions:
33
CGPIT/CE/SEM-5/Java Technologies
1. StringIndexOutOfBoundsException
2. NullPointerException
3. NumberFormatException
Code:
public class Main
{
public static void main(String[] args)
{
String s = "Hello";
try{
char c = s.charAt(7);
}
catch(StringIndexOutOfBoundsException e)
{
System.out.println("Invalid index number.");
}
s = null;
try{
if(s.equals("abc"))
System.out.println("Same");
else
System.out.println("Not same");
}
catch(NullPointerException e)
{
System.out.println("String is empty...");
}
try{
34
CGPIT/CE/SEM-5/Java Technologies
int n=Integer.parseInt("abc");
System.out.println(n);
}
catch(NumberFormatException e)
{
System.out.println("Number format Exception found...");
}
}
}
Output:
Code:
import java.util.*;
class phone
{
void validno() throws Exception
{
Scanner s = new Scanner(System.in);
String ph = s.nextLine();
int n=ph.length();
if(n<10 || n>10)
{
throw new Exception("Phone number must be of 10 digits.");
}
}
}
35
CGPIT/CE/SEM-5/Java Technologies
public class Main
{
public static void main(String[] args)
{
phone p = new phone();
try{
p.validno();
}
catch(Exception ae)
{
System.out.println(ae.getMessage);
}
}
}
Output:
Practical – 11
36
CGPIT/CE/SEM-5/Java Technologies
Aim: Write an application that executes two threads. One threadDisplays
“I’m Thread 1“every 1,000 milliseconds. And the other displays “I’m Thread
2 “every 3,000 milliseconds.Create the threads by
a) implementing the Runnable interface
Code:
class Main implements Runnable
{
public void run()
{
for(int i=1;i<=5;i++)
{
try{
Thread.sleep(3000);
}
catch(InterruptedException e)
{
System.out.println(e);
}
System.out.println("I'm "+Thread.currentThread().getName());
}
}
public static void main (String[] args)
{
Main m1=new Main();
Main m2=new Main();
Thread t1=new Thread(m1);
Thread t2=new Thread(m2);
t1.setName("Thread1");
t2.setName("Thread2");
37
CGPIT/CE/SEM-5/Java Technologies
t1.start();
t2.start();
}
}
Output:
Code:
class Main extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
try{
Thread.sleep(3000);
}
catch(InterruptedException e)
{
System.out.println(e);
}
System.out.println("I'm "+Thread.currentThread().getName());
}
}
public static void main (String[] args)
{
Main t1=new Main();
38
CGPIT/CE/SEM-5/Java Technologies
Main t2=new Main();
t1.setName("Thread1");
t2.setName("Thread2");
t1.start();
t2.start();
}
}
Output:
Practical – 12
39
CGPIT/CE/SEM-5/Java Technologies
Aim:Write a program in which user takes Celsius from file :"celsius.txt"
and convert it into Fahrenheit and put down generated output in different
file "fahrenheit.txt".
Code:
import java.io.*;
import java.util.*;
class P12
{
public static void main(String[] args)
{
FileReader r = null;
FileWriter w = null;
double c = 0,f;
try{
r = new FileReader("celsius.txt");
w = new FileWriter("fahrenheit.txt");
Scanner s1 = new Scanner(r);
while(s1.hasNextDouble())
{
c = s1.nextDouble();
System.out.println("Fetched: "+c);
}
f = ((c*9)/5)+32;
System.out.println("F: "+f);
String s = Double.toString(f);
w.write(s);
r.close();
w.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
}
Output:
Practical – 13
40
CGPIT/CE/SEM-5/Java Technologies
Aim: Write a Java program using socket programming in which client send
string of 5 numbers to server, and server arrange all numbers in descending
order sorting and send back that string of 5 numbers to client to display
result at client side.
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
Label l1,l2,l3;
TextField t1,t2;
Button b1,b2,b3,b4;
String s1=t1.getText();
String s2=t2.getText();
double a=Integer.parseInt(s1);
double b=Integer.parseInt(s2);
if(e.getSource()==b1)
else if(e.getSource()==b2)
else if(e.getSource()==b3)
41
CGPIT/CE/SEM-5/Java Technologies
{
else if(e.getSource()==b4)
setFont(new Font("Arial",Font.BOLD,15));
setForeground(Color.red);
t1=new TextField(10);
t2=new TextField(10);
b1=new Button("ADD");
b2=new Button("SUB");
b3=new Button("MUL");
b4=new Button("DIV");
l1.setBounds(100,200,200,50);
t1.setBounds(350,200,300,50);
l2.setBounds(100,300,200,50);
t2.setBounds(350,300,300,50);
b1.setBounds(375,400,50,50);
b2.setBounds(435,400,50,50);
42
CGPIT/CE/SEM-5/Java Technologies
b3.setBounds(495,400,50,50);
b4.setBounds(555,400,50,50);
l3.setBounds(100,500,400,50);
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
add(b2);
add(b3);
add(b4);
add(l3);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
Output:
Practical – 14
Aim: Write a program to create arithmetic calculator using applet.
43
CGPIT/CE/SEM-5/Java Technologies
Code:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="Cal" width=300 height=300>
</applet>
*/
Output:
Practical – 15
46
CGPIT/CE/SEM-5/Java Technologies
Aim: Create the class SumOfSquare class. It contains method findSum() and
isEqualSum(). Write a program to find sum of two different types of array are
equal or not using generic programming.
Code:
class stats<T extends Number>
{
T[] nums;
stats(T[] o)
{
nums=o;
}
double sum()
{
double sum=0.0;
for(int i=0;i<nums.length;i++)
sum+=nums[i].doubleValue();
return sum;
}
}
class Bound_types
{
public static void main(String[] args)
{
Integer inums[]={1,2,3,4,5};
stats<Integer> iob=new stats<Integer>(inums);
double v=iob.sum();
Double dnums[]={1.0,2.0,3.0,4.0,5.0};
stats<Double> dob=new stats<Double>(dnums);
double x=dob.sum();
isequal(v,x);
}
public static void isequal(double x,double y)
{
if(x==y)
System.out.println("sum is same");
else
System.out.println("sum is not same");
}
}
Output:
47
CGPIT/CE/SEM-5/Java Technologies