OOP With Java Lab - Bcs 452 - 2023-24
OOP With Java Lab - Bcs 452 - 2023-24
LABORATORY MANUAL
Object Oriented Programming with JAVA
(BCS-452)
B.TECH – II YEAR
(EVEN SEM 2024-
2025)
Name
Roll No.
Section-Batch
Output:-
Arithmetic divide by zero
rest of the code..
Public class exception1
{
public static void main(String[] args)
{try
{
int data=50/0; //may throw exception
}
// handling the exception
catch(Exception e)
{
// displaying the custom message
System.out.println ("Can't divided by zero");
}
}
}
Output:
Divided by arithmetic’s error
Can't divided by zero
Experiment-9
System.out.println("Hi Everyone");
Output:
import java.io.*;
public class class_file1
{
public static void main(String[] args) throws IOException{
File f;
f=new File("myfile.txt");
if(!f.exists()){
f.createNewFile();
System.out.println("New file \"myfile.txt\" has been created
to the current directory");
}
}
}
Output of the Program
C:\pankaj>javac C:\pankaj>javac class_file1.java C:\
pankaj>java class_file1
New file "myfile.txt" has been created to the current directory
C:\pankaj>1.java C:\
pankaj>java CreateFile1
New file "myfile.txt" has been created to the current directory
C:\pankaj>
Output :
C:\pankaj>javac C:\pankaj>javac CopyFile.java C:\
pankaj>java CopyFile
New file "myfile.txt" has been created to the current directory
C:\pankaj>1.java C:\
pankaj>java CopyFile 1
New file "myfile.txt" has been created to the current directory
C:\pankaj>
import java.io.*;
class class1{
public static void main(String[] args)
{
import java.io.FileOutputStream;
public class FileOutputStreamExample
{
public static void main(String args[])
{
try
{
FileOutputStream fout=new FileOutputStream("D:\\testout.txt");
fout.write(65);
fout.close();
System.out.println("successfully uploaded data");
}
catch(Exception e){System.out.println(e);}
}
}
Output:
Successfully uploaded data
import java.io.FileInputStream;
try{
int i=fin.read();
System.out.print((char)i);
fin.close();
}catch(Exception e){System.out.println(e);}
Output:
Successfully upload data
Experiment-11
Create industry oriented application using Spring Framework.
1) Create Java
class package
com.javatpoint; public
class Student
{ private String name;
public String getName()
{return name;
}
public void setName(String name) {
this.name = name;
}
public void
displayInfo(){ System.out.println("
Hello: "+name);
}
}
Create the java class e.g. Test. Here we are getting the object of Student class from the IOC container using
the getBean() method of BeanFactory. Let's see the code of test class.
package com.javatpoint;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
There are mainly three jar files required to run this application.
o org.springframework.core-3.0.1.RELEASE-A
o com.springsource.org.apache.commons.logging-1.1.1
o org.springframework.beans-3.0.1.RELEASE-A
Now run the Test class. You will get the output Hello: Vimal Jaiswal.
Output:-
package net.codejava;
// ...
@RestController
@RequestMapping("/users")
public class UserApiController {
private UserService service;
private ModelMapper modelMapper;
// ...
@PostMapping
public ResponseEntity<?> add(@RequestBody @Valid User user)
{ User persistedUser = service.add(user);
UserDTO userDto = entity2Dto(persistedUser);
return ResponseEntity.created(uri).body(userDto);
}
}
mockMvc;
@Test
public void testAddUserAPI() throws Exception {
package net.codejava;
// ...
@RestController
@RequestMapping("/users")
public class UserApiController
{private UserService service;
private ModelMapper modelMapper;
// ...
@PostMapping
public ResponseEntity<?> add(@RequestBody @Valid User user)
{User persistedUser = service.add(user);
UserDTO userDto = entity2Dto(persistedUser);
return ResponseEntity.created(uri).body(userDto);
}
// ...
}
@WebMvcTest(UserApiController.class)
public class UserApiControllerTests {
@Test
public void testAddUserAPI() throws Exception {
// use MockMvcResultMatchers to assert the response (status code, content type, JSON fields,...)
}
package net.codejava;
import jakarta.persistence.*;
import jakarta.validation.constraints.*;
We use ModelMapper for mapping between entity and DTO, so below is code of the UserDTO class:
package net.codejava;
private String
firstName; private
String lastName;
// getters and setters...
Spring Data JPA is used for the persistence layer, so below is code of the UserRepository interface:
package net.codejava;
import org.springframework.data.repository.CrudRepository;
package net.codejava;
import java.net.URI;
import java.util.List;
import java.util.stream.Collectors;
import org.modelmapper.ModelMapper;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;
@RestController
@RequestMapping("/users")
public class UserApiController {
private UserService service;
private ModelMapper modelMapper;
@PostMapping
public ResponseEntity<?> add(...) {
// ...
}
@GetMapping("/{id}")
public ResponseEntity<?> get(@PathVariable("id") Long id) {
// ...
}
@GetMapping
public ResponseEntity<?> list() {
// ...
}
@PutMapping("/{id}")
public ResponseEntity<?> update(...) {
// ...
}
@DeleteMapping("/{id}")
public ResponseEntity<?> delete(...) {
// …
return ResponseEntity.created(uri).body(userDto);
}
@GetMapping("/{id}")
public ResponseEntity<?> get(@PathVariable("id") Long id)
{try {
User user = service.get(id);
return ResponseEntity.ok(entity2Dto(user));
}
catch (UserNotFoundException e)
{e.printStackTrace();
return ResponseEntity.notFound().build();
}
}
Output: