Module 1 Assignments Java (MCA)
Module 1 Assignments Java (MCA)
class Demo<T>
{
T d;
public Demo(T data) {
this.d =data;
}
public T getData() {
return d;
}
}
class Generic{
public static void main (String[] args)
{
Demo<Integer> d1 = new Demo<>(100);
Demo<String> d2 = new Demo<>("This is string");
System.out.println("Displaying Integer data "+d1.getData());
System.out.println("Displaying String data "+d2.getData());
}
}
Output of Generic Class
2 : Write a program to demonstrate a Generic method
(Method to check Equality of two values)
public class GenericMethod {
public static void main (String[] args)
{ EqualityClass ec = new EqualityClass();
ec.<Integer>checking(1,2);
ec.<Integer>checking(2,2);
ec.<String>checking("Hello 100","Hello 100");
ec.<String>checking("Hi","Bye");
}
}
class EqualityClass{
public <T> void checking(T d1, T d2) {
if (d1.equals(d2)) {
System.out.println(d1+" and "+d2+" are equal");
}else {
System.out.println(d1+" and "+d2+" are not equal");
} } }
Output of Generic method
3 : Write a program to implement Wildcard
a.A generic method which prints a list of numbers using Upper Bounded Wildcards.
import java.util.*;
public class UpperBound {
public static void display(List<? extends Number> list) {
System.out.print(list);
}
public static void main(String args[]) {
import java.util.*;
import java.util.*;
public class Unbounded {
private static void display(List<?> list)
{
System.out.println(list);
}
public static void main(String[] args)
{
List<Integer> list1= Arrays.asList(1,2,3);
List<Double> list2=Arrays.asList(1.1,2.2,3.3);
display(list1);
display(list2);
}
}
Assignment No: 2 List Interface
Question 1 : Write a Java program to create a List containing a list of items of type String
and use for –each loop to print the items of the list.
Question 2 : Write a Java program to create a List containing a list of items and use
Listiterator interface to print items present in the list. Also print the list in
reverse/backward direction.
Question 1 : Write a Java program to create a List containing a list of items of type String
and use for –each loop to print the items of the list.
import java.util.*;
Question 2 : Write a Java program using Set Interface containing list of items and perform
the following operations:
a.Add items in the set.
b.Insert items of one set into another set.
c.Remove items from the set.
d.Search the specified items in the set.
Question 1 : Write a Java program to create a Set containing a list of items of type String and
print the items in the list using the Iterator interface. Also print the set elements in reverse/
backward direction.
Set<String> s1 = new TreeSet<>();
s1.add("C++");
s1.add("Java");
s1.add("C#");
if(id.contains(9)) { id.remove(9); }
System.out.println("after deletion of item in set:"); //remove operation
System.out.println(id);
Output of SetInterface
import java.util.Set;
import java.util.TreeSet;
Assignment No: 4 Map Interface
Write a Java program using Map Interface containing list of items having keys and
associated values and perform the following operations:
a) Add items in the map.
b) Remove items from the map.
c) Search specific key from the map.
d) Get value of the specified key.
e) Insert map elements of one map to another map.
f) Print all keys and values of the map. (Also separate keys and Values)
Map<Integer,String> map=new HashMap<Integer, String>();
map.put(1, "Sandeep M");
map.put(2, "Afsa S");
map.put(3, "Sandhya D");
map.forEach((k,v) -> System.out.println(k+" "+v) );
//Checked for the existence of the key and removed that element.
if(map.containsKey(2))
{ map.remove(2); }
System.out.println("After removing element with key as 2:");
map.forEach((k,v) -> System.out.println(k+" "+v));
package demo.module1;
interface Greetings {
public void sayHelloWorld();
}
interface Greetings {
public void sayHelloWorld(String name);
}
public class LambdaExpreDemo5 {
public static void main(String[] args)
{
System.out.println("Lambda Expression Demonstration for Single parameter");
Greetings greet = (name) -> System.out.println("Hello , My name is "+name);
greet.sayHelloWorld("Gaurav Singh");
}
}
3. Write a Java program using Lambda Expression with multiple parameters to add
two numbers.
interface Arithmetic {
public int add(int a, int b);
}
public class LambdaExpreDemo5 {
public static void main(String[] args) {
Arithmetic arth = (a, b) -> a + b;
int sumofnumbers;
sumofnumbers = arth.add(5, 6);
System.out.println("The sum of 2 numbers are:" + sumofnumbers);
}
}
4. Write a Java program using Lambda Expression to calculate the following:
a. Convert Fahrenheit to Celsius
b. Convert Kilometers to Miles.
interface TemperatureInterface {
public void fahrenheitTocelcius(double fahrenheit);
}
interface DistanceInterface {
public void kilometersTometers(double kilometers);
}
interface Arithmetic {
public int add(int a, int b);
}
public class LambdaExpreDemo5 {
public static void main(String[] args) {