0% found this document useful (0 votes)
37 views4 pages

ADPJ2

Uploaded by

jenasubhradeep
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)
37 views4 pages

ADPJ2

Uploaded by

jenasubhradeep
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/ 4

Department of Computer Science & Engineering

Faculty of Engineering and Technology (ITER)

Assignment - 2
Topic: Loose-Coupling,Exception Handling, Collections in JAVA and
Simple Spring Programs.
_______________________________________________________________________________________

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)”

Ans:
interface Keyboard1{
void type();
}

class LenovoKeyboard1 implements Keyboard1{


public void type() {
System.out.println("Typing on Lenovo Keyboard");
}
}

class DellKeyboard1 implements Keyboard1 {


public void type() {
System.out.println("Typing on a Dell Keyboard");
}
}

class Computer1{
public void keyboardUsed(Keyboard1 keyboard) {
keyboard.type();
}
}

public class Question1 {


public static void main(String[] args) {
Computer1 computer = new Computer1();
Keyboard1 dellKeyboard = new DellKeyboard1();
Keyboard1 lenovoKeyboard = new LenovoKeyboard1();
computer.keyboardUsed(dellKeyboard);
computer.keyboardUsed(lenovoKeyboard);
}
}

Name:_____________________ Regd. Number:_____________________


Department of Computer Science & Engineering
Faculty of Engineering and Technology (ITER)

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()
Ans:
interface Vehicle{
void name();
}
class Bike implements Vehicle{
public void name() {
System.out.println("This is a Bike");
}
}
class Car implements Vehicle{
public void name() {
System.out.println("This is a Car");
}
}
class Traveller {
private Vehicle vehicle;
public void Traveller(Vehicle vehicle) {
this.vehicle = vehicle;
}
public void travellerVehicleName() {
vehicle.name();
}
}
public class Question2 {
public static void main(String[] args) {
Vehicle bike = new Bike();
Vehicle car = new Car();
Traveller traveller1 = new Traveller();
traveller1.travellerVehicleName();
Traveller traveller2 = new Traveller();
traveller2.travellerVehicleName();
}
}

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


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

Name:_____________________ Regd. Number:_____________________


Department of Computer Science & Engineering
Faculty of Engineering and Technology (ITER)

Ans:
public class Question3 {
public static void main(String[] args) {
try
{
int result = 10 / 0;
} catch (ArithmeticException e)
{
System.out.println("Caught ArithmeticException: " + e.getMessage());
}
try {
String str = null;
System.out.println(str.length());
} catch (NullPointerException e) {
System.out.println("Caught NullPointerException: " + e.getMessage());
}
try {
String s="abc";
int a=Integer.parseInt(s);
} catch(NumberFormatException e) {
System.out.println("Caught NumberFormatException: " + e.getMessage());
}
try {
int[] arr = {1, 2, 3};
System.out.println(arr[5]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Caught ArrayIndexOutOfBoundsException: " + e.getMessage());
}
}
}

4. Execute small JAVA collection programs for the followings:


a) List
b) Queue
c) Set

Ans:
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.HashSet;
import java.util.List;
import java.util.Queue;
import java.util.Set;

public class Question4 {


public static void main(String[] args) {

Name:_____________________ Regd. Number:_____________________


Department of Computer Science & Engineering
Faculty of Engineering and Technology (ITER)

List<String> list = new ArrayList<>();


list.add("Apple");
list.add("Banana");
list.add("Orange");
for (String fruit : list) {
System.out.println(fruit);
}
Queue<String> queue = new LinkedList<>();
queue.add("First");
queue.add("Second");
queue.add("Third");
System.out.println("Head of the queue: " + queue.peek());
System.out.println("Removed: " + queue.poll());
System.out.println("Head of the queue after removal: " + queue.peek());
Set<String> set = new HashSet<>();
set.add("One");
set.add("Two");
set.add("Three");
set.add("One");
for (String number : set) {
System.out.println(number);
}
}
}

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.
Ans:
package com.example.helloworld;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class HelloworldApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(HelloworldApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
String message = helloMessage();
System.out.println(message);
}
@Bean
public String helloMessage() {
return "Hello, Spring World!";
}
}

Name:_____________________ Regd. Number:_____________________

You might also like