You are on page 1of 57

PROGRAM NO.

1
WAP to compute the Armstrong Number.

class ArmstrongNumber

public static void main(String...arg)

int num=153,temp,sum=0;

int t=num;

System.out.println("The given number is..."+num);

while(num>0)

temp=num%10;

sum=sum+(temp*temp*temp);

num=num/10;

if(t==sum)

System.out.println("The given no. is Armstrong Number");

else{

System.out.println("The given no. is not Armstrong Number");

Input and Output:

The given number is...153

The given no. is Armstrong Number


PROGRAM NO. 2
WAP to calculate and print first and Fiboacci no.

class FibonacciSeries

public static void main(String...arg)

int a=80;

long f1,f2,f3;

f1=0;

f2=1;

System.out.print(f1+","+f2);

a=80-2;

while(a>0)

f3=f2;

f2=f1+f2;

f1=f3;

System.out.print(","+f2);

a=a-1;

Intput and output

0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,177
11

,28657,46368,75025,121393,196418,317811,514229,832040,1346269,2178309,352
4578,57
02887,9227465,14930352,24157817,39088169,63245986,102334155,165580141,2
67914296,

433494437,701408733,1134903170,1836311903,2971215073,4807526976,777874
2049,12586

269025,20365011074,32951280099,53316291173,86267571272,139583862445,22
5851433717

,365435296162,591286729879,956722026041,1548008755920,2504730781961,40
5273953788

1,6557470319842,10610209857723,17167680177565,27777890035288,449455702
12853,7272

3460248141,117669030460994,190392490709135,308061521170129,4984540118
79264,80651

5533049393,1304969544928657,2111485077978050,3416454622906707,5527939
700884757,8

944394323791464,14472334024676221

PROGRAM NO. 3
WAP to computer Handsome number.

class HandsomeNumber

public static void main(String...arg)

int num=1256,temp,sum=0,t;

System.out.println("The given no. is..."+num);

t=num%10;

num=num/10;

while(num>0)

temp=num%10;
sum=sum+temp;

num=num/10;

if(sum==t)

System.out.println("The given no. is Perfect Number");

else

System.out.println("The given no. is Perfect Number");

Input and output

The given no. is...1256

The given no. is Perfect Number

PROGRAM NO. 4
WAP to compute perfect number.

class PerfectNumber

public static void main(String...arg)

int num=6,sum=0;

System.out.println("the given no. is..."+num);

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

if(num%i==0)

sum=sum+i;

if(num==sum)

System.out.println("the given no. is Perfect Number");

else{

System.out.println("the given no. is Perfect Number");

Input and Output

the given no. is...6

the given no. is Perfect Number

PROGRAM NO. 5
WAP to compute the reverse of digits of given integer.

class ReverseDigits

public static void main(String...arg)

int num=2345;
int rev=0,t;

System.out.println("The given no. is..."+num);

while(num>0)

t=num%10;

rev=rev*10+t;

num=num/10;

System.out.println("The reverse integer is..."+rev);

Input and Ouput

The given no. is...2345

The reverse integer is...5432

PROGRAM NO. 6
WAP to compute sum of digits of the integer no.

class SumOfDigits{

public static void main(String...arg)

int a=234;

int t,sum=0;

System.out.println("The given number is..."+a);

while(a>0)

t=a%10;

sum=sum+t;

a=a/10;
}

System.out.println("Sum of digits is..."+sum);

Input and Output

The given number is...234

Sum of digits is...9

PROGRAM NO. 7
WAP to sum of first last digits of the integer number.

class SumOfFirstLastDigits

public static void main(String...arg)

int a=234;

int sum=0,temp;

System.out.println("The given integer is..."+a);

temp=a%10;

sum=sum+temp;

while(a>0)

temp=a%10;

a=a/10;

sum=sum+temp;

System.out.println("The sum of First and Last Digit of given integer is..."+sum);

}
}

Input and Output

The given integer is...234

The sum of First and Last Digit of given integer is...6

PROGRAM NO. 8
WAP to swap two values without using the 3rd variable.

class Swap{

public static void main(String...arg)

int a,b;

a=4;

b=5;

System.out.println("Values before swaping");

System.out.println("a="+a+"\tb="+b);

a=a+b;

b=a-b;

a=a-b;

System.out.println("Values After Swaping");

System.out.println("a="+a+"\tb="+b);

Input and Output

Values before swaping

a=4 b=5

Values After Swaping

a=5 b=4
PROGRAM NO. 9
Calculate the area of circle and cylinder by creating methods name
areaOfCircle and areaOfCylinder in a class named Area using a constant
attribute PI=3.14.

import java.io.*;

class CalArea

final float PI=3.14f;

float r;

float h;

float area;

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

public void areaOfCircle()

area=PI*r*r;

System.out.println("Area Of Circle is..."+area);

public void areaOfCylinder()

area=PI*r*r*h;

System.out.println("Area Of Cylinder is..."+area);

public void setRadius()throws IOException

System.out.println("Enter the radius");

r=Float.parseFloat(br.readLine());

}
public void setHeight()throws IOException

System.out.println("Enter the Height");

h=Float.parseFloat(br.readLine());

public static void main(String[] arg)throws IOException

while(true)

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

CalArea ca=new CalArea();

System.out.println("1.Area Of Circle");

System.out.println("2.Area Of Cylinder");

System.out.println("3.Exit");

int ch=Integer.parseInt(br.readLine());

switch(ch)

case 1:ca.setRadius();

ca.setHeight();

ca.areaOfCircle();

break;

case 2:ca.setRadius();

ca.setHeight();

ca.areaOfCylinder();

break;

case 3:System.exit(0);
default:System.out.println("Please Choose Wright operation");

Input and Output

1.Area Of Circle

2.Area Of Cylinder

3.Exit

Enter the radius

12

Enter the Height

11

Area Of Circle is...452.16

1.Area Of Circle

2.Area Of Cylinder

3.Exit

PROGRAM NO. 10
Write a program that calculate and print the roots of a quadratic equation

ax^2+bx+c = 0 and appropriate message should be printed if root are complex.

class QuadEqu

public static void main(String[] arg)

int a=Integer.parseInt(System.console().readLine("Enter the value of a:"));


int b=Integer.parseInt(System.console().readLine("Enter the value of b:"));

int c=Integer.parseInt(System.console().readLine("Enter the value of c:"));

int d=(b*b)-(4*a*c);

if(d<0)

System.out.println("Root are Complex");

if(d==0)

int r=(-b)/(2*a);

System.out.println("Root is..."+r);

if(d>0)

float r1=(-b+(float)Math.sqrt(d))/(2*a);

float r2=(-b-(float)Math.sqrt(d))/(2*a);

System.out.println("First Root is..."+r1);

System.out.println("Second Root is..."+r2);

Input and Output

Enter the value of a:11

Enter the value of b:13

Enter the value of c:15

Root are Complex


PROGRAM NO. 11
Define a class Worker with the following specifications:

Data Member : workerNumber, nameOfWorker, wegeRatePerHour, totalWage,


hoursWorkedByAWorker

Methods :>To assign initial value for worker number, name of worker, hours
worked by worker, wage rate

per hour. [ public Worker(......) ]

> To calculate total wage to be paid to the worker. [ public void


wageToBePaid()

> To display all the information - [ public void displayWorkerDetai( ) ]

Write a program in Java to test the program.

import java.io.*;

class WorkerSpecification

int workerNo;

String name;

int wagePerHour;

int workedHours;

long totalWage;

InputStreamReader ibr=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(ibr);

WorkerSpecification()throws IOException

System.out.println("Enter the WorkerNo.:");

workerNo=Integer.parseInt(br.readLine());

System.out.println("Enter the Worker Name:");

name=br.readLine();
System.out.println("Enter the Worker wage per hour:");

wagePerHour=Integer.parseInt(br.readLine());

System.out.println("Enter the total working hours of Worker:");

workedHours=Integer.parseInt(br.readLine());

public void calTotalWage()

totalWage=wagePerHour*workedHours;

public void displayInfo()

System.out.println("WORKER NO:\t\t"+workerNo);

System.out.println("WORKER NAME:\t\t"+name);

System.out.println("PER HOUR WAGE\t\t"+wagePerHour);

System.out.println("TOTAL WORKING HOURS:\t"+workedHours);

System.out.println("TOTAL WAGE TO BE PAID:\t"+totalWage);

public static void main(String[] arg)throws IOException

InputStreamReader ibr=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(ibr);

System.out.println("Enter The Total Number Of Workers:");

int num=Integer.parseInt(br.readLine());

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

WorkerSpecification ws=new WorkerSpecification();

ws.calTotalWage();
System.out.println("*********WORKER NO."+(i+1)+" SPECIFICATION***********");

ws.displayInfo();

Input and Output

Enter The Total Number Of Workers:

Enter the WorkerNo.:

121

Enter the Worker Name:

ram

Enter the Worker wage per hour:

500

Enter the total working hours of Worker:

12

*********WORKER NO.1 SPECIFICATION***********

WORKER NO: 121

WORKER NAME: ram

PER HOUR WAGE 500

TOTAL WORKING HOURS: 12

TOTAL WAGE TO BE PAID: 6000

Enter the WorkerNo.:

122

Enter the Worker Name:

suraj

Enter the Worker wage per hour:


600

Enter the total working hours of Worker:

12

*********WORKER NO.2 SPECIFICATION***********

WORKER NO: 122

WORKER NAME: suraj

PER HOUR WAGE 600

TOTAL WORKING HOURS: 12

TOTAL WAGE TO BE PAID: 7200

PROGRAM NO. 12
Define a class named Test with an instance variable num. Define a
Constructor and a

method named getReverse(). Create an object of the class pass an integer to


the constructor to initialize num.

import java.io.*;

class Test

int num;

Test(int n)

num=n;

public int getReverse()

int temp,rev=0;

while(num>0)

{
temp=num%10;

num=num/10;

rev=rev*10+temp;

return rev;

public static void main(String[] arg)throws IOException

BufferedReader br= new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the Integer number:");

int num=Integer.parseInt(br.readLine());

Test t=new Test(num);

int rev=t.getReverse();

System.out.println("Reverse Integer is..."+rev);

Input and Ouput

Enter the Integer number:

12

Reverse Integer is...21

PROGRAM NO. 13
Write an application that reads a string and determines whether it is a
palindrome.

import java.io.*;

class Palindrome

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

String str;

int len;

int inc=0,temp;

InputStreamReader ibr=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(ibr);

System.out.println("Enter the String to Check palindrome condition:") ;

str=br.readLine();

len=str.length();

temp=len/2;

for(int i=0;i<len-i-1;i++)

if(str.charAt(i)==str.charAt(len-i-1))

inc++;

if(inc==temp)

System.out.println("The given String is palindrome");

else

System.out.println("The given String is not palindrome");

}
}

Input and Output

Enter the String to Check palindrome condition:

madam

The given String is palindrome

PROGRAM NO. 14
Write a program to enter a sentence form keyboard and also find all the words
in that sentence with starting character as vowel.

import java.io.*;

class Vowel

public static void main(String[] arg)throws IOException

String sentStr;

String[] wordStr=new String[20];

String vowel="AEIOUaeiou";

InputStreamReader ibr=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(ibr);

System.out.println("Enter the Sentence") ;

sentStr=br.readLine();

wordStr=sentStr.split(" ");

System.out.println("All the words starting with character as Vowel");

for(int i=0;i<wordStr.length;i++)

if(vowel.indexOf(wordStr[i].charAt(0))>=0)

System.out.println(wordStr[i]);
}

Input and Output

Enter the Sentence

ram is a good boy

All the words starting with character as Vowel

is

PROGRAM NO. 15
write a program to print the string 'ALLAHABAD' in following format

AL

ALL

ALLA

ALLAH

ALLAHA

ALLAHAB

ALLAHABA

ALLAHABAD

import java.io.*;

class Pattern

public static void main(String[] arg)

{
String str="ALLAHABAD";

for(int i=0;i<str.length();i++)

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

System.out.print(str.charAt(j)) ;

System.out.println("");

Input and Output

AL

ALL

ALLA

ALLAH

ALLAHA

ALLAHAB

ALLAHABA

ALLAHABAD

PROGRAM NO. 16
Create a class SimpleCalculator that has functionality of addition,
substraction division, multiplication, square and squareroot and then create
another class ScientificCalculator that has functionality of SimpleCalculator
and some other functionality like sin, cos, tan.
import java.io.*;

public class Calc

public static void main(String[] arg)throws IOException

Runtime rt=Runtime.getRuntime();

rt.exec("cmd.exe /C start D:\\ab.bat");

while(true)

int optch=Integer.parseInt(System.console().readLine("0.Exit\n1.Simple
Calculator\n2.Scientific Calculator\nChoose any type of Calculator:"));

switch(optch)

case 0:System.exit(0);

case 1:while(true)

int
ch1=Integer.parseInt(System.console().readLine("0.Exit\n1.Addition\n2.Subtraction\n
3.Multiplication\n4.Division\n5.Square\n6.Square Root\n7.Main Menu\nChoose any
one operation:"));

SimpleCalc simpc=new SimpleCalc();

if(ch1==7)

break;

else

simpc.simpleFunc(ch1);

break;
case 2:while(true)

int
ch2=Integer.parseInt(System.console().readLine("0.Exit\n1.Addition\n2.Subtraction\n
3.Multiplication\n4.Division\n5.Square\n6.Square
Root\n7.Sin\n8.Cos\n9.tan\n10.Main Menu\nChoose any one operation:"));

if(ch2==10)

break;

else

if(ch2<=6)

new SimpleCalc().simpleFunc(ch2);

else

new SciCalc().sciFunc(ch2);

break;

default:System.out.println("Please Choose correct operation\n");

class SimpleCalc

double n1;

double n2;

public double add()

{
return(n1+n2);

public double sub()

return(n1-n2);

public double mul()

return(n1*n2);

public double div()

return(n1/n2);

public double sqr()

return(n1*n1);

public double sqrt()

return(Math.sqrt(n1));

public void setdblVal()

n1=Double.parseDouble(System.console().readLine("Enter the first number:"));

n2=Double.parseDouble(System.console().readLine("Enter the second number:"));

}
public void setVal()

n1=Double.parseDouble(System.console().readLine("Enter the number:"));

public void simpleFunc(int ch1)

double temp;

switch(ch1)

case 0:System.exit(0);

case 1:setdblVal();

temp=add();

System.out.println("Added of two Numbers is..."+temp);

break;

case 2:setdblVal();

temp=sub();

System.out.println("Subtracted of two Numbers is..."+temp);

break;

case 3:setdblVal();

temp=mul();

System.out.println("Multiplied of two Numbers is..."+temp);

break;

case 4:setdblVal();

temp=div();

System.out.println("Divided of two Numbers is..."+temp);

break;

case 5:setVal();
temp=sqr();

System.out.println("Square of a Number is..."+temp);

break;

case 6:setVal();

temp=sqrt();

System.out.println("Square Root of Number is..."+temp);

break;

default:System.out.println("Please Choose correct operation\n");

class SciCalc extends SimpleCalc

public double sinFunc()

return(Math.sin(Math.toRadians(n1)));

public double cosFunc()

return(Math.cos(Math.toRadians(n1)));

public double tanFunc()

return(Math.tan(Math.toRadians(n1)));

public void sciFunc(int ch1)

{
switch(ch1)

case 7:setVal();

System.out.println("Sin of Number is..."+(sinFunc()));

break;

case 8:setVal();

System.out.println("Cos of Number is..."+(cosFunc()));

break;

case 9:setVal();

System.out.println("Tan of Number is..."+(tanFunc()));

break;

default:System.out.println("Please Choose correct operation\n");

Input and Output

0.Exit

1.Simple Calculator

2.Scientific Calculator

Choose any type of Calculator:1

0.Exit

1.Addition

2.Subtraction

3.Multiplication

4.Division

5.Square

6.Square Root
7.Main Menu

Choose any one operation:1

Enter the first number:12

Enter the second number:13

Added of two Numbers is...25.0

PROGRAM NO. 17
[Overloading]Create a java program that has three version of add method
which can add two, three, and four integers.

class OverloadAdd

void add(int a,int b)

System.out.println("Sum of two integers "+(a+b));

void add(int a,int b,int c)

System.out.println("Sum of three integers "+(a+b+c));

void add(int a,int b,int c,int d)

System.out.println("Sum of three integers "+(a+b+c+d));

public static void main(String [] arg)

System.out.println("Integer numbers are 12,13,14,15");

new OverloadAdd().add(12,13);
new OverloadAdd().add(12,13,14);

new OverloadAdd().add(12,13,14,15);

Input and output

Integers numbers are 12,13,14,15

Sum of two integers 25

Sum of three integers 39

Sum of three integers 54

PROGRAM NO. 18
[Overloading] Write a Java prgram that uses an overloaded method volume()
that returns volume of different structures.

The first version takes one float side of a cube and returns its value.

The second version takes float radius and float height and returns the volume
of a cylinder.

The third version takes float length, float width and float height of a
rectangular box and returns a volume.

class OverloadVolume

float volume(float a)

return(a*a*a);

float volume(float r,float h)

return(3.14f*r*r*h);

}
float volume(float l,float w,float h)

return(l*w*h);

public static void main(String [] arg)

OverloadVolume olv=new OverloadVolume();

System.out.println("Volume of Cube is..."+olv.volume(11.5f));

System.out.println("Volume of Cylinder is..."+olv.volume(11.5f,12.0f));

System.out.println("Volume of Rectangle is..."+olv.volume(11.5f,12.0f,15.0f));

Input and Output

Volume of Cube is...1520.875

Volume of Cylinder is...4983.18

Volume of Rectangle is...2070.0

PROGRAM NO. 19
Define a base class called animal with following members:

a. string type data member to store the name of the animal

b . integer member to store the age of animal in years

c. method to display the age and name of the animal

Derive to classes named Cat and Dog from animal class then write a driver
program to create Cat and Dog objects with

suitable values. Display the contents of objects by calling the display method
on the derived objects.

class Animal
{

String name;

int age;

void display()

System.out.println("Name="+name+" Age="+age);

class Cat extends Animal

Cat(String n,int a)

name=n;

age=a;

void talk()

System.out.println("Mewwo........");

class Dog extends Animal

Dog(String n,int a)

name=n;

age=a;

}
void talk()

System.out.println("Bowww........");

class Driver

public static void main(String[] arg)

Cat c=new Cat("tomy",11);

c.talk();

c.display();

Input and output

Mewwo........

Name=tomy Age=11

PROGRAM NO. 20
Create an abstract base class titled Shapes. It should contain a method Area
() which returns area of a particular shape. Derive two classes from Shapes
titled Rectangle and triangle. Implement the method Area () in both classes to
print the area.

class TestShape

public static void main(String [] arg)

Triangle t=new Triangle(4.0f,5.0f);


t.area();

t.display();

Rectangle r=new Rectangle(4.0f,5.0f);

r.area();

r.display();

abstract class Shape

String name;

float areaofshape;

abstract void area();

void display()

System.out.println("Area of "+name+" is..."+areaofshape);

class Triangle extends Shape

float base;

float height;

Triangle(float b,float h)

base=b;

height=h;

name="Triangle";

}
void area()

areaofshape=(0.5f)*base*height;

class Rectangle extends Shape

float length;

float breath;

Rectangle(float l,float b)

length=l;

breath=b;

name="Rectangle";

void area()

areaofshape=length*breath;

Input and Output

Area of Triangle is...10.0

Area of Rectangle is...20.0

PROGRAM NO. 21
Create an abstract class (Bank) which has both types of methods: abstract
and non-abstract. The names of the methods will be interest and display
respectively. Now create four more classes called ICICI, SBI, PNB, and
ABNAMRO. These classes should define the rate of interest for abstract class
and also implement the display method.

abstract class Bank

String name;

float rateofint;

abstract void calculateInt();

void display()

System.out.println("Rate of interest of "+name+" is "+rateofint);

class Sbi extends Bank

float pi;

float rate;

float time;

Sbi(float p,float r,float t)

pi=p;

rate=r;

time=t;

name="SBI";

void calculateInt()

rateofint=(pi*rate*time)/100;
}

class Pnb extends Bank

float pi;

float rate;

float time;

Pnb(float p,float r,float t)

pi=p;

rate=r;

time=t;

name="PNB";

void calculateInt()

rateofint=(pi*rate*time)/100;

class TestBank

public static void main(String[] arg)

Sbi s=new Sbi(100,50,60);

s.calculateInt();

s.display();

Pnb p=new Pnb(150,40,70);


p.calculateInt();

p.display();

Input and Output

Rate of interest of SBI is 3000.0

Rate of interest of PNB is 4200.0

PROGRAM NO. 22
[Package and Array] Create a package named Mathematics and add following
classes to it:

i) A class Matrix with methods to add and multiplt matrices.

ii)A class Complex with methods to add, multiply and subtract complex
numbers.

Write a Java program importing the Mathematics package and use the classes
defined in it.

Test.java:

import mathematics.Matrix;

import mathematics.Complex;

import java.io.*;

public class Test

public static void disp(String opt,int result[][])

System.out.println(opt+" Matrix is:");

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

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

System.out.print(result[i][j]+"\t");

System.out.println();

public static void main(String[] arg)

Console c=System.console();

int ch1=Integer.parseInt(c.readLine("0.Exit\n1.Complex\n2.3x3 Matrix\nChoose the


given operation:"));

switch(ch1)

case 0:System.exit(0);

case 1:

int r1,r2,i1,i2;

Complex com=new Complex();

System.out.println("Enter first complex number");

r1=Integer.parseInt(c.readLine("real:"));

i1=Integer.parseInt(c.readLine("complex:"));

System.out.println("Enter Second complex number");

r2=Integer.parseInt(c.readLine("real:"));

i2=Integer.parseInt(c.readLine("complex:"));

while(true)

int ch=Integer.parseInt(c.readLine("0.Exit\n1.Add\n2.Subtract\n3.Multiply\nChoose
the given operation:"));
switch(ch)

case 0:System.exit(0);

case 1:com.add(r1,i1,r2,i2);

break;

case 2:com.subtract(r1,i1,r2,i2);

break;

case 3:com.multiply(r1,i1,r2,i2);

break;

default:System.out.println("Please Choose the given operation\n");

//break;

case 2:

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

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

int [][]result;

Test t=new Test();

Matrix m=new Matrix();

System.out.println("Enter the Matrix 1 Elements");

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

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

a[i][j]=Integer.parseInt(c.readLine());

}
System.out.println("Enter the Matrix 2 Elements");

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

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

b[i][j]=Integer.parseInt(c.readLine());

System.out.println("Given Matrix 1 is:");

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

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

System.out.print(a[i][j]+"\t");

System.out.println();

System.out.println("Given Matrix 2 is:");

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

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

System.out.print(b[i][j]+"\t");

System.out.println();

while(true)
{

int ch2=Integer.parseInt(c.readLine("0.Exit\n1.Add\n2.Multiply\nChoose the given


operation:"));

switch(ch2)

case 0:System.exit(0);

case 1:result=m.add(a,b);

disp("Added",result);

break;

case 2:result=m.multiply(a,b);

disp("Multiplied",result);

break;

default:System.out.println("Please Choose the given operation\n");

//break;

default:System.out.println("Please Choose the given operation\n");

Matrix.java

package mathematics;

public class Matrix

int [][] temp=new int[3][3];

public int[][] add(int a[][],int b[][])

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

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

temp[i][j]=a[i][j]+b[i][j];

return temp;

public int[][] multiply(int a[][],int b[][])

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

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

int sum=0;

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

sum=sum+a[i][k]*b[k][j];

temp[i][j]=sum;

//temp[i][j]=a[i][j]+a[i][k]*b[k][j];

return temp;

}
Complex.java

package mathematics;

public class Complex

public void add(int r1,int i1,int r2,int i2)

System.out.println("ADD: "+(r1+r2)+"+"+(i1+i2)+"i");

public void subtract(int r1,int i1,int r2,int i2)

System.out.println("SUBTRACT: "+(r1-r2)+"+"+(i1-i2)+"i");

public void multiply(int r1,int i1,int r2,int i2)

int t1=r1*r2-i1*i2;

int t2=r1*i2+i1*r1;

System.out.println("MULTIPLY : "+t1+"+"+t2+"i");

Input and Output

0.Exit

1.Complex

2.3x3 Matrix

Choose the given operation:1

Enter first complex number

real:12

complex:13
Enter Second complex number

real:45

complex:23

PROGRAM NO. 23
[[Exception Handling] Define an exception called "NotEqualException" that is
thrown when a float value is not equal to 3.14f. Write a Java program that uses
the above user-defined exception.

class NotEqualException extends Exception

public String toString(){

return "Entered is notequal to 3.14";

class UserDefExcep

public static void main(String...arg)

float num=Float.parseFloat(System.console().readLine("Enter the pie value:"));

try{

if(num!=3.14f)

throw new NotEqualException();

else

System.out.println("Entered value is correct");

catch(Exception e)

System.out.println(e);
}

Input and Output

Enter the pie value:12

Entered is notequal to 3.14

PROGRAM NO. 24
[Multi-Threading] Write a Java program to

i) Create a thread which can print multiplication table of any integer.

ii) Print the name, priority, and group of the thread.

iii) Change the name of the current thread to "JAVA"

iv) Display the details of current thread.

class Table Thread

public void run()

System.out.println("2's Table via 2nd thread");

Thread s= Thread.curretThread();

System.out.println("Executin:"+s);

s.setName("java");

System.out.println("After name change:"+s);

s= Thread.curretThread();

System.out.println("Current status"+s);

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

{
System.out.println("2*"+i+"="+(2*i));

try{

Thread.sleep(1000);

}catch(Exception e)

System.out.println("Error");

class TableDemo{

public static void main(String ar[])

table t=new table();

t.start();

PROGRAM NO. 25
[Multi-Threading] Write a java program to show digital clock on console using
Thread.

import java.util.Date;

class DigClock

public static void main(String [] ar)throws Exception

ClearScreen sc = new ClearScreen();


sc.start();

while(true)

Date d = new Date();

System.out.printf("%tr",d);

Thread.sleep(1000);

class ClearScreen extends Thread

public void run()

while(true)

System.out.print("\r");

try{

Thread.sleep(1000);

}catch(Exception e){}

Input and Ouput

06:54:20 PM

PROGRAM NO. 26
[Applet] Write a Java applet to
Display the name and address of your college. Use setBackgroud(), setFont()
and setForeground() method to set color and font of text, and color of
background.

import java.awt.*;

import java.applet.*;

/*

<applet code=CollegeApplet width=400 height=200 >

<param name=color value=GREEN>

</applet>

*/

public class CollegeApplet extends Applet

public void init()

setBackground(Color.GREEN);

setForeground(Color.RED);

setFont(new Font("Times New Roman",Font.BOLD,20));

public void paint(Graphics g)

String collegeName = "United Group Of Institutions.";

String collegeAddress = "Naini Industrial Area, Naini, Allahabad";

g.drawString(collegeName,10,20);

g.drawString(collegeAddress,10,40);

}
}

Input and Output

PROGRAM NO. 27
[Applet] Write a Java applet to display the text COLOURFUL APPLET in
different colors, If the param tag consist

of name="Mycolor" and value="Cyan". The color of text must be cyan else


color of text must be yellow.

import java.awt.*;

import java.applet.*;

/*

<applet code=ParamApplet width=400 height=200 >

<param name=mycolor value=CYAN >

</applet>

*/

public class ParamApplet extends Applet

public void init()

{
setFont(new Font("Times New Roman",Font.BOLD,20));

public void paint(Graphics g)

String mycolor = getParameter("mycolor");

if(mycolor.equals("CYAN"))

setForeground(Color.CYAN);

else

setForeground(Color.YELLOW);

String message = "Hello UCER";

g.drawString(message,10,40);

Input and Output


PROGRAM NO. 28
[Applet] Write a Java applet to draw a traffic signal light.

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

/*

<applet code=TrafficApplet width=100 height=300 >

</applet>

*/

public class TrafficApplet extends Applet

public void paint(Graphics g)

g.fillRect(10,10,95,250);

g.setColor(Color.RED);
g.fillOval(20,20,70,70);

g.setColor(Color.GREEN);

g.fillOval(20,100,70,70);

g.setColor(Color.YELLOW);

g.fillOval(20,180,70,70);

g.setColor(Color.BLACK);

g.fillRect(50,250,18,50);

Input and Output

PROGRAM NO. 29
[Applet] Write a Java program to create a calculator with (add, sub, div, mul,
sqrt) functionality using applet and
test the program using <applet> HTML tags.

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

/* <applet code=Calculator

width=450

height=450>

</applet> */

public class Calculator extends Applet implements ActionListener

TextField tf1,tf2,tf3;

Button b,b1,b2,b3,b4;

Label l1,l2,l3;

public void init()

{ l1=new Label("First no.: " );

l2=new Label("Second no.:");

l3=new Label("Result ");

tf1=new TextField(10);

tf2=new TextField(10);

tf3=new TextField(10);

b=new Button("+");

b1=new Button("-");

b2=new Button("*");

b3=new Button("/");

b4 = new Button("sqrt");
b.addActionListener(this);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);

setLayout(new FlowLayout());

add(l1);

add(tf1);

add(l2);

add(tf2);

add(l3);

add(tf3);

add(b);

add(b1);

add(b2);

add(b3);

add(b4);

setSize(200,200);

public void actionPerformed(ActionEvent ae)

int n1=Integer.parseInt(tf1.getText());

int n2=Integer.parseInt(tf2.getText());

if(ae.getSource()==b)
{

int n3=n1+n2;

tf3.setText(String.valueOf(n3));

if(ae.getSource()==b1)

int n3=n1-n2;

tf3.setText(String.valueOf(n3));

if(ae.getSource()==b2)

int n3=n1*n2;

tf3.setText(String.valueOf(n3));

if(ae.getSource()==b3)

int n3=n1/n2;

tf3.setText(String.valueOf(n3));

if(ae.getSource()==b4)

double sr = Math.sqrt(n1);

tf3.setText(String.valueOf(sr));

}
}

Input and Output

PROGRAM NO. 30
[JDBC] Ceate a database named College using MS Access with table student
having following fields:

- RollNumber (PK), Branch, Name

Initialize this table with different values. Write a program in Java to print the
details in student table.

import java .sql.*;

class JDBCDemo{

public static void main(String[] arg)throws Exception{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con=DriverManager.getConnection("jdbc.odbc.united"," "," ");

Statement st=con.createStatement();

ResultSet rs=st.executeQuery("Select*from Student");

System.out.printf("+--------+--------------+--------+ \n");

System.out.printf("|%-8s|%-14s|%-8s|","Roll No.","Student Name","Branch");


System.out.printf("|n+--------+--------------+--------+ ");

while(rs.next()){

System.out.println();

System.out.printf("|%-8s|",rs.getString(1));

System.out.printf("|%-14s|",rs.getString(2));

System.out.printf("|%-8s|",rs.getString(3));

System.out.printf("|n+--------+--------------+--------+ \n");

con.close();

Input and Output

You might also like