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

35 Java

Uploaded by

kushamrathee15
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

35 Java

Uploaded by

kushamrathee15
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

A

PRACTICAL FILE OF
JAVA PROGRAMMING LAB
(PCC-CSE210P)

DEPARTMENT OF
COMPUTER SCIENCE &ENGINEERING

SUBMITTED TO: SUBMITTED BY:


Renu Mam Tripti
Asst. Professor Roll No. 220010130065
Dept. of CSE B.Tech CSE B1
GJUS&T,Hisar 4th Sem/ 2nd Year

GURU JAMBESHWAR UNIVERSITY OF


SCIENCE AND TECHNOLOGY, HISAR,
HARYANA-125001
`

INDEX

S.NO Experiment Page no. Remark


1 Class, object and method 3
Loops:
2. a) For 4
b) While
c) do-while
3. If-else and switch 5
4. Inheritance 6-8

5 Overloading 9
6 Polymorphism 10-11
7 String 12

8 Array 13

9 Interface 14

10. Exception handling 15

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 {

public static void main(String[] args) {


int area;
rectangle r1=new rectangle(); //creating objects
rectangle r2=new rectangle();
r1.getData(15,20);
area= r1.rectArea(); //accessing methods with objects
System.out.println("Area of rectangle 1 = "+ area);
r2.getData(5,8);
area= r2.rectArea();
System.out.println("Area of rectangle 2 = "+ area);

Output:
Area of rectangle 1 = 300
Area of rectangle 2 = 40

3
`

Experiment 2: Loops:

public class loops {

public static void main(String[] args) {


int i;
i=1;
while(i<5)//repeating using while loop
{
System.out.println("value of i = "+ i);
i++;
}
i=5;
do { //Repeating using do-while loop
System.out.println("Value of "+ i +" x 10 = "+ i*10 );
i++;

}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
`

Experiment 3: if-else and switch.

public class conditionals {

public static void main(String[] args) {


int n;
n=19;
if(n>0)
System.out.println("Positive Number");
else if(n<0)
System.out.println("Negative Number");
else System.out.println("Equals to Zero");
}

}
Output: Positive Number

Using Switch
public class conditionals {

public static void main(String[] args) {


int n,temp;
n=1232;
System.out.println(n + " IN REVERSE ORDER IN WORDS ");
while(n>0)
{
temp=n%10;
switch(temp)
{
case 1:System.out.print(" One");break;
case 2:System.out.print(" Two");break;
case 3:System.out.print(" Three");break;
case 4:System.out.print(" Four");break;
case 5:System.out.print(" Five");break;
case 6:System.out.print(" Six");break;
case 7:System.out.print(" Seven");break;
case 8:System.out.print(" Eight");break;
case 9:System.out.print(" Nine");break;

case 0:System.out.print(" Zero");break;


default:System.out.print("Enter a valid number");
}
n=n/10;
}
}

Outputs:

1232 IN REVERSE ORDER IN WORDS


Two Three Two One

5
`

Experiment 4: Inheritance
class one
{
public void m1()
{
System.out.print("This is ");
}
}

class two extends one


{
public void m2()
{
System.out.println("Single inheritance ");
}
}

public class inheritanceMain {

public static void main(String[] args) {


two obj = new two();
obj.m1();
obj.m2();

}
Output: This is Single inheritance

b) Multilevel inheritance
class one
{
public void m1()
{
System.out.print("This ");
}
}

class two extends one


{
public void m2()
{
System.out.print("one is ");
}
}

class three extends two


{
public void m3()
{
System.out.print("Multilevel inheritance");
}
}
public class inheritanceMain {

public static void main(String[] args) {


three obj = new three();

6
`
obj.m1();
obj.m2();
obj.m3();

Outputs: This one is Multilevel inheritance

c. Hierarchical inheritance
class one
{
public void m1()
{
System.out.print("This ");
}
}

class two extends one


{
public void m2()
{
System.out.print("one is ");
}
}

class three extends one


{
public void m3()
{
System.out.print("Hierarchical inheritance");
}
}

public class inheritanceMain {

public static void main(String[] args) {


three obj = new three();
two obj2=new two();
obj.m1();
obj2.m2();
obj.m3();

Output: This one is Hierarchical inheritance

7
`

d. multiple inheritance
interface one
{
public void m1();
}

interface two
{
public void m2();
}

interface three extends one,two


{
public void m3();
}
class child implements three
{
public void m1() {
System.out.print("this one ");
}

public void m2()


{
System.out.print("is");
}
public void m3()
{
System.out.print(" Multiple inheritance with interfaces");
}
}

public class inheritanceMain {

public static void main(String[] args) {


child c = new child();
c.m1();
c.m2();
c.m3();

Output: this one is Multiple inheritance with interfaces

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);
}

int add(int a, int b) {


return a + b;
}

double add(double a, double b) {


return a + b;
}
}

public class overload {

public static void main(String[] args) {


a obj1=new a();//calling default constructor
a obj2=new a(10);//calling parameterized constructor
System.out.println(obj1.add(10, 20));
System.out.println(obj1.add(112.22, 2.221));
}

Output:
This is default constructor
This is parameterized constructor with value = 10
30
114.441

9
`

Experiment 6: Polymorphism in java


/*In java, Only “+” operator can be overloaded:
To add integers
To concatenate strings
*/

class OperatorOVERDDN {

void operator(String str1, String str2)


{
String s = str1 + str2;
System.out.println("Concatenated String - "
+ s);
}

void operator(int a, int b)


{
int c = a + b;
System.out.println("Sum = " + c);
}
}

public class operatorOverloading {

public static void main(String[] args) {


OperatorOVERDDN obj = new OperatorOVERDDN();
obj.operator(20, 30);
obj.operator("Prince ", "Sharma");

Output:

Sum = 50
Concatenated String - Prince Sharma

2. run time polymorphism

class Parent {

void Print()
{
System.out.println(" this is parent class");
}
}

10
`
class subclass1 extends Parent {

void Print()
{
System.out.println("this is subclass1");
}
}

class subclass2 extends Parent {

void Print()
{
System.out.println("this is subclass2");
}
}
public class methodOverriding {

public static void main(String[] args) {


Parent a;
a = new subclass1();
a.Print();
a = new subclass2();
a.Print();
}
}

Outputs:

this is subclass1
this is subclass2

11
`

Experiment 7: Strings in java

public class string {

public static void main(String[] args) {


String name;
name="Prince Sharma";
System.out.println("char at index 5 = " + name.charAt(5));

System.out.println("convert whole string to Upper case : " + name.toUpperCase());


System.out.println(name.concat(" Rno = 180010130075")); //concatenate 2 strings
System.out.println("Length of string - " + name.length());
System.out.println("check if '"+ name + "' contains 'K' : " + name.contains("K"));
}

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
`

Experiment 8: Arrays in java


public class arrays {

public static void main(String[] args) {


int[] a= {18,34,23,6,2,77,32,44,221,8,5};
int i,k=0;
System.out.println("Elements in Array are : ");
for(i=0;i<a.length;i++)
{
System.out.print(a[i]+ " ");
}
for(i=0;i<a.length;i++)
{
if(a[i]%2==0)
{
k++;
}
}

System.out.println("\nNumber of even numbers in array are : "+ k);


}

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
`

Experiment 9: Interfaces in java.


interface One
{
public void one();
}
interface Two extends One
{
public void two();

}
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 {

public static void main(String[] args) {


linkall obj =new linkall();
obj.one();
obj.two();

Output:
Method from Interface 1
Method from Interface 2

14
`

Experiment 10: Exceptions handling in Java

public class Exceptions {

public static void main(String[] args) {


try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("There is an error --> " + e);
}
}
}

Outputs:
There is an error --> java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for
length 3

15
`

Experiment 11: Threading in Java


class MultithreadingDemo extends Thread
{
public void run()
{
try
{
// Displaying the thread that is running
System.out.println ("Thread " +
Thread.currentThread().getId() +
" is running");

}
catch (Exception e)
{
// Throwing an exception
System.out.println ("Exception is caught");
}
}
}

public class thread {

public static void main(String[] args) {

int n = 8; // Number of threads


for (int i=0; i<n; i++)
{
MultithreadingDemo object = new MultithreadingDemo();
object.start();
}

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
`

Experiment 12: Packages in java

package newpack;

public class pack {

public static void main(String[] args) {


System.out.println("This is from package newpack");

package javaFile;
import newpack.pack;
public class Packages {

public static void main(String[] args) {


// TODO Auto-generated method stub
System.out.println("THIS IS FROM PACKAGE JavaFile");
pack.main(null);

Outputs:
THIS IS FROM PACKAGE JavaFile
This is from package newpack

17
`
Experiment 13: Applet

// A Hello World Applet


// Save file as HelloWorld.java

import java.applet.Applet;
import java.awt.Graphics;

// HelloWorld class extends Applet


public class HelloWorld extends Applet
{
// Overriding paint() method
@Override
public void paint(Graphics g)
{
g.drawString("Hello World", 20, 20);
}

18

You might also like