0% found this document useful (0 votes)
25 views6 pages

Assignment 2 Debi

Uploaded by

devildada0987
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)
25 views6 pages

Assignment 2 Debi

Uploaded by

devildada0987
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
You are on page 1/ 6

Assignment - II

Topic: Loose-Coupling, Exception Handling, Collections


in JAVA and Simple Spring Programs
Name: Debi Prasad Rath
Registration Number: 2251018009
Section: I

1. Show Loose coupling in JAVA by using the followings:


a) Interface= Keyboard
b) class DellKeyboard= implements Keyboard
c) class LenovoKeyboard= implements Keyboard
d) class Computer=defines method “public void keyboardUsed(DellKeyboard dk)”

CODE:
package Assignment2;

interface Keyboard{
void typing();
}

class DellKeyboard implements Keyboard{


@Override
public void typing(){
System.out.println("Dell Keyboard");
}
}

class LenovoKeyboard implements Keyboard{


@Override
public void typing() {
System.out.println("Lenovo Keyboard");
}
}
class Computer{
public void keyboardUsed(Keyboard keyboard){
keyboard.typing();
}
}
public class Q1 {
public static void main(String[] args) {
Computer c=new Computer();
DellKeyboard d=new DellKeyboard();
c.keyboardUsed(d);
}
}

OUTPUT:

2. Show Loose coupling in JAVA by using the followings:


a) Interface= Vehicle
b) class Bike= implements Vehicle
c) class Car= implements Vehicle
d)class traveller=defines method “public void travellerVehicleName()

CODE:
package Assignment2;

interface Vehicle{
void name();
}

class Bike implements Vehicle{


@Override
public void name(){
System.out.println("Bike is Hero");
}
}

class Car implements Vehicle{


@Override
public void name(){
System.out.println("Car is Honda");
}
}

class Traveller{
public void travellerVehicleName(Vehicle vehicle){
vehicle.name();
}
}

public class Q2 {
public static void main(String[] args) {
Traveller t=new Traveller();
Car c=new Car();
t.travellerVehicleName(c);
}
}

OUTPUT:

3. Execute Java Exception Handling programs using try-catch statement(s) to handle


the exceptions:
a) ArithmeticException
b) NullPointerException
c) NumberFormatException
d) ArrayIndexOutOfBoundsException

CODE:
package Assignment2;

public class Q3 {
public static void main(String[] args) {
try{
int result=10/0;
}catch (ArithmeticException e){
System.out.println("This message for Arithmetic Exception: "+e.getMessage());
}

try{
String str=null;
System.out.println(str.length());
}catch(NullPointerException e){
System.out.println("This message is for NullPointerException: "+e.getMessage());
}

try{
String number="hello";
int parseInteger=Integer.parseInt(number);
}catch(NumberFormatException e){
System.out.println("This message is for NumberFormatException:
"+e.getMessage());
}

try{
int[] arr={10,20,30};
System.out.println(arr[5]);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("This message is for ArrayIndexOutOfBoundsException:
"+e.getMessage());
}
}
}

OUTPUT:

4. Execute small JAVA collection programs for the followings:


a) List
b) Queue
c) Set

CODE:
package Assignment2;
import java.util.*;

public class Q4 {
public static void main(String[] args) {
List<Integer> l=new ArrayList<>();
l.add(1);
l.add(2);
l.add(3);
for(int n:l){
System.out.println("List: "+n);
}

Queue<Integer> q=new LinkedList<>();


q.add(10);
q.add(20);
q.add(30);
while(!q.isEmpty()) {
System.out.println("Queue: " + q.poll());
}
Set<Integer> s=new HashSet<>();
s.add(100);
s.add(200);
s.add(300);
for(int n:s){
System.out.println("Set: "+n);
}
}
}

OUTPUT:

5. Write a simple Spring Application, which will print "Hello World!" or any other
message based on the configuration done in Spring Beans Configuration file.

CODE:

Message.java

package com.assignments;

public class Message {


private String message;

public void setMessage(String message){


this.message=message;
}

public void printMessage(){


System.out.println(message);
}
}

Bean.xml

<?xml version="1.0" encoding="UTF-8"?>


<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">

<bean id="Message" class="com.assignments.Message">


<property name="message" value="Hello Chandan !"/>
</bean>

</beans>

App.java

package com.assignments;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.assignments.Message;

public class App {


public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Bean.xml");

Message msg = (Message) context.getBean("Message");


msg.printMessage();
}
}

OUTPUT:

You might also like