ADP assignment1-5
ADP assignment1-5
e1.displayData();
e2.displayData();
System.out.println(whoIsOlder(e1,e2));
}
OUTPUT:
2. In a supermarket each product has minimum details like
prodId, price, quantity that is used during the biling process.
Keeping this in mind prepare a class named as Product having
the member variables ● prodId, price, quantity ● a static
variable totalPrice Initialize the value of the product through a
parameterized constructor. It consists of a display() method to
display the value of instance variables. A person went to the
market and purchased 5 different products. Using the above
mentioned class, display the details of products that the person
has purchased. Also, determine how much total amount the
person will pay for the purchase of 5 products.
public class Product {
private int prodId;
private double price;
private int quantity;
private static double totalPrice = 0.0;
}
}
OUTPUT:
public Deposit(){
this.principle= 0L;
this.time =0;
this.rate = 0.0;
this.TotalAmt= 0.0;
}
public Deposit(Long principle,Integer time,Double rate){
this.principle= principle;
this.time =time;
this.rate = rate;
this.TotalAmt= principle+(principle*rate*time)/100;
}
d1.display();
d1.calAmt();
d2.display();
d2.calAmt();
d3.display();
d3.calAmt();
d4.display();
d4.calAmt()
}
}
OUTPUT:
4. Define a base class Employee with instance variable name,
age. The instance variables are initialized through constructors.
The prototype of the constructor is as below. Employee (string,
int) Define a derived class HR with instance variables Eid,
salary. The instance variables are initialized through
constructors. The prototype of the constructor is as below. HR
(string, int, int, double). Another instance method of the HR
class is DisplayDetails() to display the information of HR
details.
OUTPUT:
@Override
double getPercentage() {
return ((markDSA+markICP+enggMechanics)/300.0)*100.0;
}
System.out.printf("Percentage: %.2f%n",c1.getPercentage());
System.out.printf("Percentage: %.3f",c2.getPercentage());
}
}
OUTPUT:
int count();
}
class Student implements DetailInfo{
private static int maxcount;
private String name;
void getter(){
System.out.println("Name: "+name);
}
@Override
public void display() {
getter();
}
@Override
public int count() {
return name.length();
}
OUTPUT:
package studentpackage;
package studentpackage;
public void inputDetails(String name, int roll, int mark1, int mark2) {
super.inputDetails(name, roll);
this.mark1 = mark1;
this.mark2 = mark2;
}
package sportspackage;
import studentpackage.Test;
import sportspackage.Sports;
studentTest.showDetails();
interface Keyboard {
void type();
}
class Computer {
public void keyboardUsed(Keyboard keyboard) {
keyboard.type();
}
}
public class LooseCouplingDemo {
public static void main(String[] args) {
Computer computer = new Computer();
computer.keyboardUsed(dellKeyboard);
computer.keyboardUsed(lenovoKeyboard);
}
}
interface Vehicle {
String getName();
}
class Bike implements Vehicle {
@Override
public String getName() {
return "Bike";
}
}
traveller1.travellerVehicleName();
traveller2.travellerVehicleName();
}
}
import java.util.*;
OUTPUT:
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
OUTPUT:
Hello, World!
Assignment - 3
Topic: Creating a Maven Project, Defining a POJO
Class, Generating runtime object using Spring IoC and
Injecting Dependencies.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
person.setPhoneNumbers(new ArrayList<>());
System.out.print("Enter phone numbers (comma-separated): ");
String[] numbers = scanner.nextLine().split(",");
for (String num : numbers) {
person.getPhoneNumbers().add(num.trim());
}
// Print details
System.out.println("Person Details:");
System.out.println("Name: " + person.getName());
System.out.println("Age: " + person.getAge());
System.out.println("City: " + person.getCity());
System.out.println("Phone Numbers: " + person.getPhoneNumbers());
}
}
OUTPUT:
import java.util.List;
import java.util.Map;
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
@Override
public String toString() {
return street + ", " + city + ", " + state + " - " + pinCode;
}
}
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class Customer {
private String name;
private List<String> phoneNumbers;
private String emailId;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {
}
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
@Override
public void destroy() throws Exception {
System.out.println("Bean is destroyed!");
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
OUTPUT:
Starting checkout...
Processing credit card payment of $100.0
Checkout completed.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Define NotificationService bean and inject dependencies via setters -->
<bean id="notificationService" class="NotificationService">
<property name="emailService" ref="emailService" />
<property name="smsService" ref="smsService" />
</bean>
</beans>
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
OUTPUT:
Email sent with message: Hello via Email!
SMS sent with message: Hello via SMS!
import org.springframework.stereotype.Repository;
@Repository
public class UserRepository {
public void saveUser() {
System.out.println("User saved to database.");
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
private UserRepository userRepository;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "your.package.name")
public class AppConfig {
}
import org.springframework.context.ApplicationContext;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
public class App {
public static void main(String[] args) {
// Load the Spring application context
ApplicationContext context = new
AnnotationConfigApplicationContext(AppConfig.class);
OUTPUT:
User saved to database.
import org.springframework.stereotype.Service;
@Service
public class GreetingServiceImplement implements GreetingService {
@Override
public void greet(String name) {
System.out.println("Hello, " + name + "! Welcome to Spring.");
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
OUTPUT:
Hello, Alice! Welcome to Spring.
5. Write POJO Java program to convert tightly coupled code
into loosely coupled code a. Create a parent class A with a
method display(). Create another class B that inherits class A
and contains a method display(). Create a main class to call the
display method. b. Create a class LightBulb with a method
SwitchOn(). Create another class Switch that has an object of
the LightBulb class and another method Operate(). Inside the
Operate() method call the SwitchOn() method
class A {
public void display() {
System.out.println("Display from Class A");
}
}
class B extends A {
@Override
public void display() {
System.out.println("Display from Class B");
}
}
class LightBulb {
public void switchOn() {
System.out.println("LightBulb is switched on");
}
}
class Switch {
private LightBulb lightBulb = new LightBulb(); // Tightly coupled to
LightBulb
interface Switchable {
void switchOn();
}
class Switch {
private Switchable device;
Dependencies:
package com.example.springbootdemo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
@GetMapping("/hello")
package com.example.springbootdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
SpringApplication.run(SpringBootDemoApplication.class, args);
OUTPUT:
package com.example.springbootdemo.model;
// Parameterized constructor
this.age = age;
@Override
return name;
return age;
package com.example.springbootdemo.controller;
import com.example.springbootdemo.model.Employee;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
@GetMapping("/employee")
return employee.toString();
package com.example.springbootdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}
OUTPUT:
Usage: You specify the property name (and optionally the value) that must be
present for the configuration to load. For example, if you want to enable a
configuration only if myapp.feature.enabled=true is set in the properties file:
@ConditionalOnBean(name = "customService")
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.0</version>
</parent>
IDE Tools: In most IDEs like IntelliJ IDEA or Eclipse, you can right-click a
dependency and select options to view its structure, transitive dependencies,
and more.
package com.example.payment;
package com.example.payment;
import org.springframework.stereotype.Component;
@Component("creditCardProcessor")
@Override
return "Processing credit card payment of " + amount + " " + currency;
@Component("payPalProcessor")
@Override
@Component("bankTransferProcessor")
@Override
package com.example.payment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
@Autowired
this.paymentProcessor = paymentProcessor;
System.out.println(result);
package com.example;
import com.example.payment.PaymentService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("com.example.payment")
SpringApplication.run(PaymentApplication.class, args);
@Bean
PaymentService paymentService =
context.getBean(PaymentService.class);
paymentService.makePayment(100.0, "USD");
};
package com.example.payment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
@Autowired
@Qualifier("creditCardProcessor")
this.paymentProcessor = paymentProcessor;
System.out.println(result);
package com.example.library;
return bookName;
this.bookName = bookName;
return authorName;
this.authorName = authorName;
this.edition = edition;
package com.example.library;
import org.springframework.stereotype.Component;
@Component
return bookName;
this.bookName = bookName;
return authorName;
this.authorName = authorName;
return edition;
this.edition = edition;
package com.example.config;
import com.example.library.Book;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@Bean
book.setBookName("Spring in Action");
book.setAuthorName("Craig Walls");
book.setEdition("5th Edition");
return book;
package com.example;
import com.example.library.Book;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import com.example.config.BookConfig;
@SpringBootApplication
SpringApplication.run(BookApplication.class, args);
@Override
book.display();
OUTPUT:
Book Name: Spring in Action
package com.example.demo;
package com.example.demo;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
SpringApplication.run(DemoApplication.class, args);
@Bean
@Override
System.out.println(student.printMessage("John"));
package com.example.demo.config;
import com.example.demo.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@Bean
package com.example.demo;
import com.example.demo.Student;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import com.example.demo.config.StudentConfig;
@SpringBootApplication
ApplicationContext context =
SpringApplication.run(DemoApplication.class, args);
// Retrieve the Student bean from the context
System.out.println(student.printMessage("John"));
OUTPUT:
Hello, John
package com.example.demo;
this.name = name;
this.regdNo = regdNo;
this.subjectName = subjectName;
this.markSecured = markSecured;
System.out.println();
package com.example.demo;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
SpringApplication.run(DemoApplication.class, args);
@Bean
@Bean
@Override
student1.display();
student2.display();
package com.example.demo.config;
import com.example.demo.MyClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@Bean
@Bean
package com.example.demo;
import com.example.demo.MyClass;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import com.example.demo.config.StudentConfig;
@SpringBootApplication
ApplicationContext context =
SpringApplication.run(DemoApplication.class, args);
// Retrieve each student bean from the context and call display
student1.display();
student2.display();
}
OUTPUT:
Name: Alice
Subject: Mathematics
Marks Secured: 88
Name: Bob
Subject: Science
Marks Secured: 92
Teacher class has name, id, mobileNo, and address fields. Use
the setter method and toString method for this class.
javaConfig class is java annotation-based configuration file
which creates two address beans and on teacher bean and
initialize the fields of both classes. In teacher class use any one
address by using @Qualifier annotation.
package com.example.demo;
public class Address {
// Setter methods
this.houseNo = houseNo;
this.city = city;
this.postOffice = postOffice;
this.pin = pin;
this.state = state;
}
// toString method for displaying Address details
@Override
return "Address{" +
'}';
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@Autowired
// Setter methods
this.name = name;
this.id = id;
this.mobileNo = mobileNo;
this.address = address;
@Override
return "Teacher{" +
", id=" + id +
package com.example.demo.config;
import com.example.demo.Address;
import com.example.demo.Teacher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@Bean
address.setHouseNo("101");
address.setCity("New York");
address.setPostOffice("NYPO");
address.setPin("10001");
address.setState("NY");
return address;
}
@Bean
address.setHouseNo("202");
address.setCity("Los Angeles");
address.setPostOffice("LAPO");
address.setPin("90001");
address.setState("CA");
return address;
@Bean
teacher.setName("John Doe");
teacher.setId(101);
teacher.setMobileNo("123-456-7890");
return teacher;
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import com.example.demo.config.javaConfig;
@SpringBootApplication
System.out.println(teacher);
OUTPUT:
package com.example.demo;
// Setter methods
this.customerName = customerName;
this.accountNo = accountNo;
this.IFSCCode = IFSCCode;
@Override
return "Customer{" +
"customerName='" + customerName + '\'' +
'}';
package com.example.demo.config;
import com.example.demo.Customer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.example.demo")
@Bean
customer.setCustomerName("Alice Johnson");
customer.setAccountNo("1234567890");
customer.setIFSCCode("ABCD0123456");
return customer;
}
}
package com.example.demo;
import com.example.demo.config.conFig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
System.out.println(customer);
OUTPUT:
package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
@Value("12")
@Value("05")
@Value("2021")
@Override
return "PubDate{" +
"day=" + day +
'}';
}
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
@Value("Spring in Action")
@Value("45.99")
@Autowired
@Override
return "Book{" +
'}';
}
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
System.out.println(book);
OUTPUT:
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@GetMapping("/home")
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
SpringApplication.run(DemoApplication.class, args);
OUTPUT: