oops file
oops file
BACHELOR OF TECHNOLOGY
IN
COMPUTER SCIENCE & ENGINEERING
2.Open Eclipse:
Launch Eclipse IDE after installation.
Enter a class name (e.g., MyFirstJavaProgram) and check the box for public static
void main (String [] args).
Click Finish.
1
CODE:
The program output (Hello World) will be displayed in the Console view at the
Bottom.
OUTPUT:
2
Experiment-2
Objective: Creating simple java program using command line argument.
import java.util.Scanner;
3
Example 2: Even Number Series in Java
CODE:
import java.util.Scanner;
scanner.close();
}
}
4
[Type text]
Experiment-3
Objective: Understand OOPS Concept and basics of Java Programming.
Theory:
1) Abstraction:
2) Polymorphism:
3) Inheritance:
5) Constructor :
1. Default Constructor
Created automatically by Java if no constructor is defined.
Has no parameters.
2. Parameterized Constructor
Accepts parameters to initialize objects with specific values.
5
[Type text]
3.Copy Constructor
6) this Keyword:
7) Constructor Overloading:
Code:
class Student {
// Instance variables
String name;
int age;
double gpa;
// 2. Parameterized Constructor
Student(String name, int age) {
this.name = name;
this.age = age;
System.out.println("Parameterized constructor (name, age) called");
}
// 4. Copy Constructor
Student(Student other) {
this.name = other.name;
this.age = other.age;
6
[Type text]
this.gpa = other.gpa;
void display() {
System.out.println("Name: " + name + ", Age: " + age + ", GPA: " + gpa);
}
}
// 1. Default constructor
Student s1 = new Student();
s1.display();
7
Experiment- 4
Objective: Create java program using inheritance and polymorphism.
THEORY:
1)Inheritance:
ypes of Inheritance
1. Single Inheritance
CODE:
class Animal
{ void
eat() {
System.out.println("This animal eats food");
}
}
8
2. Multilevel Inheritance
A class inherits from a class which itself inherits from another class.
CODE:
class Grandparent
{ void
display1() {
System.out.println("Grandparent");
}
}
9
3.Hierarchical Inheritance
CODE:
class Animal{
void sound() {
System.out.println("Animal makes a sound");
}
}
c.sound();
c.meow();
d.sound();
d.bark();
}
}
10
2)Polymorphism
It allows objects to behave differently based on their context, even if they share the same
interface or method name.
When multiple methods in the same class have the same name but different
parameters.
CODE:
class Calculator {
int add(int a, int b) {
return a + b;
}
CODE:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
a = new Dog();
a.sound();
a = new Cat();
a.sound();
}
}
12
[
Experiment-5
Objective: Implement user handling technique using exception handling and
multithreading.
Code:
//1.Arithmetic Exception
public class ExceptionHandling {
public class ExceptionHandling{
public static void main(String[] args) {
try{
int a=5/0;
}
catch(ArithmeticException e){
System.out.println("Exception caught:"+e.getMessage());
}
}
}
try{
String str=null;
System.out.print(str.length());
}
catch(NullPointerException e){
System.out.println("Exception
caught:"+e.getMessage());
}
13
[
//multithreading
public class C {
public static void main(String[] args) {
A t1 = new A();
B t2 = new B();
t1.start();
t2.start();
}
}
14
[Type text]
Example 2:
class MyTask implements Runnable {
public void run() {
try {
System.out.println("Task started.");
System.out.println("Task completed.");
} catch (Exception e) {
System.out.println("Caught exception: " + e.getMessage());
}
}
}
15
[]
Experiment-6
Experiment-6
Objective:Create a java program using java packages.
Theory:
Package Class Used Purpose
java.util ArrayList, Date Lists, date-time
java.text Simple Date Format Formatting Date objects
java.io File, I O Exception Creating/handling files
java.time Local Date Time Current date and time
java.math Big Decimal High-precision arithmetic
Code:
import java.math.BigInteger;
import java.util.Scanner;
System.out.println(base + "^" + exponent + " mod " + modulus + " = " + result);
scanner.close();
}
}
16
[Type text]
Experiment-7
Objective:Create a java program using java I/O packages.
Code:
import java.io.FileWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
try {
bufferedReader.close();
reader.close();
} catch (IOException e) {
System.out.println("An error occurred:");
e.printStackTrace();
}
}
}
17
Experiment-8
Objective:Create industry oriented application using Spring Framework
Project Structure:
src/
├── main/
│ ├── java/com/example/studentmgmt/
│ │ ├── controller/
│ │ ├── model/
│ │ ├── repository/
│ │ └── StudentMgmtApplication.java
│ └── resources/
│ ├── application.properties
│ └── templates/
└── test/
Student entity:
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String name;
Student Repository:
public interface StudentRepository extends JpaRepository<Student, Long> {
List<Student> findByDepartment(String department);
}
Student Controller:
@Controller
@RequestMapping("/students")
public class StudentController {
@Autowired
private StudentRepository studentRepository;
@GetMapping
public String listStudents(Model model) {
model.addAttribute("students", studentRepository.findAll());
return "students";
}
@GetMapping("/new")
18
public String showForm(Model model) {
model.addAttribute("student", new Student());
return "add-student";
}
@PostMapping("/add")
public String addStudent(@ModelAttribute Student student) {
studentRepository.save(student);
return "redirect:/students";
}
}
Students.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Students</title>
</head>
<body>
<h1>Student List</h1>
<table>
<tr th:each="student : ${students}">
<td th:text="${student.id}"></td>
<td th:text="${student.name}"></td>
<td th:text="${student.email}"></td>
<td th:text="${student.department}"></td>
</tr>
</table>
<a href="/students/new">Add Student</a>
</body>
</html>
Add-student form.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Add Student</title>
</head>
<body>
<h1>Add New Student</h1>
<form th:action="@{/students/add}" th:object="${student}" method="post">
Name: <input type="text" th:field="*{name}"><br>
Email: <input type="email" th:field="*{email}"><br>
Department: <input type="text" th:field="*{department}"><br>
<button type="submit">Submit</button>
</form>
</body>
</html>
Application.properties
# Database (using H2 for simplicity)
spring.datasource.url=jdbc:h2:mem:studentdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
Technologies used:
Component Technology
Backend Spring Boot 3.x
Database H2 Database
ORM Spring Data JPA
Templating Thymeleaf
Build Tool Maven / Gradle
Key Features
20
Experiment-9
Objective: Test restful web services using springboot.
Theory: pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>simple-rest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.6</version>
</parent>
<dependencies>
<!-- Web starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<properties>
<java.version>17</java.version>
</properties>
</project>
Application.java
package com.example.simplerest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
21
}
}
HelloController.java
package com.example.simplerest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
HelloControllerTest.java
package com.example.simplerest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
@SpringBootTest
@AutoConfigureMockMvc
public class HelloControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testHello() throws Exception {
mockMvc.perform(get("/hello"))
.andExpect(status().isOk())
.andExpect(content().string("Hello, World!"));
}
}
Run at browser:
https://localhost:8080/hello
22
Running the project:
Testing at browser:
Running Test:
23
Experiment-10
Objective: Test Frontened Web application with springboot.
backend/pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
HelloController.java
@RestController
@RequestMapping("/api")
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello from Spring Boot!";
}
}
@Test
void helloEndpointReturnsMessage() throws Exception {
mockMvc.perform(get("/api/hello"))
.andExpect(status().isOk())
.andExpect(content().string("Hello from Spring Boot!"));
}
}
React Frontend with Testing
App.js
function App() {
const [msg, setMsg] = useState('');
useEffect(() => {
axios.get('http://localhost:8080/api/hello')
.then(res => setMsg(res.data));
24
}, []);
return <h1>{msg}</h1>;
}
App.test.js
import { render, screen } from '@testing-library/react';
import App from './App';
import axios from 'axios';
jest.mock('axios');
render(<App />);
const msg = await screen.findByText('Hello from Spring Boot!');
expect(msg).toBeInTheDocument();
});
Run at:
http://localhost:3000
Start Backened:
Start Frontend:
25