[go: up one dir, main page]

0% found this document useful (0 votes)
5 views33 pages

JAVA AND PYTHON FILE

This document contains a collection of Java and Python programming lab exercises submitted by Chikku Kumar to Dr. Aman Tyagi. It includes various programs demonstrating concepts such as widening and narrowing conversions, constructors, inheritance, method overloading, and logical expressions in Python. Each program is accompanied by its output and is structured with a certificate of originality from both the student and the teacher.

Uploaded by

reyow54567
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)
5 views33 pages

JAVA AND PYTHON FILE

This document contains a collection of Java and Python programming lab exercises submitted by Chikku Kumar to Dr. Aman Tyagi. It includes various programs demonstrating concepts such as widening and narrowing conversions, constructors, inheritance, method overloading, and logical expressions in Python. Each program is accompanied by its output and is structured with a certificate of originality from both the student and the teacher.

Uploaded by

reyow54567
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/ 33

JAVA PROGRAMMING AND

INTRODUCTION TO PYTHON LAB

Submitted By: Submitted To:


CHIKKU KUMAR Dr. AMAN TYAGI
226320025 Assistant Professor
B. Tech VI Sem (III Year) CSE Department, FET, GKV

DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING

FACULTY OF ENGINEERING AND TECHNOLOGY, GURUKULA KANGRI


(DEEMED TO BE UNIVERSITY), HARIDWAR (2024-25)
CERTIFICATE BY STUDENT

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.

Dr. AMAN TYAGI


ASSISTANT PROFESSOR
DEPARTMENT OF COMPUTER SCIENCE & ENGG.
INDEX

SR NAME OF THE PROGRAMS PAGE SIGNATURE


NO NO.
1. WAP with the help of widening. 05
2. Make a Java Program Using Narrowing. 06
3. WAP in which class variable are assigned via method of a class. 07
4. Make a Java Program for default constructor. 08
5. Make a Java Program for parameterized constructor. 09
6. Make a Java Program using constructor overloading. 10
7. Make a Java Program using method overloading. 11
8. Make a Java Program using multi-level inheritance. 12
9. Make a Java Program using method over-riding. 13
10. Make a Java Program using interface. 14
11. Make a Java Program for extending interface. 15
12. Make a python Program for explaining logical expression “and”, “or” and 16
“not”.
13. Make a python Program for left shift and right shift. 17
14. Make a python Program to explain operator Precedence. 18
15. WAP to explain case changing of Strings in python. 19
16. WAP to explain String replace () method in python. 20
17. WAP to explain the calling a function in python. 21
18. Make a python program to find the greater number between two. 22
19. Make a python program to find whether a list is empty or not. 23
20. Make a python program using the build in max () function. 24
21. Python program to demonstrate Removal of elements in a List Creating a 25
list
22. Python program to demo for all dictionary methods 26
23. Python program to Creating a Tuple with Mixed Datatype 27
24. Python program to All elements of tuple is true 28
25. Square pattern in python program 29
26. Triangle pattern in python program 30
27. Hollow Triangle pattern in python program 31
28. Hollow Square pattern in python program 32
29. Diamond pattern in python program 33
30. Python program for Fibonacci series 34
3
1) WAP with the help of widening.

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;

System.out.println("Byte value: " + byteValue);


System.out.println("Short value: " + shortValue);
System.out.println("Int value: " + intValue);
System.out.println("Long value: " + longValue);
System.out.println("Float value: " + floatValue);
System.out.println("Double value: " + doubleValue);
}
}

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;

System.out.println("Double value: " + doubleValue);


System.out.println("Float value: " + floatValue);
System.out.println("Long value: " + longValue);
System.out.println("Int value: " + intValue);
System.out.println("Short value: " + shortValue);
System.out.println("Byte value: " + byteValue);
}
}

Output: -

6
Page
3) WAP in which class variable are assigned via method of a class.

Program: -

public class lab


{ String
name; int
age;

void setDetails(String studentName, int studentAge) {


name = studentName;
age = studentAge;
}

void displayDetails() {
System.out.println("Student Name: " + name);
System.out.println("Student Age: " + age);
}

public static void main(String[] args)


{ lab student1 = new lab();

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

public static void main(String[] args)


{ lab car1 = new lab();

car1.display();
}
}
Output: -

8
Page
5) Make a Java Program for parameterized constructor
Program: -
public class lab
{ String
name; int id;

lab(String employeeName, int employeeId) {


name = employeeName;
id = employeeId;
}

void display() {
System.out.println("Employee Name: " + name);
System.out.println("Employee ID: " + id);
}

public static void main(String[] args)


{ lab emp1 = new lab("Mohit",
101); lab emp2 = new
lab("Sandilya", 102);

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 {

int add(int a, int b) {


return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
double add(double a, double b) {
return a + b;
}
double add(double a, int b) {
return a + b;
}
public static void main(String[] args)
{ lab calc = new lab();
System.out.println("Addition of two integers: " + calc.add(10, 20));
System.out.println("Addition of three integers: " + calc.add(5, 15, 25));
System.out.println("Addition of two doubles: " + calc.add(2.5, 3.7));
System.out.println("Addition of double and integer: " + calc.add(4.5, 2));
}}

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

}public void eat() {System.out.println("The cat drinks milk");

}}public class lab {

public static void main(String[] args) { Animal myDog = new Dog();


Animal myCat = new Cat();
myDog.sound();
myDog.eat();
myCat.sound();
myCat.eat();
}}

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

result_left = num << 2

print(f"\nLeft Shift by 2: {num} << 2 = {result_left}")

print(f"Binary: {bin(result_left)}")

result_right = num >> 2

print(f"\nRight Shift by 2: {num} >> 2 = {result_right}")

print(f"Binary: {bin(result_right)}")

result_left3 = num << 3

print(f"\nLeft Shift by 3: {num} << 3 = {result_left3}")

print(f"Binary: {bin(result_left3)}")

result_right3 = num >> 3

print(f"\nRight Shift by 3: {num} >> 3 = {result_right3}")

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

print("Parentheses changes precedence:",

result2) result3 = 2 + 3 ** 2

print("Exponentiation has higher precedence:", result3)

result4 = (2 + 3) ** 2

print("Exponentiation with parentheses: ", result4)

result5 = 100 / 5 * 2 + 3 - 1

print("Mixed operations: ", result5)


result6 = True or False and False

print("Using logical operators: ", result6)

result7 = 3 + 2 > 4

print("Using logical operators: ",

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)

hidden_number = number_str.replace("3", "*").replace("6", "*")

print("\nAfter replacing '3' and '6' with '*':")

print(hidden_number)

replaced_zero = number_str.replace("0", "X")

print("\nAfter replacing '0' with 'X':")

print(replaced_zero)
pattern_replace = number_str.replace("456", "XYZ")

print("\nAfter replacing '456' with '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,

num2): if num1 > num2:

print(f"{num1} is greater than {num2}")

elif num2 > num1:

print(f"{num2} is greater than {num1}")


else:

print(f"Both numbers are equal: {num1} = {num2}")

number1 = float(input("Enter the first number: "))

number2 = float(input("Enter the second number: "))

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

_list): return not

input_list

if name == " main ":

test_list1 = []

test_list2 = [1, 2, 3]

print(f"Is test_list1 empty?

{is_list_empty(test_list1)}") print(f"Is test_list2

empty? {is_list_empty(test_list2)}")

Output: -

23
Page
20) Make a python program using the build in max () function.

Program: -

numbers = [15, 42, 7, 89, 23, 67]

largest_number = max(numbers)

print("The largest number in the


list is:", largest_number)

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: -

dict1 = {1: "Python", 2: "Java", 3: "Ruby", 4:


"Scala"}dict2= dict1.copy()
print(dict2)
dict1.clear()
print(dict1)
print(dict2.get(1)
)
print(dict2.items(
))
print(dict2.keys()
) dict2.pop(4)
print(dict)
dict2.popitem()
print(dict2)
dict2.update({3:
"Scala"})print(dict2)
print(dict2.values())

Output: -

26
Page
23) Python programto Creating a Tuple with Mixed Datatype

Program: -

Tuple1 = (5, 'Welcome', 7, 'Geeks')


print("\nTuple with Mixed
Datatypes: ")print(Tuple1)
Tuple1 = (0, 1, 2, 3)
Tuple2 = ('python',
'geek')Tuple3 =
(Tuple1, Tuple2)
print("\nTuple with nested tuples:
")print (Tuple3)
Tuple1= ('Geeks',) * 3
print("\nTuple with repetition:
")print(Tuple1)
Tuple1 =
('Geeks')n = 5
print("\nTuple with a
Loop")for i in
range(int(n)):
Tuple1
=(Tuple1,)
print(Tuple1)
Output: -
27
Page
24) Python programto All elementsof tuple is true

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("*", end=" ")

print()

Output: -

29
Page
26) Triangle pattern in python program
Program: -

rows = 5

for i in range(1, rows + 1):


for j in range(i):

print("*", end=" ")

print()
Output: -

30
Page
27) Hollow Triangle pattern in python program
Program: -

rows = 5

for i in range(1, rows + 1):


for j in range(1, i + 1):

# Print '*' for first or last column, or last row

if j == 1 or j == i or i == rows:

print("*", end=" ")

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

You might also like