Java Lab Practical File
Java Lab Practical File
Uttar Pradesh
Practical File on
Java Programming
A report submitted for the partial fulfilment of the requirement for the
bachelor’s in computer application course (2019-2022) of Amity University.
Source Code:
public class Welcome {
System.out.println("----------------------");
System.out.println("----------------------");
Output:
Source Code:
import java.util.Scanner;
public class EvenOdd {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = reader.nextInt();
if(num % 2 == 0)
System.out.println(num + " is even");
else
System.out.println(num + " is odd");
}
Output:
Source Code:
import java.util.Scanner;
System.out.println("---------------------");
scanner.close();
fact *= i; }
System.out.println("---------------------");
Output:
4. Write a Java program that counts the number of objects created by using static
variable.
Source Code:
System.out.println("---------------------");
int n = 10;
int a = 0, b = 1, c;
c = a + b;
a = b;
b = c;
System.out.println();
System.out.println("---------------------");
Output:
import java.util.Scanner;
return true;
System.out.println("---------------------");
scanner.close();
System.out.println("---------------------");
Output:
6. Write a java program to check the given number is Armstrong Number or not.
Source Code:
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber % 10;
originalNumber /= 10;
if(result == number)
else
Output:
7. Write a Java program that prompts the user for an integer and then prints out all the
prime numbers up to that Integer.
Source Code:
import java.util.Scanner;
class PrimeNumbers
{ int n;
int p;
n=s.nextInt();
for(int i=2;i<n;i++)
{ p=0;
for(int j=2;j<i;j++)
{ if(i%j==0)
p=1;
if(p==0)
System.out.println(i);
} } }
Output:
8. Write a Java program that checks whether a given string is a palindrome or not Ex:
MADAM is a palindrome.
Source Code:
class Main {
if (str.toLowerCase().equals(reverseStr.toLowerCase())) {
else {
} } }
Output:
return a + b;
return a - b;
return a * b;
return a / b;
return a % b;
System.out.println("---------------------");
System.out.println("---------------------");
}
Output:
int a[][]={{1,1,1},{2,2,2},{3,3,3}};
int b[][]={{1,1,1},{2,2,2},{3,3,3}};
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=0;
for(int k=0;k<3;k++)
c[i][j]+=a[i][k]*b[k][j];
}//end of k loop
System.out.println();//new line }
}}
Output:
Source Code:
import java.util.Scanner;
reverseStr += str.charAt(i);
return reverseStr;
System.out.println("---------------------");
scanner.close();
Output:
11. Write a Java program to sort the elements using bubble sort
Source Code:
int n = arr.length;
int temp = 0;
//swap elements
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
} }
System.out.println();
Output:
class BinarySearchExample{
public static void binarySearch(int arr[], int first, int last, int key){
int mid = (first + last)/2;
while( first <= last ){
if ( arr[mid] < key ){
first = mid + 1;
}else if ( arr[mid] == key ){
System.out.println("Element is found at index: " + mid);
break;
}else{
last = mid - 1;
}
mid = (first + last)/2;
}
if ( first > last ){
System.out.println("Element is not found!");
}
}
public static void main(String args[]){
int arr[] = {10,20,30,40,50};
int key = 30;
int last=arr.length-1;
binarySearch(arr,0,last,key);
}
}
Output:
13. Write a java program that implements Array Index out of bound Exception using built-
in-Exception
Source Code:
class ArithmeticException_Demo {
try {
int a = 30, b = 0;
catch (ArithmeticException e) {
}
}
Output:
14. Write a java program to identify the significance of finally block in handling exceptions
Source Code:
import java.io.*;
class GFG {
System.out.println(34 / 2);
catch (ArithmeticException e) {
System.out.println("Arithmetic Exception");
// Always execute
finally {
System.out.println(
} } }
Output:
15. Write a java program that implements user defined exception
Source Code:
String str1;
*/
MyException(String str2) {
str1=str2;
class Example1{
try{
}
catch(MyException exp){
System.out.println("Catch Block") ;
System.out.println(exp) ;
Output:
16. Write a Java program that displays area of different Figures (Rectangle, Square,
Triangle) using the method overloading
Source Code:
class OverloadDemo
void area(float x)
void area(double x)
double z = 3.14 * x * x;
}
}
class Overload
ob.area(5);
ob.area(11,12);
ob.area(2.5);
Output:
Write a java program to calculate gross salary & net salary taking the following data
Source Code:
import java.util.Scanner;
double basic,da,hra,gross;
basic=obj1.nextDouble();
da=40*basic/100;
hra=20*basic/100;
gross= basic+da+hra;
Output:
17. Write a java program to find the details of the students eligible to enroll for the
examination (Students, Department combinedly give the eligibility criteria for the
enrolment class) using interfaces
Source Code:
import java.util.Scanner;
String name;
name=SC.nextLine();
roll=SC.nextInt();
math=SC.nextInt();
phy=SC.nextInt();
eng=SC.nextInt();
int total=math+eng+phy;
float perc=(float)total/300*100;
Output:
18. . Write a java program to calculate gross salary & net salary taking the following data
Source Code:
import java.util.Scanner;
public class GrossSalary {
static float calculateGrossSalary(int basicSalary, int hra, int da, int pf) {
return basicSalary + (basicSalary * (hra/100f) + (basicSalary * (da/100f)) - (basicSalary * (pf/100f)));
}
public static void main(String[] args) {
System.out.println("-----------------------");
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Basic Salary: ");
int basicSalary = scanner.nextInt();
System.out.print("Enter HRA(%): ");
int hra = scanner.nextInt();
System.out.print("Enter DA(%): ");
int da = scanner.nextInt();
System.out.print("Enter PF(%): ");
int pf = scanner.nextInt();
scanner.close();
int _grossSalary = (int) calculateGrossSalary(basicSalary, hra, da, pf);
System.out.println("Gross Salary: " + _grossSalary);
System.out.println("-----------------------");
}
Output:
19. Write a Java program to find the details of the students eligible to enroll for the
examination (Students, Department combinedly give the eligibility criteria for the
enrollment class) using interfaces
Source Code:
import java.util.Scanner;
interface StudentDetails {
int id = 16;
void getData();
int attendance = 0;
attendance = scanner.nextInt();
scanner.close();
@Override
System.out.println("-----------------------");
System.out.println("-----------------------");
}}
exam.setAttendance();
exam.getData();
Output:
Implement Operators
Left Shift Operator
Source Code:
package Shift;
Output:
Source Code:
package Shift;
Output:
Source Code:
package Shift;
class JavaOperators {
void logical() {
System.out.println("-----Logical-----");
if(num > 0 && num < 10) System.out.println(num + " is between 0 - 10");
void relational() {
System.out.println("-----Relational-----");
void bitwise() {
System.out.println("-----Bitwise-----");
public class OP {
javaOperators.logical();
javaOperators.relational();
javaOperators.bitwise();
Output:
20. Implement Exceptions
Arithmetic Exception
Source Code:
package ExeptionHandling;
int num = 7;
int num2 = 0;
try {
System.out.println(num/num2);
} catch (ArithmeticException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
Output:
package ExeptionHandling;
try {
System.out.println(arr[22]);
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
Output:
Source Code:
package ExeptionHandling;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
try {
fileReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
Output:
Source Code:
package JavaArrays;
System.out.println(i);
Output:
22. Write a Program to find the percentage and grade of multiple students using methods
Source Code:
import java.util.Scanner;
/***
@author Akshat
*/
class Student {
String name;
byte javaMarks;
byte pythonMarks;
byte cMarks;
byte cppMarks;
short totalMarks;
float percentage;
String grade;
grade = "A+";
grade = "B+";
grade = "C";
grade = "D";
else
grade = "Fail";
Student(String name, byte javaMarks, byte pythonMarks, byte cMarks, byte cppMarks) {
this.name = name;
this.javaMarks = javaMarks;
this.pythonMarks = pythonMarks;
this.cMarks = cMarks;
this.cppMarks = cppMarks;
gradeCalculate();
scanner.nextLine();
scanner.close();
students[i].display();
Output:
23. Inheritance
Constructor
Default Constructor
Source Code:
package Constructor;
class Demo {
Demo() {
System.out.println("Default Constructor");
Output:
Parameterized Constructor
Source Code:
package Constructor;
class DemoTwo {
this.num = num;
this.num2 = num2;
Output:
Constructor Overloading
Source Code:
package Constructor;
class DemoThree {
DemoThree(int ...nums) {
int sum = 0;
sum += nums[i];
System.out.println("----------");
System.out.println("----------");
Output:
Source Code:
package JavaThreads;
@Override
while(true) {
@Override
while(true) {
gun1.setPriority(Thread.MAX_PRIORITY);
gun1.start();
gun2.start();
Output:
Source Code:
package JavaThreads;
@Override
super.run();
while(true) {
System.out.println("Hello Wolrd");
@Override
super.run();
System.out.println(i);
}
public class MultiThreading {
obj2.setPriority(Thread.MAX_PRIORITY);
obj1.start();
obj2.start();
Output:
Source Code:
Output:
Source Code:
package JavaThreads;
import PrimeOrNot;
import java.util.Scanner;
class Factorial extends Thread {
int fact = 1;
int num;
Factorial(int num) {
this.num = num;
@Override
fact *= i;
try {
Thread.sleep(5);
} catch (Exception e) {
e.printStackTrace();
int num;
PrimeNumbers(int num) {
this.num = num;
@Override
try {
Thread.sleep(5);
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("----------------------------");
scanner.close();
factorial.setPriority(Thread.MIN_PRIORITY);
primeNumbers.setPriority(Thread.MAX_PRIORITY);
primeNumbers.start();
factorial.start();
Output:
Source Code:
package Searching;
import Sorting.InsertionSort;
import java.util.Scanner;
if (arr[midIndex] == num)
else
arr[i] = scanner.nextInt();
scanner.close();
arr = InsertionSort.iSort(arr);
System.out.println();
System.out.println(bSearch(num, arr));
Output:
25. Java String
String compareTo()
Source Code:
package JavaStrings;
System.out.println(str.compareTo(str2));
Output:
String concat()
Source Code:
package JavaStrings;
System.out.println(str.concat(str2));
Output:
Source Code:
package JavaStrings;
import java.lang.StringBuilder;
str.append(" Srivastava");
System.out.println(str);
Output:
Source Code:
package JavaStrings;
import java.lang.StringBuffer;
Output:
Source Code:
import java.applet.Applet;
import java.awt.Graphics;
<html>
<body>
</applet>
</body>
</html>
Output:
Animation program
Source Code:
import java.awt.*;
import java.applet.*;
Image img;
graphics.drawImage(img, i, 5, this);
try {
Thread.sleep(200);
} catch (Exception e) {
e.printStackTrace();
<html>
<body>
<applet code=JavaAnimation.class width=1200 height=900>
</applet>
</body>
</html>
Output:
Ball program
Source Code:
import java.awt.*;
import java.applet.*;
int x = 0, y = 0, p = 2, q = 1;
Thread thread;
width = getSize().width;
height = getSize().height;
thread.start();
while(true) {
x += p;
y += q;
repaint();
try {
Thread.sleep(200);
} catch (Exception e) {
e.printStackTrace();
p *= -1;
y *= -1;
graphics.setColor(Color.blue);
<html>
<body>
</applet>
</body>
</html>
Output:
Rectangle program
Source Code:
import java.awt.*;
import java.applet.*;
graphics.setColor(Color.red);
graphics.setColor(Color.blue);
<html>
<body>
</applet>
</body>
</html>
Output:
27. Creating a pom.xml file and demo maven project
Source Code:
pom.xml:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>mprogram</artifactId>
<version>1.0-SNAPSHOT</version>
<name>mprogram</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom)
-->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
App:
package com.example;
/**
Hello world!
*/
System.out.println("Hello World!");