Experiment-2
1.
import [Link].*;
class X
{
int roll=474;
String name="santhoshini";
public static void main(String args[])
{
X obj=new X();
[Link]("student name is:" +[Link]);
[Link]("student rollnumber is:" +[Link]);
}
}
2.
import [Link].*;
class Xy
{
int roll;
String name;
void read(int a, String b)
{
roll=a;
name=b;
}
void display()
{
[Link]("student roll number is:" +roll);
[Link]("student name is:" +name);
}
public static void main(String args[])
{
Xy obj=new Xy();
[Link](474,"santhoshini");
[Link]();
}
}
3.
import [Link];
public class Triangle
{
public static void main(String args [])
{
float B, H;
Triangle obj = new Triangle();
Scanner in = new Scanner([Link]);
[Link]("Enter base of the triangle:");
B = [Link]();
[Link]("Enter height of the triangle:");
H = [Link]();
float area = [Link](B, H);
[Link]("The area of the triangle is " + area);
}
public static float add(float B, float H)
{
float area = (B*H) / 2;
return area;
}
}
4.
import [Link].*;
public class StudentDetails
{
public static void main(String args[])
{
String Sname;
String Regno;
String Branch;
float Height;
double Weight;
int Age;
Scanner in=new Scanner([Link]);
[Link]("enter the name:");
Sname=[Link]();
[Link]("enter reg no:");
Regno=[Link]();
[Link]("enter branch:");
Branch=[Link]();
[Link]("enter the student height");
Height=[Link]();
[Link]("enter student weight");
Weight=[Link]();
[Link]("enter student age");
Age=[Link]();
[Link]("the student name is"+Sname);
[Link]("the student regno is"+Regno);
[Link]("the student branch is"+Branch);
[Link]("the student height is"+Height);
[Link]("the student weight is"+Weight);
[Link]("the student age is"+Age);
}
}
Experiment-3
import [Link].*;
class StringMethods
{
public static void main(String args[])
{
String s1="GMRIT";
String s2="Hello Ece Students";
[Link]("Concatenation of two strings:"+[Link](s2));
[Link]("The character of 4th place of string2 is:"+[Link](7));
[Link]("Comparison of two strings is:"+[Link](s2));
[Link]("Boolean Comparison Ignoring case considerations:"+[Link](s2));
[Link]("Hashcode of string2 is:"+[Link]());
[Link]("Index of e in string2 from last is:"+[Link]('e',3));
[Link]("Length of string2 is:"+[Link]());
[Link]("String1 replacing e with k is:"+[Link]('e','k'));
[Link]("String of string2 is:"+[Link](4));
[Link]("Substring of string2 from 4 to 9 is:"+[Link](4,9));
[Link]("Lowercase of s1 is:"+[Link]());
[Link]("Uppercase of s2 is:"+[Link]());
[Link]("After trimming, s2 is:"+[Link]());
}
}
Experiment-4
1.
import [Link].*;
public class StringTokenizer_Demo
{
public static void main(String args[])
{
StringTokenizer Str_arr=new StringTokenizer("let's go for Ahaa");
[Link]("The number of tokens are:"+Str_arr.countTokens());
[Link]("Str_arr.hasMoreTokens()");
while(Str_arr.hasMoreTokens())
{
[Link]("the new token:"+Str_arr.nextToken());
}
}
}
2.
import [Link].*;
public class Test2
{
public static void main(String args[])
{
StringTokenizer st= new StringTokenizer("my,name,is,santhoshini");
[Link]("next token is:"+[Link](","));
}
}
3.
import [Link].*;
public class CountCharacter
{
public static void main(String args[])
{
String String= "the best of both worlds";
int Count=0;
for(int i = 0;i < [Link](); i++)
{
if([Link](i)!=' ')
Count++;
}
[Link]("total no. of character in a string:"+Count);
}
}
4.
import [Link].*;
public class StringTokenizer1
{
public static void main(String args[])
{
StringTokenizer Str_arr=new StringTokenizer("let's go for Ahaa");
[Link]("The number of tokens are:"+Str_arr.countTokens());
[Link]("Str_arr.hasMoreTokens()");
while(Str_arr.hasMoreTokens())
{
[Link]("the new token:"+Str_arr.nextToken());
}
}
}
5.
import [Link].*;
import [Link].*;
public class StringTokenizer_Demo2
{
public static void main(String args[])
{
Enumeration days;
Vector week=new Vector();
[Link]("sunday");
[Link]("monday");
[Link]("tuesday");
[Link]("wednesday");
[Link]("thursday");
[Link]("friday");
[Link]("saturday");
days=[Link]();
while([Link]())
{
[Link]("days="+[Link]());
}
}
}
Experiment-5
import [Link].*;
import [Link];
class Matrix
{
int r,c,k;
void add(int a[][],int b[][])
{
int c[][]=new int[[Link]][b[0].length];
for(int i=0;i<[Link];i++)
{
for(int j=0;j<b[0].length;j++)
{
c[i][j]=a[i][j]+b[i][j];
[Link](" "+c[i][j]);
}
[Link](" ");
}
}
void mul(int a[][],int b[][])
{
int c[][]=new int[[Link]][b[0].length];
for(int i=0;i<[Link];i++)
for(int j=0;j<b[0].length;j++)
for(int k=0;k<a[0].length;k++)
c[i][j]+=a[i][k]*b[k][i];
for(int i=0;i<[Link];i++)
{
for(int j=0;j<b[0].length;j++)
[Link](" "+c[i][j]);
[Link](" ");
}
}
void sub(int a[][],int b[][])
{
int c[][]=new int[[Link]][b[0].length];
for(int i=0;i<[Link];i++)
{
for(int j=0;j<b[0].length;j++)
{
c[i][j]=a[i][j]-b[i][j];
[Link](" "+c[i][j]);
}
[Link](" ");
}
}
}
class MatOperations1
{
public static void main(String args[])
{
int z;
Matrix m=new Matrix();
Scanner s=new Scanner([Link]);
int X[][]=new int[4][4];
int Y[][]=new int[4][4];
[Link]("Enter matrix A:");
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
X[i][j]=[Link]();
[Link]("Enter matrix B:");
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
Y[i][j]=[Link]();
[Link]("Enter your choice 1>Multiplication 2>addition 3>subtraction");
z=[Link]();
if(z==1)
[Link](X,Y);
else if(z==2)
[Link](X,Y);
else if(z==3)
[Link](X,Y);
else
{
[Link]("invalid options");
}
}
}
Experiment-6
import [Link];
import [Link];
public class Quadratic
{
public static void main(String[] Strings)
{
Scanner i = new Scanner([Link]);
[Link]("Enter the value of a: ");
double a = [Link]();
[Link]("Enter the value of b: ");
double b = [Link]();
[Link]("Enter the value of c: ");
double c = [Link]();
double d=(b * b)-(4.0 * a * c);
if (d> 0.0)
{
double x1 = (-b + [Link](d, 0.5)) / (2.0 * a);
double x2 = (-b - [Link](d, 0.5)) / (2.0 * a);
[Link]("\nThe roots are : "+x1+" and "+x2);
[Link]("And the roots are real and distinct");
}
else if (d == 0.0)
{
double x1 = -b / (2.0 * a);
[Link]("\nThe roots are : "+x1+" and "+x1);
[Link]("And the roots are real and equal");
}
else
{
double x1=(-b)/(2.0*a);
double x2=([Link](-d,0.5))/(2.0*a);
DecimalFormat df=new DecimalFormat("0.00");
[Link]("\nThe roots are : "+x1+"+i "+[Link](x2)+" and "+x1+"-i "+[Link](x2));
[Link]("\nAnd the roots are imaginary");
}
}
}
Experiment-7
1.
import [Link].*;
class Student
{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee)
{
[Link]=rollno;
[Link]=name;
[Link]=fee;
}
void display()
{
[Link](rollno+" "+name+" "+fee);
}
}
class Test3
{
public static void main(String args[])
{
Student S1=new Student(1, "loki",6000f);
Student S2=new Student(2, "abhi",5000f);
[Link]();
[Link]();
}
}
2.
import [Link].*;
class A
{
void m()
{
[Link]("hello abhi");
}
void n()
{
[Link]("hello loki");
this.m();
}
}
class Test5
{
public static void main(String args[])
{
A a=new A();
a.n();
}
}
3.
import [Link].*;
class A
{
A()
{
[Link]("Hello A");
}
A (int x)
{
this();
[Link](x);
}
}
class Test4
{
public static void main(String args[])
{
A a=new A(10);
}
}
Experiment-8(a)
1.
import [Link].*;
class Animal
{
void eat()
{
[Link]("eating");
}
}
class Dog extends Animal
{
void eat()
{
[Link]("eating bread");
}
void bark()
{
[Link]("barking");
}
void work()
{
bark();
eat();
[Link]();
}
}
class Super
{
public static void main(String args[])
{
Dog d=new Dog();
[Link]();
}
}
2.
import [Link].*;
class Animal
{
String color="white";
}
class Dog extends Animal
{
String color="Black";
void printcolor()
{
[Link](color);
[Link]([Link]);
}
}
class Super1
{
public static void main(String args[])
{
Dog d=new Dog();
[Link]();
}
}
3.
import [Link].*;
import [Link].*;
class Animal
{
Animal()
{
[Link]("Animal is created");
}
}
class Dog extends Animal
{
Dog()
{
super();
[Link]("object is created");
}
}
class Super2
{
public static void main(String args[])
{
Dog d=new Dog();
}
}
Experiment-8(b)
1.
import [Link].*;
class Bike
{
final int Speedlimit=90;
void run()
{
Speedlimit=400;
[Link](Speedlimit);
}
}
class Final
{
public static void main(String args[])
{
Bike b=new Bike();
[Link]();
}
}
2.
import [Link].*;
class Bike
{
final void run()
{
[Link]("running");
}
}
class Honda extends Bike
{
void run()
{
[Link]("running safely");
}
}
class Final2
{
public static void main(String args[])
{
Honda h=new Honda();
[Link]();
}
}
3.
import [Link].*;
final class Final3
{
static class Honda extends Final3
{
void run()
{
[Link]("running safely");
}
public static void main(String args[])
{
Honda h=new Honda();
[Link]();
}
}
}
4.
import [Link].*;
class Bike
{
final void run()
{
[Link]("running");
}
}
class Final4 extends Bike
{
public static void main(String args[])
{
Final4 h=new Final4();
[Link]();
}
}
Experiment-8©
import [Link].*;
class Animal
{
void eat()
{
[Link]("eating");
}
}
class Dog extends Animal
{
void eat()
{
[Link]("eating bread");
}
}
class Cat extends Animal
{
void eat()
{
[Link]("eating rat");
}
}
class Lion extends Animal
{
void eat()
{
[Link]("eating meat");
}
}
class Rtpoly
{
public static void main(String args[])
{
Animal a;
a=new Animal();
[Link]();
a=new Dog();
[Link]();
a=new Cat();
[Link]();
a=new Lion();
[Link]();
}
}
Experiment-9
1.
import [Link].*;
class Person
{
void display()
{
[Link]("I am coming from sklm");
}
}
class Student extends Person
{
void show()
{
[Link]("I am a student of GMRIT");
}
}
class SingleInheritanceExample
{
public static void main(String args[])
{
Student S = new Student();
[Link]();
[Link]();
}
}
2.
import [Link].*;
class Person
{
void showPerson()
{
[Link]("I am resident of sklm");
}
}
class Student extends Person
{
void showStudent()
{
[Link]("I am a student of GMRIT");
}
}
class Employee extends Student
{
void showEmployee()
{
[Link]("I am an employee in GMRIT Rajam");
}
}
class MultilevelInheritance
{
public static void main(String args[])
{
Employee e=new Employee();
[Link]();
[Link]();
[Link]();
}
}
3.
import [Link].*;
class Person
{
String name="LOKESH";
void showPerson()
{
[Link]("Mr. "+name+" I am a resident of Sklm");
}
}
class Student extends Person
{
String id="473";
void showStudent()
{
[Link]("Ms. "+name+" , ID: "+id+" I am a Student of GMRIT");
}
}
class Employee extends Person
{
void showEmployee()
{
[Link]("I am an employee in GMRIT at Rajam");
}
}
class HierarchialInh
{
public static void main(String args[])
{
Employee e=new Employee();
[Link]();
[Link]();
Student s=new Student();
[Link]();
}
}
4.
import [Link].*;
class A
{
void display()
{
[Link]("Hello A");
}
}
class B
{
void display()
{
[Link]("Hello B");
}
}
class Print extends A,B
//multiple inheritance cannot be achieved using classes in Java
{
void display()
{
[Link]("Hello");
}
public static void main(String args[])
{
Print p=new Print();
[Link]();
}
}
Experiment-10
import [Link].*;
interface Printable
{
void print();
}
interface Showable
{
void print();
}
class PrintC implements Printable,Showable
{
public void print()
{
[Link]("Hello");
}
public static void main(String args[])
{
PrintC p=new PrintC();
[Link]();
}
}
Experiment-11
package first;
public class Studentinfo
{
int age;
char sex;
String name,fn,mn,add;
public Studentinfo(int d,char e,String a,String b,String c,String f)
{
name=a;
fn=b;
mn=c;
age=d;
sex=e;
add=f;
}
public void display()
{
[Link]("student personal details");
[Link]("name of the student is:"+name);
[Link]("fathers name is:"+fn);
[Link]("mothers name is:"+mn);
[Link]("age is:"+age);
[Link]("student is:"+sex);
}
}
package second;
public class Academicinfo
{
int rollno,avg,att;
public Academicinfo(int f,int g,int h)
{
rollno=f;
avg=g;
att=h;
}
public void display()
{
[Link]("student educational details:");
[Link]("rollno of student is:"+rollno);
[Link]("average marks of student is:"+avg);
[Link]("attendance of student is:"+att+"%");
}
}
package third;
public class Teacherinfo
{
String name,des,pos;
public Teacherinfo(String n,String d,String p)
{
name=n;
des=d;
pos=p;
}
public void display()
{
[Link]("STAFF DETAILS: ");
[Link]("Name of the member is: "+name);
[Link]("Designation is: "+des);
[Link]("Her position is: "+pos);
}
}
import first.*;
import second.*;
import third.*;
class Demo
{
public static void main(String args[])
{
[Link] si=new [Link](19,'M',"[Link]","[Link] babu","[Link]","Srikakulam");
[Link]();
[Link] ai=new [Link](473,980,99);
[Link]();
[Link] ti=new [Link]("[Link]","Faculty","Professor");
[Link]();
}
}
Experiment-12
Try-catch
import [Link].*;
class Exception
{
public static void main(String args[])
{
try
{
int data=50/0;
}
catch(ArithmeticException e)
{
[Link](e);
}
[Link]("rest of the code");
}
}
Multi-catch
1.
import [Link].*;
import [Link];
class Exception2
{
public static void main(String args[])
{
try
{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException e)
{
[Link]("task1 is completed");
}
catch(ArrayIndexOutOfBoundsException e)
{
[Link]("task2 is completed");
}
catch(Exception e)
{
[Link]("common task completed");
}
[Link]("rest of the code");
}
}
2.
import [Link].*;
import [Link];
class Exception3
{
public static void main(String args[])
{
try
{
int a[]=new int[5];
a[5]=30/0;
}
catch(Exception e)
{
[Link]("common task completed");
}
catch(ArithmeticException e)
{
[Link]("task1 is completed");
}
catch(ArrayIndexOutOfBoundsException e)
{
[Link]("task1 is completed");
}
[Link]("rest of the code");
}
}
Nested-try
import [Link];
import [Link];
import [Link].*;
class Exception4
{
public static void main(String args[])
{
try
{
try
{
[Link]("going to divide");
int b=20/0;
}
catch(ArithmeticException e)
{
[Link](e);
}
try
{
int a[]=new int[5];
a[5]=4;
}
catch(ArrayIndexOutOfBoundsException e)
{
[Link](e);
}
[Link]("other statement");
}
catch(Exception e)
{
[Link]("handled");
}
[Link]("normal flow");
}
}
b.
case1
import [Link].*;
class Testfinally1
{
public static void main (String args[])
{
try
{
int data = 25/5;
[Link](data);
}
catch (NullPointerException e)
{
[Link](e);
}
finally
{
[Link]("Finally block is always executed");
}
[Link]("rest of the code");
}
}
Case2
import [Link].*;
class Testfinally2
{
public static void main (String args[])
{
try
{
int data = 25/0;
[Link](data);
}
catch (NullPointerException e)
{
[Link](e);
}
finally
{
[Link]("Finally block is always executed");
}
[Link]("rest of the code");
}
}
Case3
import [Link].*;
class Testfinally
{
public static void main (String args[])
{
try
{
int data = 25/0;
[Link](data);
}
catch (ArithmeticException e)
{
[Link](e);
}
finally
{
[Link]("Finally block is always executed");
}
[Link]("rest of the code");
}
}
c.
import [Link].*;
public class Throw
{
static void validate(int age)
{
if(age<18)
throw new ArithmeticException("not valid");
else
[Link]("welcome to vote");
}
public static void main(String args[])
{
validate(13);
[Link]("rest of the code...");
}
}
d.
import [Link];
import [Link];
import [Link];
class Testthrows
{
void m()throws IOException
{
throw new IOException("device error");
}
void n()throws IOException
{
m();
}
void p()
{
try
{
n();
}
catch(Exception e)
{
[Link]("exception handled");
}
}
public static void main(String args[])
{
Testthrows obj=new Testthrows();
obj.p();
[Link]("normal flow");
}
}
Case-1
import [Link];
import [Link];
import [Link].*;
class M
{
void method()throws IOException
{
throw new IOException("device error");
}
}
public class Throws2
{
public static void main(String args[])
{
try
{
M m=new M();
[Link]();
}
catch(Exception e)
{
[Link]("exception handled");
}
[Link]("normal flow");
}
}
Case-2
1.
import [Link];
import [Link];
import [Link].*;
class M
{
void method()throws IOException
{
[Link]("device operation performed");
}
}
class Throws3
{
public static void main(String args[])
{
try
{
M m=new M();
[Link]();
}
catch(IOException e)
{
[Link](e);
}
[Link]("normal flow");
}
}
2.
import [Link].*;
import [Link].*;
class M
{
void method()throws IOException
{
throw new IOException("device error");
}
}
class Throws4
{
public static void main(String args[])
{
try
{
M m=new M();
[Link]();
}
catch(IOException e)
{
[Link](e);
}
[Link]("rest of the code");
}
}
Experiment-13
1.
import [Link];
import [Link];
import [Link].*;
class InvalidAgeException extends Exception
{
InvalidAgeException (String str)
{
super(str);
}
}
public class TestCustomException1
{
static void validate(int age) throws InvalidAgeException
{
if(age<18)
{
throw new InvalidAgeException("age is not valid to vote");
}
else
{
[Link]("Welcome to vote");
}
}
public static void main(String args[])
{
try
{
validate(13);
}
catch(InvalidAgeException ex)
{
[Link]("caught the Exception");
[Link]("Exception occured:"+ex);
}
[Link]("Rest of the code");
}
}
2.
import [Link];
import [Link];
import [Link].*;
class MyCustomException extends Exception
{
}
public class TestCustomException2
{
public static void main(String args[])
{
try
{
throw new MyCustomException();
}
catch(MyCustomException ex)
{
[Link]("caught the exception");
[Link]([Link]());
}
[Link]("rest of the code");
}
}
Experiment-14
1.
import [Link];
import [Link].*;
class Table
{
void printTable(int n)
{
for(int i=1;i<=5;i++)
{
[Link](n*i);
try
{
[Link](400);
}
catch(Exception e)
{
[Link](e);
}
}
}
}
class MyThread1 extends Thread
{
Table t;
MyThread1(Table t)
{
this.t=t;
}
public void run()
{
[Link](5);
}
}
class MyThread2 extends Thread
{
Table t;
MyThread2(Table t)
{
this.t=t;
}
public void run()
{
[Link](100);
}
}
class TestSynchronization1
{
public static void main(String args[])
{
Table obj=new Table();
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
[Link]();
[Link]();
}
}
2.
import [Link];
import [Link].*;
class Table
{
synchronized void printTable(int n)
{
for(int i=1;i<=5;i++)
{
[Link](n*i);
try
{
[Link](400);
}
catch(Exception e)
{
[Link](e);
}
}
}
}
class MyThread1 extends Thread
{
Table t;
MyThread1(Table t)
{
this.t=t;
}
public void run()
{
[Link](5);
}
}
class MyThread2 extends Thread
{
Table t;
MyThread2(Table t)
{
this.t=t;
}
public void run()
{
[Link](100);
}
}
class TestSynchronization2
{
public static void main(String args[])
{
Table obj=new Table();
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
[Link]();
[Link]();
}
}
3.
import [Link];
import [Link].*;
class Customer
{
int amount=10000;
synchronized void withdraw(int amount)
{
[Link]("Going to withdraw");
if([Link]<amount)
{
[Link]("Less balance:Waiting for deposit");
try
{
wait();
}
catch(Exception e)
{
}
}
[Link]-=amount;
[Link]("Withdraw completed");
}
synchronized void deposit(int amount)
{
[Link]("Going to deposit");
[Link]+=amount;
[Link]("Deposit completed");
notify();
}
}
class Test100
{
public static void main(String args[])
{
final Customer c=new Customer();
new Thread()
{
public void run()
{
[Link](15000);
}
}.start();
new Thread()
{
public void run()
{
[Link](10000);
}
}.start();
}
}