Java Practicals
Java Practicals
1. Introduction to Java
Java is a high-level, object-oriented programming language developed
by Sun Microsystems (now owned by Oracle Corporation). It is widely
used for building web applications, mobile applications, enterprise
applications, and more. Java is known for its platform independence,
security, and robustness.
Definition:
Java is a general-purpose, class-based, object-oriented
programming language that follows the "Write Once, Run
Anywhere" (WORA) principle, meaning that Java programs can run
on any platform with a Java Virtual Machine (JVM).
Brief History:
Java was developed by James Gosling and his team at Sun
Microsystems in the early 1990s. Initially called "Oak," it was later
renamed Java in 1995. Java gained popularity due to its portability
and security features, leading to its widespread adoption in
various domains, including web development, mobile applications
(Android), and enterprise software.
Features:
Java offers several key features:
1. Platform Independence – Java programs run on any device with
a JVM.
2. Object-Oriented – Supports concepts like classes, objects,
inheritance, and polymorphism.
3. Simple and Easy to Learn – Java's syntax is similar to C and C++.
4. Robust and Secure – Provides strong memory management and
security mechanisms.
5. Multithreading – Supports concurrent execution of multiple
threads.
6. High Performance – Uses Just-In-Time (JIT) compilation for
better performance.
7. Automatic Garbage Collection – Manages memory efficiently
by removing unused objects.
8. Rich API – Offers a wide range of built-in libraries for
networking, I/O, and more.
import java.util.Scanner;
} else {
voter");
Output:
} else {
three");
three");
} else {
three");
Output:
3. Demonstrate the use of loops
WAP to find the factorial of a number using ‘For loop’
import java.util.Scanner;
public class factorial {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int temp = 1;
for (int i = 1; i <= num; i++) {
temp *= i;
}
System.out.println("Factorial is " +
temp);
}
}
Output:
WAP to find the first 10 number of Fibonacci series
Output:
WAP to print the table by user input
import java.util.Scanner;
i));
Output:
WAP to find the sum of harmonic series
import java.util.Scanner;
System.out.print("First term:");
int a = sc.nextInt();
System.out.print("Comman difference:");
int d = sc.nextInt();
System.out.print("Number of terms:");
int n = sc.nextInt();
denominator);
System.out.println(value);
}
}
Output:
System.out.print("2 3 5 7 ");
System.out.print(i+" ");
Output:
WAP to find the LCM of two numbers
import java.util.Scanner;
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
return a;
lcm_value);
}
}
Output:
}
Output:
4. Switch Case
WAP to make Simple Calculator
import java.util.Scanner;
public class calculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("+ -> 1 - -> 2 x -> 3 / -> 4");
System.out.println("Choose from given options");
System.out.print("Enter your choice:");
int choice = sc.nextInt();
System.out.print("Enter your first number:");
int num1 = sc.nextInt();
System.out.print("Enter your second number:");
int num2 = sc.nextInt();
switch (choice) {
case 1 -> System.out.printf("%d + %d = %d", num1, num2, num1 +
num2);
case 2 -> System.out.printf("%d - %d = %d", num1, num2, num1 -
num2);
case 3 -> System.out.printf("%d * %d = %d", num1, num2, num1 *
num2);
case 4 -> System.out.printf("%d / %d = %d", num1, num2, num1 /
num2);
}
}
}
Output:
Output:
WAP to create banking system for deposit and withdrawal
of money
import java.util.Scanner;
public class banking_system {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to Banking System");
System.out.println("Please select a option given below");
System.out.println("Deposit - 1 Withdrawal - 2");
System.out.print("Enter your choice:");
int choice = sc.nextInt();
int amt = 1000;
switch (choice) {
case 1:
System.out.print("Enter your amount for deposit:");
int temp = sc.nextInt();
amt += temp;
System.out.println("Amount Deposited successfully");
System.out.println("Do you want to see updated
amount?");
System.out.println("Press '1' for Yes and '2' for No");
System.out.print("Enter:");
int ch = sc.nextInt();
if (ch == 1) {
System.out.println("Amount: "+amt);
System.out.println("Thank you for banking with
us!");
} else if (ch == 2) {
System.out.println("Thank you for banking with
us!");
} else {
System.out.println("Wrong input entered");
}
break;
case 2:
System.out.print("Enter your amount for withdrawal:");
int temp1 = sc.nextInt();
amt -= temp1;
System.out.println("Amount Withdrawal successfully");
System.out.println("Do you want to see updated
amount?");
System.out.println("Press '1' for Yes and '2' for No");
System.out.print("Enter:");
int ch1 = sc.nextInt();
if (ch1 == 1) {
System.out.println("Amount: "+amt);
System.out.println("Thank you for banking with
us!");
} else if (ch1 == 2) {
System.out.println("Thank you for banking with
us!");
} else {
System.out.println("Wrong input entered");
}
break;
}
}
Output:
5. Arrays
WAP to do binary search in an array
import java.util.Scanner;
public class binary_search {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your number for search:");
int value = sc.nextInt();
int[] arr = {10, 20, 30, 40, 50, 60, 70, 80, 90};
int lb = 0, ub = arr.length;
int temp = 0;
while (true) {
temp = (lb + ub) / 2;
if (arr[temp] == value){
System.out.println("Number is found at: "+ temp);
break;
} else if (arr[temp] > value) {
ub = temp;
} else if (arr[temp] < value) {
lb = temp;
}
}
}
}
Output:
Output:
}
}
Output:
class student {
float english, hindi, maths, science, sst;
}
}
Output:
class circle {
int r;
public void setRadius(int r) {
this.r = r;
}
Output:
WAP to get
sorted reverse array in a class
class arr_rev {
int[] a;
public void setArray(int[] ls) {
this.a = ls;
}
Output:
System.out.print("Expansion: ");
for (int k = 0; k <= n; k++) {
int coeff = 1;
sc.close();
}
}
Output:
Output:
Output:
Student(String studentName) {
name = studentName;
age = 0;
}
void display() {
System.out.println("Name: " + name + ", Age: " +
age);
}
}
s1.display();
s2.display();
s3.display();
}
}
Output:
myPuppy.eat();
myPuppy.bark();
myPuppy.weep();
}
}
Output:
void display() {
System.out.println("A's display method
called");
}
}
class B extends A {
int b = 512;
public B() {
System.out.println("B's constructor called");
}
@Override
void display() {
System.out.println("B's display method
called");
}
}
Output:
Output: