You are on page 1of 33

JAVA

PRACTICAL
FILE

Submitted By Submitted To
Harish Bisht Mr. Deepak Kumar
INDEX
S.N0 PROGRAMS OF JAVA SIGNATURE

▪ 1 WAP TO DISPLAY FIBONACCI SERIES TO A GIVEN


TERMS
▪ 2 WAP TO INPUT ALL DATETYPES FROM USER
▪ 3 WAP TO CALCULATE THE FACTORIAL OF INPUT
NUMBER
▪ 4 WAP TO CHECK A NUMBER IS PALINDROME OR
NOT
▪ 5 WAP FOR IMPLEMENTING LINEAR SEARCH

▪ 6 WAP FOR IMPLEMENTING BINARY SEARCH


▪ 7 WAP TO CHECK THE INPUT NUMBER IS
ARMSTRONG OR NOT
▪ 8 WAP TO CALCULATE THE AREA OF CIRCLE.
▪ 9 WAP TO PRINT FLOYED TRIANGLE OF NUMBERS
▪ 10 WAP TO FIND THE LARGEST OF THREE INPUT
NUMBERS
▪ 11 WAP TO CHECK INPUT YEAR IS A LEAP YEAR
▪ 12 WAP TO ADD TWO INPUT MATRIX
▪ 13 WAP TO CHECK INPUT NUMBER IS PRIME OR NOT
▪ 14 WAP TO REVERSE THE INPUT NUMBER
▪ 15 WAP TO EXPLAIN THE KEYWORD STATIC
▪ 16 WAP TO EXPLAIN THE KEYWORD FINAL

▪ 17 WAP TO ILLUSTRATE CONSTRUCTOR


OVERLOADING
▪ 18 WAP TO ILLUSTRATE METHOD OVERLOADING

▪ 19 WAP TO ILLUSTRATE METHOD OVERRIDING


▪ 20 WAP TO ILLUSTRATE INTERFACE
▪ 21 WAP TO ILLUSTRATE ABSTRACT CLASS
▪ 22 WAP TO ILLUSTRATE INHERITANCE
▪ 23 WAP TO ILLUSTRATE SUPER KEYWORD
▪ 24 WAP TO ILLUSTRATE THIS KEYWORD
▪ 25 WAP TO ILLUSTRATE A PACKAGE
▪ 26 WAP TO ILLSTRATE EXCEPTION HANDLING
▪ 27 WAP TO ILLUSTRATE MULTITHREADING
▪ 28 WAP TO ILLUSTRATE APPLET
▪ 29 WAP TO ILLUSTRATE STRING CLASS
Q. WAP TO DISPLAY FIBONACCI SERIES TO A GIVEN TERMS.
import java.util.Scanner;
class Fibo
{
public static void main(String args[])
{
int n1=0,n2=1,n3,i,count;
System.out.println("Enter the number of terms");
Scanner s= new Scanner(System.in);
count= s.nextInt();
System.out.print(n1+" "+n2); //printing 0 and 1
for(i=2;i<count;++i) //loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}

}
Q. WAP TO INPUT ALL DATETYPES FROM USER.

import java.util.Scanner;

class Input
{
int n;
float f;
String o;
public void userInput()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter any integer ");
n=s.nextInt();
System.out.println("Enter any float ");
f=s.nextFloat();
System.out.println("Enter any string ");
o=s.nextLine();
}
public void display()
{
System.out.println("Integer :"+n);
System.out.println("Float :"+f);
System.out.println("String :"+o); }
public static void main(String args[])
{
Input i = new Input();
i.userInput();
i.display();
}
}
Q. WAP TO CALCULATE THE FACTORIAL OF INPUT NUMBER.

import java.util.Scanner;

class Facto
{
public static void main(String args[])
{
int i,fact=1;
int number;
System.out.println("Enter any number");
Scanner s = new Scanner(System.in);
number=s.nextInt(); //to take the input
for(i=1;i<=number;i++)
{
fact=fact*i;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}
Q. WAP TO CHECK A NUMBER IS PALINDROME OR NOT

import java.util.Scanner;

class Palindrome
{
public static void main(String args[])
{
int r,sum=0,temp;
System.out.println("Enter any number");
int n = new Scanner(System.in).nextInt();
temp=n;
while(n>0)
{
r=n%10; //getting remainder
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println("palindrome number ");
else
System.out.println("not palindrome");
}
}
Q. WAP FOR IMPLEMENTING LINEAR SEARCH.
import java.util.Scanner;
class LinearSearch
{
public static void main(String args[])
{
int counter, num, item, array[]; //To capture user input
Scanner input = new Scanner(System.in);
System.out.println("Enter number of elements:");
num = input.nextInt(); //Creating array to store the all the numbers
array = new int[num];
System.out.println("Enter " + num + " integers"); //Loop to store each numbers in array
for (counter = 0; counter < num; counter++)
array[counter] = input.nextInt();

System.out.println("Enter the search value:");


item = input.nextInt();

for (counter = 0; counter < num; counter++)


{
if (array[counter] == item)
{
System.out.println(item+" is present at location "+(counter+1)); //Item is found so to stop the search
// and to come out of the
//loop use break statement.
break;
}
}
if (counter == num)
System.out.println(item + " doesn't exist in array.");
}
}
Q. WAP FOR IMPLEMENTING BINARY SEARCH.
import java.util.Scanner;
class BinarySearch
{
public static void main(String args[])
{
int counter, num, item, array[], first, last, middle; //To capture user input
Scanner input = new Scanner(System.in);
System.out.println("Enter number of elements:");
num = input.nextInt();
array = new int[num]; //Creating array to store the all the number
System.out.println("Enter " + num + " integers");
//Loop to store each numbers in array
for (counter = 0; counter < num; counter++)
array[counter] = input.nextInt();
System.out.println("Enter the search value:");
item = input.nextInt();
first = 0;
last = num - 1;
middle = (first + last)/2;

while( first <= last )


{
if ( array[middle] < item )
first = middle + 1;
else if ( array[middle] == item )
{
System.out.println(item + " found at location " + (middle + 1) + ".");
break;
}
else
{
last = middle - 1;
}
middle = (first + last)/2;
}
if ( first > last )
System.out.println(item + " is not found.\n");
}
Q. WAP TO CHECK THE INPUT NUMBER IS ARMSTRONG OR NOT.
import java.util.Scanner;
class Armstrong
{
public static void main(String[] args)
{
int c=0,a,temp;
int n; //It is the number to check armstrong

Scanner s= new Scanner(System.in);


System.out.println("Enter any number");
n=s.nextInt();
temp=n;
while(n>0)
{
a=n%10;
n=n/10;
c=c+(a*a*a);
}
if(temp==c)
System.out.println("armstrong number");
else
System.out.println("Not armstrong number");
}
}
Q. WAP TO CALCULATE THE AREA OF CIRCLE.
import java.util.Scanner;
class Armstrong
{
public static void main(String[] args)
{
int c=0,a,temp;
int n; //It is the number to check armstrong

Scanner s= new Scanner(System.in);


System.out.println("Enter any number");
n=s.nextInt();
temp=n;
while(n>0)
{
a=n%10;
n=n/10;
c=c+(a*a*a);
}
if(temp==c)
System.out.println("armstrong number");
else
System.out.println("Not armstrong number");
}
}
Q. WAP TO PRINT FLOYED TRIANGLE OF NUMBERS.
import java.util.Scanner;

class FloydTriangle
{
public static void main(String args[])
{
int n, num = 1, c, d;
Scanner in = new Scanner(System.in);

System.out.println("Enter the number of rows of floyd's triangle you want");


n = in.nextInt();

System.out.println("Floyd's triangle :-");

for ( c = 1 ; c <= n ; c++ )


{
for ( d = 1 ; d <= c ; d++ )
{
System.out.print(num+" ");
num++;
}

System.out.println();
}
}
}
Q. WAP TO FIND THE LARGEST OF THREE INPUT NUMBERS.

import java.util.Scanner;

public class Largest


{
public static void main(String[] args)
{
int a, b, c, d;
Scanner s = new Scanner(System.in);
System.out.println("Enter all three numbers:");
a = s.nextInt();
b = s.nextInt();
c = s.nextInt();
d = c > (a > b ? a : b) ? c : ((a > b) ? a : b);
System.out.println("Largest Number:"+d);
}
}
Q. WAP TO CHECK INPUT YEAR IS A LEAP YEAR.
import java.util.Scanner;
class LeapYear
{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.print("Enter any year:");
int year = s.nextInt();
boolean flag = false;
if(year % 400 == 0)
{
flag = true;
}
else if (year % 100 == 0)
{
flag = false;
}
else if(year % 4 == 0)
{
flag = true;
}
else
{
flag = false;
}
if(flag)
{
System.out.println("Year "+year+" is a Leap Year");
}
else
{
System.out.println("Year "+year+" is not a Leap Year");
}
}
}
Q. WAP TO ADD TWO INPUT MATRIX.
import java.util.Scanner;

class AddMatrix
{
public static void main(String args[])
{
int m, n, c, d;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of matrix");
m = in.nextInt();
n = in.nextInt();
int first[][] = new int[m][n];
int second[][] = new int[m][n];
int sum[][] = new int[m][n];
System.out.println("Enter the elements of first matrix");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
first[c][d] = in.nextInt();
System.out.println("Enter the elements of second matrix");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
second[c][d] = in.nextInt();

for ( c = 0 ; c < m ; c++ )


for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d]; //replace '+' with '-' to subtract matrices
System.out.println("Sum of entered matrices:-");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
System.out.print(sum[c][d]+"\t");
}
}
}
Q. WAP TO CHECK INPUT NUMBER IS PRIME OR NOT.
import java.util.Scanner;
class PrimeCheck
{
public static void main(String args[])
{
int temp;
boolean isPrime=true;
Scanner scan= new Scanner(System.in);
System.out.println("Enter any number:");
//capture the input in an integer
int num=scan.nextInt();
scan.close();
for(int i=2;i<=num/2;i++)
{
temp=num%i;
if(temp==0)
{
isPrime=false;
break;
}
}
//If isPrime is true then the number is prime else not
if(isPrime)
System.out.println(num + " is a Prime Number");
else
System.out.println(num + " is not a Prime Number");
}
}
Q. WAP TO REVERSE THE INPUT NUMBER.
import java.util.Scanner;
class Reverse
{
public static void main(String args[])
{
int n, temp,num= 0,r;
Scanner s = new Scanner(System.in);
System.out.print("Enter any number:");
n = s.nextInt();
temp= n;
while(n > 0)
{
r = n % 10;
num = num * 10 + r;
n = n / 10;

}
System.out.println("Reverse number of "+temp+" is "+num);
}
}
Q. WAP TO EXPLAIN THE KEYWORD STATIC.
class Student
{
int roll_no;
String name;
static String College_Name="ITM";
}
class StaticDemo
{
public static void main(String args[])
{
Student s1=new Student();
s1.roll_no=100;
s1.name="abcd";
System.out.println(s1.roll_no);
System.out.println(s1.name);
System.out.println(Student.College_Name);
Student s2=new Student();
s2.roll_no=200;
s2.name="zyx";
System.out.println(s2.roll_no);
System.out.println(s2.name);
System.out.println(Student.College_Name);
}
}
WAP TO ILLUSTRATE FINAL KEWQORD

public class Test


{
final int value = 10;
public static final int BOXWIDTH = 6;
static final String TITLE = "Manager";
public void changeValue()
{
value = 12;
}
}
WAP TO DEMONSTRATE CONSTRUCTOR OVERLOADING

class Student5
{
int id;
String name;
int age;
Student5(int i,String n){
id = i;
name = n;
}
Student5(int i,String n,int a)
{
id = i;
name = n;
age=a;
}
void display()
{System.out.println(id+" "+name+" "+age);}

public static void main(String args[])


{
Student5 s1 = new Student5(111,"Karan");
Student5 s2 = new Student5(222,"Aryan",25);
s1.display();
s2.display();
}
}

++
WAP TO DEMONSTRATE METHOD OVERLOADING

class Adder
{
static int add(int a, int b)
{return a+b;}
static double add(double a, double b)
{return a+b;}
}
class TestOverloading2
{
public static void main(String[] args)
{
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(12.3,12.6));
}
}
WAP TO DEMONSTRATE METHOD OVERIDING

class Vehicle
{
void run()
{System.out.println("Vehicle is running");}
}
class Bike2 extends Vehicle
{
void run()
{System.out.println("Bike is running safely");}

public static void main(String args[])


{
Bike2 obj = new Bike2();
obj.run();
}
}
WAP TO ILLUSTRATE INTERFACE

interface printable
{
void print();
}
class demo1 implements printable
{
public void print()
{ System.out.println("Hello");
}

public static void main(String args[])


{
A6 obj = new A6();
obj.print();
}
}
WAP TO ILLUSTRATE ABSTRACT CLASS

abstract class Bike


{
abstract void run();
}
class Honda4 extends Bike
{
void run()
{
System.out.println("running safely..");
}

public static void main(String args[])


{
Bike obj = new Honda4();
obj.run();
}
}
WAP TO DEMONSTRATE INHERITANCE

class Calculation
{
int z;
public void addition(int x, int y)
{
z = x + y;
System.out.println("The sum of the given numbers:"+z);
}

public void Subtraction(int x, int y)


{
z = x - y;
System.out.println("The difference between the given numbers:"+z);
}
}

class My_Calculation extends Calculation {


public void multiplication(int x, int y) {
z = x * y;
System.out.println("The product of the given numbers:"+z);
}

public static void main(String args[]) {


int a = 20, b = 10;
My_Calculation demo = new My_Calculation();
demo.addition(a, b);
demo.Subtraction(a, b);
demo.multiplication(a, b);
}
}
WAP TO DEMOSTRATE SUPER KEYWORD

class Superclass
{
int num = 100;
}
class Subclass extends Superclass
{
int num = 110;
void printNumber(){

System.out.println(super.num);
}
public static void main(String args[])
{
Subclass obj= new Subclass();
obj.printNumber();
}
}
WAP TO DEMONSTRATE THIS KEYWORD

class JBT
{

int variable = 5;

public static void main(String args[])


{
JBT obj = new JBT();
obj.method(20);
obj.method();
}

void method(int variable)


{
variable = 10;
System.out.println("Value of variable :" + variable);
}

void method()
{
int variable = 40;
System.out.println("Value of variable :" + variable);
}
}
WAP TO ILLUSTRATE EXCEPTION HANDLING

import java.io.*;

public class ExcepTest


{

public static void main(String args[])


{
try
{
int a[] = new int[2];
System.out.println("Access element three :" + a[3]);
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("Exception thrown :" + e);
}
System.out.println("Out of the block");
}
}
WAP TO ILLUSTRATE MULTITHREADING

class RunnableDemo implements Runnable


{
private Thread t;
private String threadName;

RunnableDemo( String name)


{
threadName = name;
System.out.println("Creating " + threadName );
}

public void run()


{
System.out.println("Running " + threadName );
try
{
for(int i = 4; i > 0; i--)
{
System.out.println("Thread: " + threadName + ", " + i);
Thread.sleep(50);
}
} catch (InterruptedException e)
{
System.out.println("Thread " + threadName + " interrupted.");
}
System.out.println("Thread " + threadName + " exiting.");
}

public void start ()


{
System.out.println("Starting " + threadName );
if (t == null) {
t = new Thread (this, threadName);
t.start ();
}
}
}

public class TestThread {

public static void main(String args[]) {


RunnableDemo R1 = new RunnableDemo( "Thread-1");
R1.start();

RunnableDemo R2 = new RunnableDemo( "Thread-2");


R2.start();
}
}
WAP TO PROGRAM TO ILLUSTRATE STRING

public class StringDemo


{

public static void main(String args[])


{
char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.' };
String helloString = new String(helloArray);
System.out.println( helloString );
}
}
WAP TO ILLUSTRATE PACKAGE

package animals;
interface Animal
{
public void eat();
public void travel();
}

MAKING CLASS WHICH USES THIS PACKAGE

package animals;
/* File name : MammalInt.java */
public class MammalInt implements Animal
{
public void eat()
{
System.out.println("Mammal eats");
}
public void travel()
{
System.out.println("Mammal travels");
}
public int noOfLegs()
{
return 0;
}
public static void main(String args[])
{
MammalInt m = new MammalInt();
m.eat();
m.travel();
}
}
WAP TO ILLUSTRATE APPLET

import java.applet.*;
import java.awt.*;

public class HelloWorldApplet extends Applet


{
//<applet code="HelloWorldApplet.class" width="320" height="120"></applet>
public void paint (Graphics g)
{
g.drawString ("Hello World", 25, 50);
}
}

You might also like