Adv Java Ia1-Scheme
Adv Java Ia1-Scheme
Institute of Technology
An Autonomous Institution under VTU
Department of Computer Science and Engineering
Internal Test –I
SCHEME AND SOLUTION
Note: Answer FOUR full questions selecting one full question from each part.
Marks
1. Identify the goals of Collections framework.
1)The framework had to be high-performance.
• The implementations for the fundamental collections (dynamic arrays,
ANY 2-
linked lists, trees, and hash tables) are highly efficient. 02 Marks
2)The framework had to allow different types of collections to work in a
similar manner and with a high degree of interoperability.
3)Extending and/or adapting a collection had to be easy.
Develop a Java Program to demonstrate Addition and Updation of
Element using ArrayList.
import java.util.*;
class Main {
public static void main(String args[]){
2.
2.5Marks
ii)
2.5Marks
3. Iteration
import java.io.*;
import java.util.*;
class Geeks { 03 Marks
public static void main(String[] args) {
// Instantiate an object of Set
// Since LinkedHashSet implements Set
// Set points to LinkedHashSet
Set<String> lh = new LinkedHashSet<String>();
lh.add("Geek");
lh.add("For");
lh.add("Geeks");
lh.add("A");
lh.add("B");
lh.add("Z");
// Iterating though the LinkedHashSet using iterators
Iterator itr = lh.iterator();
while (itr.hasNext())
System.out.print(itr.next() + ", ");
System.out.println();
// Using enhanced for loop for iteration
for (String s : lh)
System.out.print(s + ", ");
System.out.println();
}
}
4. Develop a Java program to illustrate the difference between Comparable 08
and Comparator.
Comparator
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
class Movie { 04 Marks
private String n; // Movie name
private double r; // Movie rating
private int y; // Movie year
// Constructor to initialize movie details
public Movie(String n, double r, int y) {
BNMIT/T/23-06 Page 4 of 9 REV: 3
this.n = n;
this.r = r;
this.y = y;
}
// Getter methods
public String getN() {
return n;
}
public double getR() {
return r;
}
public int getY() {
return y;
}
}
// Comparator to sort movies by rating
class Rating implements Comparator<Movie> {
public int compare(Movie m1, Movie m2) {
Comparable
import java.util.*;
class Number implements Comparable<Number> {
int v; // Value of the number 04 Marks
public Number(int v) {
this.v = v;
}
// toString() for displaying the number
public String toString() {
return String.valueOf(v);
}
// compareTo() method to define sorting logic
public int compareTo(Number o) {
// Ascending order
return this.v - o.v;
}
public static void main(String[] args) {
5. What is Set. Give its syntax. Identify different methods of Sets in Java. 07
• The Set Interface is present in java.util package and extends the Collection 02 Marks
interface.
• It is an unordered collection of objects in which duplicate values cannot be
stored.
Syntax: 01 Mark
public interface Set extends Collection
Methods of Sets 04Marks
i)add (element)
ii)clear()
iii)contains(element)
iv)hashcode()
v)isempty()
vi)remove(element)
vii)size()
Factorial.jsp
BNMIT/T/23-06 Page 7 of 9 REV: 3
<html>
<body>
<%! 03 Marks
long n, result;
String str;
long fact(long n) {
if(n==0)
return 1;
else
return n*fact(n-1);
}
%>
<%
str = request.getParameter("val");
n = Long.parseLong(str);
result = fact(n);
%>
<b>Factorial value: </b> <%= result %>
</body>
</html>
CO1: Apply the concept like collections in developing modular & efficient programs.
CO2: Develop Server-Side applications using JSP.
Prepared by Scrutinized by
Signature: Signature: