0% found this document useful (0 votes)
211 views200 pages

JPR Full Manual

Here are the programs to demonstrate constructor in Java: 1. Default Constructor: class ConstructorExample { ConstructorExample() { System.out.println("Default Constructor"); } public static void main(String[] args) { ConstructorExample obj = new ConstructorExample(); } } Output: Default Constructor 2. Parameterized Constructor: class ConstructorExample { int id; String name; ConstructorExample(int i, String n){ id = i; name = n; } void display() { System.out.println(id+" "+name); } public static void main(String args[]) { ConstructorExample obj = new Constructor

Uploaded by

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

JPR Full Manual

Here are the programs to demonstrate constructor in Java: 1. Default Constructor: class ConstructorExample { ConstructorExample() { System.out.println("Default Constructor"); } public static void main(String[] args) { ConstructorExample obj = new ConstructorExample(); } } Output: Default Constructor 2. Parameterized Constructor: class ConstructorExample { int id; String name; ConstructorExample(int i, String n){ id = i; name = n; } void display() { System.out.println(id+" "+name); } public static void main(String args[]) { ConstructorExample obj = new Constructor

Uploaded by

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

SAHIL RUPESH SURVE

102 20 21

A
SAHIL RUPESH SURVE
102
COMPUTER ENGINEERING (CO)
THAKUR POLYTECHNIC
0522

20 21

MUMBAI 1905220527
18/6/2021 113000
1/4/21 15/4/21

7/4/21 15/4/21

7/4/21 15/4/21

8/4/21 15/4/21

8/4/21 15/4/21

12/4/21 15/4/21

12/4/21 15/4/21

14/4/21 20/4/21

19/4/21 25/4/21

21/4/21 25/4/21
19/4/21 20/4/21

19/4/21 20/4/21

22/4/21 2/5/21

26/4/21 2/5/21

28/4/21 2/5/21

29/4/21 2/5/21

3/5/21 6/6/21

13/5/21 23/5/21

20/5/21 23/5/21

27/5/21 30/5/21
27/5/21 30/5/21

27/5/21 30/5/21

3/6/21 6/6/21
Name: Sahil Surve
SYCO-B
Roll No.: 102

JAVA PROGRAMMING
(JPR-22412)
Experiment No. 1 & 2
Setup a Java Programming development environment and test
using small program.
Output:
Experiment No. 3
Develop programs to demonstrate use of if statements and its
different forms.

Input:
class IfElseLadder

{ public static void main(String


args[])
{
System.out.println("Sahil Surve
_102"); int a=-10; if(a>0)
System.out.println("The Number is Positive"); else
if(a<0)
System.out.println("The Number is Negative"); else
System.out.println("The Number is Zero");
}
}
Output:
Input:
class LogicalOperator
{ public static void main(String
args[])
{
System.out.println("Sahil Surve _102");
boolean x=true; booleany=false;
System.out.println("x||y = "+(x||y));
System.out.println("x&&y = "+(x&&y));
System.out.println("!(x||y) = "+!(x||y));
}
}

Output:
Input: class
EvenOdd
{ public static void main(String
args[])
{
System.out.println("Sahil
Surve_102"); int
x=Integer.parseInt(args[0]); if (x%2 == 0)
{
System.out.println("Even Number");
} else
{
System.out.println("Odd Number");
}
}

3Output:
Experiment No. 4
Develop programs to demonstrate use of Switch Case
Statement and Conditional if (?:)

Input: class
SwitchCaseWeek
{ public static void main(String
args[])
{
System.out.println("Sahil Surve _102");
char ch=4; switch(ch) {case 1:
System.out.println("Sunday");
break; case 2:
System.out.println("Monday");
break; case 3:
System.out.println("Tuesday");
break; case 4:
System.out.println("Wednesday"); break;
case 5:
System.out.println("Thursday");
break; case 6:
System.out.println("Friday");
break; case 7:
System.out.println("Saturday");
break; default:
System.out.println("Invalid choice of Day");
}
}

Output:
Input:
class TernaryOperator
{ public static void main(String
args[])
{
System.out.println("Sahil Surve _102");
int a=50,b=100,c;
c=(a>b)?a:b;
System.out.println("Greater number is:"+c);
}
}

Output:
Input: class
SwitchCaseVowel
{ public static void main(String
args[])
{
System.out.println("Sahil Surve _102");
char ch='e'; switch(ch) {case'a': case'e':
case'i': case'o': case'u':
System.out.println(ch+" is a vowel");
break; default:
System.out.println(ch+" is not a vowel");
}
}
}

Output:
Experiment No. 5
Develop programs to demonstrate use of looping statement
‘for’.

Input:
class CommandLineArg
{
System.out.println("Sahil
Surve_102"); public static void
main(String args[])
{ int
n,i;
n=Integer.parseInt(args[0]);
for(i=0;i<=10;i++)
{
System.out.println(n);
}
}
}
Output:
Input:
class ForIf {public static void
main (String args[])
{
System.out.println("Sahil
Surve _102");
int i;
for(i=20;i<=60;i++)
{ if(i%5==0)
System.out.println("i="+i);
}
}
}

Output:
Input:
class PyramidOfStar
{ public static void main(String
args[])
{
System.out.println("Sahil
Surve _102"); int a,b;
for(a=0;a<=7;a++)
{ for(b=0;b<=a;b++)
{
System.out.print("*");
}
System.out.println();
}
}
}

Output:
Experiment No. 6
Develop programs to demonstrate use of ‘while’ , ‘do while’

Input: class
Dowhilelooplogop
{ public static void main(String
args[])
{
System.out.println("Sahil
Surve _102"); int i=0; do {
if((i%2==0)||(i%5==0))
{
System.out.println(" "+i);
} i++;
}while(i<=50);
}
}
Output:
Input: class dowhile1 { public
static void main(String args[])
{
System.out.println("Sahil
Surve _102");
int i=1; do
{
System.out.print(" "+i);
i++; }while(i<=50);
}
}

Output:
Experiment No. 7 & 8
Develop programs for implementation of Implicit Type
Casting in Java.

Input:
class ImplicitTypecast1
{
public static void main(String abc[])
{
System.out.println("Sahil Surve _102");
int a,c; byte b=2; doublep;
System.out.println("b="+b); a=b;
System.out.println("a="+a); c=a+b;
System.out.println("Total="+c); p=c/3;
System.out.println("Percentage="+p);
}
}
Output:
1st Example:
Input:
class Test { public static void
main(String[] args) { int i = 100;
//automatic type conversion long
l = i;
//automatic type conversion float
f = l;
System.out.println("Int value "+i);
System.out.println("Long value "+l);
System.out.println("Float value "+f);
}
}
Output:

2nd Example:
Input:
public class ImplicitTypeConversion {

public static void main (String[] args) {

byte i=10; long j=1+8; double k=i*2.5

+j;

System.out.println("I =" +i);

System.out.println("J =" +j);

System.out.println("K =" +k);

}}

Output:
Input:

class ImplicitTypecast2
{ public static void main(String
abc[])
{
System.out.println("Sahil Surve _102");
int a,c=10; byte b=2; floatd;
System.out.println("b="+b); a=b;
System.out.println("a="+a);
System.out.println("c="+c); d=c;
System.out.println("d="+d);
}
}

Output:
Experiment No. 9
Develop programs for implementation of Explicit Type
Conversion in Java.

Input:
class ExplicitTypecast1
{ public static void main(String
args[])
{
System.out.println("Sahil Surve
_102"); int a=12,Total; byte b;double
per;
System.out.println("a as int="+a);
b=(byte)a;
System.out.println("Byte conversion of b="+b);
Total=a+b;
System.out.println("Total="+Total);
per=Total/3;
System.out.println("Percentage="+per);
}
}
Output:
Input:
class ExplicitTypecast2
{ public static void main(String
args[])
{
System.out.println("Sahil Surve
_102"); byte b;

int i = 257;double d = 323.142;


System.out.println("Conversion of int to byte."); b
= (byte) i;
System.out.println("i = " + i + " b = " + b);
System.out.println("\nConversion of double to byte."); b
= (byte) d;
System.out.println("d = " + d + " b= " + b);
}
}
Output:
Name: Sahil Surve
SYCO-B
Roll No.: 102

JAVA PROGRAMMING
(JPR-22412)
Experiment No. 11 & 12
Develop program for implementation of different functions of String
Class, Part – 1 and Part - 2.

Input: class
Exp11_12X
{ public static void main(String
args[])
{
System.out.println("Sahil Surve _102");
String s1=new String("mumbai");
System.out.println(s1.length()); // returns 6

System.out.println(s1.charAt(3)); // returns ‘b’


System.out.println("Mumbai".substring(3)); // returns "bai"
System.out.println(s1.substring(3));// returns "bai"
System.out.println(s1.substring(2, 5)); // returns "mba"
String s2 = new String(" Maharastra");
System.out.println(s1.concat(s2)); // returns "mumbaiMaharastra"
System.out.println(s1.indexOf("ba")); // returns 3
System.out.println(s1); //mumbai
System.out.println(s1.indexOf('m',1));// returns 2
Boolean b = "Thakur".equals("Thakur"); // returns true
System.out.println(b);
Boolean b1 = "Thakur".equals("thakur"); // returns false
System.out.println(b1);
Boolean b3= "Thakur".equalsIgnoreCase("Thakur"); // returns true
System.out.println(b3);
Boolean b4 = "Thakur".equalsIgnoreCase("thakur"); // returns true
System.out.println(b4);
String s4="HellooOO";
String s5="Helloo";
System.out.println(s4.compareTo(s5)); // 2 This returns difference s1-s2.
System.out.println(s4.compareToIgnoreCase(s5)); //2
String word1 = "Thakur";
System.out.println(word1.toLowerCase()); // thakur
String word2 = "Thakur";
String word3 =word1.toUpperCase();
System.out.println(word3); // THAKUR
String word4 = " Thakur Polytechnic ";
String word5 = word4.trim(); //
System.out.println(word5); //Thakur Polytechnic
String s6 = "Thakur Polytechnic";
String s7 = "Thakur Polytechnic".replace('h' ,'H'); // THakur PolytecHnic
System.out.println(s7);
}
}
Output:
Input:
public class StringBufferExample
{ public static void main(String[]
args)
{
System.out.println("Sahil Surve _102");
StringBuffer sb = new StringBuffer("ThakurPolytechnic");
int sbLength = sb.length(); int sbCapacity =
sb.capacity();g
System.out.println("String Length of " + sb + " is " + sbLength);
System.out.println("Capacity of " + sb + " is " + sbCapacity);
sb.append(",Mumbai,"); sb.append(101);
System.out.println(sb); sb.insert(6, " ");
sb.insert(sb.length(), " Maharastra"); System.out.println(sb);
System.out.println(sb.reverse());
System.out.println(sb.reverse());
System.out.println(sb.delete(11,17));
System.out.println(sb.deleteCharAt(11));
System.out.println(sb.replace(0,11," TP"));
}}
Output:
Experiment No. 13
Develop program for implementation of Arrays in Java.

Input: class
Exp13X
{ public static void main(String[]
args)
{
System.out.println("Sahil Surve
_102"); int[][] a = { {1, -2, 3},
{-4, -5, 6, 9},
{7}, }; for (int i = 0; i <
a.length; i++)
{ for(int j = 0; j < a[i].length;
j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
Output:
Input:
class Exp13XIII3
{ public static void main(String[]
args)
{
String[] arrData = {"Alpha", "Beta", "Gamma", "Delta", "Sigma"}; //The
conventional approach of using the for loop
System.out.println("Sahil Surve _102");
System.out.println("Using conventional For Loop:"); for(int
i=0; i< arrData.length; i++)
{
System.out.println(arrData[i]);
}
System.out.println("\nUsing Foreach loop:"); //The optimized method
of using the for loop - also called the foreach loop for (String strTemp :
arrData)
{
System.out.println(strTemp);
}
}
}
Output:
Name: Sahil Surve
SYCO-B
Roll No.: 102

JAVA PROGRAMMING
(JPR-22412)
Experiment No. 10
Develop program for implementation of constructor and multiple
constructors.

Input:
class Exp10X
{
Exp10X ()
{
System.out.println("This is zero argument constructor");
}
Exp10X (int i)
{
System.out.println(i);
}
Exp10X (int i,String str)
{
System.out.println(i);
System.out.println(str);
} public static void main (String
[]args)
{
System.out.println("Sahil Surve_102");
Exp10X t1=new Exp10X ();
Exp10X t2=new Exp10X (10);
Exp10X t3=new Exp10X (100,"Hello"); }}
Output:
Modified Code:
class Point{ int
m_x,m_y;
public Point(){
} public Point(int x,int
y){ m_x=x; m_y=y;
}
public static void main(String args[]) {
System.out.println("Sahil Surve _102");
Point p1=new Point();
Point p=new Point(2,3);
System.out.println("X " + p.m_x);
System.out.println("Y "+ p.m_y);
System.out.println("X "+ p1.m_x);
System.out.println("Y "+ p1.m_x);
}}
User defined zero augmented (default) constructor and 1 parametrized
constructor is used.
Output:
Input:
class Exp10XIII3
{ int real; double imag; Exp10XIII3
()
{ real=5;
imag=2.6;
}
Exp10XIII3(int r,double i)
{ real=r;
imag=i;
} void sum(Exp10XIII3
t5)
{
real=real+t5.real; imag=imag+t5.imag;
System.out.println("The addition of complex number
is:"+real+"+"+imag+"i");
} public static void main(String[]
args)
{
System.out.println("Sahil Surve _102"); Exp10XIII3
t1=new Exp10XIII3 (); Exp10XIII3t2=new
Exp10XIII3(8,7.5); t2.sum(t1);}}
Output:
Experiment No. 14
Develop program for implementation of Vectors in java.

Input:
import java.util.*; class
Exp14X
{ public static void main(String[]
arg)
{
System.out.println("Sahil Surve _102");
Vector v = new Vector();
System.out.println("the size of vector is:"+v.size());
System.out.println("the capacity of vector is:"+v.capacity()); v.add(2);
v.add(3);
v.add(0, 1);
v.add(3, 4);
v.add("thakur");
v. add("poly");
v.add(5);
System.out.println("Vector is " + v);
System.out.println("the size of vector is:"+v.size());
System.out.println("the capacity of vector is:"+v.capacity());
}
}
Output:
Input:
import java.util.*; class
Exp14XIII1
{ public static void main(String[]
arg)
{
System.out.println("Sahil Surve _102");
// create default vector
Vector v = new Vector();
System.out.println("the size of vector is:"+v.size());
System.out.println("the capacity of vector is:"+v.capacity()); v.add(2);
v.add(3);
v.add(0, 1);
v.add(3, 4);
v.add("thakur");
v.add("poly");
v.add(3);
System.out.println("Vector is " + v); if
(v.contains("poly"))
System.out.println("poly");
System.out.println("element at indexx 2 is: " + v.get(2));
System.out.println("index of thakur is: " + v.indexOf("thakur")); if
(v.isEmpty())
System.out.println("Vector is clear");
System.out.println("first element of vector is: " + v.firstElement());
System.out.println("capacity is: " + v.capacity()); v.clear();
System.out.println("after clearing: " + v);
System.out.println("the size of vector is:"+v.size());
System.out.println("the capacity of vector is:"+v.capacity());
}
}

Output:
Experiment No. 15 & 16
Develop a program for implementation of Wrapper class to
convert primitive into object and object into primitive.

Input:
public class Exp15X
{ public static void main(String
args[])
{
System.out.println("Sahil Surve
_102"); int b = 55; String bb ="45";
Integer x = new Integer(b);
Integer y = new Integer(bb);
System.out.println("toString(b) = " + Integer.toString(b));
System.out.println("toHexString(b) =" + Integer.toHexString(b));
System.out.println("toOctalString(b) =" + Integer.toOctalString(b));
System.out.println("toBinaryString(b) =" + Integer.toBinaryString(b));
Integer z = Integer.valueOf(b);
System.out.println("valueOf(b) = " + z); z
= Integer.valueOf(bb);
System.out.println("ValueOf(bb) = " + z); z
= Integer.valueOf(bb, 6);
System.out.println("ValueOf(bb,6) = " + z);
int zz = Integer.parseInt(bb);
System.out.println("parseInt(bb) = " + zz); zz
= Integer.parseInt(bb, 6);
System.out.println("parseInt(bb,6) = " + zz);
String decimal = "45";
String octal = "005";
String hex = "0x0f";
Integer dec = Integer.decode(decimal);
System.out.println("decode(45) = " + dec); dec
= Integer.decode(octal);
System.out.println("decode(005) = " + dec);
dec = Integer.decode(hex);
System.out.println("decode(0x0f) = " + dec);
}
}

Output:
Input:

public class Exp15XIII1


{ public static void main(String[]
args)
{
System.out.println("Sahil Surve _102");
//We can convert String to Integer using following ways.
//1. Construct Integer using constructor.
Integer intObj1 = new Integer("100");
System.out.println(intObj1); //100
//2. Use valueOf method of Integer class. This method is static.
String str = "100";
Integer intObj2 = Integer.valueOf(str);
System.out.println(intObj2); //100
}
}

Output:
Input:
public class Exp15XIII2
{ public static void main(String[]
args)
{
System.out.println("Sahil Surve _102");
System.out.println(Character.isLetter('A')); //true
System.out.println(Character.isLetter('0')); //false
System.out.println(Character.isDigit('A')); //false
System.out.println(Character.isDigit('0')); //true
System.out.println(Character.isWhitespace('A')); //false
System.out.println(Character.isWhitespace(' ')); //true
System.out.println(Character.isWhitespace('\n')); //true
System.out.println(Character.isWhitespace('\t')); //true
//ASCII value of tab
System.out.println(Character.isWhitespace(9)); //true
System.out.println(Character.isWhitespace('9')); //false
System.out.println(Character.isUpperCase('A')); //true
System.out.println(Character.isUpperCase('a')); //false
System.out.println(Character.isUpperCase(65)); //false
System.out.println(Character.isLowerCase('A')); //false
System.out.println(Character.isLowerCase('a')); //true
System.out.println(Character.isLowerCase(97)); //true
System.out.println(Character.toUpperCase('a')); //A
System.out.println(Character.toUpperCase(97)); //65
System.out.println(Character.toUpperCase(48)); //48
System.out.println(Character.toLowerCase('A')); //a
System.out.println(Character.toLowerCase(65)); //97
System.out.println(Character.toLowerCase(48)); //48
System.out.println(Character.toString('x')); //x
System.out.println(Character.toString('Y')); //Y
}

Output:
Input:
public class Exp15XIII3
{ public static void main(String[]
args)
{
System.out.println("Sahil Surve _102");
Integer intObj = new Integer("10");
//use byteValue method of Integer class to convert it into byte type.
byte b = intObj.byteValue();
System.out.println(b); //10
//use shortValue method of Integer class to convert it into short type.
short s = intObj.shortValue();
System.out.println(s); //10
//use intValue method of Integer class to convert it into int type.
int i = intObj.intValue();
System.out.println(i); //10
//use floatValue method of Integer class to convert it into float type.
float f = intObj.floatValue();
System.out.println(f); //10.0
//use doubleValue method of Integer class to convert it into double type.
double d = intObj.doubleValue();
System.out.println(d); //10.0
}}
Output:
Name: Sahil Surve
SYCO-B
Roll No.: 102

JAVA PROGRAMMING
(JPR-22412)
Experiment No. 17
Develop program for implementation the concept of
overriding.

Input:
class SuperClass
{ void
display()
{
System.out.println("This is display method of super class");
} } class SubClass extends
SuperClass
{ void
display()
{
System.out.println("This is display method of sub class");
}
} class
Exp17X
{ public static void main(String
args[])
{
System.out.println("Sahil Surve_102");
SuperClass sup= new SuperClass();
//call to display method using object of base class sup.display();
SubClass sub=new SubClass();
//call to display method using object of derived class sub.display();
}
}

Output:
Input: class
Bank
{ int
getRateOfInterest() {
return 0;
} }
class SBI extends Bank
{ int
getRateOfInterest() {
return 8;
}
}
class AXIS extends Bank
{ int
getRateOfInterest() {
return 9;
}
}
//Test class to create objects and call the methods
class Exp17XIII1{ public static void
main(String args[])
{
System.out.println("Sahil Surve_102");SBI
s=new SBI();
AXIS a=new AXIS();
System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());
System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest());
}
}

Output:
Input:
class Animal
{
void move()
{
System.out.println("move of Animal class(base class)...");
}
}
class Dog extends Animal
{
void move()
{
System.out.println("move of Dog class(derived class)..."); super.move();
}
}
class Exp17XIII3
{
public static void main(String args[])
{
System.out.println("Sahil
Surve_102”); Dog d=new Dog();
d.move();
}
}
Output:
Experiment No. 18
Develop program for implementation of single and multilevel
inheritance.

Single Inheritance:
Input:
class Parent
{
public void m1()
{
System.out.println("Class Parent method");
}
}
public class Child extends Parent
{
public void m2()
{
System.out.println("Class Child method");
}
public static void main(String args[])
{
System.out.println(“Sahil Surve_102”);
Child obj = new Child();
obj.m1();
obj.m2();
}
}
Output:

Multilevel Inheritance:

Input:

class Grand
{ public void
m1()
{
System.out.println("Class Grand method");
} } class Parent extends
Grand
{ public void
m2()
{
System.out.println("Class Parent method");
} } class Child1 extends
Parent
{ public void
m3()
{
System.out.println("Class Child method");
} public static void main(String
args[])
{
System.out.println("Sahil Surve_102");
Child1 obj = new Child1(); obj.m1();
obj.m2(); obj.m3();
}
}

Output:
Input:
import java.lang.*;
import java.io.*; class
Account
{
String cust_name; int acc_no;
Account(String a, int b)
{ cust_name=a;
acc_no=b;
} void
display()
{
System.out.println ("Customer Name: "+cust_name);
System.out.println ("Account No: "+acc_no);
}
} class Saving_Acc extends
Account
{ int min_bal,saving_bal; Saving_Acc(String a, int b, int c, int
d)
{ super(a,b); min_bal=c;
saving_bal=d;
} void
display()
{ super.display();
System.out.printl
n ("Minimum
Balance:
"+min_bal);

System.out.println ("Saving Balance: "+saving_bal);


} } class Acct_Details extends
Saving_Acc
{
int deposits, withdrawals;
Acct_Details(String a, int b, int c, int d, int e, int f)
{ super(a,b,c,d);
deposits=e;
withdrawals=f;
} void
display()
{ super.display();
}
} class
Exp18EX1
{ public static void main(String
args[])
{
System.out.println("Sahil Surve_102");
Acct_Details A = new Acct_Details("Sahil",666,1000,5000,500,9000);
A.display();
}
}

Output:
Input: class RoomData
{ int length; int breadth; int height; void
getdata(int x,int y,int z)
{ length=x; breadth=y; height=z;
} void displaydata()
{
System.out.println("length is:"+length);
System.out.println("breadth is:"+breadth);
System.out.println("height is:"+height);
}
}
class RoomCalculation extends RoomData //Inheriting RoomData
{
int area;
int volume;
void areacalculate()
{
{ area=length*breadth;
System.out.println("Area of room is:"+area);
} void
volumecalculate()
{
volume=length*breadth*height;
System.out.println("volume of room is:"+volume);
}
} class
Exp18XIII2
{ public static void main(String
args[])
{
System.out.println("Sahil Surve_102");
RoomCalculation rc=new RoomCalculation();
rc.getdata(4,5,6); rc.displaydata();
rc.areacalculate(); rc.volumecalculate();
}
}
Output:
Experiment No. 19
Develop a program for implementation of multiple inheritance.

Input: interface
X
{ public void
myMethod();
} interface
Y
{ public void
myMethod1();
} class Exp19X implements X,
Y
{ public void
myMethod()
{
System.out.println("Implementing myMethod");
} public void
myMethod1()
{
System.out.println("Implementing myMethod1");
} public static void main(String
args[])
{
System.out.println("Sahil
Surve_102"); Exp19X obj = new
Exp19X(); obj.myMethod();
obj.myMethod1();
}
}

Output:
Input:
interface NewShape
{
void draw();
}
interface Circle extends NewShape
{ void
getRadius(); int
radius=10;
}
class NewCircle implements Circle
{
public void getRadius()
{
System.out.println(radius);
}
public void draw()
{
System.out.println("Implemented draw method of NewShape interface");
}}
class ExtendInterface extends NewCircle
{
public static void main(String[] args)
{
System.out.println("Sahil Surve_102");
Circle nc=new NewCircle();
nc.getRadius();
}
}

Output:
Input:
interface area
{
double pi = 3.14;
double calc(double x,double y);
}
class rect implements area
{
public double calc(double x,double y)
{
return(x*y);
}}
class cir implements area
{
public double calc(double x,double y)
{
return(pi*x*x);
}
}
class Exp19EX2
{
public static void main(String arg[])
{
System.out.println("Sahil Surve_102");
rect r = new rect(); cir c =new cir();
System.out.println("\nArea of Rectangle is : " +r.calc(2,3));
System.out.println("\nArea of Circle is : " +c.calc(3,4));
}
}
Output:
Experiment No. 20
Develop a program to import different classes in package.

Input:
package pack; public
class A
{ public void
msg()
{
System.out.println("Hello");
} } import pack.A; class B
{ public static void main(String
args[])
{
A obj = new A(); obj.msg();
}
}
Output:
Input:
package myInstitute;
public class Department
{ public void display(String
dept)
{
if(dept=="co")
System.out.println("name of the staff is abc");
else if(dept=="if")
System.out.println("name of the staff is mno");
else if(dept=="me")
System.out.println("name of the staff is xyz");
}
} class
Exp20Ex2
{ public static void main(String
args[])
{
Department d=new Department(); d.display("co");
d.display("if");
d.display("me");
}
}
Output:
Input:
package letmecalculate;
public class Calculator
{ public int add(int a, int
b)
{ return
a+b;
} } import
letmecalculate.Calculator; public
class Demo
{ public static void main(String
args[])
{
Calculator obj = new Calculator();
System.out.println(obj.add(100, 200));
}
}

Output:
Name: Sahil Surve
SYCO-B
Roll No.: 102

JAVA PROGRAMMING
(JPR-22412)
Experiment No. 21 & 22
Develop a program for implementation of multithreading
operation Part I and Part II.

Input: class MyThread


extends Thread
{
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println("child thread");
}
}}
class ThreadDemo
{
public static void main(String args[])
{
MyThread t=new MyThread();
t.start();
for(int i=0;i<10;i++){
System.out.println("main thread");
}
}
}
Output:
Input:
class A extends Thread
{
synchronized public void run()
{ try
{
System.out.println("good"); sleep(1000);
}
catch(Exception e)
{}
}}
class B extends Thread
{
synchronized public void run()
{ try
{
System.out.println("morning");
sleep(2000);} catch(Exception
e)
{}
}}
class C extends Thread
{
synchronized public void run()
{ try
{
System.out.println("india");
sleep(3000);
}
catch(Exception e)
{}
}}
class ThreadDemo1
{
public static void main(String args[])
{ A t1=new A(); B t2=new B();C
t3=new C();
t1.start(); t2.start();
t3.start();
}
}
Output:
The code will not compile successfully.
Corrected Code:
class t1 extends Thread
{
public void run()
{
System.out.println("This is Thread1 class");
}}
class t2 extends Thread
{
public void run()
{
System.out.println("This is Thread2 class");
}
}public class ThreadP
{
public static void main(String[] args)
{
System.out.println("Sahil Surve_102"); t1
t=new t1();
t2 tt=new t2();
t.setPriority(Thread.MIN_PRIORITY);
tt.setPriority(Thread.MIN_PRIORITY);
t.start(); tt.start();
}
}

Output:

Input:
import java.lang.*;
class ThreadDemo3 extends Thread
{
public void run()
{
System.out.println("Inside run method");
}
public static void main(String[]args)
{
System.out.println("Sahil Surve_102");
ThreadDemo3 t1 = new ThreadDemo3();
ThreadDemo3 t2 = new ThreadDemo3();
ThreadDemo3 t3 = new ThreadDemo3();
System.out.println("t1 thread priority : " + t1.getPriority()); // Default 5
System.out.println("t2 thread priority : " + t2.getPriority()); // Default 5
System.out.println("t3 thread priority : " + t3.getPriority()); // Default 5
t1.setPriority(2); t2.setPriority(5); t3.setPriority(8);
// t3.setPriority(21); will throw IllegalArgumentException
System.out.println("t1 thread priority : " + t1.getPriority()); //2
System.out.println("t2 thread priority : " + t2.getPriority()); //5
System.out.println("t3 thread priority : " + t3.getPriority());//8 // Main
threadSystem.out.println(Thread.currentThread().getName());
System.out.println("main thread priority : " + Thread.
currentThread().
getPriority());
// Main thread priority is set to 10
Thread.currentThread().setPriority(10);
System.out.println("main thread priority : "+ Thread. currentThread().
getPriority());
}
}
Output:
Name: Sahil Surve
SYCO-B
Roll No.: 102

JAVA PROGRAMMING
(JPR-22412)
Experiment No. 23, 24 & 25
Develop a program for implementation of try, catch and
finally block.
Input:
import java.util.*; class AuthenticationException
extends Exception
{ public AuthenticationException(String
message)
{ super(message);
} } public class
Exp23XIII2
{ public static void main(String[]
args)
{
System.out.println("Sahil Surve_102");
Scanner sc=new Scanner(System.in);
String pwd;
System.out.print("Enter password :: "); pwd = sc.nextLine(); try {
if(!pwd.equals("123")) throw new AuthenticationException("Incorrect
password...Type correct password"); else
System.out.println("Welcome User !!!");
} catch(AuthenticationException
e)
{
System.out.println(e.getMessage());
}
System.out.println("BYE BYE");
}
}

Output:
Experiment No. 26 & 27
Develop a program for implementation of throw and throws
clause.

Input:
import java.util.Scanner; class
InvalidAgeException extends Exception
{
} class
Exp26X
{ void status(int age)throws
InvalidAgeException
{ if
(age>=18)
{
System.out.println("eligible for voting");
} else { throw new
InvalidAgeException();
} } public static void main(String[] args)throws
InvalidAgeException {

System.out.println("Sahil Surve_102");
Exp26X e=new Exp26X();
Scanner s = new Scanner(System.in);
System.out.println("enter u r age");
int age = s.nextInt(); e.status(age);
}
}

Output:
Input:

import java.util.Scanner; class


InvalidWithdrawalAmountException extends Exception
{ public InvalidWithdrawalAmountException(String
s)
{
super(s);
} } class
Bank
{ void checkbalance(int ba,int
wa)
{
try
{ if
(wa<ba)
{ ba=ba-wa;
System.out.println("withdrawal amount is:"+wa);
System.out.println("balance after withdrawal is:"+ba);
}
else
{ throw new InvalidWithdrawalAmountException("withdrawal amount
is greater than balance amount. \n please try again");
}
} catch( InvalidWithdrawalAmountException
e) {
System.out.println(e);
}
} } public class
Exp26XII3
{ public static void main(String[]
args)
{
Bank b=new Bank();
Scanner s = new Scanner(System.in);
System.out.println("enter balance amount in your account"); int
balamt = s.nextInt();
System.out.println("enter amount for withdrawal"); int
withamt = s.nextInt();
b.checkbalance(balamt,withamt);
}
}

Output:
Input:
import java.util.Scanner; class
NotMatchException extends Exception
{ public NotMatchException(String
s)
{ super(s);
} } public class
Exp26XIII1
{ public static void main(String[]
args)
{
System.out.println("Sahil Surve_102");
Scanner s = new Scanner(System.in);
System.out.println("enter any String:");
String str = s.nextLine(); try {
if(str.equals("India"))
System.out.println("String entered is "+str);
else
throw new NotMatchException("Entered String is not India");
} catch(NotMatchException
e)
{
System.out.println(e);
}
}
}

Output:
Name: Sahil Rupesh Surve
SYCO-B
Roll No.: 102

JAVA PROGRAMMING
(JPR-22412)
Experiment No. 28
Develop minimum two basic Applets. Display output with
applet viewer and browser.
a. Develop a program on basic applet.
b. Develop a program using control loops in applet.

Input:
Second Way
Code:
import java.applet.Applet; import
java.awt.Graphics; public class
First1 extends Applet
{ public void paint(Graphics
g)
{
g.d rawString("welcome to applet",150,150);
}
}
/*
<applet code="First1.class" width="300" height="300"></applet> */
Output:
Prscticql Related Queetions

more such questions so as to ensure the achieveme


1. Differentiate between init( ) and start( )methods.
2. Explain use of start( ) and stop( ) method in applet lifecycle?
3. The method paint( ) belongs to which class?
Input:
import java.applet.*; import
java.awt.*; public class Exp28Ex1
extends Applet
{ public void paint (Graphics
g)
{
g.drawString("Welcome to the World of Applet", 25, 50);
}
}
/*
<APPLET CODE="Exp28Ex1.class" width=500 height=400>
</APPLET>
*/
Output:
Input:
import java.awt.*; import
java.applet.*; public class Exp28Ex2
extends Applet
{
String msg=""; public void
paint(Graphics g)
{
Font f=new Font("arial",Font.BOLD,20); g.setFont(f);
g.drawString("Thakur Polytechnic "+msg,100,200);
} public void init()
{ msg=msg+"initialization"+" ";
System.out.println("init()");
} public void
start()
{ msg=msg+"starting"+" ";
System.out.println("start()");
} public void
stop()
{ msg=msg+"stoping";
System.out.println("stop()");
} public void
destroy()
{ msg=msg+"destroyed";
System.out.println("destroy()");
}
}
/*<applet code="Exp28Ex2.class" width="500" height="500"></applet>*/

Output:
Input:
Import java.awt.*; Import
java.applet.*; public class Exp28Ex3
extends Applet
{ public void paint(Graphics
g)
{ for(int
i=1;i<=4;i++)
{ if(i%2==0)
{
g.fillOval(90,i*50+10,50,50);
g.setColor(Color.black);
} else
{
g.drawOval(90,i*50+10,50,50);
g.setColor(Color.red);
}
}
}
}
/* <applet code="Exp28Ex3" width=300 height=300></applet> */
Output:
Experiment No. 29
Write a program to create animated shapes using graphics
and applets.

Input:
import java.awt.*; import java.applet.*; public
class GeometricsShapes extends Applet
{ public void paint(Graphics
g)
{
g.drawLine(10,10,50,50);
g.drawRect(10,60,40,30);
g.setColor(Color.blue);
g.fillRect(60,10,30,80);
g.setColor(Color.green);
g.drawRoundRect(10,100,80,50,10,10);
g.setColor(Color.yellow);
g.fillRoundRect(20,110,60,30,5,5);
g.setColor(Color.red);
g.drawLine(100,10,230,140);
g.drawLine(100,140,230,10);
g.drawString("Line Rectangles Demo",65,180);
g.drawOval(230,10,200,150);
g.setColor(Color.blue);
g.fillOval(245,25,100,100);
}
}
/*
<applet code="GeometricsShapes.class" width=600 height=600></applet>
*/
Output:
Practical Related Questlons

i. Differentiate between executing the applet with appletviewer and HTML fi1e.
2. Explain methods required to draw difTerent shapes with dilTerent colors?
3. DilYerentiate between setforeground( ) and setColor{ ) methods.
4. Differentiate between appleM and applications.
Input:
import java.awt.*; import
java.applet.*;
/*
<applet code="Polygon.class" width=300 Height=300>
</applet> */ public class Polygon
extends Applet
{ public void paint(Graphics
g)
{ int
xpoints[]={30,200,30,200,30}; int
ypoints[]={30,30,200,200,30}; int
num=5;
g.drawPolygon(xpoints,ypoints,num);
}
}
Output:
Input:
import java.awt.*; import
java.applet.*; public class Face
extends Applet
{ public void paint(Graphics
g)
{
g.drawOval(40,40,120,150); //Head
g.drawOval(57,75,30,20); //Left eye
g.drawOval(110,75,30,20); //Right eye
g.fillOval(68,81,10,10); //Pupil (left)
g.fillOval(121,81,10,10); //Pupil (right)
g.drawOval(85,100,30,30); //Nose
g.fillArc(60,125,80,40,180,180); //Mouth
g.drawOval(25,92,15,30); //Left ear
g.drawOval(160,92,15,30); //Right ear
}
}
/*
<Applet code="Face.class" width=300 height =300></applet>
*/
Output:

3. Write the output of theprograms. iiaaport


java.awt.*;
import java.applet.*;
<applet codc="Arcs" width=300 Height=300>
</applet>
*/ public class Arcs extends
Applet
public void paint(Graphics g)

g.drawAi c(1(),4(),70,7(1,(),75);
g.tillArc( 100,40,70.70,0,75);
g.drawArc(10,100,70.80.0.175);
g.fillArc(100,100,70,90,0,270);
g.drawAi c(200,80,80.80,0,180);

.4pplet

Anant Sinah 93

Experiment No. 30
Develop a program to draw following shapes, graphics and
applets.
a. Cone
b. Cylinders
c. Cube
d. Square inside a circle
e. Circle inside a square

Input:
import java.applet.Applet; import
java.awt.*; public class ConeDemo
extends Applet
{ public void paint(Graphics
g)
{
Font font = new Font("Serif", Font.PLAIN, 26);
g.setFont(font); setBackground(Color.blue);
setForeground(Color.yellow);
g.drawOval(80,280,320,100);
g.drawLine(240,50,82,320);
g.drawLine(240,50,398,320);
g.drawLine(240,330,398,330);
g.drawLine(240,50,240,330);
g.d rawString("Radius(R)",260,360);
g.drawString("Height(H)",246,255);
g.drawString("Slant Height(L)",340,210);
g.drawString("CONE",220,420);
}
}
/*<applet code="ConeDemo.class" height=500 width=500></applet>*/
Output:

Practiced Related Questioog Note.• Below giwen are feir more such question
no an to ensure the seLieve tent ofidentlfied CO 1. Which of these methods
is a part of Abstract Window Toolkit (AWT)?
a) display( ) b) paint( ) c) drawstring( ) d) none of the above
Ans.
2. Enlist the methods required to draw cone/cylinder.
3. Explain the method with syntax to draw circle.
4. Differentiate Applet and application.
XIII. Exerclse:

1. Write output of the program.


/*
<applet codo="SetBackgroundColorExample" width=200 height =200>
</appIct>

imyon java.applet.Applet;
lmpori java.awt.Color.
import jave,owt ,Graphics;
public class SetBackgroundColorExample extends Applei
public void paint(Graphics g){

• Set background color of an applet using


• void set Background(Color c) method.
*/
set Background(Color.red);

A App...
Input:
import java.awt.*; import
java.applet.*; public class Shapes
extends Applet
{ public void paint(Graphics
g)
{

/*Cylinder*/
g.drawString("(a).Cylinder",10,110);
g.drawOval(10,10,50,10);
g.drawOval(10,80,50,10);
g.drawLine(10,15,10,85);
g.drawLine(60,15,60,85);

/*Cube*/
g.drawString("(b).Cube",95,110);
g.drawRect(80,10,50,50);
g.drawRect(95,25,50,50);
g.drawLine(80,10,95,25);
g.drawLine(130,10,145,25);
g.drawLine(80,60,95,75);
g.drawLine(130,60,145,75);

/*Square Inside A Circle*/


g.drawString("(c).Square Inside A Circle",150,110);
g.drawOval(180,10,80,80);
g.drawRect(192,22,55,55);

/*Circle Inside a Square*/


g.drawString("(d).Circle Inside a Square",290,110);
g.drawRect(290,10,80,80);
g.drawOval(290,10,80,80);

/*Polygon*/
g.drawString("(e).Polygon",90,250);
int a[]={200,400,200,400}; int
b[]={200,500,500,200};
g.drawPolygon(a,b,4);
}
}
/*
<applet code="Shapes.class" width=500 height=500></applet>
*/
Output:
Name: Sahil Surve
SYCO-B
Roll No.: 102

JAVA PROGRAMMING
(JPR-22412)
Experiment No. 31 & 32
Develop a program for implementation of I/O Stream and
File Stream Classes.

Input:
import java.io.*;
import java.util.*;
class ReadWrite
{
public static void main(String[] args) throws
FileNotFoundException,IOException
{
String s,s1;
FileOutputStream fo =new FileOutputStream("temp.txt");
Scanner sc=new Scanner(System.in);
System.out.println("enter the data to be written to file:");
s=sc.nextLine(); byte b[]=s.getBytes();
fo.write(b);
System.out.println("write operation in file performed successfully");
FileInputStream fin =new FileInputStream("temp.txt"); int
x=0;while((x=fin.read())!=-1)
System.out.print((char)x);
System.out.println("\nread operation in file performed successfully");
}
}
Output:
Input:
import java.io.*; class
filecopy1
{
public static void main(String args[]) throws IOException
{
System.out.println("Sahil Surve_102");
FileReader fr=new FileReader("filecopy1.java");
FileWriter fo=new FileWriter("filecopy2.java");
int ch; try
{
while((ch=fr.read())!= -1)
{ fo.write(ch);
}
System.out.println("file copied successfully");
fr.close(); fo.close();
} finally
{ if(fr!=null)
fr.close();
if(fo!=null) fo.close();
}}}
Output:

Input:
import java.io.*; public
class WriteBytes {
public static void main (String[] args)
{
System.out.println("Sahil Surve_102");
byte cities[] = {'M', 'e', 'l', 'b', 'o', 'u', 'r', 'n', 'e', '\n', 'S', 'y','d', 'n', 'e', 'y', '\n'};
FileOutputStream outFile;
try
{
outFile = new FileOutputStream("City.txt");
outFile.write(cities); outFile.close();
}
catch(IOException e)
{
System.out.println("Exception generated");
}
}

Output:
Input:
import java.io.*; public
class FileDisplay {
public static void main (String[] args)
{ if(args.length != 1)
{
System.out.println("Error: in sufficient arguments");
System.out.println("Usage - java FileDisplay SourceFile");
System.exit(-1);
}
try {
FileReader srcFile = new FileReader(args[0]); int
ch;
while((ch=srcFile.read()) != -1) System.out.print((char)ch);
srcFile.close();
}
catch(IOException e)
{
System.out.println(e);
System.exit(-1);
}}
}
Output:

You might also like