TABLE OF CONTENTS
SR. TITLE PAGE REMARKS
NO. NO.
1. Write a program to print a 3
message; Welcome to
java
2. Write a program to find 4
square root of a number.
3. Write a program to use 5
command line in java
4. Write a program to find 6
area of rectangle using
multiple classes
5. Write a program to use 7
constructor in java.
6. Write a program to use 8-9
constructor overloading
7. Write a program to use 10
static member function
8. Write a program to use 11
nested function
9. Write a program to use 12-13
super function in java
10. Write a program to use 14
arithmetic operations
11. Write a program to use 15
relational operators
12. Write a program to use 16
increment operator
13. Write a program to use 17
decrement operators
14. Write a program to use 18
nested function
1
15. Write a program to use 19
break statement
16. Write a program to use 20
continue statement
17. Write a program to use 21-22
method overriding
18. Write a program to use 23
while statement
19. Write a program to print 24
even numbers
20. Write a program to print 25
matrix using do while
loop
21. Write a program to show 26
compile time error
22. Write a program to show 27
use of applets
23. Write a program to show 28
runtime error
24. Write a program to use try 29
and catch block
25. Write a program to use 30
multiple try and catch
blocks
26. Write a program to use 31
finally statement
27. Write a program to show 32
sorting of strings
28. Write a program to use 33
two-dimensional array
29. Write a program to show 34
use of interfaces
30. Write a program to show 35
use of packages
2
[Link] a program to print a message;
Welcome to java
public class Welcome {
public static void main(String[] args) {
[Link]("Welcome to java");
}
}
***Output***
3
2. Write a program to find square root of a
number
import [Link];
public class squareroot {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a number: ");
double num = [Link]();
double result = [Link](num);
[Link]("Square root: " + result);
}
}
***Output***
4
3. Write a program to use command line in
java
public class commandline {
public static void main(String[] args) {
if ([Link] > 0) {
[Link]("First argument: " + args[0]);
} else {
[Link]("No arguments provided");
}
}
}
***Output***
5
4. Write a program to find area of rectangle
using multiple classes
class Rectangle {
double length, width;
void setData(double l, double w) {
length = l;
width = w;
}
double getArea() {
return length * width;
}
}
public class areacalculator {
public static void main(String[] args) {
Rectangle rect = new Rectangle();
[Link](5, 3);
[Link]("Area: " + [Link]());
}
}
***Output***
6
5. Write a program to use constructor in java
public class student {
String name;
int age;
public student(String n, int a) {
name = n;
age = a;
}
void display() {
[Link]("Name: " + name + ", Age: " +
age);
}
public static void main(String[] args) {
student s1 = new student("John", 20);
[Link]();
}
}
***Output***
7
6. Write a program to use constructor
overloading
public class box {
int length, width, height;
box() {
length = width = height = 1;
}
box(int l, int w, int h) {
length = l;
width = w;
height = h;
}
box(int side) {
length = width = height = side;
}
int volume() {
return length * width * height;
8
}
public static void main(String[] args) {
box b1 = new box();
box b2 = new box(2, 3, 4);
box b3 = new box(5);
[Link]("Volume 1: " + [Link]());
[Link]("Volume 2: " + [Link]());
[Link]("Volume 3: " + [Link]());
}
}
***Output***
9
7. Write a program to use static member
function
public class mathutils {
static int add(int a, int b) {
return a + b;
}
static int multiply(int a, int b) {
return a * b;
}
public static void main(String[] args) {
[Link]("Sum: " + [Link](5, 3));
[Link]("Product: " + [Link](5,
3));
}
}
***Output***
10
8. Write a program to use nested function
public class nestedfunction {
void outerFunction() {
[Link]("In outer function");
innerFunction();
}
void innerFunction() {
[Link]("In inner function");
}
public static void main(String[] args) {
nestedfunction obj = new nestedfunction();
[Link]();
}
}
***Output***
11
9. Write a program to use super function in
java
class Animal {
String color = "white";
}
class Dog extends Animal {
String color = "brown";
void printColor() {
[Link]("Dog color: " + color);
[Link]("Animal color: " + [Link]);
}
}
public class superexample
{
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
}
12
}
***Output***
13
10. Write a program to use arithmetic
operations
public class arithmetic {
public static void main(String[] args) {
int a = 10, b = 3;
[Link]("Addition: " + (a + b));
[Link]("Subtraction: " + (a - b));
[Link]("Multiplication: " + (a * b));
[Link]("Division: " + (a / b));
[Link]("Modulus: " + (a % b));
}
}
***Output***
14
11. Write a program to use relational
operators
public class relational {
public static void main(String[] args) {
int a = 10, b = 5;
[Link](a + " == " + b + " : " + (a == b));
[Link](a + " != " + b + " : " + (a != b));
[Link](a + " > " + b + " : " + (a > b));
[Link](a + " < " + b + " : " + (a < b));
[Link](a + " >= " + b + " : " + (a >= b));
[Link](a + " <= " + b + " : " + (a <= b));
}
}
***Output***
15
12. Write a program to use increment
operator
public class increment {
public static void main(String[] args) {
int a = 5;
[Link]("Original: " + a);
[Link]("Post-increment: " + a++);
[Link]("After post-increment: " + a);
[Link]("Pre-increment: " + ++a);
[Link]("After pre-increment: " + a);
}
}
***Output***
16
13. Write a program to use decrement
operators
public class decrement {
public static void main(String[] args) {
int a = 8;
[Link]("Original: " + a);
[Link]("Post-decrement: " + a--);
[Link]("After post-decrement: " + a);
[Link]("Pre-decrement: " + --a);
[Link]("After pre-decrement: " + a);
}
}
***Output***
17
14. Write a program to use nested function
public class nestedexample {
void first() {
[Link]("First method");
second();
}
void second() {
[Link]("Second method");
}
public static void main(String[] args) {
nestedexample obj = new nestedexample();
[Link]();
}
}
***Output***
18
15. Write a program to use break statement
public class breakexample {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
[Link](i);
}
}
}
***Output***
19
16. Write a program to use continue
statement
public class continueexample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
[Link](i);
}
}
}
***Output***
20
17. Write a program to use method
overriding
class Vehicle {
void run() {
[Link]("Vehicle is running");
}
}
class Bike extends Vehicle {
void run() {
[Link]("Bike is running safely");
}
}
public class overriding {
public static void main(String[] args) {
Bike obj = new Bike();
[Link]();
}
}
21
***Output***
22
18. Write a program to use while statement
public class whileexample {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
[Link]("Count: " + i);
i++;
}
}
}
***Output***
23
19. Write a program to print even numbers
public class evennumbers {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
[Link](i);
}
}
}
}
***Output***
24
20. Write a program to print matrix using
do while loop
public class matrix {
public static void main(String[] args) {
int rows = 3, cols = 3;
int i = 0;
do {
int j = 0;
do {
[Link]((i * cols + j + 1) + " ");
j++;
} while (j < cols);
[Link]();
i++;
} while (i < rows);
}
}
***Output***
25
21. Write a program to show compile time
error
public class compileerror {
public static void main(String[] args) {
int x = 10
[Link](x);
}
}
***Output***
26
22. Write a program to show use of applets
import [Link];
import [Link];
public class simpleapplet extends Applet {
public void paint(Graphics g) {
[Link]("Hello Applet", 20, 20);
}
}
***Output***
27
23. Write a program to show runtime error
public class runtimeerror {
public static void main(String[] args) {
int a = 10, b = 0;
int result = a / b;
[Link](result);
}
}
***Output***
28
24. Write a program to use try and catch
block
public class trycatch {
public static void main(String[] args) {
try {
int data = 50 / 0;
} catch (ArithmeticException e) {
[Link]("Exception caught: " + e);
}
[Link]("Rest of the code");
}
}
***Output***
29
25. Write a program to use multiple try and
catch blocks
public class multipletrycatch {
public static void main(String[] args) {
try {
int a[] = new int[5];
a[5] = 30 / 0;
} catch (ArithmeticException e) {
[Link]("Arithmetic exception");
} catch (ArrayIndexOutOfBoundsException e) {
[Link]("Array index exception");
} catch (Exception e) {
[Link]("General exception");
}
}
}
***Output***
30
[Link] a program to use finally statement
public class finallyexample {
public static void main(String[] args) {
try {
int data = 25 / 5;
[Link](data);
} catch (NullPointerException e) {
[Link](e);
} finally {
[Link]("Finally block executed");
}
}
}
***Output***
31
[Link] a program to show sorting of strings
import [Link];
public class stringsort {
public static void main(String[] args) {
String[] names = {"John", "Alice", "Bob", "David"};
[Link](names);
[Link]("Sorted names: " +
[Link](names));
}
}
***Output***
32
[Link] a program to use two dimensional
array
public class twodarray {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < matrix[i].length; j++) {
[Link](matrix[i][j] + " ");
}
[Link]();
} }}
***Output***
33
[Link] a program to show use of interfaces
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() {
[Link]("Woof");
}
}
public class interfaceexample {
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
}
}
***Output***
34
[Link] a program to show use of packages
package mypackage;
public class packageexample {
public void display() {
[Link]("This is from mypackage");
}
public static void main(String[] args) {
packageexample obj = new packageexample();
[Link]();
}
}
***Output***
35