[Go to site: main page, start]

0% found this document useful (0 votes)
10 views13 pages

Java Lab Manual for B.Tech Students

This document is a lab manual for a Java programming course at Guru Nanak Dev Engineering College, detailing practical exercises for B.Tech students in the 5th semester. It includes various coding examples covering data types, type casting, arrays, control structures, decision structures, recursion, and method overloading. Each practical section provides input code and expected output for students to follow and learn from.

Uploaded by

aditya tulsyan
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)
10 views13 pages

Java Lab Manual for B.Tech Students

This document is a lab manual for a Java programming course at Guru Nanak Dev Engineering College, detailing practical exercises for B.Tech students in the 5th semester. It includes various coding examples covering data types, type casting, arrays, control structures, decision structures, recursion, and method overloading. Each practical section provides input code and expected output for students to follow and learn from.

Uploaded by

aditya tulsyan
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

GURU NANAK DEV ENGINEERINGCOLLEGE,

LUDHIANA

DEPARTMENT OF
INFORMATIONTECHNOLOGY

[Link]. 5th SEMESTER

(Batch 2020-2024)

LAB MANUAL FOR JAVA LAB

SUBMITTED TO: SUBMITTED BY:


NAVDEEP KAUR Irshad Khan
(ASSISTANT PROFESSOR) C.R.N : 2021050
U.R.N : 2004929
Section : D3IT-A2
Practical No : 1
Aim - Handling different datatypes
Input code:
class DataTypes {

public static void main(String[] args) {


//Integral data types
byte a = 34;
short b = 100;
int c = a+b;
long d = 23424525;
//Floats decimal data types
oat e = 23.4f;
double f = 45353.3445;
//Char data types
char g = 'a';
//Boolean data types
boolean i = true;
[Link]("Integral data types output :");

[Link]("byte = "+a+"\n short = "+b+"\n int =


"+c+"\n long = "+d);
[Link]("Float Decimal data types output:");
[Link](" oat = "+e+”\n double = "+f);
[Link]("Character data types:");
[Link]("char literal : = "+g);
[Link]("Boolean data types :");
[Link]("boolean literal := "+i);
fl
fl
}

Output:

.
Practical No :2

Aim - Type Casting

Input Code

public class TypeCasting {


public static void main(String[] args) {

// Automatic casting
[Link] ("Automatic casting:");
int myInt = 9;
double myDouble = myInt;
[Link]("myInt = "+myInt);
[Link]("myDouble = "+myDouble);
// Manual casting:
[Link] ("Manual casting:");
double myDouble1 = 9.78d;
int myInt1= (int) myDouble1;
[Link]("myDouble1 = "+myDouble1);
[Link]("myInt1 = "+myInt1);
}
}

Output:
Practical No : 3

Aim - Arrays – 1D and 2 D


Input Code:

class Arrays_1D_and_2D {
public static void main(String[] args) {
String [] name = {"irshad","neha","anil"};
[Link] ("1 D Arrays display :");
[Link] (name[0]+"\n"+name[1]+"\n"+name[2]);
int [][] square= {
{1,4,9},
{16,25,36},
{49,64,81},
};
[Link] ("2D arrays display :");
[Link] (square[0][0]+" "+square[0][1]+" "+square[0][2]);
[Link] (square[1][0]+" "+square[1][1]+" "+square[1][2]);
[Link] (square[2][0]+" "+square[2][1]+" "+square[2][2]);
}
}

Output:
Practical No : 4

Aim - Various control structures


Input Code:
public class ControlStructures {
public static void loops(int num){
//for loop
[Link] ("Table display using For loops :");
for(int i = 1; i<=num; i++){
[Link] (num+"x"+i+" = "+num*i);
}
int i = 1;
[Link] ("Numbers display using While loops:”);
while (i<=num){
[Link] (i+" ");
i++;
}
[Link] ("\n Square display using Do” while loops);
int k = 1;
do{
[Link] (k*k+" ");
k++;
}while(k<=num);
}
public static void main(String[] args) {
int num = 10;
loops(num);
}
}
Output:
Practical No : 5

Aim - Various decision structures


Input Code
public class DecisionStructures {
public static void main(String[] args) {

[Link] ("If Else :");


int i = 10;
if (i < 18)
[Link](+i+" is smaller than 18");
else
[Link]("i is greater than 18");
[Link] ("If else If");
int a = 2;
int b = 3;
int c = 1;
if(a>b){
if(a>c){
[Link] (a+" is greater than");
}else{
[Link] (c+" is greater than");
}
}else if(b>c){
[Link] (b+ " is greater than");
}else {
[Link] (c + " is greater than");
}

[Link] ("switch case :");


String day = "Monday";
switch(day){
case "Monday" :
[Link] ("Today is Monday");
break;
case "Tuesday":
[Link] ("Today is Tuesday");
break;
case "Wednesday":
[Link] ("Today is Wednesday");
break;
case "Thursday":
[Link] ("Today is Thursday");
break;
case "Friday":
[Link] ("Today is Friday");
break;
case "Saturday" :
[Link] ("Today is Saturday");
break;
case "Sunday":
[Link] ("Today is Sunday");
break;
}
}
}
Output
Practical No : 6

Aim - Recursion

Input Code
package basicJava1;

public class Recursion {


public static int sumOfNaturalNumber(int n){
if(n==0)
return n;
return n+sumOfNaturalNumber (n-1);
}
public static void main(String[] args) {
int n = 5;
[Link] ("sum of "+n+" natural number
is :"+sumOfNaturalNumber(n));
}
}

Output
Practical No : 7

Aim - Method Overloading by passing objects as


arguments

Input Code

package basicJava1;

class Point {
private int x;
private int y;
Point(int x, int y){
this.x = x;
this.y = y;
}
public double ndDistance( Point p2){
double d = [Link] ([Link]((p2.x-this.x),2)+[Link]((p2.y-this.y),2));
return d;
}

@Override
public String toString() {
return "Point{" +
"x=" + x +
", y=" + y +
'}';
}
}
public class MethodOverloadingByPassingObjects {
public static void main(String[] args) {
Point p1 = new Point (10,20);
Point p2 = new Point (20,10);
[Link] (p1);
[Link] (p2);
fi
double distance = p1. ndDistance (p2);
[Link] ("Distance between two points : "+distance);

}
}

Output :
fi

You might also like