PRACTICAL:-1
AIM: Write a program to print hello.
public class hello {
public static void main(String[] args) {
[Link]("hello");
}
}
OUTPUT:
1
PRACTICAL:-2
AIM: Write a program of implementation of Class and object.
1. import [Link];
public class Result {
public static void main(String[] args) {
int m,e,s,total,avg;
Scanner sc=new Scanner([Link]);
[Link]("Enter Math Marks=");
m=[Link]();
[Link]("Enter English Marks=");
e=[Link]();
[Link]("Enter Science Marks=");
s=[Link]();
total=m+e+s;
[Link]("Total Marks="+total);
avg=total/3;
[Link]("Average Marks="+avg);
}
}
OUTPUT:
2
2. import [Link];
public class Scholarship {
public static void main(String[] args) {
int choice=0;
int om,tm,per=0;
[Link]("what's your qualification");
[Link]("[Link] Medical [Link] [Link]");
[Link]("Enter your choice"+choice);
Scanner sc=new Scanner([Link]);
choice=[Link]();
if(choice==1)
{
[Link]("Cograts you are eligible for btech if you are pass");
[Link]("enter obtain marks");
om=[Link]();
[Link]("enter total marks");
tm=[Link]();
per=(om*100)/tm;
[Link]("percentage="+per);
if(per>=60 && per<=70)
{
[Link]("congrats you are eligible for 70% scholarship");
}
else
if(per>=70 && per<=80)
{
[Link]("congrats you are eligible for 80% scholarship");
}
else
if(per>=80 && per<=90)
{
[Link]("congrats you are eligible for 90% scholarship");
}
else
if(per>=90)
{
[Link]("congrats you are eligible for 100% scholarship");
}
}
if(choice==2)
{
[Link]("Cograts you are eligible for medical if you are pass");
[Link]("enter obtain marks");
om=[Link]();
[Link]("enter total marks");
tm=[Link]();
3
per=(om*100)/tm;
[Link]("percentage="+per);
if(per>=60 && per<=70)
{
[Link]("congrats you are eligible for 70% scholarship");
}
else
if(per>=70 && per<=80)
{
[Link]("congrats you are eligible for 80% scholarship");
}
else
if(per>=80 && per<=90)
{
[Link]("congrats you are eligible for 90% scholarship");
}
else
if(per>=90)
{
[Link]("congrats you are eligible for 100% scholarship");
}
}
if(choice==3)
{
[Link]("Cograts you are eligible for graduation if you are pass");
[Link]("enter obtain marks");
om=[Link]();
[Link]("enter total marks");
tm=[Link]();
per=(om*100)/tm;
[Link]("percentage="+per);
if(per>=60 && per<=70)
{
[Link]("congrats you are eligible for 70% scholarship");
}
else
if(per>=70 && per<=80)
{
[Link]("congrats you are eligible for 80% scholarship");
}
else
if(per>=80 && per<=90)
{
[Link]("congrats you are eligible for 90% scholarship");
}
else
4
if(per>=90)
{
[Link]("congrats you are eligible for 100% scholarship");
}
}
}
}
OUTPUT:
5
PRACTICAL:-3
AIM: Write a program of implementation of inheritance.
1. SINGLE INHERITANCE
public class Father {
void family(){
[Link]("I am in Father");
}
}
public class Son extends Father {
void family(){
[Link]("I am in Son");
}
}
public class Family {
public static void main(String[] args) {
Father f=new Father();
[Link]();
Son s=new Son();
[Link]();
}
}
OUTPUT:
6
2. MULTILEVEL INHERITANCE
public class GrandFather {
void family(){
[Link]("I am in Grand Father");
}
}
public class Father extends GrandFather {
void family(){
[Link]("I am in Father");
}
}
public class Son extends Father {
void family(){
[Link]("I am in Son");
}
}
public class Family {
public static void main(String[] args) {
GrandFather gf=new GrandFather();
[Link]();
Father f=new Father();
[Link]();
Son s=new Son();
[Link]();
}
}
OUTPUT:
7
3. HIERARCHICAL INHERITANCE
public class GrandFather {
void family(){
[Link]("I am in Grand Father");
}
}
public class Father extends GrandFather {
void family(){
[Link]("I am in Father");
}
}
public class Son extends Father {
void family(){
[Link]("I am in Son");
}
}
public class Family {
public static void main(String[] args) {
Father f=new Father();
[Link]();
Son s=new Son();
[Link]();
GrandFather gf=new GrandFather();
[Link]();
}
}
OUTPUT:
8
PRACTICAL:-4
AIM: Write a program of implementation of packages and interfaces.
package mimit;
interface Bank {
float rateofinterest();
}
package mimit;
public class SBI implements Bank {
public float rateofinterest(){
return(10.5f);
}
}
package mimit;
public class PNB implements Bank{
public float rateofinterest(){
return(12.5f);
}
}
package mimit;
public class InterfaceTest {
public static void main(String[] args) {
Bank b=new SBI();
[Link]("Rate of interest="+[Link]());
PNB p=new PNB();
[Link]("Rate of interest="+[Link]());
}
}
OUTPUT:
9
PRACTICAL:-5
AIM: Write a program of implementation of Threads.
import [Link].*;
public class A extends Thread
{
public void run()
{
for (int i=1; i<=5; i++)
{
[Link]("\n thread A i="+i);
}
[Link]("exit from thread A");
}
}
import [Link].*;
public class B extends Thread{
public void start()
{
for (int j=1; j<=5; j++)
{
[Link]("\n thread B j="+j);
}
[Link]("exit from thread B");
}
}
import [Link].*;
public class C extends Thread{
public void run()
{
for (int k=1; k<=5; k++)
{
[Link]("\n thread C k="+k);
}
[Link]("exit from thread C");
}
}
import [Link].*;
public class TestThread {
public static void main(String[] args) {
new A().start();
10
new B().start();
new C().start();
}
}
OUTPUT:
11
PRACTICAL:-6
AIM: Write a program of implementation of Applet.
JAVA File
import [Link].*;
import [Link].*;
public class HelloJava extends Applet{
public void paint(Graphics g)
{
[Link](10, 60, 40, 30);
[Link](10, 10, 50, 50);
[Link](60, 10, 30,80 );
[Link](10, 100, 80, 50, 10, 10);
[Link](20, 110, 60, 40, 5, 5);
}
}
HTML File
<html>
<head>
<title>my page</title>
</head>
<body>
<p>My Applet</p>
<APPLET CODE="[Link]" height=400 width=200>
</APPLET>
OUTPUT:
12
PRACTICAL:-7
AIM: Write a program to implement the mouse events and keyboard events.
Mouse event
import [Link].*;
import [Link].*;
public class FrameMouseAdapterExample extends Frame
{
public FrameMouseAdapterExample()
{
// adding MouseAdapter to frame
PleaseHandle ph = new PleaseHandle();
addMouseListener(ph);
setSize(300, 300);
setVisible(true);
}
public static void main(String args[])
{
new FrameMouseAdapterExample();
}
}
class PleaseHandle extends MouseAdapter // second class
{
public void mouseClicked(MouseEvent e)
{
13
[Link]("You clicked mouse at coordinates " + [Link]() + ", " + [Link]());
}
}
Output:
14
Keyboard event
import [Link];
import [Link];
import [Link];
import [Link];
public class AppletKeyListener extends Applet implements KeyListener
{
char ch;
String str;
public void init() // link the KeyListener with Applet
{
addKeyListener(this);
}
// override all the 3 abstract methods of KeyListener interface
public void keyPressed(KeyEvent e) { }
public void keyReleased(KeyEvent e) { }
public void keyTyped(KeyEvent e)
{
ch = [Link]();
if(ch == 'a')
str = "a for apple";
else if(ch == 'e')
str = "e for elephant";
else if(ch == 'i')
str = "i for igloo";
else if(ch == 'o')
str = "o for ox";
15
else if(ch == 'u')
str = "u for umbrella";
else
str = "Type only vowels a, e, i, o, u only";
repaint();
}
public void paint(Graphics g)
{
[Link](str, 100, 150);
showStatus("You typed " + ch + " character");
}
}
Output:
16
PRACTICAL :- 8
AIM: Write a program to implement basic file reading and writing methods.
File Writer
package [Link];
import [Link];
public class FileWriterExample {
public static void main(String args[]){
try{
FileWriter fw=new FileWriter("D:\\[Link]");
[Link]("Welcome to javaTpoint.");
[Link]();
}catch(Exception e){[Link](e);}
[Link]("Success...");
}
}
Output:
17
File Reader
package [Link];
import [Link];
public class FileReaderExample {
public static void main(String args[])throws Exception{
FileReader fr=new FileReader("D:\\[Link]");
int i;
while((i=[Link]())!=-1)
[Link]((char)i);
[Link]();
}
}
Output:
18
PRACTICAL:- 9
AIM: Write a program using basic networking features.
1. Client to server communication (one-way)
Server Program – [Link]
import [Link];
import [Link];
import [Link];
import [Link];
public class WishesServer
{
public static void main(String args[]) throws Exception
{
ServerSocket sersock = new ServerSocket(5000);
[Link]("server is ready"); // message to know the server is running
Socket sock = [Link]();
InputStream istream = [Link]();
DataInputStream dstream = new DataInputStream(istream);
String message2 = [Link]();
[Link](message2);
dstream .close(); [Link](); [Link](); [Link]();
}
}
19
Client Program – [Link]
import [Link];
import [Link];
import [Link];
public class WishesClient
{
public static void main(String args[]) throws Exception
{
Socket sock = new Socket("[Link]", 5000);
String message1 = "Accept Best Wishes, Serverji";
OutputStream ostream = [Link]();
DataOutputStream dos = new DataOutputStream(ostream);
[Link](message1);
[Link]();
[Link]();
[Link]();
}
}
20
21
2. Server to client communication (one-way)
Server Program – [Link]
import [Link].*;
import [Link].*;
public class DateInfoServer
{
public static void main(String args[]) throws Exception
{
ServerSocket sersock = new ServerSocket(7000);
[Link]("Server ready........");
Socket sock = [Link]( );
OutputStream ostream = [Link]();
BufferedWriter bw1 = new BufferedWriter(new OutputStreamWriter(ostream));
String s2 = "Thanks client for your calling on " + new [Link]();
[Link](s2);
[Link](); [Link](); [Link](); [Link]( );
}
}
Client Program – [Link]
import [Link].*;
import [Link].*;
public class DateInfoClient
22
{
public static void main(String args[]) throws Exception
{
Socket sock = new Socket("[Link]", 7000);
InputStream istream = [Link]();
BufferedReader br1 = new BufferedReader(new InputStreamReader(istream));
String s1 = [Link]();
[Link](s1);
[Link](); [Link](); [Link]( );
}
}
23
3. Server sends file contents to client (two-way, non-continuous)
Server program: [Link]
import [Link].*;
import [Link].*;
public class ContentsServer
{
public static void main(String args[]) throws Exception
{ // establishing the connection with the server
ServerSocket sersock = new ServerSocket(4000);
[Link]("Server ready for connection");
Socket sock = [Link](); // binding with port: 4000
[Link]("Connection is successful and wating for chatting");
// reading the file name from client
InputStream istream = [Link]( );
BufferedReader fileRead =new BufferedReader(new InputStreamReader(istream));
String fname = [Link]( );
// reading file contents
BufferedReader contentRead = new BufferedReader(new FileReader(fname) );
// keeping output stream ready to send the contents
OutputStream ostream = [Link]( );
PrintWriter pwrite = new PrintWriter(ostream, true);
String str;
while((str = [Link]()) != null) // reading line-by-line from file
{
[Link](str); // sending each line to client
24
}
[Link](); [Link](); // closing network sockets
[Link](); [Link](); [Link]();
}
}
Client program: [Link]
import [Link].*;
import [Link].*;
public class ContentsClient
{
public static void main( String args[ ] ) throws Exception
{
Socket sock = new Socket( "[Link]", 4000);
// reading the file name from keyboard. Uses input stream
[Link]("Enter the file name");
BufferedReader keyRead = new BufferedReader(new InputStreamReader([Link]));
String fname = [Link]();
// sending the file name to server. Uses PrintWriter
OutputStream ostream = [Link]( );
PrintWriter pwrite = new PrintWriter(ostream, true);
[Link](fname);
// receiving the contents from server. Uses input stream
25
InputStream istream = [Link]();
BufferedReader socketRead = new BufferedReader(new InputStreamReader(istream));
String str;
while((str = [Link]()) != null) // reading line-by-line
{
[Link](str);
}
[Link](); [Link](); [Link]();
}
}
26
4. Chat program (two-way, continuous)
Server program: [Link]
import [Link].*;
import [Link].*;
public class GossipServer
{
public static void main(String[] args) throws Exception
{
ServerSocket sersock = new ServerSocket(3000);
[Link]("Server ready for chatting");
Socket sock = [Link]( );
// reading from keyboard (keyRead object)
BufferedReader keyRead = new BufferedReader(new InputStreamReader([Link]));
// sending to client (pwrite object)
OutputStream ostream = [Link]();
PrintWriter pwrite = new PrintWriter(ostream, true);
// receiving from server ( receiveRead object)
InputStream istream = [Link]();
BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
String receiveMessage, sendMessage;
while(true)
{
if((receiveMessage = [Link]()) != null)
{
[Link](receiveMessage);
}
27
sendMessage = [Link]();
[Link](sendMessage);
[Link]();
}
}
}
Client program: [Link]
import [Link].*;
import [Link].*;
public class GossipClient
{
public static void main(String[] args) throws Exception
{
Socket sock = new Socket("[Link]", 3000);
// reading from keyboard (keyRead object)
BufferedReader keyRead = new BufferedReader(new InputStreamReader([Link]));
// sending to client (pwrite object)
OutputStream ostream = [Link]();
PrintWriter pwrite = new PrintWriter(ostream, true);
// receiving from server ( receiveRead object)
InputStream istream = [Link]();
BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
28
[Link]("Start the chitchat, type and press Enter key");
String receiveMessage, sendMessage;
while(true)
{
sendMessage = [Link](); // keyboard reading
[Link](sendMessage); // sending to server
[Link](); // flush the data
if((receiveMessage = [Link]()) != null) //receive from server
{
[Link](receiveMessage); // displaying at DOS prompt
}
}
}
}
29
PRACTICAL:- 10
AIM: Write a program to connect to Database using JDBC.
//first create a table in oracle database.
create table emp(id number(10),name varchar2(40),age number);
// program to read data from emp table
package project;
import [Link].*;
public class OracleCon {
public static void main(String[] args) throws Exception
//load the driver class
[Link]("[Link]");
//create the connection object
Connection
cn=[Link]("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
//create the statement object
Statement st=[Link]();
//execute qwery
ResultSet rs=[Link]("select * from emp");
while([Link]())
{
int e=[Link](1);
String en=[Link](2);
int e1=[Link](3);
[Link](e+""+en+""+e1);
}
}
}
30
OUTPUT:
31
PRACTICAL:- 11
AIM: Write a program of type conversion.
1. Widening or Automatic type conversion.
public class Implicit
{
public static void main(String[] args)
{
int i = 100;
long l = i; //no explicit type casting required
float f = l; //no explicit type casting required
[Link]("Int value "+i);
[Link]("Long value "+l);
[Link]("Float value "+f);
}
}
OUTPUT:
32
2. Narrowing or Explicit type conversion.
public class Explicit
{
public static void main(String[] args)
{
double d = 100.04;
long l = (long)d; //explicit type casting required
int i = (int)l; //explicit type casting required
[Link]("Double value "+d);
[Link]("Long value "+l);
[Link]("Int value "+i);
}
}
OUTPUT:
33
PRACTICAL:-12
AIM: Write a program of control statement.
1. If Else Statement:
class IfElseTest
{
public static void main(String[] args)
{
int num=5;
if(num==0)
[Link]("It is Zero");
else if(num>0)
[Link]("It is +ve");
else
[Link]("It is -ve");
}
}
OUTPUT:
34
2. Do-While loop:
class DoWhileTest
{
public static void main(String[] args)
{
int x=1;
do
{
[Link](x+"\t");
x++;
}
while (x<=10);
}
}
OUTPUT:
35
3. While loop:
class WhileTest
{
public static void main(String[] args)
{
int x=2;
while (x<=10)
{
[Link](x+"\t");
x+=2;
}
}
}
OUTPUT:
36
4. For loop:
class ForTest
{
public static void main(String[] args)
{
for(int i=0;i<=100;i++)
{
[Link](i+"\t");
}
}
}
OUTPUT:
37
5. For each loop:
class ForEachTest
{
public static void main(String[] args)
{
int arr[]={10,33,56,20,90};
for(int x:arr)
{
[Link](x+"\t");
}
}
}
OUTPUT:
38
6. Switch statement:
class SwitchTest
{
public static void main(String[] args)
{
char color='g';
switch(color)
{
case 'r':
case 'R':
[Link]("Red");
break;
case 'g':
case 'G':
[Link]("Green");
break;
case 'b':
case 'B':
[Link]("Blue");
break;
case 'w':
case 'W':
[Link]("White");
break;
default:
[Link]("No color");
}
}
39
}
OUTPUT:
40
7. Break statement:
class BreakTest
{
public static void main(String args[])
{
for(int i=0;i<=10;i++)
{
if(i==4)
break;
[Link](i);
}
[Link]("break executed");
}
}
OUTPUT:
41
8. Continue statement:
class ContinueStatementTest
{
public static void main(String[] args)
{
for(int i=10;i>=1;i--)
{
if(i>5)
continue;
[Link](i+"\t");
}
}
}
OUTPUT:
42
9. Return statements:
class ReturnTest
{
public static void main(String[] args)
{
int num=1;
[Link]("Before return");
if(num==1)
return; // [Link](0);
[Link]("After return");
}
}
OUTPUT:
43
PRACTICAL:- 13
AIM: Declaring variables of various data types and their effect by changing the
access modifiers like private, public, protected, default.
1. Private access modifier:
public class AccessPrivatemodifiers
{
String text="text";
public String getText()
{
return text;
}
public static void main(String[] args)
{
subAccessmodifiers accessmodifiers=new subAccessmodifiers();
//error (access of private member)
[Link]("public instance variables : "+[Link]);
[Link]("public functions : "+[Link]());
}
}
class subAccessmodifiers extends AccessPrivatemodifiers
{
public String subGetText()
{
//error (access of private member)
return text;
}
}
44
OUTPUT:
45
2. Public access modifier:
public class AccessPublicmodifiers
{
public String text="text";
public String getText()
{
return text;
}
public static void main(String[] args)
{
AccessPublicmodifiers accessmodifiers=new AccessPublicmodifiers();
[Link]("public instance variables : "+[Link]);
[Link]("public functions : "+[Link]());
}
}
OUTPUT:
46
3. Protected access modifier:
public class AccessProtectedmodifiers
{
protected String text="text";
public String getText()
{
return text;
}
public static void main(String[] args)
{
subAccessmodifiers accessmodifiers=new subAccessmodifiers();
//work with no error
[Link]("public instance variables : "+[Link]);
[Link]("public functions : "+[Link]());
}
}
class subAccessmodifiers extends AccessProtectedmodifiers
{
public String subGetText()
{
//work with no error
return text;
}
}
OUTPUT:
47
4. Default access modifier:
public class AccessDefaultmodifiers
{
String text="text";//Default access modifier
public String getText()
{
return text;
}
public static void main(String[] args)
{
subAccessmodifiers accessmodifiers=new subAccessmodifiers();
//Can be accessed
[Link]("public instance variables:"+[Link]);
[Link]("public functions : "+[Link]()); }
}
class subAccessmodifiers extends AccessDefaultmodifiers
{
public String subGetText()
{
//Can be accessed ,
return text;
}
}
OUTPUT:
48
PRACTICAL:- 14
AIM: Usage of Java keywords final at appropriate places in java programs.
final class C1
{
void disp()
{
[Link]("Inside disp method");
}
}
class FinalClassDemo extends C1
{
public static void main(String s[])
{
new FinalClassDemo();
}
}
OUTPUT:
49
PRACTICAL:- 15
AIM: Write a program of different Operators.
Arithematic operator:
class AOperators
{
int add(int a,int b)
{
return a+b;
}
int sub(int a,int b)
{
return a-b;
}
int multi(int a,int b)
{
return a*b;
}
int divide(int a,int b)
{
return a/b;
}
int mod(int a,int b)
{
return a%b;
}
}
class AOperatorsMain
{
50
public static void main(String s[])
{
AOperators obj=new AOperators();
[Link]("Addition is:"+[Link](24,24));
[Link]("Subtraction is:"+[Link](45,34));
[Link]("Multiplication is:"+[Link](12,12));
[Link]("Division is:"+[Link](45,9));
[Link]("Modulus is:"+[Link](3,2));
}
}
OUTPUT:
51
Logical Operator:
class LOperators
{
void leap()
{
int y=2020;
if(y%4==0 && y%100!=0 || y%400==0)
[Link]("Leap year");
else
[Link]("Not a Leap year");
}
}
class LOperatorsMain
{
public static void main(String s[])
{
LOperators obj=new LOperators();
[Link]();
}
}
OUTPUT:
52
Conditional Operator:
class COperatorsMain
{
public static void main(String s[])
{
int i,k;
i=10;
k=i<0?-i:i;
[Link]("Absolute Value of"+i+"is"+k);
i=-10;
k=i<0?-i:i;;
[Link]("Absolute Value of"+i+"is"+k);
}
}
OUTPUT:
53
Bitwise Operator:
class BitwiseOR
{
public static void main(String args[])
{
[Link](" | OR opeartor");
int x = 0 | 0;
[Link]("0 | 0 = " + x);
}
}
OUTPUT:
54