Java Lab Rrecord
Java Lab Rrecord
Java Lab Rrecord
Paralakhemundi
Campus
LAB RECORD
SUBJECT: JAVA
TECHNOLOGIES CODE:
CUTM1031
2
Employee emp1 = new Employee("John", 30, "Manager", 50000.0);
Employee emp2 = new Employee("Alice", 25, "Engineer", 30000.0);
emp1.display();
emp2.display();
3
}
}
Output:
Name: John
Age: 30
Designation:
Manager Salary:
50000.0 Name: Alice
Age: 25
Designation: Engineer
Salary: 30000.0
2. Write a Java program to create an inheritance
hierarchy. Ans: public class Animal {
String
name; int
age;
public Animal(String name, int age)
{ this.name = name;
this.age = age;
}
public void display() {
System.out.println("Name: " +
name); System.out.println("Age: " +
age);
}
}
4
{ String breed;
public Dog(String name, int age, String breed) {
5
super(name, age);
this.breed =
breed;
}
public void display()
{ super.display();
System.out.println("Breed: " + breed);
}
public void bark() {
System.out.println("Woof!");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog("Buddy", 5,
"Labrador"); dog.display();
dog.bark();
}
}
Output: Name: Buddy
Age: 5
Breed:
Labrador Woof!
3. Program to demonstrate
polymorphism. Ans:
public class Shape {
public void draw() {
System.out.println("Drawing a shape");
6
}
}
public class Circle extends Shape {
public void draw() {
System.out.println("Drawing a
circle");
}
}
public class Square extends Shape {
public void draw() {
System.out.println("Drawing a
square");
}
}
public class Main {
public static void main(String[] args) {
Shape[] shapes = {new Shape(), new Circle(), new Square()};
for(Shape shape : shapes) {
shape.draw();
}
}
}
Output: Drawing a
shape Drawing a circle
Drawing a square.
4.Program to demonstrate
Encapsulation. Ans: public class Person {
private String
7
name; private int
age;
8
public String getName()
{ return name;
}
public void setName(String name)
{ this.name = name;
}
public int getAge()
{ return age;
}
public void setAge(int age)
{ this.age = age;
}
}
public class EncapsulationExample {
public static void main(String[] args)
{
Person person = new
Person();
person.setName("John Doe");
person.setAge(30);
10
6. Write a Java program to check if a given number is prime or
not. Ans: import java.util.Scanner;
11
7. Program to demonstrate Arithmetic
Operators. Ans: public class
ArithmeticOperatorsExample {
public static void main(String[] args)
{ int num1 = 10;
int num2 = 5;
System.out.println("num1 + num2 = " + (num1 + num2)); // 15
System.out.println("num1 - num2 = " + (num1 - num2)); // 5
System.out.println("num1 * num2 = " + (num1 * num2)); // 50
System.out.println("num1 / num2 = " + (num1 / num2)); // 2
System.out.println("num1 % num2 = " + (num1 % num2)); // 0
}
}
Output:
num1 + num2 =
15 num1 - num2 =
5 num1 * num2 =
50 num1 / num2 =
2 num1 % num2 =
0
8. program to demonstrate Relational
Operators. Ans: public class
RelationalOperatorsExample {
public static void main(String[] args)
{ int num1 = 10;
int num2 = 5;
System.out.println("num1 > num2 is " + (num1 > num2)); // true
System.out.println("num1 < num2 is " + (num1 < num2)); // false
12
System.out.println("num1 >= num2 is " + (num1 >= num2)); // true
System.out.println("num1 <= num2 is " + (num1 <= num2)); // false
13
System.out.println("num1 == num2 is " + (num1 == num2)); // false
System.out.println("num1 != num2 is " + (num1 != num2)); // true
}
}
Output:
num1 > num2 is true
num1 < num2 is false
num1 >= num2 is true
num1 <= num2 is
false num1 == num2
is false num1 != num2
is true
9. Java program that demonstrates abstraction using abstract classes
and methods.
Ans: // Abstract class
abstract class Animal
{
public abstract void makeSound();
}
class Dog extends Animal {
public void makeSound()
{
System.out.println("Woof woof!");
}
class Cat extends Animal {
public void makeSound()
{
System.out.println("Meow!");
14
}
}
public class AbstractionExample {
public static void main(String[] args)
{
15
Animal animal1 = new Dog(); // Create a Dog
object Animal animal2 = new Cat(); // Create a Cat
object
animal1.makeSound(); // Call the makeSound method on Dog object
animal2.makeSound(); // Call the makeSound method on Cat object
}
}
Output: Woof woof!
Meow!
10. Program to demonstrate
overloading. Ans: public class
OverloadingExample {
public void print(int num) {
System.out.println("Printing integer: " +
num);
}
public void print(String str) {
System.out.println("Printing string: " +
str);
}
public void print(double num) {
System.out.println("Printing double: " +
num);
}
public static void main(String[] args) {
OverloadingExample obj = new
OverloadingExample() obj.print(10); // Calls
print(int)
16
obj.print("hello"); // Calls
print(String) obj.print(3.14); //
Calls print(double)
}
}
Output: Printing integer: 10
17
Printing string: hello
Printing double:
3.14
11. Program to demonstrate over
ridding. Ans: class Animal {
public void makeSound() {
System.out.println("Some
sound");
}
}
class Dog extends Animal {
public void makeSound()
{
System.out.println("Bark");
}
}
public class OverridingExample {
public static void main(String[] args)
{ Animal animal1 = new Animal();
Animal animal2 = new Dog();
animal1.makeSound(); // Calls makeSound() method of Animal class
animal2.makeSound(); // Calls makeSound() method of Dog class
}
}
Output: Some sound
Bark
12. Program to demonstrate Default
constructor. Ans: public class
18
DefaultConstructorExample {
int num;
String str;
19
public void display() {
System.out.println("Number: " +
num); System.out.println("String: " +
str);
}
21
public static void main(String[] args) {
ParameterizedConstructorExample obj =
new ParameterizedConstructorExample(10,
"hello");
obj.display();
}
}
Output: Number: 10
String: hello
14. Program to demonstrate Copy
Constructor. Ans: public class
CopyConstructorExample {
int num;
String str;
public CopyConstructorExample(int n, String s) {
num = n;
str = s;
}
public CopyConstructorExample(CopyConstructorExample obj) {
num = obj.num;
str = obj.str;
}
public void display() {
System.out.println("Number: " +
num); System.out.println("String: " +
str);
}
public static void main(String[] args) {
CopyConstructorExample obj1 = new CopyConstructorExample(10,
22
"hello");
23
CopyConstructorExample obj2 = new CopyConstructorExample(obj1); //
Copy constructor called
obj1.display();
obj2.display();
}
}
Output: Number: 10
String:
hello
Number: 10
String:
hello
15. program to demonstrate do-while
loop. Ans: public
class DoWhileLoopExample {
public static void main(String[] args)
{ int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
}
}
Output:
1
2
3
4
24
5
25