JAVA LAB COVERING ALL TOPICS
============================
1. WAP in java to show Simple First Hello World Program
-------------------------------------------------------
class First
{
public static void main(String[] args)
{
[Link]("Hello World!");
}
}
o/p
---
Hello World!
2. WAP in Java to add sum of three nos.?
class Add {
public static void main(String[] args) {
int a, b, c, sum;
a = 10;
b = 20;
c = 30;
sum = a + b + c;
[Link]("Sum = " + sum);
}
}
o/p
---
Sum = 60
[Link] in java to display the sum of 10 and 20 is 30 in java?
class SumExample
{
public static void main(String[] args)
{
int a = 10;
int b = 20;
int c = a + b;
[Link]("The sum of " + a + " and " + b + " is " + c);
}
}
o/p
---
The sum of 10 and 20 is 30
4. WAP in Java to show the even numbers?
class evenno
{
public static void main(String args[])
{
int i=2;
while(i<=50)
{
[Link](i+" ");
i=i+2;
}
}
}
o/p
---
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
5. WAP in java to show the three greatest numbers?
import [Link].*;
class greatest
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("enter three numbers :");
int x = [Link]();
int y = [Link]();
int z = [Link]();
if((x>y)&&(x>z))
[Link]("greatest number is "+x);
else if((y>x)&&(y>z))
[Link]("greatest number is "+y);
else
[Link]("greatest number is "+z);
}
}
i/p
---
Enter three numbers:
10
25
15
o/p
---
Greatest number is 25
6. WAP in java to add two numbers using import [Link] or import
[Link].* package?
import [Link];
class AddTwoNumbers
{
public static void main(String[] args)
{
int a, b, c;
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
a = [Link]();
[Link]("Enter second number: ");
b = [Link]();
c = a + b;
[Link]("Sum = " + c);
[Link]();
}
}
o/p
---
javac [Link]
java AddTwoNumbers
Enter first number: 10
Enter second number: 20
Enter first number: 10
Enter second number: 20
Sum = 30
7. WAP to show the execution of java program in command line arguments?
class cmdline
{
public static void main(String[] args)
{
[Link](args[0]);
[Link](args[1]);
}
}
o/p
---
java cmdline Hello
Exception in thread "main" [Link]: 1
Alternative correct program for this
------------------------------------
class CmdLine {
public static void main(String[] args)
{
if ([Link] >= 2) {
[Link]("First Argument: " + args[0]);
[Link]("Second Argument: " + args[1]);
} else {
[Link]("Please provide at least two arguments!");
}
}
}
8) WAP using addition of two numbers using command line arguments?
class cmdline
{
public static void main(String[] args)
{
int x = [Link](args[0]);
int y = [Link](args[1]);
[Link]("sum=" + (x + y));
}
}
o/p
---
java cmdline 10 20
sum=30
9) WAP to show the functionality of operator?
class operator1 {
public static void main(String[] args)
{
[Link](10 % 3);
[Link](10 > 3);
[Link](10 == 3);
[Link](10 < 3);
[Link](10 != 3);
}
}
o/p
---
1
true
false
false
true
10) WAP in java to show the logical operators?
class LogicalOps
{
public static void main(String[] args)
{
int a = 5, b = 6;
[Link](a & b); // Bitwise AND
[Link](a | b); // Bitwise OR
[Link](a ^ b); // Bitwise XOR
}
}
o/p
---
4
7
3
11) Write a Java program to demonstrate the use of the switch statement?
class SwitchExample
{
public static void main(String[] args)
{
int x = 2;
switch(x)
{
case 1:
[Link]("hello");
break;
case 2:
[Link]("world");
break;
case 3:
[Link]("bye");
break;
case 4:
[Link]("stop");
break;
}
}
}
o/p
---
world
12) Write a Java program that accepts a number (1–12) from the user and displays
the corresponding month name using the switch statement?
import [Link].*;
class Month
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("enter the number :");
int month = [Link]();
switch(month)
{
case 1:
[Link]("Jan");
break;
case 2:
[Link]("Feb");
break;
case 3:
[Link]("Mar");
break;
case 4:
[Link]("April");
break;
case 5:
[Link]("May");
break;
case 6:
[Link]("Jun");
break;
case 7:
[Link]("jul");
break;
case 8:
[Link]("aug");
break;
case 9:
[Link]("Sep");
break;
case 10:
[Link]("Oct");
break;
case 11:
[Link]("Nov");
break;
case 12:
[Link]("Dec");
break;
default:
[Link]("pls enter value 1 to 12");
}
}
}
o/p
---
enter the number :
5
May
13) WAP in java to demonstrate the finally block?
class FinalTest
{
public static void main(String args[])
{
try
{
int x = [Link](args[0]);
int y = [Link](args[1]);
int z = x / y;
[Link]("result = " + z);
}
catch (ArithmeticException e)
{
[Link]("Denominator must be non-zero");
}
catch (NumberFormatException n)
{
[Link]("Arguments must be integers only");
}
catch (ArrayIndexOutOfBoundsException a)
{
[Link]("Provide exact number of arguments");
}
finally
{
[Link]("I will definitely execute");
}
}
}
o/p
---
java FinalTest 10 2
result = 5
I will definitely execute
java FinalTest 10 0
Denominator must be non-zero
I will definitely execute
14) Write a Java program to demonstrate multiple catch blocks and finally?
class finaltest
{
public static void main(String args[])
{
try {
int x = [Link](args[0]);
int y = [Link](args[1]);
int z = x / y;
[Link]("result=" + z);
}
catch (ArithmeticException e)
{
[Link]("Denominator must be non-zero");
}
catch (NumberFormatException n)
{
[Link]("Arguments must be integers");
}
catch (ArrayIndexOutOfBoundsException a)
{
[Link]("Provide exact number of arguments");
}
finally
{
[Link]("I will definitely execute");
}
}
}
o/p
---
> java finaltest 10 2
result=5
I will definitely execute
> java finaltest 10 0
Denominator must be non-zero
I will definitely execute
> java finaltest 10 abc
Arguments must be integers
I will definitely execute
> java finaltest 10
Provide exact number of arguments
I will definitely execute
15) Write a Java program to demonstrate the use of the throw and throws keywords by
dividing two numbers. Handle the ArithmeticException when the denominator is zero
using a try-catch block?
class throwtest {
public static void test(int x, int y) throws ArithmeticException
{
int z = x / y;
[Link]("result=" + z);
}
public static void main(String args[])
{
try
{
test(10, 5);
test(10, 0);
}
catch (ArithmeticException e)
{
[Link]("Exception caught");
}
}
}
o/p
---
result=2
Exception caught
16) WAP in java to check whether a number is perfect or not using loops?
import [Link];
class PerfectNumber {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int sum = 0;
// Find divisors and calculate sum
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) {
sum += i;
}
}
// Check perfect number condition
if (sum == num) {
[Link](num + " is a Perfect Number");
} else {
[Link](num + " is NOT a Perfect Number");
}
}
}
i/p and o/p
-----------
6
6 is a Perfect Number
12
12 is NOT a Perfect Number
17) WAP in java to check a number is strong number using both for and while loop?
import [Link];
class StrongNumber {
static int factorial(int n) {
int fact = 1;
for (int i = 1; i <= n; i++) {
fact = fact * i;
}
return fact;
}
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int temp = num;
int sum = 0;
while (temp > 0) {
int digit = temp % 10;
sum = sum + factorial(digit);
temp = temp / 10;
}
if (sum == num) {
[Link](num + " is a Strong Number");
} else {
[Link](num + " is NOT a Strong Number");
}
}
}
18) WAP in java based on abstract class?
import [Link];
abstract class Shape {
abstract void area();
}
class Circle extends Shape {
double radius;
Circle(double r) {
radius = r;
}
void area() {
double result = 3.14 * radius * radius;
[Link]("Area of Circle: " + result);
}
}
class Rectangle extends Shape {
double length, breadth;
Rectangle(double l, double b) {
length = l;
breadth = b;
}
void area() {
double result = length * breadth;
[Link]("Area of Rectangle: " + result);
}
}
class AbstractDemo {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("Enter radius of Circle: ");
double r = [Link]();
Shape c = new Circle(r); // runtime polymorphism
[Link]();
// Rectangle
[Link]("Enter length and breadth of Rectangle: ");
double l = [Link]();
double b = [Link]();
Shape rect = new Rectangle(l, b);
[Link]();
}
}
o/p
---
Enter radius of Circle: 5
Area of Circle: 78.5
Enter length and breadth of Rectangle: 4 6
Area of Rectangle: 24.0
19) WAP in java based on static variable?
class Student {
int rollno;
String name;
static String college = "UTKAL"
Student(int r, String n) {
rollno = r;
name = n;
}
void display() {
[Link](rollno + " " + name + " " + college);
}
}
class StaticDemo {
public static void main(String args[]) {
Student s1 = new Student(101, "Ram");
Student s2 = new Student(102, "Arushi");
[Link]();
[Link]();
}
}
o/p
---
101 Ram UTKAL
102 Arushi UTKAL
20) WAP in java based on static class?
class Outer {
static int data = 50;
// static nested class
static class Inner {
void display() {
[Link]("Data is: " + data);
}
}
}
public class StaticClassDemo {
public static void main(String args[]) {
// creating object of static nested class without Outer object
[Link] obj = new [Link]();
[Link]();
}
}
o/p
---
Data is: 50
21) WAP in java based on constructor?
class Student {
int id;
String name;
// Constructor
Student(int i, String n) {
id = i;
name = n;
}
void display() {
[Link]("ID: " + id + ", Name: " + name);
}
}
public class ConstructorDemo {
public static void main(String args[]) {
Student s1 = new Student(101, "Rahul");
[Link]();
}
}
o/p
---
ID: 101, Name: Rahul
22) WAP in java based on constructor overloading?
class Person {
String name;
int age;
// Constructor 1
Person(String n) {
name = n;
age = 0;
}
// Constructor 2
Person(String n, int a) {
name = n;
age = a;
}
void show() {
[Link]("Name: " + name + ", Age: " + age);
}
}
public class ConstructorOverloadingDemo {
public static void main(String args[]) {
Person p1 = new Person("Anita");
Person p2 = new Person("Suresh", 25);
[Link]();
[Link]();
}
}
Name: Anita, Age: 0
Name: Suresh, Age: 25
23) WAP in java based on Multiple Exception Handling?
public class MultipleExceptionDemo {
public static void main(String args[]) {
try {
int a = 10, b = 0;
int c = a / b; // ArithmeticException
int arr[] = new int[5];
arr[10] = 50; // ArrayIndexOutOfBoundsException
}
catch (ArithmeticException e) {
[Link]("Error: Cannot divide by zero");
}
catch (ArrayIndexOutOfBoundsException e) {
[Link]("Error: Array index out of range");
}
catch (Exception e) {
[Link]("Other Exception caught");
}
}
}
o/p
---
Error: Cannot divide by zero
24) WAP in java based on super keyword?
class Animal {
String color = "White";
void sound() {
[Link]("Animal makes sound");
}
}
class Dog extends Animal {
String color = "Black";
void printColor() {
[Link]("Dog color: " + color);
[Link]("Animal color: " + [Link]);
}
void sound() {
[Link](); // call parent method
[Link]("Dog barks");
}
}
public class SuperKeywordDemo {
public static void main(String args[]) {
Dog d = new Dog();
[Link]();
[Link]();
}
}
o/p
---
Dog color: Black
Animal color: White
Animal makes sound
Dog barks
25) WAP in java based on Array of Objects?
class Employee {
int id;
String name;
Employee(int i, String n) {
id = i;
name = n;
}
void show() {
[Link]("ID: " + id + ", Name: " + name);
}
}
public class ArrayOfObjectsDemo {
public static void main(String args[]) {
Employee emp[] = new Employee[3];
emp[0] = new Employee(101, "Amit");
emp[1] = new Employee(102, "Ahana");
emp[2] = new Employee(103, "Kiran");
for (int i = 0; i < [Link]; i++) {
emp[i].show();
}
}
}
o/p
---
ID: 101, Name: Amit
ID: 102, Name: Ahana
ID: 103, Name: Kiran