0% found this document useful (0 votes)
16 views17 pages

II B. Tech. /I SEM/CSE (AI&ML) /20AM3302/OOP Through JAVA LAB /PVP20

The document outlines a series of Java programming lab experiments for B.Tech students in Computer Science focusing on Object-Oriented Programming concepts. Each experiment includes an aim, program code, and expected output, covering topics such as method overloading, inheritance, static methods, final variables, abstract classes, and method overriding. The experiments are designed to enhance students' understanding of Java programming and OOP principles.

Uploaded by

krishnatejesh99
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)
16 views17 pages

II B. Tech. /I SEM/CSE (AI&ML) /20AM3302/OOP Through JAVA LAB /PVP20

The document outlines a series of Java programming lab experiments for B.Tech students in Computer Science focusing on Object-Oriented Programming concepts. Each experiment includes an aim, program code, and expected output, covering topics such as method overloading, inheritance, static methods, final variables, abstract classes, and method overriding. The experiments are designed to enhance students' understanding of Java programming and OOP principles.

Uploaded by

krishnatejesh99
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

II B. Tech.

/I SEM/CSE (AI&ML)/20AM3302/OOP through JAVA LAB /PVP20


DATE OF EXPERIMENT: PAGE NO:

WEEK: 4 Exp. No.: 1

AIM:

Create a class named 'PrintNumber' to print various numbers of different datatypes by creating
different methods with the same name 'printn' having a parameter for each datatype.

PROGRAM:

import java.util.*;

class PrintNumber
{
void printn(int n)
{
System.out.println("integer : "+n);
}
void printn(double n)
{
System.out.println("double : "+n);
}
void printn(float n)
{
System.out.println("float : "+n);
}
void printn(long n)
{
System.out.println("long : "+n);
}
public static void main(String[] args)
{
PrintNumber p=new PrintNumber();
p.printn(38);
p.printn(38.);
p.printn(3.25f);
p.printn(38L);
}
}

OUTPUT:

DEPARTMENT OF CSE (ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING)


P.V.P SIDDHARTHA INSTITUTE OF TECHNOLOGY
II B. Tech. /I SEM/CSE (AI&ML)/20AM3302/OOP through JAVA LAB /PVP20
DATE OF EXPERIMENT: PAGE NO:

DESCRIPTION:

DEPARTMENT OF CSE (ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING)


P.V.P SIDDHARTHA INSTITUTE OF TECHNOLOGY
II B. Tech. /I SEM/CSE (AI&ML)/20AM3302/OOP through JAVA LAB /PVP20
DATE OF EXPERIMENT: PAGE NO:

WEEK: 4 Exp. No.: 2

AIM:

Create a class to print the area of a square and a rectangle. The class has two methods with the
same name but different number of parameters. The method for printing area of rectangle has
two parameters which are length and breadth respetively while the other method for printing
area of square has one parameter which is side of square.

PROGRAM:

import java .util.*;

class Area{
public void area(double l,double b){
System.out.println("area of rectangle : "+(l*b));
}
public void area(double s){
System.out.println("area of square : "+(s*s));
}
public static void main(String[] args){
Area a=new Area();
a.area(3.2,3);
a.area(6);
}
}

OUTPUT:

DESCRIPTION:

DEPARTMENT OF CSE (ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING)


P.V.P SIDDHARTHA INSTITUTE OF TECHNOLOGY
II B. Tech. /I SEM/CSE (AI&ML)/20AM3302/OOP through JAVA LAB /PVP20
DATE OF EXPERIMENT: PAGE NO:

WEEK: 4 Exp. No.: 3

AIM:

Create a class 'Degree' having a method 'getDegree' that prints "I got a degree". It has two subclasses
namely 'Undergraduate' and 'Postgraduate' each having a method with the same name that prints "I
am an Undergraduate" and "I am a Postgraduate" respectively. Call the method by creating an object
of each of the three classes.

PROGRAM:

import java.util.*;
class Degree
{
void getDegree(){
System.out.println("I got a degree");
}
}
class Undergraduate extends Degree
{
void getDegree(){
System.out.println("I am an Undergraduate");
}
}
class Postgraduate extends Degree
{
void getDegree(){
System.out.println("I am a Postgraduate");
}
}
class Got
{
public static void main(String[] args){
Degree d=new Degree();
Undergraduate u=new Undergraduate();
Postgraduate p=new Postgraduate();
d.getDegree();
u.getDegree();
p.getDegree();
}
}

OUTPUT:

DEPARTMENT OF CSE (ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING)


P.V.P SIDDHARTHA INSTITUTE OF TECHNOLOGY
II B. Tech. /I SEM/CSE (AI&ML)/20AM3302/OOP through JAVA LAB /PVP20
DATE OF EXPERIMENT: PAGE NO:

DESCRIPTION:

DEPARTMENT OF CSE (ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING)


P.V.P SIDDHARTHA INSTITUTE OF TECHNOLOGY
II B. Tech. /I SEM/CSE (AI&ML)/20AM3302/OOP through JAVA LAB /PVP20
DATE OF EXPERIMENT: PAGE NO:

WEEK: 4 Exp. No.: 4

AIM:

A class has an integer data member 'i' and a method named 'printNum' to print thevalue of 'i'.
Its subclass also has an integer data member 'j' and a method named 'printNum' to print the
value of 'j'. Make an object of the subclass and use it to assign a value to 'i' and to 'j'. Now call
the method 'printNum' by this object.

PROGRAM:

class A
{
int i;
void printNum(){
System.out.println(i);
}
}
class B extends A
{
int j;
void printNum(){
System.out.println(j);
}
public static void main(String[] args){
B b=new B();
b.i=4;
b.j=8;
b.printNum();
}
}

OUTPUT:

DEPARTMENT OF CSE (ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING)


P.V.P SIDDHARTHA INSTITUTE OF TECHNOLOGY
II B. Tech. /I SEM/CSE (AI&ML)/20AM3302/OOP through JAVA LAB /PVP20
DATE OF EXPERIMENT: PAGE NO:

DESCRIPTION:

DEPARTMENT OF CSE (ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING)


P.V.P SIDDHARTHA INSTITUTE OF TECHNOLOGY
II B. Tech. /I SEM/CSE (AI&ML)/20AM3302/OOP through JAVA LAB /PVP20
DATE OF EXPERIMENT: PAGE NO:

WEEK: 4 Exp. No.: 5

AIM:

Suppose a class 'A' has a static method to print "Parent". Its subclass 'B' also has a static method with
the same name to print "Child". Now call this method by the objects of the two classes. Also, call this
method by an object of the parent class refering to the child class i.e. A obj = new B() (method hiding)

PROGRAM:

import java.util.*;
class A
{
static void m1(){
System.out.println("Parent");
}
}
class B extends A
{
static void m1(){
System.out.println("Child");

}
class Call
{
public static void main(String[] args){
A a=new A();
B b=new B();
A n=new B();
a.m1();
b.m1();
n.m1();
}
}

OUTPUT:

DEPARTMENT OF CSE (ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING)


P.V.P SIDDHARTHA INSTITUTE OF TECHNOLOGY
II B. Tech. /I SEM/CSE (AI&ML)/20AM3302/OOP through JAVA LAB /PVP20
DATE OF EXPERIMENT: PAGE NO:

DESCRIPTION:

DEPARTMENT OF CSE (ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING)


P.V.P SIDDHARTHA INSTITUTE OF TECHNOLOGY
II B. Tech. /I SEM/CSE (AI&ML)/20AM3302/OOP through JAVA LAB /PVP20
DATE OF EXPERIMENT: PAGE NO:

WEEK: 4 Exp. No.: 6

AIM:

Write a Java program which demonstrates the uses of super keyword.

PROGRAM:
class Vehicle {
String brand;
Vehicle(String brand) {
this.brand = brand;
}
void start() {
System.out.println("Starting the " + brand + " vehicle.");
}
}
class Car extends Vehicle
{
int year;
Car(String brand, int year)
{
super(brand);
this.year = year;
}
void start() {
super.start();
System.out.println(brand + " car is revving up.");
}
}
public class SuperKeywordDemo {
public static void main(String[] args) {
Car myCar = new Car("Toyota", 2022);
myCar.start();
}
}

OUTPUT:

DEPARTMENT OF CSE (ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING)


P.V.P SIDDHARTHA INSTITUTE OF TECHNOLOGY
II B. Tech. /I SEM/CSE (AI&ML)/20AM3302/OOP through JAVA LAB /PVP20
DATE OF EXPERIMENT: PAGE NO:

DESCRIPTION:

DEPARTMENT OF CSE (ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING)


P.V.P SIDDHARTHA INSTITUTE OF TECHNOLOGY
II B. Tech. /I SEM/CSE (AI&ML)/20AM3302/OOP through JAVA LAB /PVP20
DATE OF EXPERIMENT: PAGE NO:

WEEK: 4 Exp. No.: 7

AIM:

How to initialize an instance blank final variable & static blank final variable? Demonstrate
with an example.

PROGRAM:
class MyClass {
final int instanceBlankFinalVar;
static final int staticBlankFinalVar;
static {
staticBlankFinalVar = 200;
}
MyClass(int value)
{
instanceBlankFinalVar = value;
}
void displayValues()
{
System.out.println("Instance Blank Final Variable: " + instanceBlankFinalVar);
System.out.println("Static Blank Final Variable: " + staticBlankFinalVar);
}
}
public class BlankFinalVariablesDemo {
public static void main(String[] args) {
MyClass obj1 = new MyClass(100);
MyClass obj2 = new MyClass(300);
obj1.displayValues();
obj2.displayValues();
}
}

OUTPUT:

DESCRIPTION:

DEPARTMENT OF CSE (ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING)


P.V.P SIDDHARTHA INSTITUTE OF TECHNOLOGY
II B. Tech. /I SEM/CSE (AI&ML)/20AM3302/OOP through JAVA LAB /PVP20
DATE OF EXPERIMENT: PAGE NO:

WEEK: 4 Exp. No.: 8

AIM:

Write a Java program which demonstrates final keyword.

PROGRAM:
public class FinalKeywordDemo
{
public static void main(String[] args)
{
final int x = 10;
final String greeting = "Hello";
System.out.println(greeting);
final int[] numbers = {1, 2, 3};
numbers[0] = 4;
displayMessage();
final MyClass myObj = new MyClass();
myObj.display();
}
final static void displayMessage() {
System.out.println("This is a final method.");
}
}
final class MyClass
{
void display()
{
System.out.println("This is a final class.");
}
}
OUTPUT:

DESCRIPTION:

DEPARTMENT OF CSE (ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING)


P.V.P SIDDHARTHA INSTITUTE OF TECHNOLOGY
II B. Tech. /I SEM/CSE (AI&ML)/20AM3302/OOP through JAVA LAB /PVP20
DATE OF EXPERIMENT: PAGE NO:

WEEK: 4 Exp. No.: 9

AIM:

Create an abstract class, extends it in subclasses which are having a method with same signature.
Declare that method in abstract class & implement it in subclasses.
PROGRAM:
abstract class Animal {
String name;
Animal(String name) {
this.name = name;
}
abstract void makeSound();
void sleep() {
System.out.println(name + " is sleeping.");
}
}
class Dog extends Animal {
Dog(String name) {
super(name);
}
void makeSound() {
System.out.println(name + " says Woof!");
}
}
class Cat extends Animal {
Cat(String name) {
super(name);
}
void makeSound() {
System.out.println(name + " says Meow!");
}
}
public class AbstractClassExample {
public static void main(String[] args) {
Animal dog = new Dog("Buddy");
Animal cat = new Cat("Whiskers");
dog.makeSound();
dog.sleep();
cat.makeSound();
cat.sleep();
}
}
OUTPUT:

DEPARTMENT OF CSE (ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING)


P.V.P SIDDHARTHA INSTITUTE OF TECHNOLOGY
II B. Tech. /I SEM/CSE (AI&ML)/20AM3302/OOP through JAVA LAB /PVP20
DATE OF EXPERIMENT: PAGE NO:

DESCRIPTION:

DEPARTMENT OF CSE (ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING)


P.V.P SIDDHARTHA INSTITUTE OF TECHNOLOGY
II B. Tech. /I SEM/CSE (AI&ML)/20AM3302/OOP through JAVA LAB /PVP20
DATE OF EXPERIMENT: PAGE NO:

WEEK: 4 Exp. No.: 10

AIM:

Write a Java program for method overriding with upcasting

PROGRAM:
class Shape {
void draw() {
System.out.println("Drawing a shape");
}
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing a circle");
}
}
class Square extends Shape {
void draw() {
System.out.println("Drawing a square");
}
}
public class MethodOverridingWithUpcasting {
public static void main(String[] args) {
Shape shape;
shape = new Circle();
shape.draw();
shape = new Square();
shape.draw();
}
}
OUTPUT:

DESCRIPTION:

DEPARTMENT OF CSE (ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING)


P.V.P SIDDHARTHA INSTITUTE OF TECHNOLOGY
II B. Tech. /I SEM/CSE (AI&ML)/20AM3302/OOP through JAVA LAB /PVP20
DATE OF EXPERIMENT: PAGE NO:

DEPARTMENT OF CSE (ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING)


P.V.P SIDDHARTHA INSTITUTE OF TECHNOLOGY

You might also like