0% found this document useful (0 votes)
3 views25 pages

Experiment Java

The document outlines a series of Java programming experiments, each with specific objectives and code examples. It covers topics such as basic Java syntax, command-line argument handling, class creation, inheritance, polymorphism, exception handling, file I/O, Spring framework usage, RESTful services, and web application development. Each experiment includes code snippets and expected outputs to demonstrate the concepts effectively.
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)
3 views25 pages

Experiment Java

The document outlines a series of Java programming experiments, each with specific objectives and code examples. It covers topics such as basic Java syntax, command-line argument handling, class creation, inheritance, polymorphism, exception handling, file I/O, Spring framework usage, RESTful services, and web application development. Each experiment includes code snippets and expected outputs to demonstrate the concepts effectively.
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

EXPERIMENT-1

Objective:
To write a simple Java program using Eclipse IDE to display the message "Hello, Java World from
Eclipse!" on the console, using all major escape sequences including newline (\n), tab (\t), backslash (\),
double quote ("), single quote ('), and carriage return (\r), and to demonstrate the basic structure of a
Java application including the main() method and [Link]() function for output.

Code:-

package LAB;

public class HelloWorld {

public static void main(String[] args) {

[Link]("Hello, Java World from Eclipse!");

[Link]("Hello, \nJava World from Eclipse!");

[Link]("Hello, \tJava World from Eclipse!");

[Link]("Hello, \\ Java World from Eclipse!");

[Link]("Hello, \"Java World from Eclipse!");

[Link]("Hello, \'Java World from Eclipse!");

[Link]("Hello, \rOverwrite!");

[Link]("\nAviral");

[Link]("2401641530059");

}
OUTPUT:->
EXPERIMENT-2

Objective:
To write a Java program that accepts two integer numbers as command-line arguments, calculates their
sum, and displays the result, ensuring proper parsing of command-line arguments and handling basic
integer operations.

Code:-
package LAB;

public class SumCommandLine {


public static void main(String[] args) {

[Link]("Aviral");
[Link]("2401641530059");

int a = [Link](args[0]);
int b = [Link](args[1]);

int sum = a + b;

[Link]("Sum = " + sum);


}
}
OUTPUT:->
EXPERIMENT-3
Objective:
To write a Java program to create two classes: Student and Rectangle. The Student class should have
data members name and rollNo with a method to display student details, and the Rectangle class should
have data members length and width with a method to calculate and display the area. Objects should be
created for both classes, values assigned, and respective methods invoked to display the output.

Code:-

package LAB;

class Student {
String name;
long rollNo;

void displayDetails() {
[Link]("Name: " + name);
[Link]("Roll No: " + rollNo);
}
}

class Rectangle {
double length;
double width;

void displayArea() {
[Link]("Area: " + (length * width));
}
}

public class Classes {


public static void main(String[] args) {

Student s = new Student();


[Link] = "Aviral";
[Link] = 2401641530059L;

Rectangle r = new Rectangle();


[Link] = 10;
[Link] = 5;

[Link]();
[Link]();
}
}
OUTPUT:->
EXPERIMENT-4
Objective:
To write a Java program to demonstrate the concepts of inheritance and polymorphism by creating two classes,
Animal and Dog, where Dog inherits from Animal to demonstrate single-level inheritance, overrides a method in
the Dog class to demonstrate runtime polymorphism, and implements method overloading within the Dog class by
creating multiple versions of a bark() method with different parameters to demonstrate compile-time
polymorphism. The program should create objects and invoke methods to show these behaviors.

Code:-

package LAB;

class Animal {
void sound() {
[Link]("Animal makes sound");
}
}

class Dog extends Animal {


void sound() {
[Link]("Dog barks");
}

void bark() {
[Link]("Bark");
}
void bark(int n) {
[Link]("Barks " + n + " times");
}
}

public class AnimalDog {


public static void main(String[] args) {

Animal a = new Dog();


[Link]();

Dog d = new Dog();


[Link]();
[Link](3);

[Link]("Aviral");
[Link]("2401641530059");
}
}
OUTPUT:->
EXPERIMENT-5
Objective:
To write a Java program that demonstrates exception handling using try, catch, and finally blocks to
handle arithmetic exceptions, and extend the program to implement multithreading by creating and
running two threads that print a message concurrently.

Code:-

package LAB;

class MyThread extends Thread {


public void run() {
for (int i = 1; i <= 3; i++) {
[Link]("Thread " + i);
}
}
}

public class TryCatch {


public static void main(String[] args) {

try {
int a = 10 / 0;
} catch (Exception e) {
[Link]("Divide by zero error");
}

MyThread t = new MyThread();


[Link]();

[Link]("Aviral");
[Link]("2401641530059");
}
}
OUTPUT:->
EXPERIMENT-6

Objective:
To write a Java program that creates a user-defined package named studentinfo containing a class
Student with basic details, and in another class import this package and display the student information,
demonstrating the use of Java packages for modular development.

Code:-
package LAB;
class Student {
String name;
int roll;
Student(String n, int r) {
name = n;
roll = r;
}
void display() {
[Link](name + " " + roll);
}
}
public class Main {
public static void main(String[] args) {
Student s = new Student("Aviral", 59);
[Link]();
[Link]("Aviral");
[Link]("2401641530059");
} }
OUTPUT:->
EXPERIMENT-7
Objective:
To write a Java program that uses Java I/O streams to read data from a text file and write data to
another text file, demonstrating file reading using FileReader and writing using FileWriter, along with
proper exception handling.

Code:-

package LAB;

import [Link].*;

public class FileReadWrite {


public static void main(String[] args) {

try {
// Writing to file
FileWriter fw = new FileWriter("[Link]");
[Link]("Hello Aviral\n");
[Link]("Roll No: 2401641530059");
[Link]();

// Reading from file


BufferedReader br = new BufferedReader(new
FileReader("[Link]"));
String line;

while ((line = [Link]()) != null) {


[Link](line);
}

[Link]();

} catch (IOException e) {
[Link]("Error occurred");
}
}
}
OUTPUT:->
EXPERIMENT-8
Objective:
To create a simple Spring-based Java application using annotation-based configuration, demonstrating
dependency injection, and including a service class and a main class to invoke a method using Spring’s
@Component and @Autowired annotations.

Code:-

package LAB;

class Service {
void serve() {
[Link]("Service method called");
}
}

class Client {
Service service;

Client(Service s) {
service = s;
}

void execute() {
[Link]();
}
}

public class DependencyInjectionDemo {


public static void main(String[] args) {

Service s = new Service(); // Injected


Client c = new Client(s);

[Link]();

[Link]("Aviral");
[Link]("2401641530059");
}
}
OUTPUT:->
EXPERIMENT-9
Objective:
To develop a RESTful web service using Spring Boot by creating a controller that responds to HTTP GET
requests and returns a simple JSON message, using annotations like @RestController and @GetMapping
to handle requests.

Code:-

package LAB;

class RestController {
String getMessage() {
return "Hello from REST API";
}
}

public class RestDemo {


public static void main(String[] args) {

RestController r = new RestController();

[Link]([Link]());

[Link]("Aviral");
[Link]("2401641530059");
}
}
OUTPUT:->
EXPERIMENT-10
Objective:
To build a basic frontend web application using Spring Boot and Thymeleaf that collects user input from
a form and displays the submitted data back to the user, demonstrating integration of backend logic
with frontend rendering using @Controller and Model.

Code:-

package LAB;

class Controller {
String getData(String name) {
return "Welcome " + name;
}
}

public class WebAppDemo {


public static void main(String[] args) {

Controller c = new Controller();

String result = [Link]("Aviral");

[Link](result);
[Link]("2401641530059");
}
}
OUTPUT:->

You might also like