Nothing Special   »   [go: up one dir, main page]

Java (Experiment Codes)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 29

Experiment-2

1.
import java.io.*;
class X
{
int roll=474;
String name="santhoshini";
public static void main(String args[])
{
X obj=new X();
System.out.println("student name is:" +obj.name);
System.out.println("student rollnumber is:" +obj.roll);
}
}

2.

import java.io.*;
class Xy
{
int roll;
String name;
void read(int a, String b)
{
roll=a;
name=b;
}
void display()
{
System.out.println("student roll number is:" +roll);
System.out.println("student name is:" +name);
}
public static void main(String args[])
{
Xy obj=new Xy();
obj.read(474,"santhoshini");
obj.display();
}
}

3.
import java.util.Scanner;
public class Triangle
{
public static void main(String args [])
{
float B, H;
Triangle obj = new Triangle();
Scanner in = new Scanner(System.in);
System.out.println("Enter base of the triangle:");
B = in.nextFloat();
System.out.println("Enter height of the triangle:");
H = in.nextFloat();
float area = obj.add(B, H);
System.out.println("The area of the triangle is " + area);
}
public static float add(float B, float H)
{
float area = (B*H) / 2;
return area;
}
}

4.
import java.util.*;
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(System.in);
System.out.println("enter the name:");
Sname=in.nextLine();
System.out.println("enter reg no:");
Regno=in.nextLine();
System.out.println("enter branch:");
Branch=in.nextLine();
System.out.println("enter the student height");
Height=in.nextFloat();
System.out.println("enter student weight");
Weight=in.nextDouble();
System.out.println("enter student age");
Age=in.nextInt();
System.out.println("the student name is"+Sname);
System.out.println("the student regno is"+Regno);
System.out.println("the student branch is"+Branch);
System.out.println("the student height is"+Height);
System.out.println("the student weight is"+Weight);
System.out.println("the student age is"+Age);
}
}
Experiment-3

import java.util.*;
class StringMethods
{
public static void main(String args[])
{
String s1="GMRIT";
String s2="Hello Ece Students";
System.out.println("Concatenation of two strings:"+s1.concat(s2));
System.out.println("The character of 4th place of string2 is:"+s2.charAt(7));
System.out.println("Comparison of two strings is:"+s1.compareTo(s2));
System.out.println("Boolean Comparison Ignoring case considerations:"+s1.equalsIgnoreCase(s2));
System.out.println("Hashcode of string2 is:"+s2.hashCode());
System.out.println("Index of e in string2 from last is:"+s2.lastIndexOf('e',3));
System.out.println("Length of string2 is:"+s2.length());
System.out.println("String1 replacing e with k is:"+s1.replace('e','k'));
System.out.println("String of string2 is:"+s2.substring(4));
System.out.println("Substring of string2 from 4 to 9 is:"+s2.substring(4,9));
System.out.println("Lowercase of s1 is:"+s1.toLowerCase());
System.out.println("Uppercase of s2 is:"+s2.toUpperCase());
System.out.println("After trimming, s2 is:"+s2.trim());
}
}

Experiment-4

1.

import java.util.*;
public class StringTokenizer_Demo
{
public static void main(String args[])
{
StringTokenizer Str_arr=new StringTokenizer("let's go for Ahaa");
System.out.println("The number of tokens are:"+Str_arr.countTokens());
System.out.println("Str_arr.hasMoreTokens()");
while(Str_arr.hasMoreTokens())
{
System.out.println("the new token:"+Str_arr.nextToken());
}
}
}
2.
import java.util.*;
public class Test2
{
public static void main(String args[])
{
StringTokenizer st= new StringTokenizer("my,name,is,santhoshini");
System.out.println("next token is:"+st.nextToken(","));
}
}

3.
import java.util.*;
public class CountCharacter
{
public static void main(String args[])
{
String String= "the best of both worlds";
int Count=0;
for(int i = 0;i < String.length(); i++)
{
if(String.charAt(i)!=' ')
Count++;
}
System.out.println("total no. of character in a string:"+Count);
}
}

4.
import java.util.*;
public class StringTokenizer1
{
public static void main(String args[])
{
StringTokenizer Str_arr=new StringTokenizer("let's go for Ahaa");
System.out.println("The number of tokens are:"+Str_arr.countTokens());
System.out.println("Str_arr.hasMoreTokens()");
while(Str_arr.hasMoreTokens())
{
System.out.println("the new token:"+Str_arr.nextToken());
}
}
}
5.

import java.io.*;
import java.util.*;
public class StringTokenizer_Demo2
{
public static void main(String args[])
{
Enumeration days;
Vector week=new Vector();
week.add("sunday");
week.add("monday");
week.add("tuesday");
week.add("wednesday");
week.add("thursday");
week.add("friday");
week.add("saturday");
days=week.elements();
while(days.hasMoreElements())
{
System.out.println("days="+days.nextElement());
}
}
}

Experiment-5

import java.io.*;
import java.util.Scanner;
class Matrix
{
int r,c,k;
void add(int a[][],int b[][])
{
int c[][]=new int[a.length][b[0].length];
for(int i=0;i<a.length;i++)
{
for(int j=0;j<b[0].length;j++)
{
c[i][j]=a[i][j]+b[i][j];
System.out.println(" "+c[i][j]);
}
System.out.println(" ");
}
}
void mul(int a[][],int b[][])
{
int c[][]=new int[a.length][b[0].length];
for(int i=0;i<a.length;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<a.length;i++)
{
for(int j=0;j<b[0].length;j++)
System.out.println(" "+c[i][j]);
System.out.println(" ");
}
}
void sub(int a[][],int b[][])
{
int c[][]=new int[a.length][b[0].length];
for(int i=0;i<a.length;i++)
{
for(int j=0;j<b[0].length;j++)
{
c[i][j]=a[i][j]-b[i][j];
System.out.println(" "+c[i][j]);
}
System.out.println(" ");
}
}
}
class MatOperations1
{
public static void main(String args[])
{
int z;
Matrix m=new Matrix();
Scanner s=new Scanner(System.in);
int X[][]=new int[4][4];
int Y[][]=new int[4][4];
System.out.println("Enter matrix A:");
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
X[i][j]=s.nextInt();
System.out.println("Enter matrix B:");
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
Y[i][j]=s.nextInt();
System.out.println("Enter your choice 1>Multiplication 2>addition 3>subtraction");
z=s.nextInt();
if(z==1)
m.mul(X,Y);
else if(z==2)
m.add(X,Y);
else if(z==3)
m.sub(X,Y);
else
{
System.out.println("invalid options");
}
}
}
Experiment-6
import java.util.Scanner;
import java.text.DecimalFormat;
public class Quadratic
{
public static void main(String[] Strings)
{
Scanner i = new Scanner(System.in);
System.out.print("Enter the value of a: ");
double a = i.nextDouble();
System.out.print("Enter the value of b: ");
double b = i.nextDouble();
System.out.print("Enter the value of c: ");
double c = i.nextDouble();
double d=(b * b)-(4.0 * a * c);
if (d> 0.0)
{
double x1 = (-b + Math.pow(d, 0.5)) / (2.0 * a);
double x2 = (-b - Math.pow(d, 0.5)) / (2.0 * a);
System.out.println("\nThe roots are : "+x1+" and "+x2);
System.out.println("And the roots are real and distinct");
}
else if (d == 0.0)
{
double x1 = -b / (2.0 * a);
System.out.println("\nThe roots are : "+x1+" and "+x1);
System.out.println("And the roots are real and equal");
}
else
{
double x1=(-b)/(2.0*a);
double x2=(Math.pow(-d,0.5))/(2.0*a);
DecimalFormat df=new DecimalFormat("0.00");
System.out.printf("\nThe roots are : "+x1+"+i "+df.format(x2)+" and "+x1+"-i "+df.format(x2));
System.out.println("\nAnd the roots are imaginary");
}
}
}
Experiment-7
1.
import java.io.*;
class Student
{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee)
{
this.rollno=rollno;
this.name=name;
this.fee=fee;
}
void display()
{
System.out.println(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);
S1.display();
S2.display();
}
}

2.
import java.io.*;
class A
{
void m()
{
System.out.println("hello abhi");
}
void n()
{
System.out.println("hello loki");
this.m();
}
}
class Test5
{
public static void main(String args[])
{
A a=new A();
a.n();
}
}

3.
import java.io.*;
class A
{
A()
{
System.out.println("Hello A");
}
A (int x)
{
this();
System.out.println(x);
}
}
class Test4
{
public static void main(String args[])
{
A a=new A(10);
}
}

Experiment-8(a)
1.
import java.io.*;
class Animal
{
void eat()
{
System.out.println("eating");
}
}
class Dog extends Animal
{
void eat()
{
System.out.println("eating bread");
}
void bark()
{
System.out.println("barking");
}
void work()
{
bark();
eat();
super.eat();
}
}
class Super
{
public static void main(String args[])
{
Dog d=new Dog();
d.work();
}
}

2.
import java.io.*;
class Animal
{
String color="white";
}
class Dog extends Animal
{
String color="Black";
void printcolor()
{
System.out.println(color);
System.out.println(super.color);
}
}
class Super1
{
public static void main(String args[])
{
Dog d=new Dog();
d.printcolor();
}
}

3.

import java.io.*;
import java.util.*;
class Animal
{
Animal()
{
System.out.println("Animal is created");
}
}
class Dog extends Animal
{
Dog()
{
super();
System.out.println("object is created");
}
}
class Super2
{
public static void main(String args[])
{
Dog d=new Dog();
}
}

Experiment-8(b)
1.
import java.io.*;
class Bike
{
final int Speedlimit=90;
void run()
{
Speedlimit=400;
System.out.println(Speedlimit);
}
}
class Final
{
public static void main(String args[])
{
Bike b=new Bike();
b.run();
}
}

2.

import java.io.*;
class Bike
{
final void run()
{
System.out.println("running");
}
}
class Honda extends Bike
{
void run()
{
System.out.println("running safely");
}
}
class Final2
{
public static void main(String args[])
{
Honda h=new Honda();
h.run();
}
}

3.

import java.io.*;
final class Final3
{
static class Honda extends Final3
{
void run()
{
System.out.println("running safely");
}
public static void main(String args[])
{
Honda h=new Honda();
h.run();
}
}
}

4.

import java.io.*;
class Bike
{
final void run()
{
System.out.println("running");
}
}
class Final4 extends Bike
{
public static void main(String args[])
{
Final4 h=new Final4();
h.run();
}
}
Experiment-8©
import java.io.*;
class Animal
{
void eat()
{
System.out.println("eating");
}
}
class Dog extends Animal
{
void eat()
{
System.out.println("eating bread");
}
}
class Cat extends Animal
{
void eat()
{
System.out.println("eating rat");
}
}
class Lion extends Animal
{
void eat()
{
System.out.println("eating meat");
}
}
class Rtpoly
{
public static void main(String args[])
{
Animal a;
a=new Animal();
a.eat();
a=new Dog();
a.eat();
a=new Cat();
a.eat();
a=new Lion();
a.eat();
}
}

Experiment-9
1.
import java.io.*;
class Person
{
void display()
{
System.out.println("I am coming from sklm");
}
}
class Student extends Person
{
void show()
{
System.out.println("I am a student of GMRIT");
}
}
class SingleInheritanceExample
{
public static void main(String args[])
{
Student S = new Student();
S.display();
S.show();
}
}

2.

import java.io.*;
class Person
{
void showPerson()
{
System.out.println("I am resident of sklm");
}
}
class Student extends Person
{
void showStudent()
{
System.out.println("I am a student of GMRIT");
}
}
class Employee extends Student
{
void showEmployee()
{
System.out.println("I am an employee in GMRIT Rajam");
}
}
class MultilevelInheritance
{
public static void main(String args[])
{
Employee e=new Employee();
e.showPerson();
e.showStudent();
e.showEmployee();
}
}

3.

import java.io.*;
class Person
{
String name="LOKESH";
void showPerson()
{
System.out.println("Mr. "+name+" I am a resident of Sklm");
}
}
class Student extends Person
{
String id="473";
void showStudent()
{
System.out.println("Ms. "+name+" , ID: "+id+" I am a Student of GMRIT");
}
}
class Employee extends Person
{
void showEmployee()
{
System.out.println("I am an employee in GMRIT at Rajam");
}
}
class HierarchialInh
{
public static void main(String args[])
{
Employee e=new Employee();
e.showPerson();
e.showEmployee();
Student s=new Student();
s.showStudent();
}
}

4.

import java.io.*;
class A
{
void display()
{
System.out.println("Hello A");
}
}
class B
{
void display()
{
System.out.println("Hello B");
}
}
class Print extends A,B
//multiple inheritance cannot be achieved using classes in Java
{
void display()
{
System.out.println("Hello");
}
public static void main(String args[])
{
Print p=new Print();
p.display();
}
}

Experiment-10
import java.io.*;
interface Printable
{
void print();
}
interface Showable
{
void print();
}
class PrintC implements Printable,Showable
{
public void print()
{
System.out.println("Hello");
}
public static void main(String args[])
{
PrintC p=new PrintC();
p.print();
}
}

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()
{
System.out.println("student personal details");
System.out.println("name of the student is:"+name);
System.out.println("fathers name is:"+fn);
System.out.println("mothers name is:"+mn);
System.out.println("age is:"+age);
System.out.println("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()
{
System.out.println("student educational details:");
System.out.println("rollno of student is:"+rollno);
System.out.println("average marks of student is:"+avg);
System.out.println("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()
{
System.out.println("STAFF DETAILS: ");
System.out.println("Name of the member is: "+name);
System.out.println("Designation is: "+des);
System.out.println("Her position is: "+pos);
}
}

import first.*;
import second.*;
import third.*;
class Demo
{
public static void main(String args[])
{
first.Studentinfo si=new first.Studentinfo(19,'M',"J.Lokesh","Mr.butchi babu","Mrs.Bharathi","Srikakulam");
si.display();
second.Academicinfo ai=new second.Academicinfo(473,980,99);
ai.display();
third.Teacherinfo ti=new third.Teacherinfo("Ms.Bharathi","Faculty","Professor");
ti.display();
}
}

Experiment-12
Try-catch

import java.io.*;
class Exception
{
public static void main(String args[])
{
try
{
int data=50/0;
}
catch(ArithmeticException e)
{
System.out.println(e);
}
System.out.println("rest of the code");
}
}

Multi-catch

1.
import java.io.*;
import java.lang.Exception;
class Exception2
{
public static void main(String args[])
{
try
{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException e)
{
System.out.println("task1 is completed");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("task2 is completed");
}
catch(Exception e)
{
System.out.println("common task completed");
}
System.out.println("rest of the code");
}
}

2.

import java.io.*;
import java.lang.Exception;
class Exception3
{
public static void main(String args[])
{
try
{
int a[]=new int[5];
a[5]=30/0;
}
catch(Exception e)
{
System.out.println("common task completed");
}
catch(ArithmeticException e)
{
System.out.println("task1 is completed");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("task1 is completed");
}
System.out.println("rest of the code");
}
}

Nested-try

import java.lang.Throwable;
import java.lang.Exception;
import java.io.*;
class Exception4
{
public static void main(String args[])
{
try
{
try
{
System.out.println("going to divide");
int b=20/0;
}
catch(ArithmeticException e)
{
System.out.println(e);
}
try
{
int a[]=new int[5];
a[5]=4;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
System.out.println("other statement");
}
catch(Exception e)
{
System.out.println("handled");
}
System.out.println("normal flow");
}
}

b.

case1

import java.io.*;
class Testfinally1
{
public static void main (String args[])
{
try
{
int data = 25/5;
System.out.println(data);
}
catch (NullPointerException e)
{
System.out.println(e);
}
finally
{
System.out.println("Finally block is always executed");
}
System.out.println("rest of the code");
}
}

Case2

import java.io.*;
class Testfinally2
{
public static void main (String args[])
{
try
{
int data = 25/0;
System.out.println(data);
}
catch (NullPointerException e)
{
System.out.println(e);
}
finally
{
System.out.println("Finally block is always executed");
}
System.out.println("rest of the code");
}
}

Case3

import java.io.*;
class Testfinally
{
public static void main (String args[])
{
try
{
int data = 25/0;
System.out.println(data);
}
catch (ArithmeticException e)
{
System.out.println(e);
}
finally
{
System.out.println("Finally block is always executed");
}
System.out.println("rest of the code");
}
}

c.

import java.io.*;
public class Throw
{
static void validate(int age)
{
if(age<18)
throw new ArithmeticException("not valid");
else
System.out.println("welcome to vote");
}
public static void main(String args[])
{
validate(13);
System.out.println("rest of the code...");
}
}

d.

import java.lang.Throwable;
import java.lang.Exception;
import java.io.IOException;
class Testthrows
{
void m()throws IOException
{
throw new IOException("device error");
}
void n()throws IOException
{
m();
}
void p()
{
try
{
n();
}
catch(Exception e)
{
System.out.println("exception handled");
}
}
public static void main(String args[])
{
Testthrows obj=new Testthrows();
obj.p();
System.out.println("normal flow");
}
}

Case-1

import java.lang.Throwable;
import java.lang.Exception;
import java.io.*;
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();
m.method();
}
catch(Exception e)
{
System.out.println("exception handled");
}
System.out.println("normal flow");
}
}

Case-2

1.

import java.lang.Throwable;
import java.lang.Exception;
import java.io.*;
class M
{
void method()throws IOException
{
System.out.println("device operation performed");
}
}
class Throws3
{
public static void main(String args[])
{
try
{
M m=new M();
m.method();
}
catch(IOException e)
{
System.out.println(e);
}
System.out.println("normal flow");
}
}

2.
import java.io.*;
import java.util.*;
class M
{
void method()throws IOException
{
throw new IOException("device error");
}
}
class Throws4
{
public static void main(String args[])
{
try
{
M m=new M();
m.method();
}
catch(IOException e)
{
System.out.println(e);
}
System.out.println("rest of the code");
}
}

Experiment-13
1.

import java.lang.Throwable;
import java.lang.Exception;
import java.io.*;
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
{
System.out.println("Welcome to vote");
}
}
public static void main(String args[])
{
try
{
validate(13);
}
catch(InvalidAgeException ex)
{
System.out.println("caught the Exception");
System.out.println("Exception occured:"+ex);
}
System.out.println("Rest of the code");
}
}

2.
import java.lang.Throwable;
import java.lang.Exception;
import java.io.*;
class MyCustomException extends Exception
{

}
public class TestCustomException2
{
public static void main(String args[])
{
try
{
throw new MyCustomException();
}
catch(MyCustomException ex)
{
System.out.println("caught the exception");
System.out.println(ex.getMessage());
}
System.out.println("rest of the code");
}
}

Experiment-14

1.
import java.lang.Exception;
import java.io.*;
class Table
{
void printTable(int n)
{
for(int i=1;i<=5;i++)
{
System.out.println(n*i);
try
{
Thread.sleep(400);
}
catch(Exception e)
{
System.out.print(e);
}
}
}
}
class MyThread1 extends Thread
{
Table t;
MyThread1(Table t)
{
this.t=t;
}
public void run()
{
t.printTable(5);
}
}
class MyThread2 extends Thread
{
Table t;
MyThread2(Table t)
{
this.t=t;
}
public void run()
{
t.printTable(100);
}
}
class TestSynchronization1
{
public static void main(String args[])
{
Table obj=new Table();
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();
}
}
2.
import java.lang.Exception;
import java.io.*;
class Table
{
synchronized void printTable(int n)
{
for(int i=1;i<=5;i++)
{
System.out.println(n*i);
try
{
Thread.sleep(400);
}
catch(Exception e)
{
System.out.print(e);
}
}
}
}
class MyThread1 extends Thread
{
Table t;
MyThread1(Table t)
{
this.t=t;
}
public void run()
{
t.printTable(5);
}
}
class MyThread2 extends Thread
{
Table t;
MyThread2(Table t)
{
this.t=t;
}
public void run()
{
t.printTable(100);
}
}
class TestSynchronization2
{
public static void main(String args[])
{
Table obj=new Table();
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();
}
}

3.
import java.lang.Exception;
import java.io.*;
class Customer
{
int amount=10000;
synchronized void withdraw(int amount)
{
System.out.println("Going to withdraw");
if(this.amount<amount)
{
System.out.println("Less balance:Waiting for deposit");
try
{
wait();
}
catch(Exception e)
{
}
}
this.amount-=amount;
System.out.println("Withdraw completed");
}
synchronized void deposit(int amount)
{
System.out.println("Going to deposit");
this.amount+=amount;
System.out.println("Deposit completed");
notify();
}
}
class Test100
{
public static void main(String args[])
{
final Customer c=new Customer();
new Thread()
{
public void run()
{
c.withdraw(15000);
}
}.start();
new Thread()
{
public void run()
{
c.deposit(10000);
}
}.start();
}
}

You might also like