Java Lab Rrecord

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 25

School of Engineering and Technology

Paralakhemundi
Campus

LAB RECORD

SUBJECT: JAVA
TECHNOLOGIES CODE:
CUTM1031

Name: Shivam Kumar Singh

Registration No.: 210101120085


Branch: B.tech(CSE) Academic Session: 2022-2023

Centurion University of Technology and Management


Shaping lives………….Empowering Communities…………..
JAVA LAB
RECORD
CUTM1031
1. Write a Java program to create a class and
objects. Ans: public class Employee {
String
name; int
age;
String
designation;
double salary;
public Employee(String name, int age, String designation, double salary) {
this.name = name;
this.age = age;
this.designation =
designation; this.salary =
salary;
}
public void display() {
System.out.println("Name: " +
name); System.out.println("Age: " +
age);
System.out.println("Designation: " + designation);
System.out.println("Salary: " + salary);
}
}
public class Main {
public static void main(String[] args) {

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);
}
}

public class Dog extends Animal

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);

System.out.println("Name: " + person.getName());


System.out.println("Age: " + person.getAge());
}
}
Output:
Name: John
Doe Age: 30
9
5. Write a Java Program to find the largest among three numbers.
import
java.util.Scanner; public
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter three
numbers:"); int num1 = sc.nextInt();
int num2 =
sc.nextInt(); int num3
= sc.nextInt(); int
largest = num1;
if(num2 > largest) {
largest = num2;
}
if(num3 > largest)
{ largest = num3;
}
System.out.println("The largest number is " + largest);
}
}
Output:
Enter three
numbers: 1
2
3
The largest number is 3

10
6. Write a Java program to check if a given number is prime or
not. Ans: import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a
number:"); int num = sc.nextInt();
boolean isPrime = true;
for(int i=2; i<num; i++)
{ if(num%i == 0) {
isPrime =
false; break;
}
}
if(isPrime) {
System.out.println(num + " is a prime number.");
} else {
System.out.println(num + " is not a prime number.");
}
}
}
Output:
Enter a number: 4
4 is not a prime number.
Enter a number: 2
2 is not a prime number.

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);
}

public static void main(String[] args) {


DefaultConstructorExample obj = new DefaultConstructorExample();
obj.display();
}
}
Output:
Number: 0
String: null
13. Program to demonstrate Parameterized
Constructor. Ans: public class
ParameterizedConstructorExample {
int num;
String str;
public ParameterizedConstructorExample(int n, String s) {
num = n;
str = s;
}
public void display() {
System.out.println("Number: " +
num); System.out.println("String: " +
str);
20
}

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

You might also like