0% found this document useful (0 votes)
4 views19 pages

Java Lab Manual 01

The document is a laboratory manual for Object Oriented Programming with Java (BCS-452) for the Bachelor of Technology in Computer Science and Engineering for the session 2023-2024. It includes various programming exercises demonstrating concepts such as inheritance, abstraction, encapsulation, method overloading, interfaces, exception handling, and Spring framework integration. Each section provides code examples and expected outputs to illustrate the concepts effectively.

Uploaded by

kmkze
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views19 pages

Java Lab Manual 01

The document is a laboratory manual for Object Oriented Programming with Java (BCS-452) for the Bachelor of Technology in Computer Science and Engineering for the session 2023-2024. It includes various programming exercises demonstrating concepts such as inheritance, abstraction, encapsulation, method overloading, interfaces, exception handling, and Spring framework integration. Each section provides code examples and expected outputs to illustrate the concepts effectively.

Uploaded by

kmkze
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Lab Manual – Object Oriented Programming with Java (BCS-452)

Object Oriented
Programming with Java
(BCS-452)

LABORATORY MANUAL
FOR
Bachelor of Technology
In
Computer Science and Engineering
Session: 2023-2024

MGM'S COLLEGE OF ENGINEERING &


TECHNOLOGY
A-09, Sec-62, NOIDA, U.P.
Lab Manual – Object Oriented Programming with Java (BCS-452)

1 Write a program to print Hello World


// File will save by the name of [Link]
// compile with javac on command Prompt

Class Hello
{
Public static void main(String args[])
{
[Link](“Hello world”);
}
}

Understand OOPS Concept and basic java programs

2 Write a program Java program to illustrate the concept of inheritance base


class

class Bicycle {
// the Bicycle class has two fields
public int gear;
public int speed;

// the Bicycle class has one constructor


public Bicycle(int gear, int speed)
{
[Link] = gear;
[Link] = speed;
}

// the Bicycle class has three methods


public void applyBrake(int decrement)
{
speed = decrement;
}

public void speedUp(int increment)


{
speed =increment;
}

// toString() method to print info of Bicycle


public String toString()
{
return ("No of gears are " + gear + "\n" + "speed of bicycle is " + speed);
}
Lab Manual – Object Oriented Programming with Java (BCS-452)

// derived class
class MountainBike extends Bicycle {

// the MountainBike subclass adds one more field


public int seatHeight;

// the MountainBike subclass has one constructor


public MountainBike(int gear, int speed,int startHeight)
{
// invoking base-class(Bicycle) constructorsuper(gear, speed);
seatHeight = startHeight;
}

// the MountainBike subclass adds one more method


public void setHeight(int newValue)
{
seatHeight = newValue;
}

// overriding toString() method


// of Bicycle to print more info @Override

public String toString()


{
return ([Link]() + "\nseat height is " +seatHeight);
}
}

// driver class
public class Test {
public static void main(String args[])
{

MountainBike mb = new MountainBike(3, 100, 25);


[Link]([Link]());
}
}

Output
No of gears are 3
speed of bicycle is 100
seat height is 25
Lab Manual – Object Oriented Programming with Java (BCS-452)
Lab Manual – Object Oriented Programming with Java (BCS-452)

3 Write a program Java Program to Illustrate, Invocation of Constructor,


Calling Without Usage of super Keyword

// Base class is Super class

class Base {

// Constructor of super class


Base()
{
// Print statement
[Link]("Base Class Constructor Called ");
}
}

// Class 2
// Sub class
class Derived extends Base {

// Constructor of sub class


Derived()
{

// Print statement
[Link]("Derived Class Constructor Called ");
}
}

// Class 3
// Main class
class Abcd {

// Main driver method


public static void main(String[] args)
{

// Creating an object of sub class


// inside main() method
Derived d = new Derived();

// Note: Here first super class constructor will be


// called there after derived(sub class) constructor
Lab Manual – Object Oriented Programming with Java (BCS-452)

// will be called
}
}
Lab Manual – Object Oriented Programming with Java (BCS-452)

Abstraction in java

4 Write a program Java program to illustrate of Abstraction


abstract class Shape {
String color;

// these are abstract methods


abstract double area();
public abstract String toString();

// abstract class can have the constructor


public Shape(String color)
{
[Link]("Shape constructor called");
[Link] = color;
}

// this is a concrete method


public String getColor() { return color; }
}
class Circle extends Shape {
double radius;

public Circle(String color, double radius)


{

// calling Shape constructor


super(color);
[Link]("Circle constructor called");
[Link] = radius;
}

// @Override
double area()
{
return [Link] * [Link](radius, 2);
}

// @Override
public String toString()
{
return "Circle color is " + [Link]()
+ "and area is : " + area();
}
}
class Rectangle extends Shape {
Lab Manual – Object Oriented Programming with Java (BCS-452)

double length;
double width;

public Rectangle(String color, double length,


double width)
{
// calling Shape constructor
super(color);
[Link]("Rectangle constructor called");
[Link] = length;
[Link] = width;
}

//@Override
double area() { return length * width; }

//@Override
public String toString()
{
return "Rectangle color is " + [Link]()
+ "and area is : " + area();
}
}
public class Test {
public static void main(String[] args)
{
Shape s1 = new Circle("Red", 2.2);
Shape s2 = new Rectangle("Yellow", 2, 4);

[Link]([Link]());
[Link]([Link]());
}
}

Output
Shape constructor called
Circle constructor called
Shape constructor called
Rectangle constructor called
Circle color is Redand area is : 15.205308443374602
Rectangle color is Yellowand area is : 8.0
Lab Manual – Object Oriented Programming with Java (BCS-452)

Encapsulation in JAVA

5 Java Program to demonstrate, Java Encapsulation

// Person Class

class Person {
// Encapsulating the name and age
// only approachable and used using
// methods defined
private String name;
private int age;

public String getName() { return name; }

public void setName(String name) { [Link] = name; }

public int getAge() { return age; }

public void setAge(int age) { [Link] = age; }


}

// Driver Class
public class Main {
// main function
public static void main(String[] args)
{
// person object created
Person person = new Person();
[Link]("John");
[Link](30);

// Using methods to get the values from the


// variables
[Link]("Name: " + [Link]());
[Link]("Age: " + [Link]());
}
}

Output
Name: John
Age: 30
Lab Manual – Object Oriented Programming with Java (BCS-452)

6 Write a Java Program for Method overloading By using Different Types of


Arguments

// Class 1
// Helper class
class Helper {

// Method with 2 integer parameters


static int Multiply(int a, int b)
{
// Returns product of integer numbers
return a * b;
}

// Method 2
// With same name but with 2 double parameters
static double Multiply(double a, double b)
{
// Returns product of double numbers
return a * b;
}
}

// Class 2
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Calling method by passing
// input as in arguments
[Link]([Link](2, 4));
[Link]([Link](5.5, 6.3));
}
}

Output
8
34.65
Lab Manual – Object Oriented Programming with Java (BCS-452)

Java Interfaces

7 Write a program java program to demonstrate how diamond problem is


handled in case of default methods

// Interface 1
interface API {
// Default method
default void show()
{

// Print statement
[Link]("Default API");
}
}

// Interface 2
// Extending the above interface
interface Interface1 extends API {
// Abstract method
void display();
}

// Interface 3
// Extending the above interface
interface Interface2 extends API {
// Abstract method
void print();
}

// Main class
// Implementation class code
class TestClass implements Interface1, Interface2 {
// Overriding the abstract method from Interface1
public void display()
{
[Link]("Display from Interface1");
}
// Overriding the abstract method from Interface2
public void print()
{
[Link]("Print from Interface2");
}
// Main driver method
public static void main(String args[])
{
// Creating object of this class
Lab Manual – Object Oriented Programming with Java (BCS-452)

// in main() method
TestClass d = new TestClass();

// Now calling the methods from both the interfaces


[Link](); // Default method from API
[Link](); // Overridden method from Interface1
[Link](); // Overridden method from Interface2
}
}
Lab Manual – Object Oriented Programming with Java (BCS-452)

8. Write a program to access Packages in java


// All the classes and interfaces of this package
// will be accessible but not subpackages.

//illustrate Only mentioned class of this package will be accessible.


import [Link];

// Class name is generally used when two packages have the same
// class name. For example in below code both packages have
// date class so using a fully qualified name to avoid conflict

import [Link];
import [Link];
// Java program to demonstrate accessing of members
when
// corresponding classes are imported and not imported.
import [Link];

public class ImportDemo


{
public ImportDemo()
{
// [Link] is imported, hence we are
// able to access directly in our code.
Vector newVector = new Vector();

// [Link] is not imported, hence


// we were referring to it using the complete
// package.
[Link] newList = new [Link]();
}

public static void main(String arg[])


{
new ImportDemo();
}
}
Lab Manual – Object Oriented Programming with Java (BCS-452)

Exception Handling
9. Java Program to Illustrate Exception Handling with Method Overriding
Case 1: If SuperClass doesn’t declare any exception and subclass declare checked
exception.
// Where SuperClass does not declare any exception and
// subclass declare checked exception
// Importing required classes
import [Link].*;

class SuperClass {

// SuperClass doesn't declare any exception


void method() {
[Link]("SuperClass");
}
}

// SuperClass inherited by the SubClass


class SubClass extends SuperClass {

// method() declaring Checked Exception IOException


void method() throws IOException {

// IOException is of type Checked Exception


// so the compiler will give Error

[Link]("SubClass");
}

// Driver code
public static void main(String args[]) {
SuperClass s = new SubClass();
[Link]();
}
}

Output:
Lab Manual – Object Oriented Programming with Java (BCS-452)

Case 2: If SuperClass doesn’t declare any exception and SubClass declare Unchecked
exception

10. Java Program to Illustrate Exception Handling with Method Overriding

// Where SuperClass doesn't declare any exception and


// SubClass declare Unchecked exception

// Importing required classes


import [Link].*;

class SuperClass {

// SuperClass doesn't declare any exception


void method()
{
[Link]("SuperClass");
} }

// SuperClass inherited by the SubClass


class SubClass extends SuperClass {

// method() declaring Unchecked Exception ArithmeticException


void method() throws ArithmeticException
{

// ArithmeticException is of type Unchecked Exception


// so the compiler won't give any error

[Link]("SubClass");
}

// Driver code
public static void main(String args[])
{
SuperClass s = new SubClass();
[Link]();
}
}
Output
SubClass
Lab Manual – Object Oriented Programming with Java (BCS-452)

11. Java program to illustrate standard input output streams


import [Link].*;
public class SimpleIO {

public static void main(String args[])


throws IOException
{
// InputStreamReader class to read input
InputStreamReader inp = null;

// Storing the input in inp


int = new InputStreamReader([Link]);

[Link]("Enter characters, " + " and '0' to quit.");


char c;
do {
c = (char)[Link]();
[Link](c);
} while (c != '0');
}}

Input:

ComputerScience0

Output:

Enter characters, and '0' to quit.


C
O
M
P
U
T
E
R
S
C
I
E
N
C
E
0
Lab Manual – Object Oriented Programming with Java (BCS-452)

12. Write a Program to Implementing WebApplicationInitializer using Spring


public class MyWebApplicationInitializer implements WebApplicationInitializer {

// Servlet container

public void onStartup(ServletContext container) throws ServletException {


AnnotationConfigWebApplicationContext context = new
AnnotationConfigWebApplicationContext();
[Link]([Link]);
[Link](container);

// Servlet configuration
}
}

// Class // Implementing WebApplicationInitializer


public class MyXmlWebApplicationInitializer implements WebApplicationInitializer {

// Servlet container
public void onStartup(ServletContext container) throws ServletException {
XmlWebApplicationContext context = new XmlWebApplicationContext();
[Link]("/WEB-INF/spring/[Link]");
[Link](container);

// Servlet configuration
}}

String path = "Documents/demoProject/src/main/resources/applicationcontext/student-


[Link]";

ApplicationContext context = new FileSystemXmlApplicationContext(path);


AccountService accountService = [Link]("studentService",
[Link]);

//AppConfig class
@Configuration

// Class
public class AppConfig {

@Bean

// Method
public Student student() {

return new Student(1, "Geek");


Lab Manual – Object Oriented Programming with Java (BCS-452)

}
}

// Class
public class Student {

// member variables
private int id;
private String name;

// Constructor 1
public Student() {}

// Constructor 2
public Student(int id, String name) {
[Link] = id;
[Link] = name; }

// Method of this class


// @Override
public String toString() {

return "Student{" + "id=" + id + ", name='" + name + '\'' + '}';


} }

// Class
// @SpringBootApplication
public class DemoApplication {

// Main driver method


public static void main(String[] args) {

// [Link]([Link], args);

// Creating its object

ApplicationContext context = new


AnnotationConfigApplicationContext([Link]);
Student student = [Link]([Link]);

// Print and display


[Link](student);
}}

// Main driver method


public static void main(String[] args) {
Lab Manual – Object Oriented Programming with Java (BCS-452)

ApplicationContext context = [Link]([Link], args);

Student student = [Link]([Link]);

// Print and display


[Link](student); }

You might also like