0% found this document useful (0 votes)
37 views15 pages

Java Lab Manual

Uploaded by

Manish Verma
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)
37 views15 pages

Java Lab Manual

Uploaded by

Manish Verma
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/ 15

Content Page

S. No. Experiment Title

1 Use Java Compiler and Eclipse Platform to Write and Execute Java Program

2 Create Java Program using Command Line Arguments

3 Understand OOP Concepts and Basics of Java Programming

4 Create Java Programs using Inheritance and Polymorphism

5 Implement Error Handling using Exception Handling and Multithreading

6 Create Java Programs using Packages

7 Construct Java Programs using Java I/O Package

8 Create Industry-Oriented Application using Spring Framework

9 Test RESTful Web Services using Spring Boot

10 Test Frontend Web Application with Spring Boot


Experiment No. 1 – Use Java Compiler and Eclipse Platform to Write and Execute Java
Program

Aim:

To write, compile, and execute a simple Java program using Java compiler and Eclipse IDE.

Tools Required:

 JDK (Java Development Kit)

 Eclipse IDE

Procedure:

1. Open Eclipse IDE and create a new Java project.

2. Create a new Java class file (HelloWorld.java).

3. Write a basic Java program using main() method.

4. Save the program and click Run to compile and execute.

Sample Code:

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello, World!");

Output:

Hello, World!

Conclusion:

A Java program was successfully written, compiled, and executed using Eclipse IDE, verifying
basic setup and understanding of Java environment.
Experiment No. 2 – Create Java Program using Command Line Arguments

Aim:

To create a Java program that accepts input through command line arguments.

Tools Required:

 JDK

 Command Prompt / Terminal

Procedure:

1. Open any text editor and write the Java program.

2. Save it as CommandLineDemo.java.

3. Open terminal, compile using javac, and run using java followed by arguments.

Sample Code:

public class CommandLineDemo {

public static void main(String[] args) {

for (int i = 0; i < args.length; i++) {

System.out.println("Argument " + i + ": " + args[i]);

Execution:

javac CommandLineDemo.java

java CommandLineDemo Hello Java World

Output:

Argument 0: Hello

Argument 1: Java
Argument 2: World

Conclusion:

Java programs can accept inputs at runtime using command line arguments, which are
accessible through the args[] array in the main() method.

Experiment No. 3 – Understand OOP Concepts and Basics of Java Programming

Aim:

To understand and implement basic Object-Oriented Programming concepts using Java.

Tools Required:

 JDK

 Eclipse IDE or any text editor

Procedure:

1. Create a Java class with fields and methods.

2. Demonstrate OOP principles like Class, Object, Encapsulation, and Method Calling.

3. Compile and run the program.

Sample Code:

class Student {

String name;

int age;

void display() {

System.out.println("Name: " + name + ", Age: " + age);

}
public class TestStudent {

public static void main(String[] args) {

Student s = new Student();

s.name = "Rahul";

s.age = 20;

s.display();

Output:

Name: Rahul, Age: 20

Conclusion:

Basic OOP principles such as class creation, object instantiation, and encapsulation were
successfully implemented in Java.

Experiment No. 4 – Create Java Programs using Inheritance and Polymorphism

Aim:

To implement Inheritance and Polymorphism in Java.

Tools Required:

 JDK

 Any Java IDE or text editor

Procedure:

1. Create a base class and a derived class to demonstrate inheritance.


2. Override a method in the derived class to show runtime polymorphism.

3. Compile and execute the program.

Sample Code:

class Animal {

void sound() {

System.out.println("Animal makes sound");

class Dog extends Animal {

void sound() {

System.out.println("Dog barks");

public class TestPolymorphism {

public static void main(String[] args) {

Animal a = new Dog(); // Runtime polymorphism

a.sound();

Output:

Dog barks

Conclusion:

Inheritance allows one class to acquire properties of another, and polymorphism enables
method overriding, which was successfully implemented using dynamic method dispatch.
Experiment No. 5 – Implement Error Handling using Exception Handling and Multithreading

Aim:

To implement exception handling and multithreading in Java.

Tools Required:

 JDK

 Any Java IDE or text editor

Procedure:

1. Write code using try, catch, and finally blocks for exception handling.

2. Create and run multiple threads using the Thread class.

3. Compile and execute the program.

Sample Code:

class MyThread extends Thread {

public void run() {

System.out.println("Thread is running: " + getName());

public class ExceptionAndThreadDemo {

public static void main(String[] args) {

try {

int a = 5 / 0;

} catch (ArithmeticException e) {

System.out.println("Exception caught: " + e);

MyThread t1 = new MyThread();


t1.start();

Output:

Exception caught: java.lang.ArithmeticException: / by zero

Thread is running: Thread-0

Conclusion:

Java provides built-in support for handling runtime errors using exception handling, and
multithreading enables concurrent execution of code, both of which were successfully
demonstrated.

Experiment No. 6 – Create Java Programs using Packages

Aim:

To create and use packages in Java for modular programming.

Tools Required:

 JDK

 Any Java IDE or terminal

Procedure:

1. Create a Java file with a package declaration.

2. Save it inside a folder named as the package.

3. Compile using javac -d . FileName.java.

4. Import and use the package in another class.

Sample Code:

File: mypack/Message.java

package mypack;
public class Message {

public void show() {

System.out.println("Hello from Package!");

File: TestPackage.java

import mypack.Message;

public class TestPackage {

public static void main(String[] args) {

Message m = new Message();

m.show();

Output:

Hello from Package!

Conclusion:

Packages in Java help organize classes and interfaces into namespaces, improving code
maintainability and modularity.

Experiment No. 7 – Construct Java Programs using Java I/O Package

Aim:

To perform file input and output operations using Java I/O package.

Tools Required:
 JDK

 Any Java IDE or text editor

Procedure:

1. Import java.io.* package.

2. Use FileWriter to write data into a file.

3. Use FileReader to read data from the file.

4. Compile and run the program.

Sample Code:

import java.io.*;

public class FileIODemo {

public static void main(String[] args) throws IOException {

FileWriter fw = new FileWriter("output.txt");

fw.write("Hello Java I/O");

fw.close();

FileReader fr = new FileReader("output.txt");

int i;

while ((i = fr.read()) != -1)

System.out.print((char) i);

fr.close();

Output:

Hello Java I/O

Conclusion:
Java I/O package provides classes to read from and write to files, allowing basic file handling
operations like reading, writing, and closing file streams.

Experiment No. 8 – Create Industry-Oriented Application using Spring Framework

Aim:

To create a basic Spring Framework application demonstrating dependency injection.

Tools Required:

 Spring Framework

 Java IDE (e.g., Eclipse/IntelliJ)

 Maven/Gradle (build tool)

Procedure:

1. Create a Java project with Spring dependencies.

2. Define a simple component class and a service class.

3. Use @Component, @Autowired, and @Configuration annotations.

4. Initialize context using AnnotationConfigApplicationContext.

5. Run and observe the injected output.

Sample Code:

@Component

class HelloService {

public void sayHello() {

System.out.println("Hello from Spring!");

@Configuration
@ComponentScan("com.example")

class AppConfig {}

public class MainApp {

public static void main(String[] args) {

AnnotationConfigApplicationContext context = new


AnnotationConfigApplicationContext(AppConfig.class);

HelloService hs = context.getBean(HelloService.class);

hs.sayHello();

context.close();

Output:

Hello from Spring!

Conclusion:

Spring Framework simplifies Java development by providing built-in support for dependency
injection and component management, making applications more modular and maintainable.

Experiment No. 9 – Test RESTful Web Services using Spring Boot

Aim:

To develop and test a basic RESTful API using Spring Boot.

Tools Required:

 Spring Boot

 IDE (Eclipse/IntelliJ)

 Postman or browser

Procedure:
1. Create a Spring Boot project using Spring Initializr with Spring Web dependency.

2. Create a REST controller using @RestController.

3. Map a GET request using @GetMapping.

4. Run the application and test the endpoint using Postman or browser.

Sample Code:

@RestController

public class HelloController {

@GetMapping("/hello")

public String hello() {

return "Hello from REST API!";

Application runs at:

http://localhost:8080/hello

Output:

Hello from REST API!

Conclusion:

Spring Boot makes it easy to create REST APIs. Using @RestController and annotations like
@GetMapping, web services can be tested quickly and effectively.
Experiment No. 10 – Test Frontend Web Application with Spring Boot

Aim:

To test a simple frontend web application built using Spring Boot and Thymeleaf or HTML
templates.

Tools Required:

 Spring Boot

 IDE (Eclipse/IntelliJ)

 Browser

Procedure:

1. Create a Spring Boot project with Spring Web and Thymeleaf dependencies.

2. Create an HTML page inside src/main/resources/templates/.

3. Create a controller to map the HTML page using @Controller and @GetMapping.

4. Run the application and test the UI in browser.

Sample Code:

HelloController.java

@Controller

public class HelloController {

@GetMapping("/home")

public String homePage() {

return "index"; // returns index.html

index.html (in templates folder)

<!DOCTYPE html>

<html>

<head><title>Home</title></head>
<body>

<h1>Welcome to Spring Boot Web App!</h1>

</body>

</html>

Run on:

http://localhost:8080/home

Output:

Browser displays:

Welcome to Spring Boot Web App!

Conclusion:

Spring Boot with Thymeleaf allows easy integration of backend with frontend, enabling full-
stack web development using Java.

You might also like