Nothing Special   »   [go: up one dir, main page]

Oopj - Lab Manual

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 26

Your Name Enroll No:number

PTACTICAL-1

AIM: - Install JDK and Setup a Java Programming


development environment by using: 1) Command Prompt
(SET PATH command and using Environment Variable). 2)
Any open source IDE (Eclipse, Jcreater etc)

Step 1: Download and Install Java Development Kit (JDK)

1
Your Name Enroll No:number

Step 2: Configure Environment


Variables

To set the Environment Variables, you need to search


Environment Variables in the Task Bar and click on “Edit the
system environment variables”.

Under the Advanced section, Click on “Environment Variables”.

2
Your Name Enroll No:number

Under System variables, select the “Path” variable and click on


“Edit”. Click on “New” then paste the Path Address i.e.
C:\Program Files\Java\jdk-{YOUR_JDK_VERSION}\bin.
Click on “OK”.

Step 3: Check the Java Version


Open Command Prompt and enter the following commands
java –version
javac -version

3
Your Name Enroll No:number

4
Your Name Enroll No:number

PRCTICAL-2

AIM: Test the java development environment setup by


implementing a simple java program (print: “OOP with
JAVA”).

INPUT:

class oop
{
public static void main(String arg[])
{
System.out.print("oop with java");
}
}
OUTPUT:
oop with java

5
Your Name Enroll No:number

PRACTICAL-3

AIM: Develop a basic java program that demonstrates data


types of JAVA.

INPUT:

class data2
{
public static void main (String arg[])
{
char c ='K';
short s=1234;
byte b=12;
int i=125349;
float f=13.68437282f;long l=3632645;
double d =34.847739382293382;
{
System.out.println("char:"+ c);
System.out.println("short:"+ s);
System.out.println("byte:"+ b);
System.out.println("int:"+ i);
System.out.println("float:"+ f);
System.out.println("double:"+d);
System.out.println("long:"+ l);
}
}
}
OUTPUT:
char:K
short:1234
byte:12
int:125349
float:13.684373
double:34.84773938229338
long:3632645

6
Your Name Enroll No:number

PRCTICAL-4

AIM: Develop a Java program to swap two numbers


without using a temporary variable and with using a
temporary variable (use command line argument to accept
value from user).

INPUT:
//without using temporary varible

class Swap
{public static void main (String arg[])
{
int a=20;
int b=30;
a=a+b;
b=a-b;
a=a-b;
{
System.out.println(“a:”+a);
System.out.println(“b:”+b);
}
}
}
OUTPUT:
a:30
b:20
//Using temporary variable(use cmd)
class Argument
{
public static void main(String arg[])
{
if(arg.length !=2)
{
System.out.println("please provide two number from command
line
argument");

7
Your Name Enroll No:number

return;
}
int x=Integer.parseInt(arg [0]);
int y=Integer.parseInt(arg [1]);
{int temp=x;
x=y;
y=temp;
System.out.println(“x”+x);
System.out.println(“y”+y);
}
}
}
OUTPUT:
x:6
y:12

8
Your Name Enroll No:number

PRACTICAL-5

AIM: Develop programs to demonstrate use of - 1) if


statement and its different form 2) switch case statement

INPUT:
// if-else statement
class If
{
public static void main(String arg[])
{
int x=70;
if(x<45)
{
System.out.println("pass in exam");
}
else
{System.out.println("failed in exam");
}
}
}
OUTPUT:

failed in exam

//else-if

class Else
{
public static void main(String arg[])
{
int number=3;
if(number>3)
{
System.out.println("POSITIVE");
}
else if(number<0)

9
Your Name Enroll No:number

{
System.out.println("NEGATIVE");
}
else
{
System.out.println("ZERO");
}
}
}
OUTPUT:
ZERO

//nested if-else

class Switch
{
public static void main(String arg[])
{
int age=25;
int weight=50;
if(age>=25)
{
if(weight>48)
{
System.out.println("you are eligiable for donated blood");
}
else
{
System.out.println("you are eligiable for donated blood");
}
}
else
{
System.out.println("Age must be greated than 18 ");
}
}
}
OUTPUT:

10
Your Name Enroll No:number

you are eligiable for donated blood

2] Switch case statement:

class Switch
{
public static void main(String arg[])
{
String Branch=("It");
switch(Branch)
{
case "Civil":
System.out.println("Mittal");
break;
case "Mechanical":
System.out.println("Kriya");
break;
case "Computer":
System.out.println("Krupa");
break;
case "It":
System.out.println("Khushi");
break;
default:
System.out.println("invalid Branch");
}
System.out.println("khushi is " + Branch + " Student");
}
}
OUTPUT:
Khushi
khushi is It Student

11
Your Name Enroll No:number

PRACTICAL-6

AIM:Develop program to demonstrate use of1) for loop 2)


‘while’ and ‘do while’ loop

INPUT:
//for loop

class For
{
public static void main (String arg[])
{
int s ,r ,k=6;
{
for(s=1; s<=k; s++)
{
for(r=1; r<=s; r++)
{
System.out.print("*");
}
System.out.println( );
}
}
}
}
2]while and do while

class sum
{
public static void main(String arg[])
{
int x=12 ,sum=0;
{
while(x<=13)
{
sum=sum+x;
{

12
Your Name Enroll No:number

x++;
{
System.out.println("Addition:"+sum);
}
}
}
}
}
OUTPUT:
Addition:12
Addition:25

//Do while
class While
{
public static void main(String arg[])
{
int x=2;
do
{
System.out.println(x);
x++;
}
while(x<=20);
}
}
OUTPUT:
2
3
4
5
6
7
8
9
10
11

13
Your Name Enroll No:number

12
13
14
15
16
17
18
19
20

14
Your Name Enroll No:number

PRACTICAL-7

AIM:Develop a Java program to find maximum and


minimum numbers from array elements.

INPUT:

import java.util.Arrays;
class Array
{
public static void main(String args[])
{
int a[]={1,423,6,46,34,23,13,53,4};
Arrays.sort(a);
System.out.println("min-"+a[0]+" max-"+a[a.length-1]);
}
}
OUTPUT:
min-1 max-423

15
Your Name Enroll No:number

PRACTICAL-8

AIM:Develop a basic java program that demonstrates the


use of Class & Object.

INPUT:

class Student
{
int id;
String name;
}
class TestStudent2
{
public static void main(String args[])
{
Student s1=new Student();
s1.id=101;
s1.name="sujal";
System.out.println(s1.id+" "+s1.name);
}
}

OUTPUT:
101 sujal

16
Your Name Enroll No:number

PRACTICAL-9

AIM:Develop a java program to find the factorial of a given


number using a recursive function

INPUT:
class Factorial
{
static int factorial(int n)
{
if(n!=0)
return n*factorial(n-1);
else
return 1;
}
public static void main (String args[])
{
int number=6,result;
result=factorial(number);
System.out.println(number+ "factorial" +result);
}
}
OUTPUT:
6factorial:720

17
Your Name Enroll No:number

PRACTICAL-10

AIM:Develop a java program that demonstrates method


overloading

INPUT:

class Adder
{
static int add(int a,int b)
{
return a+b;
}
static double add(double a,double b)
{
return a+b;
}
class TestOverloading
{
public static void main(String arg[])
{
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(12.3,12.6));
}
}
}
OUTPUT:
22
24.9

18
Your Name Enroll No:number

PRACTICAL-11

AIM:Develop a program for implementation of different


functions of String class.

INPUT:

class operation
{
public static void main(String arg[])
{
String s="String operation";
System.out.println(s);
System.out.println(s.startsWith("st"));
System.out.println(s.endsWith("n"));
System.out.println(s.charAt(0));
System.out.println(s.charAt(3));
System.out.println(s.length());
System.out.println(s.substring(2));
System.out.println(s.substring(0,2));
}
}

OUTPUT:
String operation
false
true
S
i
16
ring operation
St

19
Your Name Enroll No:number

PRACTICAL-12

AIM:Develop a program for implementation of Wrapper


Class to convert primitive value into object (Boxing) and
object into primitive value (Un-boxing).

INPUT:

class Wrapper
{
public static void main(String args[])
{
byte b=10;
short s=20;
int i=30;
long l=40;
float f=50.0F;
double d=60.0D;
char c='a';
boolean b2=true;

//Autoboxing: Converting primitives into objects


Byte byteobj=b;
Short shortobj=s;
Integer intobj=i;
Long longobj=l;
Float floatobj=f;
Double doubleobj=d;
Character charobj=c;
Boolean boolobj=b2;

System.out.println("object values:");
System.out.println("Byte object: "+byteobj);
System.out.println("Short object: "+shortobj);
System.out.println("Integer object: "+intobj);
System.out.println("Long object: "+longobj);
System.out.println("Float object: "+floatobj);

20
Your Name Enroll No:number

System.out.println("Double object: "+doubleobj);


System.out.println("Character object: "+charobj);
System.out.println("Boolean object: "+boolobj);

//Unboxing: Converting Objects to Primitives


byte bytevalue=byteobj;
short shortvalue=shortobj;
int intvalue=intobj;
long longvalue=longobj;
float floatvalue=floatobj;
double doublevalue=doubleobj;
char charvalue=charobj;
boolean boolvalue=boolobj;

System.out.println(" primitive values:");


System.out.println("byte value: "+bytevalue);
System.out.println("short value: "+shortvalue);
System.out.println("int value: "+intvalue);
System.out.println("long value: "+longvalue);
System.out.println("float value: "+floatvalue);
System.out.println("double value: "+doublevalue);
System.out.println("char value: "+charvalue);
System.out.println("boolean value: "+boolvalue);
}
}

OUTPUT:
object values:

Byte object: 10
Short object: 20
Integer object: 30
Long object: 40
Float object: 50.0
Double object: 60.0
Character object: a

21
Your Name Enroll No:number

Boolean object: true

primitive values:

byte value: 10
short value: 20
int value: 30
long value: 40
float value: 50.0
double value: 60.0
char value: a
boolean value: true

22
Your Name Enroll No:number

PRACTICAL-13

AIM:Develop a program with a static block and show that it


will be executed before the main ( ) method in a class.

INPUT:

class Static
{

static
{
System.out.println("Inside Static Block.");
}

public static void main(String args[])


{
System.out.println("Inside main method.");
}
}

OUTPUT:
Inside Static Block.
Inside main method.

23
Your Name Enroll No:number

PRACTICAL-14

AIM:Develop a program to demonstrate use of static


functions.

INPUT:

class Demo
{

void display()
{
System.out.println("A non-static function is called.");
}

static void show()


{
System.out.println("The static function is called.");
}
}
class Static
{
public static void main(String args[])
{

Demo obj = new Demo();

obj.display();

Demo.show();
}
}

OUTPUT:
A non-static function is called.
The static function is called.

24
Your Name Enroll No:number

PRACTICAL-15

AIM:Develop a program to demonstrate use of ‘this’


keyword. Check whether ‘this’ can access the private
members of the class or not.Develop

INPUT:
class Student
{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee)
{
this.rollno=rollno;
this.name=name;
this.fee=fee;
}
void display(){System.out.println(rollno+" "+name+" "+fee);
}
}

class This
{
public static void main(String args[])
{
Student s1=new Student(111,"Khushi",5000f);
Student s2=new Student(112,"Mittal",6000f);
s1.display();
s2.display();
}
}

OUTPUT:
111 Khushi 5000.0
112 Mittal 6000.0

25
Your Name Enroll No:number

26

You might also like