JAVA AND PYTHON FILE
JAVA AND PYTHON FILE
This is to certify that the programs in this file are compiled and executed by me, and I have not copied
them from anywhere. This work is done by me and I have not presented it anywhere else.
CHIKKU KUMAR
ECE III YEAR
226320025
CERTIFICATE BY TEACHER
This is to certify that the program in this file is done under my supervision and are not being copied from
anywhere or presented anywhere for any sort of benefit.
Program: -
public class WideningExample {
public static void main(String[] args) {
byte byteValue = 10;
// Widening conversions
short shortValue = byteValue; short int
intValue = shortValue;
long longValue = intValue;
float floatValue = longValue; double
doubleValue = floatValue;
Output: -
5
Page
2) Make a Java Program Using Narrowing.
Program: -
public class NarrowingExample {
public static void main(String[] args)
{ double doubleValue = 123.456;
float floatValue = (float) doubleValue;
long longValue = (long)
floatValue; int intValue = (int)
longValue;
short shortValue = (short)
intValue; byte byteValue = (byte)
shortValue;
Output: -
6
Page
3) WAP in which class variable are assigned via method of a class.
Program: -
void displayDetails() {
System.out.println("Student Name: " + name);
System.out.println("Student Age: " + age);
}
student1.setDetails("Mohit", 20);
student1.displayDetails();
}
}
Output: -
7
Page
4) Make a Java Program for default constructor.
Program: -
public class lab
{ String
brand; int
year;
lab() {
brand = "Mahindra Auto";
year = 2020;
}
void display() {
System.out.println("Car Brand: " + brand);
System.out.println("Car Year: " + year);
}
car1.display();
}
}
Output: -
8
Page
5) Make a Java Program for parameterized constructor
Program: -
public class lab
{ String
name; int id;
void display() {
System.out.println("Employee Name: " + name);
System.out.println("Employee ID: " + id);
}
emp1.display();
emp2.display();
}
}
Output: -
9
Page
6) Make a Java Program using constructor overloading.
Program: -
public class
lab { String
name; String
color;double
weight;
lab() {name = "Unknown"; color = "No color"; weight = 0.0;
}lab(String fruitName) { name = fruitName; color = "No color"; weight = 0.0;
}lab(String fruitName, String fruitColor) { name = fruitName;
color = fruitColor;
weight = 0.0;
}lab(String fruitName, String fruitColor, double fruitWeight) { name = fruitName;
color = fruitColor;
weight = fruitWeight;
}void display() {
System.out.println("Fruit Name: " + name);
System.out.println("Fruit Color: " + color);
System.out.println("Fruit Weight: " + weight + " kg");
System.out.println(" ---------------------------- ");
}public static void main(String[] args) { lab fruit1 = new lab();
lab fruit2 = new lab("Apple");
lab fruit3 = new lab("Banana", "Yellow");
lab fruit4 = new lab("Mango", "Orange", 0.5);
fruit1.display
();
fruit2.display
();
fruit3.display
();
fruit4.display
();}}
Output: -
10
Page
10
7) Make a Java Program using method overloading.
Program: -
public class lab {
Output: -
11
Page
11
8) Make a Java Program using multi-level inheritance.
Program: -
class Room {
void roomDetails() {
System.out.println("This is a room.");
}
}
class Bedroom extends Room {
void bedroomDetails() {
System.out.println("This is a bedroom with a bed and a wardrobe.");
}
}
class MasterBedroom extends Bedroom {
void masterBedroomDetails() {
System.out.println("This is a master bedroom with an attached bathroom and
balcony.");
}
}
public class lab {
public static void main(String[] args) {
MasterBedroom master = new MasterBedroom();
master.roomDetails();
master.bedroomDetails()
;
master.masterBedroomDetails();
}
}
Output: -
12
Page
12
9) Make a Java Program using method over-riding.
Program: -
class Vehicle {
void run() {
System.out.println("The vehicle is running");
}
}
class Bike extends Vehicle
{ @Override
void run() {
System.out.println("The bike is running safely");
}
}
public class lab {
public static void main(String[] args)
{ Bike bike = new Bike();
bike.run();
}
}
Output: -
13
Page
13
10) Make a Java Program using interface.
Program: -
interface Animal {
void sound();
void eat();}class Dog implements Animal { public void sound() {
System.out.println("The dog barks");
}public void eat() {System.out.println("The dog eats bones");}}
class Cat implements Animal {
public void sound() {
System.out.println("The cat meows");
Output: -
14
Page
14
11 Make a Java Program for extending interface.
Program: -
interface Animal {
void sound();
void eat();}interface Mammal extends Animal { void sleep();
}class Dog implements Mammal { public void sound() {
System.out.println("The dog barks");}public void eat() {
System.out.println("The dog eats bones");
}public void sleep() {System.out.println("The dog sleeps peacefully");
}}public class lab {public static void main(String[] args) { Dog myDog = new Dog();
myDog.sound();
myDog.eat();
myDog.sleep();
}}
Output: -
15
Page
15
12) Make a python Program for explaining logical expression “and”, “or” and
“not”.
Program: -
def logical_operators_demo():
a = True
b = False
print("Values of a and
b:") print("a =", a)
print("b =", b)
print("\nUsing 'and'
operator:") print("a and
a =", a and a)
print("a and b =", a and
b) print("b and b =", b
and b)print("\nUsing
'or' operator:") print("a
or a =", a or a)
print("a or b =",
a or b) print("b
or b =", b or b)
print("\nUsing 'not'
operator:") print("not a
=", not a)print("not b =",
not
b)logical_operators_demo)
Output: -
16
Page
16
13) Make a python Program for left shift and right shift.
Program: -
def shift_example():
num = 12
print(f"Original number: {num}")
print(f"Binary: {bin(num)}")
print(f"Binary: {bin(result_left)}")
print(f"Binary: {bin(result_right)}")
print(f"Binary: {bin(result_left3)}")
print(f"Binary: {bin(result_right3)}")
shift_example()
Output: -
17
Page
1
14) Make a python Program to explain operator Precedence.
Program: -
def operator_precedence():
print("Operator Precedence Examples:\n") result1 = 10 + 5 * 2
print("Multiplication vs Addition: ", result1)
result2 = (10 + 5) * 2
result2) result3 = 2 + 3 ** 2
result4 = (2 + 3) ** 2
result5 = 100 / 5 * 2 + 3 - 1
result7 = 3 + 2 > 4
result7)operator_precedence()
Output: -
18
Page
1
15) WAP to explain case changing of Strings in python.
Program: -
def case_changing():
text = "Hello World"
print("Original Text:", text)
lower_text = text.lower()
print("Lowercase:", lower_text)
upper_text = text.upper()
print("Uppercase:", upper_text)
title_text = text.title()
print("Title Case:", title_text)
swap_text = text.swapcase()
print("Swap Case:", swap_text)
capitalize_text =
text.capitalize()
print("Capitalize:", capitalize_text)
case_changing()
Output: -
19
Page
1
16) WAP to explain String replace () method in python.
Program: -
def replace_numbers():
number = 1234567890
print("Original Number:")
print(number)
number_str = str(number)
print(hidden_number)
print(replaced_zero)
pattern_replace = number_str.replace("456", "XYZ")
print(pattern_replace) replace_numbers()
Output: -
20
Page
1
17) WAP to explain the calling a function in python.
Program: -
def add_numbers(a,
b): result = a + b
print(f"The sum of {a} and {b} is: {result}")
def multiply_numbers(a, b):
result = a * b
print(f"The product of {a} and {b} is: {result}")
def greet_user():
print("Hello! Welcome to the function calling example.")
greet_user()
add_numbers(5, 10)
multiply_numbers(4, 3)
Output: -
21
Page
1
18) Make a python program to find the greater number between two.
Program: -
def find_greater(num1,
find_greater(number1, number2)
Output: -
22
Page
1
19) Make a python program to find whether a list is empty
or not.
Program: -
def
is_list_empty(input
input_list
test_list1 = []
test_list2 = [1, 2, 3]
empty? {is_list_empty(test_list2)}")
Output: -
23
Page
20) Make a python program using the build in max () function.
Program: -
largest_number = max(numbers)
Output: -
24
Page
21) Python program to demonstrate Removal of elements in a List Creating a
List
Program: -
List = ['G', 'E', 'E', 'K', 'S', 'F',
'0', 'R', 'G', 'E', 'E', 'K', 'S']
print("Initial List:
")print(List)
print("\nSlicing elements in a range 3-
8: ")Sliced_List = List[3:8]
print(Sliced_List)
Sliced_List=
List[5:]
print("\nElements sliced from 5th
")"element till the end: "
print(Sliced_List)
Sliced_List = List[:]
print("\nPrinting all elements using slice
operation: ")print(Sliced_List)
Output: -
25
Page
22) Python programto demo for all dictionary
methods
Program: -
Output: -
26
Page
23) Python programto Creating a Tuple with Mixed Datatype
Program: -
Program: -
t = (2, 4, 6)
print(all(t))
t=(0, False, False)
print(all(t))
t = (5, 0, 3, 1, False)
print(all(t))t = ()
print(all(t))
l= (2,4,6,8,10)
print(all(ele%2==0 for ele in l))
Output
28
Page
25) Square pattern in python program
Program: -
size = 5
for i in range(size):
for j in range(size):
print()
Output: -
29
Page
26) Triangle pattern in python program
Program: -
rows = 5
print()
Output: -
30
Page
27) Hollow Triangle pattern in python program
Program: -
rows = 5
if j == 1 or j == i or i == rows:
else:
print(" ", end=" ")
print()
Output: -
31
Page
28) Hollow Square pattern in python program
Program: -
size = 5
for i in range(size):
for j in range(size):
# Print '*' on borders, space inside
if i == 0 or i == size - 1 or j == 0 or j == size - 1:
print("*", end=" ")
else:
print(" ", end=" ")
print()
Output: -
32
Page
29) Diamond pattern in python program
Program: -
n=5
# Top half
for i in range(1, n + 1):
print(" " * (n - i) + "* " * i)
# Bottom half
for i in range(n - 1, 0, -1):
print(" " * (n - i) + "* " * i)
Output: -
33
Page
30) Python program for Fibonacci series
Program: -
n = 10
a, b = 0, 1
print("Fibonacci Series:")
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
Output: -
34
Page