35 Java
35 Java
PRACTICAL FILE OF
JAVA PROGRAMMING LAB
(PCC-CSE210P)
DEPARTMENT OF
COMPUTER SCIENCE &ENGINEERING
INDEX
5 Overloading 9
6 Polymorphism 10-11
7 String 12
8 Array 13
9 Interface 14
11 Threading 16
12 Packages 17
13 Applet 18
2
`
Experiment 1: Class, object and method
class rectangle {
int length,width;
void getData(int x,int y) //method to get dimensions
{
length=x;
width=y;
}
int rectArea() //method to calculate area
{
return (length*width);
}
}
//creating a class having main method
public class rectangleArea {
Output:
Area of rectangle 1 = 300
Area of rectangle 2 = 40
3
`
Experiment 2: Loops:
}while(i<=10);
for(i=11;i<15;i++) //repeating using for loop
{
System.out.println("Value of " + i+ " % 10 = "+ i%10 );
}
Output:
value of i = 1
value of i = 2
value of i = 3
value of i = 4
Value of 5 x 10 = 50
Value of 6 x 10 = 60
Value of 7 x 10 = 70
Value of 8 x 10 = 80
Value of 9 x 10 = 90
Value of 10 x 10 = 100
Value of 11 % 10 = 1
Value of 12 % 10 = 2
Value of 13 % 10 = 3
Value of 14 % 10 = 4
4
`
}
Output: Positive Number
Using Switch
public class conditionals {
Outputs:
5
`
Experiment 4: Inheritance
class one
{
public void m1()
{
System.out.print("This is ");
}
}
}
Output: This is Single inheritance
b) Multilevel inheritance
class one
{
public void m1()
{
System.out.print("This ");
}
}
6
`
obj.m1();
obj.m2();
obj.m3();
c. Hierarchical inheritance
class one
{
public void m1()
{
System.out.print("This ");
}
}
7
`
d. multiple inheritance
interface one
{
public void m1();
}
interface two
{
public void m2();
}
8
`
Experiment 5: Overloading
class a {
a() {
System.out.println("This is default constructor");
}
a(int n) {
System.out.println("This is parameterized constructor with value = " + n);
}
Output:
This is default constructor
This is parameterized constructor with value = 10
30
114.441
9
`
class OperatorOVERDDN {
Output:
Sum = 50
Concatenated String - Prince Sharma
class Parent {
void Print()
{
System.out.println(" this is parent class");
}
}
10
`
class subclass1 extends Parent {
void Print()
{
System.out.println("this is subclass1");
}
}
void Print()
{
System.out.println("this is subclass2");
}
}
public class methodOverriding {
Outputs:
this is subclass1
this is subclass2
11
`
Outputs:
char at index 5 = e
convert whole string to Upper case : PRINCE SHARMA
Prince Sharma Rno = 180010130075
Length of string - 13
check if 'Prince Sharma' contains 'K' : false
12
`
Outputs:
Elements in Array are :
18 34 23 6 2 77 32 44 221 8 5
Number of even numbers in array are : 7
13
`
}
class linkall implements Two
{
public void one()
{
System.out.println("Method from Interface 1");
}
public void two()
{
System.out.println("Method from Interface 2");
}
}
public class InterFaces {
Output:
Method from Interface 1
Method from Interface 2
14
`
Outputs:
There is an error --> java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for
length 3
15
`
}
catch (Exception e)
{
// Throwing an exception
System.out.println ("Exception is caught");
}
}
}
Outputs:
Thread 12 is running
Thread 13 is running
Thread 15 is running
Thread 16 is running
Thread 18 is running
Thread 14 is running
Thread 17 is running
Thread 19 is running
16
`
package newpack;
package javaFile;
import newpack.pack;
public class Packages {
Outputs:
THIS IS FROM PACKAGE JavaFile
This is from package newpack
17
`
Experiment 13: Applet
import java.applet.Applet;
import java.awt.Graphics;
18