Java 2 Sycs 13
Java 2 Sycs 13
PRACTICAL JOURNAL
SYCS
Class: _____________ 3
Sem: _____ Date of Performance: ___________
Introduction to Java Programming
Course Name:_________________________ BH.USCSP302
Course Number: __________
Practical Number: _______ Page Number: _______
Aim:
Practical -6
Demonstrate the use this keyword, static keyword and abstract classes
1)Write a java program to demonstrate the working of this keyword.
CODE:
class Emp{
String name,eid;
Emp(String name,String eid){
this.name=name;
this.eid=eid;
}
String retName(){
return name;
}
String retEid(){
return eid;
}
}
class EmpData{
public static void main(String arr[]){
Emp e1=new Emp("Anu","SYCS23");
System.out.println("Employee Details \nName: "+e1.retName());
System.out.println("ID: "+e1.retEid());
}
}
OUTPUT:
C:\Users\LAB01-PC22\Documents\Java>javac EmpData.java
C:\Users\LAB01-PC22\Documents\Java>java EmpData
Employee Details
Name: Anu
ID: SYCS23
Aim:
C:\Users\LAB01-PC22\Documents\Java>java StaticDemo
I am the static block...called/executed before main()
Called via obj name 300
num1=100num2= 200
Called via class name num1=900
Called via class name num2=100
Called via class name result addtion=1000
C:\Users\LAB01-PC22\Documents\Java>java AbstractDemo
Regular Defined method inside abstract class...
Abstract method ab_method() defined in child clas
Page 2 of 25
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
SYCS
Class: _____________ 3
Sem: _____ Date of Performance: ___________
Introduction to Java Programming
Course Name:_________________________ BH.USCSP302
Course Number: __________
Practical Number: _______ Page Number: _______
Aim:
PRACTICAL-07
Packages and Interfaces
• Package
• Interface
• Package and Interfaces
Q1. Write a Java program to demonstrate the concept of user defined packages.
Page 3 of 25
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
SYCS
Class: _____________ 3
Sem: _____ Date of Performance: ___________
Introduction to Java Programming
Course Name:_________________________ BH.USCSP302
Course Number: __________
Practical Number: _______ Page Number: _______
Aim:
C:\Users\LAB1\Desktop\java>java PackDemo
Package class A is imported
Package class B is imported
}
CLASS InterFaceDemo1.java
class InterFaceDemo1 implements Callback{
public void callme(int p){
if(p>MAX)
System.out.println("p value is more than MAX "+MAX);
}
void myMethod(){
System.out.println("The classes can have their own methods other than
implemented methods");
}
public static void main(String arg[]){
InterFaceDemo1 d1=new InterFaceDemo1();
d1.callme(108);
d1.myMethod();
}
}
OUTPUT:
//Compiling the interface
C:\Users\LAB1\Desktop\java>javac Callback.java
Page 4 of 25
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
SYCS
Class: _____________ 3
Sem: _____ Date of Performance: ___________
Introduction to Java Programming
Course Name:_________________________ BH.USCSP302
Course Number: __________
Practical Number: _______ Page Number: _______
Aim:
C:\Users\LAB1\Desktop\java>javac InterFaceDemo1.java
C:\Users\LAB1\Desktop\java>java InterFaceDemo1
p value is more than MAX 100
The classes can have their own methods other than implemented methods
Q.3 Write a java program to find out the factorial of given number by using concept of
recursive function
package sycspack;
public class FactImp implements FactDemo{
Code:
Inside java//sycspack folder
a) Package sycspack with interface FactDemo
package sycspack;
}
b)Package sycspack with class implementing the above interface
package sycspack;
Page 5 of 25
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
SYCS
Class: _____________ 3
Sem: _____ Date of Performance: ___________
Introduction to Java Programming
Course Name:_________________________ BH.USCSP302
Course Number: __________
Practical Number: _______ Page Number: _______
Aim:
else if(x<0){
return -1;
}
else{
return x*factorial(x-1);
}
}
}
//OUTSIDE sycspack folder package
c)class using the class and interface of package
import sycspack.*;
class CallFact{
public static void main(String arg[]){
int x=Integer.parseInt(arg[0]);
FactImp f=new FactImp();
int result=f.factorial(x);
if(result!=-1){
System.out.println("Factorial of "+ x +" is "+result);
}
else
System.out.println("Factorial of negative number is not possible");
}
}
OUTPUT:
C:\Users\Rag\Desktop\java\New folder\p6>set path="C:\Users\Rag\Downloads\jdk-
20_windows-x64_bin\jdk-20.0.1\bin"
C:\Users\Rag\Desktop\java\New folder\p6\sycspack>cd..
Page 6 of 25
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
SYCS
Class: _____________ 3
Sem: _____ Date of Performance: ___________
Introduction to Java Programming
Course Name:_________________________ BH.USCSP302
Course Number: __________
Practical Number: _______ Page Number: _______
Aim:
Practical-08
Strings
Q1. Write a java program to get data from user using BufferedReader class
(4th method to get input data from user)
CODE:
import java.io.*; // io means inputoutput package
class ConsoleInput{
public static void main(String args[]) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str[]=new String[100];
System.out.println("Enter the lines of code....");
System.out.println("Enter 'stop' to quit...");
for(int i=0;i<100;++i){
str[i]=br.readLine();
if(str[i].equals("stop"))
break;
}
System.out.println("\n\n Here is your Content .....\n\n");
for(int i=0;i<100;++i){
if(str[i].equals("stop"))
break;
System.out.println(str[i]);
}
}
}
OUTPUT:
C:\Users\LAB1\Desktop\java2>javac ConsoleInput.java
C:\Users\LAB1\Desktop\java2>java ConsoleInput
Enter the lines of code....
Enter 'stop' to quit...
hi
hello
good
morning
stop
hi
Page 7 of 25
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
SYCS
Class: _____________ 3
Sem: _____ Date of Performance: ___________
Introduction to Java Programming
Course Name:_________________________ BH.USCSP302
Course Number: __________
Practical Number: _______ Page Number: _______
Aim:
hello
good
morning
Q.2 Write a java program to demonstrate the working of String and StringBuffer class
methods .
CODE:
class StringDemo{
public static void main(String args[]){
String s1 = "Bhavans college";
String s2 = " RJ ";
String s3 ="Bhavans college";
System.out.println("\nStrings before using methods of String Class" );
System.out.println("s1="+s1+" s2="+s2+" s3="+s3);
System.out.println("\n..........Using Methods of String Class..........");
System.out.println("1 .Char at 4 index is :"+s1.charAt(4));
System.out.println("2 .Length of string is:"+s1.length());
System.out.println("3 .Comparing string s1 and s3 : "+s1.compareTo(s3));
System.out.println("4 .Lower Case :"+s1.toLowerCase());
System.out.println("5 .Upper Case :"+s1.toUpperCase());
System.out.println("6 .Value of string is :"+s1.toString());
System.out.println("8 .Index at char 'B' :"+s1.indexOf("Bha"));
System.out.println("9 .Concatenating s1 to s2 :"+s1.concat(s2));
System.out.println("10 .Using trim :"+s2.trim());
System.out.println("11.After Replace:"+s1.replace("Bhavans",s2));
System.out.println("12.Substring from index 0 to 7 :"+s1.substring(1,8));
System.out.println("13.Is string s1 empty?:"+s1.isEmpty());
System.out.println("14.Is string s1 and s2 equal?:"+s1.equals(s2));
System.out.println("15.Ignoring case, comparing :"+s1.equalsIgnoreCase(s2));
System.out.println("16.String joined :"+String.join("@",s1,s2));
System.out.println("\nStrings after using methods of String Class : no change in
strings" );
System.out.println("s1="+s1+" s2="+s2+" s3="+s3);
Page 8 of 25
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
SYCS
Class: _____________ 3
Sem: _____ Date of Performance: ___________
Introduction to Java Programming
Course Name:_________________________ BH.USCSP302
Course Number: __________
Practical Number: _______ Page Number: _______
Aim:
C:\Users\Rag\Desktop\java\p11>java StringDemo
Page 9 of 25
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
SYCS
Class: _____________ 3
Sem: _____ Date of Performance: ___________
Introduction to Java Programming
Course Name:_________________________ BH.USCSP302
Course Number: __________
Practical Number: _______ Page Number: _______
Aim:
Page 10 of 25
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
SYCS
Class: _____________ 3
Sem: _____ Date of Performance: ___________
Introduction to Java Programming
Course Name:_________________________ BH.USCSP302
Course Number: __________
Practical Number: _______ Page Number: _______
Aim:
Q3) Write a java program to demonstrate the working of string tokenizer class methods
CODE 1:
import java.util.StringTokenizer;
public class StringToken{
public static void main(String arg[]){
StringTokenizer st=new StringTokenizer("i am bhavanite");
System.out.println("Total Token are ..."+st.countTokens());
while(st.hasMoreTokens()){
System.out.println(st.nextToken());
}
}
}
OUTPUT: By default, delimiter is space.
C:\Users\LAB01PC16\Desktop\SYCS36>javac StringToken.java
C:\Users\LAB01PC16\Desktop\SYCS36>java StringToken
Total Token are ...3
i
am
bhavanite
CODE 2:
import java.util.StringTokenizer;
public class StringToken{
public static void main(String arg[]){
StringTokenizer st=new StringTokenizer("i am + bhavanite" , "+");
System.out.println("Total Token are ..."+st.countTokens());
while(st.hasMoreTokens()){
System.out.println(st.nextToken());
}
}
}
C:\Users\LAB01PC16\Desktop\SYCS36>javac StringToken.java
C:\Users\LAB01PC16\Desktop\SYCS36>java StringToken
Total Token are ...2
i am
bhavanite
Page 11 of 25
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
SYCS
Class: _____________ 3
Sem: _____ Date of Performance: ___________
Introduction to Java Programming
Course Name:_________________________ BH.USCSP302
Course Number: __________
Practical Number: _______ Page Number: _______
Aim:
Practical-09
Exceptional Handling
Q.1 Write a java program to demonstrate the basics of exception handling.
CODE:
class ExceptionDemo{
public static void main(String arg[]){
int d,a;
try{
d=0;
a=42/d;
System.out.println("This will not be printed.");
}
catch(ArithmeticException e){
System.out.println("Division by zero");
}
System.out.println("After catch statement");
}
}
OUTPUT:
C:\Users\LAB01PC16\Desktop\SYCS36>javac ExceptionDemo.java
C:\Users\LAB01PC16\Desktop\SYCS36>java ExceptionDemo
Division by zero
After catch statement
Q2. Write a Java program to demonstrate multi-catch block .
CODE:
class MultiCatch{
public static void main(String args[]){
try{
int a=args.length;
System.out.println("a = "+a);
int b=42/a;
int c[]={1};
c[42]=99; //Exception thrown
}
catch(ArithmeticException e){
System.out.println("Divide by 0: "+e);
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Array index object: "+e.toString());
}
catch(Exception e){
System.out.println("Caught Exception: "+e);
}
System.out.println("After try/catch blocks.");
Page 12 of 25
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
SYCS
Class: _____________ 3
Sem: _____ Date of Performance: ___________
Introduction to Java Programming
Course Name:_________________________ BH.USCSP302
Course Number: __________
Practical Number: _______ Page Number: _______
Aim:
}
}
OUTPUT 1:
C:\Users\LAB01-PC19\Desktop\java excep>javac MultiCatch.java
Page 13 of 25
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
SYCS
Class: _____________ 3
Sem: _____ Date of Performance: ___________
Introduction to Java Programming
Course Name:_________________________ BH.USCSP302
Course Number: __________
Practical Number: _______ Page Number: _______
Aim:
Division by zero.
You have Exceptionjava.lang.ArithmeticException: / by zero
a =0
Division by zero.
You have Exceptionjava.lang.ArithmeticException: / by zero
a =0
//throw keyword: Used to raise an built-in or user defined exception manually and also to
rethrow an exception
//throws keyword: Gives list of Exceptions that can be raised by the program
//caller of the method should safeguard the code using try block which is prone to exceptions
mentioned in throws list
Page 14 of 25
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
SYCS
Class: _____________ 3
Sem: _____ Date of Performance: ___________
Introduction to Java Programming
Course Name:_________________________ BH.USCSP302
Course Number: __________
Practical Number: _______ Page Number: _______
Aim:
Page 15 of 25
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
SYCS
Class: _____________ 3
Sem: _____ Date of Performance: ___________
Introduction to Java Programming
Course Name:_________________________ BH.USCSP302
Course Number: __________
Practical Number: _______ Page Number: _______
Aim:
catch(MyException e){
System.out.println("Caught My Exception"+e);
System.out.println(e.getMessage());
}
finally{
System.out.println("....I am always here....");
}
}
}
OUTPUT:
C:\Users\LAB01-PC19\Desktop\java excep>javac TestException.java
C:\Users\LAB01-PC19\Desktop\java excep>java TestException
Caught My ExceptionMyException: Number is too small ....
Number is too small ....
....I am always here....
Page 16 of 25
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
SYCS
Class: _____________ 3
Sem: _____ Date of Performance: ___________
Introduction to Java Programming
Course Name:_________________________ BH.USCSP302
Course Number: __________
Practical Number: _______ Page Number: _______
Aim:
PRACTICAL-10
Java programming on FILE HANDLING
Q.1 Write a java program for reading data from one file and copy it to another file by using
FileReader and FileWriter class.
CODE:
import java.io.*;
class CopyFile{
public static void main(String args[]){
File FI=new File("input.txt");
File FO=new File("output.txt");
FileReader fr=null;
FileWriter fw=null;
try{
fr=new FileReader(FI);
fw=new FileWriter(FO);
int ch;
while((ch=fr.read())!=-1){
fw.write(ch);
System.out.print(""+ch);
}
}
catch(IOException e){
System.out.println(e);
System.exit(-1);
}
finally{
try{
fr.close();
fw.close();
System.out.println("\n\n.........Its Done ..........\n");
System.out.println("...See input.txt and output.txt file...\n\n");
}
catch(IOException e) { }
}
}
}
Page 17 of 25
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
SYCS
Class: _____________ 3
Sem: _____ Date of Performance: ___________
Introduction to Java Programming
Course Name:_________________________ BH.USCSP302
Course Number: __________
Practical Number: _______ Page Number: _______
Aim:
OUTPUT in Console:
C:\Users\LAB01-PC19\Desktop\java file>javac CopyFile.java
Page 18 of 25
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
SYCS
Class: _____________ 3
Sem: _____ Date of Performance: ___________
Introduction to Java Programming
Course Name:_________________________ BH.USCSP302
Course Number: __________
Practical Number: _______ Page Number: _______
Aim:
Practical-11
Java Programming on threads and multithreading
Q1. Write a java program to create and execute a thread.
CODE:
//Creating only one thread
class CurrentThreadDemo{
public static void main(String args[]){ //Main is always the first thread to be formed
Thread t=Thread.currentThread();
System.out.println("Current thread:"+t);
//change the name of the thread
t.setName("My SYCS Thread");
C:\Users\LAB01-PC18\Desktop\java>java CurrentThreadDemo
Current thread:Thread[main,5,main]
After name change:Thread[My SYCS Thread,5,main]
5
4
3
2
1
Java threads can be created in two ways:
• By Extending Thread Class
• Implementing a Runnable interface
Page 19 of 25
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
SYCS
Class: _____________ 3
Sem: _____ Date of Performance: ___________
Introduction to Java Programming
Course Name:_________________________ BH.USCSP302
Course Number: __________
Practical Number: _______ Page Number: _______
Aim:
CODE:
class MyThread extends Thread{
MyThread(){
//Create a new, second thread
super("Demo Thread");
System.out.println("Child Thread:"+this);
start(); //Start the threads
//start() method calls the run() method
}
//This is the entry point for the second thread i.e. run() method
C:\Users\LAB01-PC18\Desktop\java>java ExtendThread
Child Thread:Thread[Demo Thread,5,main]
Page 20 of 25
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
SYCS
Class: _____________ 3
Sem: _____ Date of Performance: ___________
Introduction to Java Programming
Course Name:_________________________ BH.USCSP302
Course Number: __________
Practical Number: _______ Page Number: _______
Aim:
Main Thread: 5
Child Thread: 5
Child Thread: 4
Main Thread: 4
Child Thread: 3
Child Thread: 2
Main Thread: 3
Child Thread: 1
Exiting child thread
Main Thread: 2
Main Thread: 1
Main thread exiting
Page 21 of 25
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
SYCS
Class: _____________ 3
Sem: _____ Date of Performance: ___________
Introduction to Java Programming
Course Name:_________________________ BH.USCSP302
Course Number: __________
Practical Number: _______ Page Number: _______
Aim:
catch(InterruptedException e){
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting");
}
}
OUTPUT:
C:\Users\Rag\Desktop\java\p11>javac ThreadDemo.java
C:\Users\Rag\Desktop\java\p11>java ThreadDemo
Child Thread:Thread[#20,Demo Thread,5,main]
Main Thread :5
Child Thread:5
Child Thread:4
Main Thread :4
Child Thread:3
Child Thread:2
Child Thread:1
Main Thread :3
Exiting child thread
Main Thread :2
Main Thread :1
Main thread exiting
Page 22 of 25
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
SYCS
Class: _____________ 3
Sem: _____ Date of Performance: ___________
Introduction to Java Programming
Course Name:_________________________ BH.USCSP302
Course Number: __________
Practical Number: _______ Page Number: _______
Aim:
t.start();
}
public void run() {
target.call(msg);
}
}
class Synch {
public static void main(String args[]) {
Callme target = new Callme();
Caller ob1 = new Caller(target, "Hello");
Caller ob2 = new Caller(target, "Synchronized");
Caller ob3 = new Caller(target, "World");
// wait for threads to end
try {
ob1.t.join();
ob2.t.join();
ob3.t.join();
}
catch(InterruptedException e) {
System.out.println("Interrupted");
}
}
}
OUTPUT1 : Without Using synchronised keyword
C:\Users\Rag\Desktop\java\p11>javac Synch.java
C:\Users\Rag\Desktop\java\p11>java Synch
[Hello[World[Synchronized]
]
]
OUTPUT 2: Using synchronised keyword
C:\Users\Rag\Desktop\java\p11>javac Synch.java
C:\Users\Rag\Desktop\java\p11>java Synch
[Hello]
[Synchronized]
[World]
Page 23 of 25
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
SYCS
Class: _____________ 3
Sem: _____ Date of Performance: ___________
Introduction to Java Programming
Course Name:_________________________ BH.USCSP302
Course Number: __________
Practical Number: _______ Page Number: _______
Aim:
Practical-12
Java socket programming
Page 24 of 25
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
SYCS
Class: _____________ 3
Sem: _____ Date of Performance: ___________
Introduction to Java Programming
Course Name:_________________________ BH.USCSP302
Course Number: __________
Practical Number: _______ Page Number: _______
Aim:
}
dout.close();
s.close();
}
}
Note:
➢ Open two Command Prompt – one for server and another for client program execution.
➢ Run the server code first. Send a first message from client to server and then message
from server to client. Continue messaging.
➢ When messaging is done then Type ‘stop’ from client side first then type stop from
server side.
Server OUTPUT:
C:\Users\Rag\Desktop\java\p11>cd C:\Users\Rag\Desktop\java\p11
C:\Users\Rag\Desktop\java\p11>set path="C:\Users\Rag\Downloads\jdk-20_windows-
x64_bin\jdk-20.0.1\bin"
C:\Users\Rag\Desktop\java\p11>javac MyServer.java
C:\Users\Rag\Desktop\java\p11>java MyServer
Client says:Client speaking
Server here, hi hello client
Client says:i being client request for the data access
ok Request processing
Client says:Waiting ..... for server reply
Access permitted to client
Client says:stop
stop
Client OUTPUT:
C:\Users\sycs13\Desktop\java\p11>cd C:\Users\sycs13\Desktop\java\p11
C:\Users\sycs13\Desktop\java\p11>set path="C:\Users\sycs13\Downloads\jdk-20_windows-
x64_bin\jdk-20.0.1\bin"
C:\Users\sycs13\Desktop\java\p11>javac MyClient.java
C:\Users\sycs13\Desktop\java\p11>java MyClient
Client speaking
Server says:Server here, hi hello client
i being client request for the data access
Server says:ok Request processing
Waiting ..... for server reply
Server says:Access permitted to client
stop
Server says:stop
Page 25 of 25