JAVA LAB COVERING ALL TOPICS
============================
1. WAP in java to show Simple First Hello World Program
-------------------------------------------------------
class First
{
public static void main(String[] args)
{
System.out.println("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;
System.out.println("Sum = " + sum);
}
}
o/p
---
Sum = 60
3.WAP 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;
System.out.println("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)
{
System.out.println(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 java.util.*;
class greatest
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("enter three numbers :");
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
if((x>y)&&(x>z))
System.out.println("greatest number is "+x);
else if((y>x)&&(y>z))
System.out.println("greatest number is "+y);
else
System.out.println("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 java.util.scanner or import
java.util.* package?
import java.util.Scanner;
class AddTwoNumbers
{
public static void main(String[] args)
{
int a, b, c;
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
a = sc.nextInt();
System.out.print("Enter second number: ");
b = sc.nextInt();
c = a + b;
System.out.println("Sum = " + c);
sc.close();
}
}
o/p
---
javac AddTwoNumbers.java
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)
{
System.out.print(args[0]);
System.out.print(args[1]);
}
}
o/p
---
java cmdline Hello
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
Alternative correct program for this
------------------------------------
class CmdLine {
public static void main(String[] args)
{
if (args.length >= 2) {
System.out.println("First Argument: " + args[0]);
System.out.println("Second Argument: " + args[1]);
} else {
System.out.println("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 = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);
System.out.println("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)
{
System.out.println(10 % 3);
System.out.println(10 > 3);
System.out.println(10 == 3);
System.out.println(10 < 3);
System.out.println(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;
System.out.println(a & b); // Bitwise AND
System.out.println(a | b); // Bitwise OR
System.out.println(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:
System.out.println("hello");
break;
case 2:
System.out.println("world");
break;
case 3:
System.out.println("bye");
break;
case 4:
System.out.println("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 java.util.*;
class Month
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("enter the number :");
int month = sc.nextInt();
switch(month)
{
case 1:
System.out.println("Jan");
break;
case 2:
System.out.println("Feb");
break;
case 3:
System.out.println("Mar");
break;
case 4:
System.out.println("April");
break;
case 5:
System.out.println("May");
break;
case 6:
System.out.println("Jun");
break;
case 7:
System.out.println("jul");
break;
case 8:
System.out.println("aug");
break;
case 9:
System.out.println("Sep");
break;
case 10:
System.out.println("Oct");
break;
case 11:
System.out.println("Nov");
break;
case 12:
System.out.println("Dec");
break;
default:
System.out.println("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 = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);
int z = x / y;
System.out.println("result = " + z);
}
catch (ArithmeticException e)
{
System.out.println("Denominator must be non-zero");
}
catch (NumberFormatException n)
{
System.out.println("Arguments must be integers only");
}
catch (ArrayIndexOutOfBoundsException a)
{
System.out.println("Provide exact number of arguments");
}
finally
{
System.out.println("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 = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);
int z = x / y;
System.out.println("result=" + z);
}
catch (ArithmeticException e)
{
System.out.println("Denominator must be non-zero");
}
catch (NumberFormatException n)
{
System.out.println("Arguments must be integers");
}
catch (ArrayIndexOutOfBoundsException a)
{
System.out.println("Provide exact number of arguments");
}
finally
{
System.out.println("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;
System.out.println("result=" + z);
}
public static void main(String args[])
{
try
{
test(10, 5);
test(10, 0);
}
catch (ArithmeticException e)
{
System.out.println("Exception caught");
}
}
}
o/p
---
result=2
Exception caught
16) WAP in java to check whether a number is perfect or not using loops?
import java.util.Scanner;
class PerfectNumber {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
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) {
System.out.println(num + " is a Perfect Number");
} else {
System.out.println(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 java.util.Scanner;
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(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int temp = num;
int sum = 0;
while (temp > 0) {
int digit = temp % 10;
sum = sum + factorial(digit);
temp = temp / 10;
}
if (sum == num) {
System.out.println(num + " is a Strong Number");
} else {
System.out.println(num + " is NOT a Strong Number");
}
}
}
18) WAP in java based on abstract class?
import java.util.Scanner;
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;
System.out.println("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;
System.out.println("Area of Rectangle: " + result);
}
}
class AbstractDemo {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter radius of Circle: ");
double r = sc.nextDouble();
Shape c = new Circle(r); // runtime polymorphism
c.area();
// Rectangle
System.out.print("Enter length and breadth of Rectangle: ");
double l = sc.nextDouble();
double b = sc.nextDouble();
Shape rect = new Rectangle(l, b);
rect.area();
}
}
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() {
System.out.println(rollno + " " + name + " " + college);
}
}
class StaticDemo {
public static void main(String args[]) {
Student s1 = new Student(101, "Ram");
Student s2 = new Student(102, "Arushi");
s1.display();
s2.display();
}
}
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() {
System.out.println("Data is: " + data);
}
}
}
public class StaticClassDemo {
public static void main(String args[]) {
// creating object of static nested class without Outer object
Outer.Inner obj = new Outer.Inner();
obj.display();
}
}
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() {
System.out.println("ID: " + id + ", Name: " + name);
}
}
public class ConstructorDemo {
public static void main(String args[]) {
Student s1 = new Student(101, "Rahul");
s1.display();
}
}
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() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
public class ConstructorOverloadingDemo {
public static void main(String args[]) {
Person p1 = new Person("Anita");
Person p2 = new Person("Suresh", 25);
p1.show();
p2.show();
}
}
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) {
System.out.println("Error: Cannot divide by zero");
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Error: Array index out of range");
}
catch (Exception e) {
System.out.println("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() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
String color = "Black";
void printColor() {
System.out.println("Dog color: " + color);
System.out.println("Animal color: " + super.color);
}
void sound() {
super.sound(); // call parent method
System.out.println("Dog barks");
}
}
public class SuperKeywordDemo {
public static void main(String args[]) {
Dog d = new Dog();
d.printColor();
d.sound();
}
}
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() {
System.out.println("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 < emp.length; i++) {
emp[i].show();
}
}
}
o/p
---
ID: 101, Name: Amit
ID: 102, Name: Ahana
ID: 103, Name: Kiran