JAVA PRACTICALS

JAVA PRACTICALS



AddMatrix.java

import java.util.Scanner;
public class AddMatrix
{
public static void main (String args[])
{
int p,q,m,n;
Scanner s=new Scanner(System.in);
System.out.println("enter no. of rows for 1st matrix");
p=s.nextInt();
System.out.println("enter no. of cols for 1st matrix");
q=s.nextInt();
System.out.println("enter no. of rows for 2nd matrix");
m=s.nextInt();
System.out.println("enter no. of cols for 2nd matrix");
n=s.nextInt();
if(p==m && q==n)
{
int a[][]=new int[p][q];
int b[][]=new int[m][n];
int c[][]=new int[m][n];
System.out.println("enter elements for 1st matrix");
for(int i=0;i<p;i++)
{
for(int j=0;j<q;j++)
{
a[i][j]=s.nextInt();
}
}
System.out.println("enter elements for 2nd matrix");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
b[i][j]=s.nextInt();
}
}
System.out.println("the 1st matrix");
for(int i=0;i<p;i++)
{
for(int j=0;j<q;j++)
{
System.out.print("\t"+a[i][j]);
}
System.out.println("");
}
System.out.println("the 2nd matrix");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.println("\t"+b[i][j]);
}
System.out.println("");
}
System.out.println("addition of 2 matrices");
for(int i=0;i<p;i++)
{
for(int j=0;j<q;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
for(int i=0;i<p;i++)
{
for(int j=0;j<q;j++)
{
System.out.println("\t"+c[i][j]);
}
System.out.println("");
}
}
else
System.out.println("addition is not possible");
}
}



AddMatrix_1.java


import java.util.Scanner;
public class AddMatrix
{
public static void main (String args[])
{
int p,q,m,n;
Scanner s=new Scanner(System.in);
System.out.println("enter no. of rows for 1st matrix");
p=s.nextInt();
System.out.println("enter no. of cols for 1st matrix");
q=s.nextInt();
System.out.println("enter no. of rows for 2nd matrix");
m=s.nextInt();
System.out.println("enter no. of cols for 2nd matrix");
n=s.nextInt();
if(p==m && q==n)
{
int a[][]=new int[p][q];
int b[][]=new int[m][n];
int c[][]=new int[m][n];
System.out.println("enter elements for 1st matrix");
for(int i=0;i<p;i++)
{
for(int j=0;j<q;j++)
{
a[i][j]=s.nextInt();
}
}
System.out.println("enter elements for 2nd matrix");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
b[i][j]=s.nextInt();
}
}
System.out.println("the 1st matrix");
for(int i=0;i<p;i++)
{
for(int j=0;j<q;j++)
{
System.out.print("\t"+a[i][j]);
}
System.out.println("");
}
System.out.println("the 2nd matrix");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.println("\t"+b[i][j]);
}
System.out.println("");
}
System.out.println("addition of 2 matrices");
for(int i=0;i<p;i++)
{
for(int j=0;j<q;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
for(int i=0;i<p;i++)
{
for(int j=0;j<q;j++)
{
System.out.println("\t"+c[i][j]);
}
System.out.println("");
}
}
else
System.out.println("addition is not possible");
}
}



AddMatrix_2.java



import java.util.Scanner;
public class AddMatrix
{
public static void main (String args[])
{
int p,q,m,n;
Scanner s=new Scanner(System.in);
System.out.println("enter no. of rows for 1st matrix");
p=s.nextInt();
System.out.println("enter no. of cols for 1st matrix");
q=s.nextInt();
System.out.println("enter no. of rows for 2nd matrix");
m=s.nextInt();
System.out.println("enter no. of cols for 2nd matrix");
n=s.nextInt();
if(p==m && q==n)
{
int a[][]=new int[p][q];
int b[][]=new int[m][n];
int c[][]=new int[m][n];
System.out.println("enter elements for 1st matrix");
for(int i=0;i<p;i++)
{
for(int j=0;j<q;j++)
{
a[i][j]=s.nextInt();
}
}
System.out.println("enter elements for 2nd matrix");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
b[i][j]=s.nextInt();
}
}
System.out.println("the 1st matrix");
for(int i=0;i<p;i++)
{
for(int j=0;j<q;j++)
{
System.out.print("\t"+a[i][j]);
}
System.out.println("");
}
System.out.println("the 2nd matrix");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.println("\t"+b[i][j]);
}
System.out.println("");
}
System.out.println("addition of 2 matrices");
for(int i=0;i<p;i++)
{
for(int j=0;j<q;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
for(int i=0;i<p;i++)
{
for(int j=0;j<q;j++)
{
System.out.println("\t"+c[i][j]);
}
System.out.println("");
}
}
else
System.out.println("addition is not possible");
}
}


Animal.java


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



Demo.java

class Demo
{
  public static void main(String a[])
    {
       System.out.println("HELLO WORLD");
    }
}


ExceptionDemo.java


class MyException extends Exception
{
private int detail;
MyException(int a)
{
detail=a;
}
public String toString()
{
return "MyException["+detail+"]";
}
}
class ExceptionDemo
{
static void compute(int a) throws MyException
{
System.out.println("called compute ("+a+")");
if(a>10) throw new MyException(a);
System.out.println("normal exit");
}
public static void main(String args[])
{
try
{
compute(1);
compute(20);
}
catch(Exception e)
{
System.out.println(e);
}
}
}


ListDemo.java


import java.util.*;
public class ListDemo
{
public static void main(String args[])
{
List l1=new ArrayList();
l1.add(0,1);
l1.add(1,20);
System.out.println("elements of list1="+l1);
List l2=new ArrayList();
l2.add(10);
l2.add(20);
l2.add(30);
System.out.println("elements of list2="+l2);
l1.addAll(1,l2);
System.out.println("new list elements ="+l1);
l1.remove(1);
System.out.println("after removal of elements of list1="+l1);
System.out.println("elements at position 3 is="+l1.get(3));
l1.set(0,5);
System.out.println("after replacement elements of list1="+l1);
List<String> l=new ArrayList<String>(5);
l.add("hi");
l.add("hi");
l.add("welcome");
l.add("java");
System.out.println("first index of hi  "+l.indexOf("hi"));
System.out.println("last index of hi  "+l.lastIndexOf("hi"));
System.out.println("index of element not present "+l.indexOf("too"));
List<String>range= new ArrayList<String>();
range=l.subList(2,4);
System.out.println("elements of new list"+range);
}
}



MammalInt.java


package animals;
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 2;
}
public static void main(String args[])
{
MammalInt m=new MammalInt();
   m.eat();
   m.travel();
System.out.println("class mtd returns" + m.nooflegs());
 }
}


MethodOverridingDemo.java


class Bank{
int getRateOfIntrest()
{
   return 0;
}}
class SBI extends Bank{
 int getRateOfIntrest()
 {
   return 8;
 }}
class ICICI extends Bank{
 int getRateOfIntrest()
 {
   return 7;
 }}
class AXIS extends Bank{
 int getRateOfIntrest()
 {
   return 9;
 }}
class MethodOverridingDemo
{
   public static void main(String args[])
    {
      SBI s=new SBI();
      ICICI i=new ICICI();
      AXIS a=new AXIS();
    System.out.println("SBI note Interest"+ s.getRateOfIntrest());
    System.out.println("ICICI note Interest"+ i.getRateOfIntrest());
    System.out.println("AXIS note Interest"+ a.getRateOfIntrest());
 }
}


OneArray.java


import java.util.Scanner;
public class OneArray
{
  public static void main() {
  int n;
  String temp;
  Scanner s=new Scanner(System.in);
  System.out.println("enter no of names");
  n=s.nextInt();
  String names[]=new String[n];
  Scanner s1=new Scanner(System.in);
  System.out.println("Enter names to be sorted");
  for(int i=0;i<n;i++)
  {
    names[i]=s1.nextLine();
  }
   for(int i=0;i<n;i++)
 {
    for(int j=i+1;j<n;j++)
 {
    if (names[i].compareTo(names[j])>0)
   {
     temp=names[i];
     names[i]=names[j];
     names[j]=temp;
   }
  }
 }
System.out.println("the sorted array");
for(int i=0;i<n;i++)
{
  System.out.println(names[i] +",");
}
 System.out.println(" ");
 }
}


Quadratic.java


import java.util.Scanner;
public class Quadratic
{
 public static void main(String args[])
 {
   int a,b,c;
   double r1,r2,d;
   Scanner s=new Scanner(System.in);
   System.out.println("Given quadratic eqn=ax^2+bx+c");
   System.out.println("enter value for a ");
   a=s.nextInt();
   System.out.println("enter value for b ");
   b=s.nextInt();
   System.out.println("enter value for c ");
   c=s.nextInt();
   d=b*b-4*a*c;
   if(d>0)
   {
      System.out.println("roots are real &unequal ");
      r1=(-b+Math.sqrt(d))/(2*a);
      r2=(-b+Math.sqrt(d))/(2*a);
      System.out.println("The root1="+r1);
      System.out.println("The root1="+r2);
   }
   else if (d==0)
{ System.out.println("roots are real &equal ");
   r1=r2=(-b+Math.sqrt(d))/(2*a);
   System.out.println("The root1 & root2 =" +r1);
}
else
  System.out.println("roots are imaginary");
}
}


StudentResume.java


import java.awt.*;
import javax.swing.*;
class Student extends Jframe
{
 String msg=" ";
 JButton btnClr,btnSubmit;
 JLabel lblName.lblAge.lblAdr,lblGender,lblQuo,lbl1,lbl2;
 JTextField txtName,txtAge;
 JTextArea txtAddr;
 JCheckBox chkMCA.chkBCA,chkMBA,chkBBA;
 Resume(String name)
  super (name);
  setLayout(new GridLayout(9,2));
  lblName=new JLabel("Name:");
  lblAge=new JLabel("Age:");
  lblAddr=new JLabel("Address:");
  lblGender=new JLabel("Gender:");
  lblQua=new JLabel("Qualification:");
  lbl1=new JLabel();
  lbl2=new JLabel();
  txtName=newJTextField(20);
  txtAge=newJTextField(20);
  txtAddr=newJTextAddr(20);
  


Table.java


class furniture
{
int h,w,l;
furniture(int h,int w,int l)
{
this.h=h;
this.w=w;
this.l=l;
}
void display()
{
System.out.println("height="+h);
System.out.println("weight="+w);
System.out.println("length="+l);
}
}

class table extends furniture
{
int legs;
table(int h, int w,int l,int legs)
{
super(h,w,l);
this.legs=legs;
}
void display()
{
super.display();
System.out.println("no. of legs of table"+legs);
}
public static void main(String args[])
{
table t=new table(100,200,300,4);
t.display();
}
}








videowithcontrol.html

<html>
<head>
<title>video with control</title>
</head>
<body bgcolor=yellow text=blue>
<centre>
<h1 video with control</h1>
<hr color=green size=6 width=80%>
<embed src=clock.avi height=40% width=40% controls=console autostart=false>
</centre>
</body>
</html>


PRACTICAL NO.: 3


import java.util.Scanner;
public class OneArray
{
public static void main(String args[])
{
int n;
String temp;
Scanner s=new Scanner(System.in);
System.out.println("Enter no. of names");
n=s.nextInt();
String names[]=new String[n];
Scanner s1=new Scanner(System.in);
System.out.println("Enter names to be sorted");
for(int i=0;i<n;i++)
{
names[i]=s1.nextLine();
}
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(names[i].compareTo(names[j])>0)
{
temp=names[i];
names[i]=names[j];
names[j]=temp;
}
}
}
System.out.println("The sorted array");
for(int i=0;i<n;i++)
{
System.out.println(names[i]+",");
}
System.out.println(" ");
}
}


PRACTICAL NO.: 4

[SAVE FILE= Animal.java]
package animals;
interface Animal
{
public void eat();
public void travel();
}
[javac -d . Animal.java]


[SAVE FILE= MammalInt.java]


package animals;
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 2;
}
public static void main(String args[])
{
MammalInt m=new MammalInt();
m.eat();
m.travel();
System.out.println("class mtd returns"+m.nooflegs());
}
}
[javac –d . MammalInt.java
Java animals.MammlInt]


PRACTICAL NO.: 5

class Furniture
{
int h,w,l;
Furniture(int h,int w,int l)
{
this.h=h;
this.w=w;
this.l=l;
}
void display()
{
System.out.println("height="+h);
System.out.println("weight="+w);
System.out.println("length="+l);
}
}
class Table extends Furniture
{
int legs;
Table(int h, int w,int l,int legs)
{
super(h,w,l);
this.legs=legs;
}
void display()
{
super.display();
System.out.println("no. of legs of
table"+legs);
}
public static void main(String args[])
{
Table t=new Table(100,200,300,4);
t.display();
}
}


PRACTICAL NO. : 6(A) 
Aim: Demonstrate overloading of function or method.

Class OverloadDemo{
void test(){
System.out.println(“No parameter “);
}
void test(int a){
System.out.println(“a:=”+a);
}
void test(int a,int b){
System.out.println(“a:=”+a+”b=”+b);
}
double test(double a){
System.out.println(“a=”+a);
return a*a;
}
}
class MtdOverloading{
public static void main(String a[]){
double r;
OverloadDemo o=new OverloadDemo();
o.test();
o.test(10);
r=o.test(122.2);
System.out.println(“Result=”+r);
o.test(20,30);
}
}}




PRACTICAL NO. : 7 
Aim: Demonstrate creating your own Exception in Java.

class MyException extends Exception
{
private int detail;
MyException(int a)
{
detail=a;
}
public String toString()
{
return "MyException["+detail+"]";
}
}
class ExceptionDemo
{
static void compute(int a) throws MyException
{
System.out.println("Called compute("+a+")");
if(a>10) throw new MyException(a);
System.out.println("Normal Exit");
}
public static void main(String args[])
{
try{
compute(1);
compute(20);
}
catch(Exception e)
{
System.out.println(e);
}
}
}




PRACTICAL NO. : 9 
Aim: Write a Java List example & demonstrate the method of Java List interface.

import java.util.*;
public class ListDemo{
public static void main(String a[]){
List l1= new ArrayList();
l1.add(0,1);
l1.add(1,20);
System.out.println("Element of list 1="+l1);
List l2= new ArrayList();
l2.add(10);
l2.add(20);
l2.add(30);
System.out.println("Element of list 2"+l2);
l1.addAll(1,l2);
System.out.println("New list Element="+l1);
l1.remove(1);
System.out.println("After removal Element of list1="+l1);
System.out.println("element at position is"+l1.get(2));
l1.set(0,5);
System.out.println("After replacement of elemenet of list1="+l1);
List <String> l=new ArrayList<String>(5);
l.add("hi");
l.add("hi");
l.add("Welcome");
l.add("to");
l.add("Java");
System.out.println("First of hi "+l.indexOf("hi"));
System.out.println("last index of hi "+l.lastIndexOf("hi"));
System.out.println("Index of Element not present "+l.indexOf("to"));
List<String> range=new ArrayList<String>();
range=l.subList(2,4);
System.out.println("Element of new list "+range);
}
}





PRACTICAL NO. : 10 
(Something wrong in this code plz recheck 😁😁 )

import java.util.*;
import javax.swing.x.*;
class Student extends Jframe{
String msg="";
JButton btnClr,btnSubmit;
JLable lblName,lblAge,lblAddr,lblGender,lblQua,lbl1,lbl2;
JTextFeild txtName,txtAge;
JTextArea txtAddr;
JCheckbox chkMale,chkFemale;
JCheckbox chkMca,chkBca ,chkMba,chkBba;
Resume(String name){
Super(name);
setLayout(new GridLayout(9,2));
lblName=new Jlable("Name:");
lblAge=new Jlable("Age:");
lblAddr=new Jlable("Address:");
lblGender=new Jlable("Gender:");
lblQua=new Jlable("Qualification:");
lbl1=new Jlable();
lbl2=new JLable();
txtName=new JTextFeild(20);
txtAge=new JTextFeild(20);
txtAddr=new JTextArea();
chkMale=new JCheckBox("Male",flase);
chkFemale=new JCheckBox("Female",False);

}
}

Comments