Java Practical File
Java Practical File
PRACTICAL
FILE
Submitted By Submitted To
Harish Bisht Mr. Deepak Kumar
INDEX
S.N0 PROGRAMS OF JAVA SIGNATURE
}
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();
class FloydTriangle
{
public static void main(String args[])
{
int n, num = 1, c, d;
Scanner in = new Scanner(System.in);
System.out.println();
}
}
}
Q. WAP TO FIND THE LARGEST OF THREE INPUT NUMBERS.
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();
}
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
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);}
++
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");}
interface printable
{
void print();
}
class demo1 implements printable
{
public void print()
{ System.out.println("Hello");
}
class Calculation
{
int z;
public void addition(int x, int y)
{
z = x + y;
System.out.println("The sum of the given numbers:"+z);
}
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;
void method()
{
int variable = 40;
System.out.println("Value of variable :" + variable);
}
}
WAP TO ILLUSTRATE EXCEPTION HANDLING
import java.io.*;
package animals;
interface Animal
{
public void eat();
public void travel();
}
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.*;