exercise 1 java
exercise 1 java
PRACTICAL NO. 1
1. Generics
The term generics mean parameterized types.
Parameterized types are important because they enable you to create classes, interfaces,
and methods in which the type of data upon which they operate is specified as a parameter.
Using generics, it is possible to create a single class, for example, that automatically works
with different types of data.
A class, interface, or method that operates on a parameterized type is called generic, as in
generic class or generic method.
With generics, all casts are automatic and implicit. Thus, generics expand your ability to
reuse code and let you do so safely and easily.
It also allows programmer to write a single sort of method that could sort the elements in
an Integer array, a String array, or an array of any type that supports ordering.
2. Collections
• A collection — sometimes called a container — is simply an object that groups multiple elements
into a single unit.
• Collections are used to store, retrieve, manipulate, and communicate aggregate data. Typically,
they represent data items that form a natural group, such as a poker hand (a collection of
cards), a mail folder (a collection of letters), or a telephone directory (a mapping of names
to phone numbers).
• Collections in java is a framework that provides an architecture to store and manipulate
the group of objects.
FINOLEX ACADEMY OF MANAGEMENT AND TECHNOLOGY, RATNAGIRI FY MCA
• All the operations that you perform on data such as searching, sorting, insertion,
manipulation, deletion etc. can be performed by Java Collections.
• Some of the most common types of collections include:
• Lists: A list is a collection of objects that are stored in a specific order.
• Sets: A set is a collection of objects that are unique.
• Maps: A map is a collection of key-value pairs.
• Java Collection framework provides many interfaces (Set, List, Queue, Deque etc.) and
classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet etc).
• The java.util package contains all the classes and interfaces for Collection framework.
3. Lambda Expression
• Lambda expression provides a clear and concise way to represent one method interface
using an expression.
• It is very useful in collection library. It helps to iterate, filter and extract data from
collection.
• Java lambda expressions are Java's first step into functional programming.
• A Java lambda expression is thus a function which can be created without belonging to any
class.
• A lambda expression can be passed around as if it was an object and executed on demand.
• Here is an example of a lambda expression in Java:
(int x) -> { return x+1; }
• It tells that a lambda expression is something like a method without a name.
• It has everything that a method has: an argument list, which is the (int x) part of the
sample lambda, and a body, which is the { return x+1; } part after the arrow symbol.
• Return type and exceptions are inferred by the compiler from the lambda body; in our
example the return type is “int” and the throws clause is empty.
• Lambda expression syntax can be divided into three parts.
1. List of Parameters or Argument List
2. Arrow (->) token
3. Body of a lambda expression
FINOLEX ACADEMY OF MANAGEMENT AND TECHNOLOGY, RATNAGIRI FY MCA
1. Develop a Java Program to demonstrate a generic concept with generic methodto swap two
different elements in an array.
Code:
import java.util.*;
class SwapArray<T>{
public void swapArrayElement(T [] arr1,T [] arr2)
{
System.out.println("Before swap : ");
for(int i=0;i<arr1.length;i++)
{
System.out.print(arr1[i]+" ");
}
System.out.println("");
for(int i=0;i<arr2.length;i++)
{
System.out.print(arr2[i]+" ");
}
for(int i=0;i<arr1.length;i++)
{
T temp=arr1[i];
arr1[i]=arr2[i];
arr2[i]=temp;
}
}
}
class Program
{
public static void main(String [] args)
{
Integer [] arr1={6,7,8,9,10};
Integer [] arr2={10,9,8,7,6};
SwapArray<Integer> obj=new SwapArray<>();
obj.swapArrayElement(arr1,arr2);
System.out.println("\nAfter swap : ");
for(int i=0;i<arr1.length;i++)
{
System.out.print(arr1[i]+" ");
}
System.out.println("");
for(int i=0;i<arr2.length;i++)
{
System.out.print(arr2[i]+" ");
}
}
}
FINOLEX ACADEMY OF MANAGEMENT AND TECHNOLOGY, RATNAGIRI FY MCA
Output:
FINOLEX ACADEMY OF MANAGEMENT AND TECHNOLOGY, RATNAGIRI FY MCA
2. Write a Java program using Lambda Expression with multiple parameters to addtwo numbers.
Code:
package mypack;
interface AddNumber
{
public int addNumber(int a,int b);
}
Output:
FINOLEX ACADEMY OF MANAGEMENT AND TECHNOLOGY, RATNAGIRI FY MCA
3. Write a Java program using Lambda Expression with multiple parameters tocompare and
find maximum of two numbers.
Code:
package mypack;
interface MaxNumber
{
public int maxNumber(int a,int b);
}
Output:
FINOLEX ACADEMY OF MANAGEMENT AND TECHNOLOGY, RATNAGIRI FY MCA
4. Develop a Java Program to demonstrate Java Generics for user defined class – Laptop with
members like generic model, String companyNm, double price,double screenSize.
Code:
package mypack;
import java.util.Scanner;
class Laptop<T>
{
T model;
String companyNm; double price; double screenSize;
Output:
FINOLEX ACADEMY OF MANAGEMENT AND TECHNOLOGY, RATNAGIRI FY MCA
5. Create a program that demonstrates the use of the generic TreeSet class that accepts objects of
Laptop class (created in question 4) and perform variousoperations.
Code:
import java.util.*;
class Laptop<T> implements Comparable<Laptop<T>> {
T model;
String companyNm;
double price;
double screenSize;
public Laptop(T model, String companyNm, double price, double screenSize) {
this.model = model;
this.companyNm = companyNm;
this.price = price;
this.screenSize = screenSize;
}
public void displayData() {
System.out.println("Model : " + model);
System.out.println("Company Name : " + companyNm);
System.out.println("Price : " + price);
System.out.println("Screen Size : " + screenSize);
}
public int compareTo(Laptop<T> ob) {
return Double.compare(this.price, ob.price);
}
public String toString() {
return "Laptop{" +
"model=" + model +
", companyNm='" + companyNm + '\'' +
", price=" + price +
", screenSize=" + screenSize +
'}';
}
}
class Program {
public static void main(String[] args) {
Laptop<String> obj1 = new Laptop<>("mod1", "HP", 85000, 20.30);
Laptop<String> obj2 = new Laptop<>("mod2", "Asus", 60000, 10.30);
Laptop<String> obj3 = new Laptop<>("mod3", "Dell", 20000, 15.35);
TreeSet<Laptop<String>> laptopSet = new TreeSet<>();
laptopSet.add(obj1);
laptopSet.add(obj2);
laptopSet.add(obj3);
System.out.println("Laptops in sorted order by price:");
for (Laptop<String> laptop : laptopSet) {
laptop.displayData();
System.out.println();
}
System.out.println("Total number of laptops: " + laptopSet.size());
Laptop<String> searchLaptop = new Laptop<>("mod2", "Asus", 60000, 10.30);
System.out.println("Contains Asus Laptop: " + laptopSet.contains(searchLaptop));
}
FINOLEX ACADEMY OF MANAGEMENT AND TECHNOLOGY, RATNAGIRI FY MCA
}
Output: