Java Programs
Java Programs
Regd.No.
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.
Expt No 1:
Write a java program to calculate the Armstrong numbers between 1 to 1000.
class Armstrong
{
public static void main(String args[])
{
int n=150,temp,r,s=0,t=0;
while(n<=1000)
{
temp=n;
t=n;
while(t>0)
{
r=t%10;
s=s+(r*r*r);
t=t/10;
}
if(temp==s)
{
System.out.println(" "+s);
}
n=n+1;
s=0;
t=0;
}
}
}
Page | 1
Output:
Page | 2
Expt No 2:
Write a java program to print the student details bu using Command line Arguments
import java.io.*;
class Student
{
public static void main(String args[]) throws IOException
{
System.out.println("Name: "+args[0]);
System.out.println("ID: "+args[1]);
System.out.println("Dept: "+args[2]);
System.out.println("Subjects: "+args[3]);
}
}
Output:
Page | 3
Expt No 3:
Illustrate the method overriding in java.
class Super
{
int x;
Super(int x)
{
this.x=x;
}
void display()
{
System.out.println("Super x= "+x);
}
}
class Sub extends Super
{
int y;
Sub(int x,int y)
{
super(x);
this.y=y;
}
void display()
{
System.out.println("Super x= "+x);
System.out.println("Sub y= "+y);
}
}
class OverrideTest
{
public static void main(String args[])
{
Sub s1=new Sub(100,200);
s1.display();
}
}
Page | 4
Output:
Page | 5
Expt No 4
Write a java program to print the multiplication of two matrices.
import java.io.*;
class MulMatrix
{
public static void main(String args[]) throws IOException
{
int m,n,p,q,i,j,k;
DataInputStream in=new DataInputStream(System.in);
System.out.println("enter value for m,n for first matrix");
m=Integer.parseInt(in.readLine());
n=Integer.parseInt(in.readLine());
int a[][]=new int[m][n];
System.out.println("enter values for p,q for second matrix");
p=Integer.parseInt(in.readLine());
q=Integer.parseInt(in.readLine());
int b[][]=new int[p][q];
if(n==p)
{
System.out.println("enter the elements for matrix A");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=Integer.parseInt(in.readLine());
}
}
Page | 6
}
}
System.out.println("the multiplication of matrices A & B is");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
System.out.print("\t"+c[i][j]);
}
System.out.println(" ");
}
}
else
{
System.out.println("multiplication is not possible");
}
}
}
Page | 7
Output:
Page | 8
Expt No 5:
Write a java program to convert the strings to uppercase.
class StringManipulation
{
static String s1="how are you";
static String s2="welcome";
static String s3;
public static void main(String args[])
{
s3=s1.toUpperCase();
System.out.println("The String in upperCase is "+s3);
s3=s2.toUpperCase();
System.out.println("The string in uppercase is "+s3);
}
}
Output:
Page | 9
Expt No 6
Write a java program to Sort the given strings to alphabetical order
class StringOrdering
{
static String
name[]={"Madras","Delhi","Ahmedhbad","Calcutta","Bombay"};
public static void main(String args[])
{
int size=name.length;
String temp=null;
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;j++)
{
if (name[j].compareTo(name[i])<0)
{
//swap the strings
temp=name[i];
name[i]=name[j];
name[j]=temp;
}
}
}
for(int i=0;i<size;i++)
{
System.out.println(name[i]);
}
}
}
Page | 10
Output:
Page | 11
Expt No 7
Write a program to identify a duplicate value in a vector and removes them.
import java.util.*;
class DupValue
{
public static void main(String args[])
{
Vector v=new Vector();
v.addElement("Delhi");
v.addElement("Mumbai");
v.addElement("Calcutta");
v.addElement("Andhra");
v.addElement("Mumbai");
Vector tmpVector=new Vector();
String tmpValue;
for(int j=0;j<v.size();j++)
{
tmpValue=(String)v.elementAt(j);
if(tmpValue!=null)
{
if(tmpVector.isEmpty())
tmpVector.addElement(tmpValue);
if(tmpVector.indexOf(tmpValue)==-1)
{
tmpVector.addElement(tmpValue);
}
}
}
for(int j=0;j<tmpVector.size(); j++)
System.out.println(tmpVector.elementAt(j));
}
}
Page | 12
Output:
Page | 13
Expt No 8
Write a java program to implement the interfeces.
import java.io.*;
interface Area
{
final static float pi=3.14f;
float compute(float x,float y);
}
class Rectangle implements Area
{
public float compute(float x,float y)
{
return(x*y);
}
}
class Circle implements Area
{
public float compute(float x,float y)
{
return (pi*x*y);
}
}
class InterfaceTest
{
public static void main(String args[])
{
Rectangle r =new Rectangle();
Circle c=new Circle();
Area a;
a=r;
System.out.println("the area of rectangle is="+a.compute(10,20));
a=c;
System.out.println("the area of circle is="+a.compute(10,1));
}
}
Page | 14
Output:
Page | 15
Expt No 9:
Create a package called “Arithmetic” that contains methods to deal with all arithmetic
operations.
Also, write a program to use the package.
package arithmetic;
public class Operation
{
int a=0,b=0,c=0;
float d;
public void add(int x,int y)
{
a=x;
b=y;
c=a+b;
System.out.println("sum= "+c);
}
public void sub(int x,int y)
{
a=x;
b=y;
c=a-b;
System.out.println("subtraction= "+c);
}
public void mul(int x,int y)
{
a=x;
b=y;
c=a*b;
System.out.println("multiplication= "+c);
}
public void div(float h,float k)
{
h=0.0f;
k=0.0f;
d=h/k;
System.out.println("division= "+d);
}
public void mod(int x,int y)
{
a=x;
b=y;
c=a%b;
Page | 16
System.out.println("modulation= "+c);
}
}
import arithmetic.*;
class Arithmetic
{
public static void main(String args[])
{
Operation m=new Operation();
m.add(10,20);
m.sub(20,15);
m.mul(10,20);
m.div(25.5,5.5);
m.mod(30,15);
}
}
Page | 17
Output:
Page | 18
Expt No 10:
Create two threads such that one of the thread print even numbers and another thread
prints odd numbers up to a given range.
import java.lang.Thread;
class Odd extends Thread
{
public void run()
{
for(int i=1;i<=10;i=i+2)
{
System.out.println("i="+i);
}
System.out.println("exit from A");
}
}
class Even extends Thread
{
public void run()
{
for(int j=2;j<=10;j=j+2)
{
System.out.println("j="+j);
}
System.out.println("exit from B");
}
}
class EvenOddThr
{
public static void main(String args[])
{
Odd a=new Odd();
Even b=new Even();
System.out.println("Start thread Odd");
a.start();
System.out.println("Start thread Even");
b.start();
}
}
Page | 19
Output:
Page | 20
Expt No 11:
Define an exception called “MarksOutOfBound” Exception, that is thrown if the
entered marks are greater than 100.
class MarksOutOfBound
{
public static void main(String args[])
{
int invalid=0;
int number,count=0;
for(int i=0;i<args.length;i++)
{
try
{
number=Integer.parseInt(args[i]);
if(number>100)
{
throw new NumberFormatException( );
}
}
catch(NumberFormatException e)
{
invalid=invalid+1;
System.out.println("Invalid number"+args[i]);
continue;
}
count=count+1;
}
System.out.println("Valid numbers="+count);
System.out.println("Invallid numbers="+invalid);
}
}
Page | 21
Output:
Page | 22
Expt No 12
Write an applet program to insert the text at the specified position.
HelloApplet.java
import java.awt.*;
import java.applet.*;
public class HelloApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello Java", 50,100);
}
}
HelloApplet.html
<html>
<head>
<center> welcome to applet</center></head>
<body>
<applet code=HelloApplet.class width=300 height=200>
</applet>
</body>
</html>
Page | 23
Output:
Page | 24
Expt No 13:
Write an application in java to store data in a file.
import java.io.FileOutputStream;
public class FileOutputStreamExample
{
public static void main(String args[])
{
try{
FileOutputStream fout=new FileOutputStream("D:\testout.txt");
String s=" A stream is a sequence of data";
byte b[]=s.getBytes();//converting string into byte array
fout.write(b);
fout.close();
System.out.println("success...");
}
catch(Exception e){System.out.println(e);}
}
}
Output
Success...
The content of a text file testout.txt is set with the data “A stream is a sequence of data”.
D:\testout.txt
A stream is a sequence of data
Page | 25
Expt No 14:
Write an application in java to read data in a file.
import java.io.FileInputStream;
public class DataStreamExample
{
public static void main(String args[])
{
try
{
FileInputStream fin=new FileInputStream("D:\\testout.txt");
int i=0;
while((i=fin.read())!=-1){
System.out.print((char)i);
}
fin.close();
}
catch(Exception e){System.out.println(e);}
}
}
Output:
Page | 26
Expt No 15
Write a java program to determine the sum of the following harmonic series for a
given value of ‘n’. 1+1/2+1/3+…………….+1/n
import java.io.*;
class HarmonicSeries
{
public static void main(String args[]) throws IOException
{
DataInputStream I n=new DataInputStream(System.in);
System.out.println("enter the value for number");
int num = Integer.parseInt(in.readLine());
double result = 0.0;
while(num > 0)
{
result = result + (double) 1 / num;
num--;
}
System.out.println("the sum of Harmonic Series is "+result);
}
}
Page | 27
Output:
Page | 28
Expt No 16
write a java program to generate a Fibonacci series for given value of ‘n’
import java.io.*;
class Fibbonacci
{
public static void main(String args[]) throws IOException
{
DataInputStream in=new DataInputStream(System.in);
int n,c;
System.out.print("The value of n= ");
n=Integer.parseInt(in.readLine());
int a=0,b=1,i=1;
System.out.print("\t"+a);
System.out.print("\t"+b);
do
{
c=a+b;
System.out.print("\t"+c);
a=b;
b=c;
} while (i++<n-2);
}
}
Page | 29
Output:
Page | 30
Expt No 17
Write a java program to find the factorial of a given value of ‘n’
import java.io.*;
class Factorial
{
public static void main(String args[]) throws IOException
{
int n,fact=1,num;
DataInputStream in=new DataInputStream(System.in);
System.out.print("enter the value of n ");
n=Integer.parseInt(in.readLine());
for(num=n;num>=1;num--)
{
fact*=num;
}
System.out.println("The Factorial of a given value is "+fact);
}
}
Page | 31
Output:
Page | 32
Expt No 18:
Write a java program to generate the prime numbers between 1 to 100.
class Prime
{
public static void main(String args[])
{
int r,i=1,c=0,n=1;
while(n<=100)
{
while(i<=n)
{
r=n%i;
if (r==0)
{
c=c+1;
}
i=i+1;
}
if(c==2)
{
System.out.print(" "+n);
}
n=n+1;
c=0;
i=1;
}}
}
Page | 33
Output:
Page | 34
Expt No 19:
Write a java program to print the arithmetic operations on any two integers by using
‘switch…case’ statement.
import java.io.*;
class SwitchPrg
{
public static void main(String args[]) throws IOException
{
int x;
int a,b,c;
DataInputStream in=new DataInputStream(System.in);
System.out.println("enter the value for a");
a=Integer.parseInt(in.readLine());
System.out.println("enter the value for b");
b=Integer.parseInt(in.readLine());
System.out.println("1. addition");
System.out.println("2. subtraction");
System.out.println("3. multiplication");
System.out.println("4. division");
System.out.println("enter your choice between 1-4");
x=Integer.parseInt(in.readLine());
switch(x)
{
case 1: c=a+b;
System.out.println("the addition is "+c);
break;
case 2: c=a-b;
System.out.println("the subtraction is "+c);
break;
case 3: c=a*b;
System.out.println("the multiplication is "+c);
break;
case 4: c=a/b;
System.out.println("the division is "+c);
break;
default: System.out.println("wrong input try again");
break;
}
}
}
Page | 35
Output:
Page | 36
Expt No 20
Write a program to fill elements into a list. Also copy them in reverse order into another
list.
import java.util.ArrayList;
import java.util.Collections;
public class ReverseOrderArrayList
{
public static void main(String[] args)
{
//create an ArrayList object
ArrayList arrayList = new ArrayList();
Page | 37
Output:
Page | 38