[go: up one dir, main page]

0% found this document useful (0 votes)
34 views27 pages

LAB Experiments Aaradhya

The document describes experiments conducted on Java programming and introduction to Python. It includes programs demonstrating concepts like widening and narrowing in Java, constructors, method overloading, inheritance, interfaces and logical operators in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views27 pages

LAB Experiments Aaradhya

The document describes experiments conducted on Java programming and introduction to Python. It includes programs demonstrating concepts like widening and narrowing in Java, constructors, method overloading, inheritance, interfaces and logical operators in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

LAB Experiments

ON
JAVA PROGRAMMING AND INTRODUCTION OF PYTHON

BACHELOR OF TECHNOLOGY
IN
ELECTRONICS AND COMMUNICATION ENGINEERING

Department of Electronics and Communication Engineering


Faculty of Engineering and Technology
Gurukul Kangri (Deemed to be University), Haridwar-249404

Summitted To
Dr. Aman Tyagi
Assistant Professor, Department of Computer Science & Engineering, Faculty
of Engineering & Technology, Gurukul Kangri Deemed to be University,
Haridwar

Submitted By
Aaradhya Srivastava (216320088)
Certificate
This is to certify that Mr. AARADHYA SRIVASTAVA of B.Tech VI semester has
satisfactorily completed his experiments for Java programming and Introduction
of python lab in requirement for partial fulfillment of bachelor degree in
Electronics & Communication Engineering prescribed by Faculty of Engineering
& Technology, Gurukula Kangri Deemed to be University, Haridwar during the
year 2023-2024
Index

S.No Program Name Page


1. Write a Java program with the help of widening. 01
2. Make a Java Program using Narrowing. 02
3. Write a Java Program in which class variables are assigned via method 03
of a class.
4. Make a Java program for default constructor. 04
5. Make a Java program for parameterized constructor 05
6. Make a Java program using Constructor Overloading. 06
7. Make a Java program using Method Overloading 08
8. Make a java program using multi-level inheritance 09
9. Make a java program using method overriding. 11
10. Make a java program using interface. 12
11. Make a java program for extending interface 14
12. Make a python program for Explaining Logical expressions “and”, “or” 15
and not”
13. Make a python program for left shift and right shift. 17
14. Make a python program to explain Operator Precedence 18
15. Write a python program to explain Case Changing of Strings. 19
16. Write a python program to explain String replace() Method 20
17. Write a python program to explain the calling a function 22
18. Make a python program to find the greater number between two. 23
19. Make a python program to find whether a list is empty or not 24
PROGRAM – 01
Java program with the help of widening.

Code:
public class aaradhya { public static void main(String[] args) {
System.out.println("Aaradhya Srivastava 216320088 ECE 6th SEM");
byte byteVar = 10;
short shortVar = byteVar;
int intVar = shortVar;
long longVar = intVar;
float floatVar = longVar;
double doubleVar = floatVar;

System.out.println("Widening Conversion Example:");


System.out.println("Byte: " + byteVar);
System.out.println("Short: " + shortVar);
System.out.println("Int: " + intVar);
System.out.println("Long: " + longVar);
System.out.println("Float: " + floatVar);
System.out.println("Double: " + doubleVar);
}
}
Output:
PROGRAM – 02
Java Program using Narrowing.

Code:
public class aaradhya { public static void main(String[] args) {
System.out.println("Aaradhya Srivastava 216320088 ECE 6th SEM");
// Declare and initialize a double variable
double doubleValue = 123.456;

// Narrowing conversion: converting double to int


// Note: This may result in loss of information
int intValue = (int) doubleValue;

// Display the original double value and the narrowed int value
System.out.println("Original double value: " + doubleValue);
System.out.println("Narrowed int value: " + intValue);
}
}

Output:
PROGRAM – 03
Java Program in which class variables are assigned via method of a class.

Code:
class MyClass {
// Class variables
static int num1;
static int num2;
// Method to assign values to class variables

static void assignValues(int a, int b) {


num1 = a;
num2 = b;
}

// Method to display values of class variables


static void displayValues() {
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);

}
}
public class Main {
public static void main(String[] args) {
// Assign values to class variables via method

MyClass.assignValues(10, 20);

// Display values of class variables


MyClass.displayValues();

}
}
PROGRAM – 04
Java program for default constructor

Code:
class MyClass {

// Class variables

static int num1;

static int num2;

// Method to assign values to class variables

static void assignValues(int a, int b) {

num1 = a;

num2 = b;

// Method to display values of class variables

static void displayValues() {

System.out.println("num1 = " + num1);

System.out.println("num2 = " + num2);

public class Main {

public static void main(String[] args) {

// Assign values to class variables via method

MyClass.assignValues(10, 20);

// Display values of class variables

MyClass.displayValues();

Output:
PROGRAM – 05
Java program for parameterized constructor.

Code:
class Car {

String make;

String model;

int year;

// Parameterized constructor

Car(String make, String model, int year) {

this.make = make;

this.model = model;

this.year = year;

// Method to display car information

void displayInfo() {

System.out.println("Make: " + make);

System.out.println("Model: " + model);

System.out.println("Year: " + year);

public class Main {

public static void main(String[] args) {

// Creating an object of Car class using parameterized constructor

Car myCar = new Car("Toyota", "Corolla", 2020);

// Displaying car information

System.out.println("My Car:");

myCar.displayInfo();}}

Output:
PROGRAM – 06
Java program using Constructor Overloading.

Code:
class Rectangle {
double length;
double width;

// Default constructor
Rectangle() {
length = 0;
width = 0;
}

// Parameterized constructor with one parameter


Rectangle(double side) {
length = side;
width = side;
}

// Parameterized constructor with two parameters


Rectangle(double l, double w) {
length = l;
width = w;
}

// Method to calculate area of rectangle


double calculateArea() {
return length * width;
}
}

public class Main {


public static void main(String[] args) {
// Creating objects using different constructors
Rectangle rectangle1 = new Rectangle();
Rectangle rectangle2 = new Rectangle(5.0);
Rectangle rectangle3 = new Rectangle(4.0, 6.0);

// Displaying areas of rectangles


System.out.println("Area of rectangle1: " + rectangle1.calculateArea());
System.out.println("Area of rectangle2: " + rectangle2.calculateArea());
System.out.println("Area of rectangle3: " + rectangle3.calculateArea());
}
}

Output:
PROGRAM – 07
Java program using Method Overloading.

Code:
public class Sum {

// Overloaded sum(). This sum takes two int parameters

public int sum(int x, int y) { return (x + y); }

// Overloaded sum(). This sum takes three int parameters

public int sum(int x, int y, int z)

return (x + y + z);

// Overloaded sum(). This sum takes two double

// parameters

public double sum(double x, double y)

return (x + y);

// Driver code

public static void main(String args[])

Sum s = new Sum();

System.out.println(s.sum(10, 20));

System.out.println(s.sum(10, 20, 30));

System.out.println(s.sum(10.5, 20.5)); } }

Output:
PROGRAM – 08
Java program using multi-level inheritance

Code:
// Parent class
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}

// Child class inheriting from Animal


class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
}
}

// Grandchild class inheriting from Dog


class Labrador extends Dog {
void color() {
System.out.println("Labrador is black");
}
}

public class Main {


public static void main(String[] args) {
Labrador labrador = new Labrador();
// Calling methods from different levels of inheritance
labrador.eat(); // Inherited from Animal
labrador.bark(); // Inherited from Dog
labrador.color(); // Defined in Labrador class
}
}

Output:
PROGRAM – 09
Java program using method overriding

Code:
// Parent class
class Animal {
// Method to make sound

void makeSound() {
System.out.println("Animal is making a sound");
}
}
// Child class inheriting from Animal
class Dog extends Animal {
// Overriding the makeSound method of the parent class
@Override
void makeSound() {

System.out.println("Dog is barking");
}
}
public class Main {

public static void main(String[] args) {


// Creating an object of Dog class
Dog dog = new Dog();
// Calling the makeSound method of Dog class

dog.makeSound(); // This will call the overridden method in Dog class}}

Output:
PROGRAM – 10
Java program using interface.

Code:
// Interface defining the methods that must be implemented by classes
interface Animal {
void makeSound();
void move();
}

// Class implementing the Animal interface


class Dog implements Animal {
// Implementing the makeSound method
@Override
public void makeSound() {
System.out.println("Dog is barking");
}

// Implementing the move method


@Override
public void move() {
System.out.println("Dog is running");
}
}

// Class implementing the Animal interface


class Bird implements Animal {
// Implementing the makeSound method
@Override
public void makeSound() {
System.out.println("Bird is chirping");
}

// Implementing the move method


@Override
public void move() {
System.out.println("Bird is flying");
}
}

public class Main {


public static void main(String[] args) {
// Creating objects of Dog and Bird classes
Dog dog = new Dog();
Bird bird = new Bird();
// Calling methods defined in the Animal interface
dog.makeSound();
dog.move();
bird.makeSound();
bird.move();
}
}

Output:
PROGRAM – 11
java program for extending interface

Code:
// Parent class
class Animal {
// Method to make sound
void makeSound() {
System.out.println("Animal is making a sound");
}}
// Child class inheriting from Animal
class Dog extends Animal {
// Overriding the makeSound method of the parent class
@Override
void makeSound() {
System.out.println("Dog is barking");
}}
public class Main {
public static void main(String[] args) {
// Creating an object of Dog class
Dog dog = new Dog();
// Calling the makeSound method of Dog class
dog.makeSound(); // This will call the overridden method in Dog class
}}
Output
PROGRAM – 12
Python program for Explaining Logical expressions “and”, “or” and not

Code:
# Define variables
a = True
b = False

# Logical AND (and)


result_and = a and b
print("Logical AND (a and b):", result_and)

# Logical OR (or)
result_or = a or b
print("Logical OR (a or b):", result_or)

# Logical NOT (not)


result_not_a = not a
result_not_b = not b
print("Logical NOT (not a):", result_not_a)
print("Logical NOT (not b):", result_not_b)

Output
PROGRAM – 13
Python program for left shift and right shift.

Code:
def left_shift(num, shift):
return num << shift

def right_shift(num, shift):


return num >> shift

# Example usage:
number = 10
shift_amount = 2

# Left shift
result_left_shift = left_shift(number, shift_amount)
print("Left shift:", result_left_shift)

# Right shift
result_right_shift = right_shift(number, shift_amount)
print("Right shift:", result_right_shift)

Output
PROGRAM – 14
A python program to explain Operator Precedence.

Code:
# Define variables
a = 10
b=5
c=2

# Arithmetic operations
result = a + b * c
print("Result of a + b * c:", result)

# Operator precedence can be overridden by using parentheses


result_with_parentheses = (a + b) * c
print("Result of (a + b) * c:", result_with_parentheses)

# More examples
result_ex1 = a + b / c
print("Result of a + b / c:", result_ex1)

result_ex2 = (a + b) / c
print("Result of (a + b) / c:", result_ex2)
Output
PROGRAM – 15
A python program to explain Case Changing of Strings.

Code:
# Example of Case Changing of Strings
# Original string
string = "Hello, World!"
# Convert the string to uppercase
uppercase_string = string.upper()
print("Uppercase:", uppercase_string) # Output: HELLO, WORLD!
# Convert the string to lowercase
lowercase_string = string.lower()
print("Lowercase:", lowercase_string) # Output: hello, world!
# Convert the first character of each word to uppercase
titlecase_string = string.title()
print("Titlecase:", titlecase_string) # Output: Hello, World!
# Swap the case of each character in the string
swapcase_string = string.swapcase()
print("Swapcase:", swapcase_string) # Output: hELLO, wORLD!

Output:
PROGRAM – 16
A python program to explain String replace() Method.

Code:
# Example of String replace() Method

# Original string
string = "Hello, World!"

# Replace "Hello" with "Hi"


new_string = string.replace("Hello", "Hi")
print("Original string:", string)
print("After replacing 'Hello' with 'Hi':", new_string)

# Replace multiple occurrences of a substring


string = "apple, apple, cherry, apple"
new_string = string.replace("apple", "banana")
print("\nOriginal string:", string)
print("After replacing 'apple' with 'banana':", new_string)

# Replace with an empty string to remove a substring


string = "Hello, World!"
new_string = string.replace("Hello, ", "")
print("\nOriginal string:", string)
print("After removing 'Hello,':", new_string)

# Replace all occurrences of a character


string = "Hello, World!"
new_string = string.replace("l", "X")
print("\nOriginal string:", string)
print("After replacing 'l' with 'X':", new_string)

Output
PROGRAM – 17
A python program to explain the calling a function

Code:
# Define a function named 'greet' which takes one parameter 'name'
def greet(name):
# Print a greeting message with the provided name
print("Hello, " + name + "!")

# Call the 'greet' function with an argument


greet("Aaradhya") # Output: Hello, Adarsh!

# Call the 'greet' function with a different argument


greet("shubham") # Output: Hello, shubham!
PROGRAM – 18
A python program to find the greater number between two.

Code:
def find_greater_number(num1, num2):

if num1 > num2:


return num1
elif num2 > num1:
return num2
else:
return "Both numbers are equal"

num1 = input("Type a number: ")


num2 = input("Type another number: ")
result = find_greater_number(num1, num2)
print("The greater number between", num1, "and", num2, "is:", result)

Output:
PROGRAM – 19
A python program to find whether a list is empty or not

Code:
def is_list_empty(input_list):
if not input_list:
return True
else:
return False

# Example usage:
empty_list = []
non_empty_list = [1, 2, 3]

if is_list_empty(empty_list):
print("The list is empty")
else:
print("The list is not empty")

if is_list_empty(non_empty_list):
print("The list is empty")
else:
print("The list is not empty")

You might also like