0% found this document useful (0 votes)
3 views

java programs

The document contains several Java programming examples demonstrating key concepts such as classes, inheritance, interfaces, packages, exceptions, and multithreading. Each example includes code snippets along with their expected outputs, illustrating how to perform operations like addition, calculate student marks, implement shapes, handle exceptions, and manage threads. The examples serve as practical demonstrations for understanding Java programming fundamentals.

Uploaded by

lovelykavin598
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

java programs

The document contains several Java programming examples demonstrating key concepts such as classes, inheritance, interfaces, packages, exceptions, and multithreading. Each example includes code snippets along with their expected outputs, illustrating how to perform operations like addition, calculate student marks, implement shapes, handle exceptions, and manage threads. The examples serve as practical demonstrations for understanding Java programming fundamentals.

Uploaded by

lovelykavin598
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

EX:NO=1 Objects And Class

import java.io.*;
class addition
{
int a,b,sum=0;
public void add()
{
a=5;
b=3;
sum=a+b;
System.out.println("value of sum:"+sum);
}
}
class textadd
{
public static void main(String args[])
{
addition a=new addition();
a.add();
}
}

OUTPUT: value of sum: 8

EX.NO:2 Inheritance
import java.io.*;
class student
{
int regno;
String name;
int age;
public void details()
{
regno=12345;
name="abi";
age=20;
}
}
class mark extends student
{
int m1,m2,m3,total;
float avg;
public void calculate()
{
m1=71;
m2=83;
m3=64;
total=m1+m2+m3;
avg=total/3;
}
public void display()
{
System.out.println("Name:"+name);
System.out.println("Regno:"+regno);
System.out.println("Age:"+age);
System.out.println("Total:"+total);
System.out.println("Average:"+avg);
}
}
class inherit
{
public static void main(String arge[])
{
mark mk=new mark();
mk.details();
mk.calculate();
mk.display();
}
}

OUTPUT:
Name : Abi
Rego :12345
Age: 20
Total: 218
Average:72.67

EX.NO:3 Interface
import java.io.*;
interface shape
{
void area();
}
class rect implements shape
{
int l,b,A;
public void area()
{
l=5;
b=3;
A=l*b;
System.out.println("Area of Rectngle"+A);
}
}
class square implements shape
{
int r,A;
public void area()
{
r=3;
A=r*r;
System.out.println("Area of square"+A);
}
}
class intershape
{
public static void main(String args[])
{
rect R=new rect();
square S=new square();
R.area();
S.area();
}
}

OUTPUT:
Area of Rectngle : 15
Area of square : 9

EX.NO:4 Package

package arith;
public class arithpac
{
public void add()
{
int a,b;
a=5;
b=6;
System.out.println("addition:"+(a+b));
}
public void sub()
{
int a,b;
a=15;
b=6;
System.out.println("subraction:"+(a-b));
}
}

import arith.*;
import java.io.*;
class packtxt
{
public static void main(String args[])
{
arithpac ag=new arithpac();
ag.add();
ag.sub();
}
}
OUTPUT:
bin> javac arith/arithpack.java
bin>javac packtest.java
bin>java packtest
Addition : 11
Subtraction : 9

EX.NO:5.1 Build in Exception


import java.io.*;
import java.lang.Exception;
class exception3
{
public static void main(String args[])
{
try
{
try
{
int[]arr={1,2,3};
int a=arr[1]+arr[3];
}
catch(ArrayIndexOutOfBoundsException e2)
{
System.out.println("Array Index Out of Bounds Exception");
}
try
{
int b=5/0;
}
catch(ArithmeticException e1)
{
System.out.println("Divided by zero exception");
}
int i[]=null;
int j=i[1];
}
catch(NullPointerException e3)
{
System.out.println("null pointer exception");
}
finally
{
System.out.println("program Ends with finally block");
}
}
}
OUTPUT:
Array Index out of Bounds Exception
Divided by zero Exception
Null pointer Exception
Program Ends with finally block

EX.NO:5.2 User defined Exception


import java.io.*;
import java.lang.Exception;
class AgeException extends Exception
{
public AgeException()
{
System.out.println("Age is not Valid to vote");
}
}
class userexcep
{
public static void main(String arg[])throws AgeException
{
try
{
int age=17;
if(age<18)
{ throw new AgeException(); }
else
{ System.out.println("valid to vote");}
}
catch(AgeException ae)
{
System.out.println("Age is under 18:"+ae);
}
System.out.println("Validation Completed");
}
}

OUTPUT:
Age is not valid to vote
Age is under 18: Age Exception
Validation Completed

EX.No:6 Multithreading
import java.io.*;
class thread1 extends Thread
{
public void run()
{
for (int i=1;i<5;i++)
{
System.out.println("I am T1");
}
}
}
class thread2 implements Runnable
{
public void run()
{
for (int i=1;i<5;i++)
{
System.out.println("I am T2");
}
}
}
class threadtest
{
public static void main(String arg[])
{
thread1 t1=new thread1();
t1.start();
thread2 ru=new thread2();
Thread t2=new Thread (ru);
t2.start();
try
{
System.out.println("Suspend Thread1");
t1.suspend();
t2.sleep(100);
System.out.println("Resume Thread1");
t1.resume();
}
catch(InterruptedException e)
{}
}
}
OUTPUT:
Suspend Thread1
I am T2
I am T2
I am T2
I am T2
Resume Thread1
I am T1
I am T1
I am T1
I am T1

You might also like