GALGOTIAS UNIVERSITY
Plot No.2, Sector -17 A, Yamuna Expressway,
Greater Noida, Gautam Buddha Nagar, U.P.,
India - 201310.
SCHOOL OF COMPUTING SCIENCE &
ENGINEERING
Lab File On
“Java Programming.”
Course Name: Java Programming
Course Code: BCSE2333
School: SCSE Year: 2
nd
Semester: 3RD
Program: [Link]
Session: 2021-2025
Submitted By: VAIBHAV DUBEY Submitted To: MR
PRABHAKARAN M.
Title of Lab Experiments
a)Write a JAVA program to print “Hello World.”
b)WAP in java to print the values of various primitive data types.
WAP in java to demonstrate operator precedence.
WAP in java to create a class Addition and a method add() to add two numbers.
Write a program that uses length property for displaying any number of command
line arguments.
WAP in java to find the average of N numbers.
WAP in java to find out factorial of a number.
WAP in java to find out the Fibonacci series.
WAP in java to sort elements in 1D array in ascending order.
WAP in java to perform matrix addition using two 2D arrays.
WAP in java to find out the number of characters in a string.
WAP in java that implements method overloading.
Create a class Shape and override area() method to calculate area of rectangle, square
and circle.
WAP in java to demonstrate the properties of public private and protected variables
and methods (with package).
WAP that illustrates method overriding.
a) WAP in java demonstrating arithmetic exception and ArrayOutOf Bounds
Exception using try catch block
b) WAP in java to demonstrate the use of nested try block and nested catch block.
Write a program to count the number of times a character appears in a File. [Note :
The character check is case insensitive... i.e, ‘a’ and ‘A’ are considered to be the same]
WAP in java in two different ways that creates a thread by (i) extending thread class
(ii) implementing runnable interface which displays 1st 10 natural numbers.
a) WAP in java that connects to a database using JDBC & insert values into table.
b) WAP to connect to a database using JDBC and delete values from table.
Experiment 1a
Aim:- Write a JAVA program to print “Hello
World.” Code:-
public class Helloworld {
public static void main(String[] args)
{ [Link]("Hello World");
}
}
Output:-
Experiment-1b
Aim:- WAP in java to print the values of various primitive data
types Code:-
public class PrimitiveDataTypes
{ static byte a;
static short b;
static int c;
static long d;
static float e;
static double f;
static char g;
static boolean
h;
public static void main(String args[]) {
[Link]("Value of different Primitive Data Types :-");
[Link]("Value of byte : "+a);
[Link]("Value of short : "+b);
[Link]("Value of int : "+c);
[Link]("Value of long : "+d);
[Link]("Value of float : "+e);
[Link]("Value of double : "+f);
[Link]("Value of char : "+g);
[Link]("Value of byte : "+h);
}
}
Output:-
Experiment 2
Aim:- WAP in java to demonstrate operator precedence.
Code :- public class OperatorPrecedence {
public static void main(String[] args) {
int a = 10, b = 5, c = 1, result;
result = a-++c-++b;
[Link]("Result = "+result);
}
Output :-
Experiment-3
Aim:- WAP in java to create a class Addition and a method add() to add two numbers.
Code:-
class
Addition{ int
a=10,b=5; int
sum=a+b;
}
public class sum {
public static void main(String[] args)
{ Addition a1=new Addition();
[Link]("Sum of "+a1.a+" & " +a1.b+"="+[Link]);
}
}
Output :-
Experiment 4
Aim:- Write a program that uses length property for displaying any number of command line
arguments. Code:-
public class test{
public static void main(String args[])
{ for (int i = 0; i < [Link]; i++)
[Link](args[i]);
}
}
Output :-
Experiment 5
Aim:- WAP in java to find the average of N numbers.
Code :-
import [Link];
public class Average {
public static void main(String args[]) {
int i;
float n,j=0,k=0,avg;
[Link]("Enter value of N :");
Scanner sc= new Scanner([Link]);
n = [Link]();
for(i=1;i<=n;i++) {
[Link]("Enter number "+i+" :");
j=[Link]();
k=k+j;
}
avg = k/n;
[Link]("Average of given numbers are "+avg);
}
}
Output :-
Experiment 6
Aim:- WAP in java to find out factorial of a
number. Code:-
import [Link];
public class Factorial {
public static void main (String args[]) {
int n,i,fact=1; Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
n=[Link]();
[Link]();
for(i=1;i<=n;i++) {
fact=fact*i;
[Link]("Factorial of "+n+" is "+fact);
Output :-
Experiment 7
Aim:- WAP in java to find out the Fibonacci series
Code:-
import [Link];
public class Fibonacci {
public static void main(String args[])
{ int n1=0,n2=1,n3,i,count;
Scanner sc= new Scanner([Link]);
[Link]("Enter no of terms :");
count=[Link]();
[Link]();
[Link](n1+" "+n2);
for(i=2; i<count; ++i) {
n3=n1+n2;
[Link](" "+n3); n1=n2; n2=n3;
}
}
}
Output :-
Experiment-8
Aim:- WAP in java to short elements in 1D array in ascending order.
Code :-
public class Ascending {
public static void main(String args[]) {
int arr[]={3,5,36,7,8,13,25,24,1,4},temp=0;
[Link]("Elements of original array: ");
for (int i = 0; i < [Link]; i++) {
[Link](arr[i] + " ");
}
for (int i = 0; i < [Link]; i++) {
for (int j = i+1; j < [Link]; j++)
{ if(arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
[Link]();
[Link]("Elements of array sorted in ascending order: ");
for (int i = 0; i < [Link]; i++) {
[Link](arr[i] + " ");
}
}
}
Output :-
Experiment 9
Aim:- WAP in java to perform matrix addition using two 2D arrays.
Code :-
public class MatrixAddition {
public static void main(String args[])
{ int a[][]={{1,3,4},{2,4,3},{3,4,5}};
int b[][]={{1,3,4},{2,4,3},{1,2,4}};
int c[][]=new int[3][3];
[Link]("Sum of Matrix: ");
for(int i=0; i <3; i++){
for(int j=0; j<3; j++){
c[i][j]=a[i][j]+b[i][j];
[Link](c[i][j]+" ");
}
[Link]();
}
}
}
Output :-
Experiment 10
Aim:- WAP in java to find out the number of characters in a string.
Code:-
public class CountCharacter {
public static void main(String[] args) {
String string = "Simplicity is the best thing.";
int count = 0;
for(int i = 0; i < [Link](); i++)
{ if([Link](i) != ' ')
count++;
}
[Link]("Total number of characters in a string: " + count);
}
}
Output :-
Experiment 11
Aim:- WAP in java that implements method overloading.
Code:-
public class Sum {
public int sum(int x, int y) { return (x + y); }
public int sum(int x, int y, int z)
{
return (x + y + z);
}
public double sum(double x, double y)
{
return (x + y);
}
public static void main(String args[])
{
Sum s = new Sum();
[Link]([Link](10, 20));
[Link]([Link](10, 20, 30));
[Link]([Link](10.5, 20.5));
}
}
Output :-
Experiment 12
Aim:- Create a class Shape and override area() method to calculate area of rectangle, square and circle.
Code:-
class Shape {
void area(float x) {
[Link]("The area of the square is "+[Link](x, 2)+" sq units");
}
void area(float x, float y) {
[Link]("The area of the rectangle is "+x*y+" sq units");
}
void area(double x) {
double z = 3.14 * x * x;
[Link]("The area of the circle is "+z+" sq units");
}
}
public class Area {
public static void main(String args[])
{ Shape ob = new Shape();
[Link](5);
[Link](11,12);
[Link](2.5);
}
}
Output :-
Experiment 13
Aim:- WAP in java to demonstrate the properties of public private and protected variables and methods
(with package).
Code :-
// Java program to illustrate error while
// using class from different package with
// default modifier
package p2;
import p1.*;
// This class is having default access modifier
class GeekNew
{
public static void main(String args[])
{
// Accessing class Geek from package p1
Geeks obj = new Geek();
[Link]();
}
}
Output :- Compile time error
// Java program to illustrate error while
// using class from different package with
// private modifier
package p1;
class A
{
private void display()
{
[Link]("GeeksforGeeks");
}
}
class B
{
public static void main(String args[])
{
A obj = new A();
// Trying to access private method
// of another class
[Link]();
}
}
Output :- error: display() has private access in A
[Link]();
// Java program to illustrate
// protected modifier
package p2;
import p1.*; // importing all classes in package p1
// Class B is subclass of A
class B extends A
{
public static void main(String args[])
{
B obj = new B();
[Link]();
}
}
Output :- GeeksforGeeks
Experiment-14
Aim:- WAP that illustrates method overriding
Code :-
class subtraction{
public double sub(double x, double y)
{ return (x - y);
}
}
public class Sum extends subtraction
{ public int sum(int x, int y) {
return (x + y);
}
public int sum(int x, int y, int z)
{ return (x + y + z);
}
public double sum(double x, double y) {
return (x + y);
}
public static void main(String args[])
{ Sum s = new Sum();
[Link]("Sum of 10 &20 ="+[Link](10, 20));
[Link]("Sum of 10 , 20& 30="+[Link](10, 20, 30));
[Link]("Sum of 10.5 & 20.5="+[Link](10.5, 20.5));
[Link]("Subtraction of 20.5 & 10.5="+[Link](20.5, 10.5));
}
}
Output :-
Experiment 15a
Aim:- WAP in java demonstrating arithmetic exception and ArrayOutOf Bounds Exception using try catch block
Code :-
public class test3{
public static void main(String args[]) {
[Link]("1");
[Link]("2");
[Link]("3");
try{
[Link](100/0);
} catch(ArithmeticException e)
{ [Link]("Arithmatic exception is found");
}
[Link]("5");
[Link]("6");
int ar[] = { 1, 2, 3, 4, 5 };
try {
for (int i = 0; i <= [Link]; i++)
[Link](ar[i]+" ");
} catch (ArrayIndexOutOfBoundsException e) { [Link]("\
nArrayOutOf Bounds Exception caught");
}
}
}
Output :-
Experiment 15b
Aim:- WAP in java to demonstrate the use of nested try block and nested catch block.
Code :-
public class test {
public static void main(String args[]){
//outer try block
try{
//inner try block 1
try{
[Link]("going to divide by 0");
int b =39/0;
}
//catch block of inner try block 1
catch(ArithmeticException e) {
[Link](e);
}
//inner try block 2
try{
int a[]=new int[5];
//assigning the value out of array bounds
a[5]=4;
}
//catch block of inner try block 2
catch(ArrayIndexOutOfBoundsException e) {
[Link](e);
}
[Link]("other statement");
}
//catch block of outer try block
catch(Exception e) {
[Link]("handled the exception (outer catch)");
}
[Link]("normal flow..");
}
}
Output :-
Experiment 16
Aim:- Write a program to count the number of times a character appears in a File.
Code :-
import [Link];
public class CountOccuranceOfChar1 {
public static void main(String args[])
{ String str;
int i, len;
int counter[] = new int[256];
Scanner scanner = new Scanner([Link]);
[Link]("Please enter a string: ");
//reading a string from the user
str = [Link]();
//finds the length of the
string len = [Link]();
// loop through the string and count frequency of every character and store
// it in counter array
for (i = 0; i < len; i++) {
counter[(int) [Link](i)]++;
} //print Frequency of characters
for (i = 0; i < 256; i++) {
if (counter[i] != 0) {
//prints frequency of characters
[Link]((char) i + " --> " + counter[i]);
}
}
}
}
Output :-
Experiment-17
Aim:- WAP in java in two different ways that creates a thread by (i) extending thread class (ii)
implementing runnable interface which displays 1st 10 natural numbers.
Code :-
class MyNumber implements Runnable
{ public void run() {
try {
for(int i=1;i<=10;i++) {
[Link](5000);
[Link](i);
}
} catch(InterruptedException e) {
[Link]("Exception..."+e);
}
}
}
class Number {
public static void main(String [] args)
{ MyNumber m1 = new MyNumber();
Thread thread = new Thread(m1);
[Link]();
}
}
Output :-
Experiment-18
Aim:- WAP in java that connects to a database using JDBC & insert values into table.
Code :-
import [Link];
import [Link];
import [Link];
import [Link];
public class JDBC {
static final String DB_URL = "jdbc:mysql://localhost/TUTORIALSPOINT";
static final String USER = "guest";
static final String PASS = "guest123";
public static void main(String[] args) {
// Open a connection
try(Connection conn = [Link](DB_URL, USER, PASS);
Statement stmt = [Link](); ) {
// Execute a query
[Link]("Inserting records into the table...");
String sql = "INSERT INTO Registration VALUES (100, 'Zara', 'Ali', 18)";
[Link](sql);
sql = "INSERT INTO Registration VALUES (101, 'Mahnaz', 'Fatma', 25)";
[Link](sql);
sql = "INSERT INTO Registration VALUES (102, 'Zaid', 'Khan', 30)";
[Link](sql);
sql = "INSERT INTO Registration VALUES(103, 'Sumit', 'Mittal', 28)";
[Link](sql);
[Link]("Inserted records into the table...");
} catch (SQLException e) {
[Link]();
}
}
}
Output :- Inserting records into the table...
Inserted records into the table...