Java_file_final
Java_file_final
Objective: Write a simple Java program using Eclipse IDE to display the message "Hello, Java
World from Eclipse!" on the console, also use all major escape sequences including \n
(newline), \t (tab), \\ (backslash), \" (double quote), \' (single quote), and \r (carriage return).
The program should demonstrate the basic structure of a Java application including the main()
method and System.out.println() function for output.
Program:
Page | 1
OUTPUT:-
Page | 2
Experiment No. 2
Objective: Write a Java program that accepts two integer numbers as command-line
arguments, calculates their sum, and displays the result. Ensure that the program properly
parses the command-line arguments and handles basic integer operations.
Program:
try {
// Convert input strings to integers
int firstNumber = Integer.parseInt(args[0]);
int secondNumber = Integer.parseInt(args[1]);
Page | 3
OUTPUT:
Command-line args: 12 7
Sum: 19
Page | 4
Experiment No. 3
Objective: Write a Java program to create two classes: Student and Rectangle.
• The student class should have two data members: name and rollNo, with a method
to display the student's details.
• The Rectangle class should have two data members: length and width, with a
method to calculate and display the area of the rectangle.
Create objects for both classes, assign values to the data members, and invoke their
respective methods to display the output.
Program:
class Learner {
String fullName;
int studentId;
void showInfo() {
System.out.println("Learner Name: " + fullName);
System.out.println("Student ID: " + studentId);
}
}
class ShapeRectangle {
double height;
double breadth;
void showArea() {
double area = height * breadth;
System.out.println("Area of Rectangle: " + area);
}
}
Page | 5
Learner learner1 = new Learner("Bob", 202);
learner1.showInfo();
System.out.println();
OUTPUT:-
Page | 6
Experiment No. 4
Program:
class Creature {
void sound() {
System.out.println("The creature produces a sound.");
}
}
void howl() {
System.out.println("Puppy is howling softly.");
}
wildCreature.sound();
petPuppy.sound();
OUTPUT:-
Page | 8
Experiment No. 5
Objective: Write a Java program that demonstrates exception handling using try,
catch, and finally blocks to handle arithmetic exceptions. Extend the program to
implement multithreading by creating and running two threads that print a message
concurrently.
Program:
class Demo {
void divide(int a, int b) {
try {
System.out.println("Result: " + (a / b));
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero.");
} finally {
System.out.println("Division done.");
}
}
}
t1.join();
Page | 9
t2.join();
System.out.println("Main finished.");
}
}
OUTPUT:-
Result: 5
Division done.
Cannot divide by zero.
Division done.
Thread2 running: 0
Thread1 running: 0
Thread2 running: 1
Thread1 running: 1
Thread2 running: 2
Thread1 running: 2
Main finished.
Page | 10
Experiment No. 6
Program:
class Student {
String name;
int rollNo;
String department;
Page | 11
OUTPUT:
Page | 12
Experiment No. 7
Objective: Write a Java program that uses Java I/O streams to read data from a text
file and write data to another text file. The program should demonstrate file reading
using FileReader and writing using FileWriter, along with proper exception handling.
Program:
import java.io.*;
int ch;
while ((ch = reader.read()) != -1) {
writer.write(ch);
}
reader.close();
writer.close();
Page | 13
OUTPUT:-
Page | 14
Experiment No. 8
Program:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;
@Component
class MyService {
public void serve() {
System.out.println("Service method called");
}
}
@SpringBootApplication
public class MyApp implements CommandLineRunner {
@Autowired
MyService service;
@Override
public void run(String... args) throws Exception {
service.serve();
}
}
Page | 15
OUTPUT:-
Page | 16
Experiment No. 9
Objective: Develop a RESTful web service using Spring Boot. Create a controller that
responds to HTTP GET requests and returns a simple JSON message. Use Spring Boot
annotations like @RestController and @GetMapping to handle requests.
Program:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class RestServiceApp {
public static void main(String[] args) {
SpringApplication.run(RestServiceApp.class, args);
}
}
@RestController
class HelloController {
@GetMapping("/hello")
public Message sayHello() {
return new Message("Hello, this is a JSON response!");
}
}
class Message {
private String message;
Page | 17
public void setMessage(String message) {
this.message = message;
}
}
OUTPUT:-
{
"message": "Hello, this is a JSON response!"
}
Page | 18
Experiment No. 10
Objective: Build a basic frontend web application using Spring Boot and Thymeleaf.
Create a webpage that collects user input from a form and displays the submitted data
back to the user. Demonstrate integration of backend logic with frontend rendering
using @Controller and Model.
Program:
<!—form.html -->
<form action="/submit" method="post">
<input type="text" name="username" />
<button type="submit">Submit</button>
</form>
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class FormController {
@GetMapping("/")
public String showForm() {
return "form";
}
@PostMapping("/submit")
public String submit(@RequestParam String username, Model model) {
model.addAttribute("user", username);
return "result";
}
}
Page | 19
OUTPUT:-
Page | 20