0% found this document useful (0 votes)
2 views9 pages

Adv Java Ia1-Scheme

This document outlines the internal test scheme and solutions for the Advanced Java Programming course at B.N.M. Institute of Technology. It includes various programming tasks related to Java Collections Framework, Linked Lists, LinkedHashSets, Comparable and Comparator interfaces, and JSP applications. The test covers theoretical questions and practical programming assignments, with a focus on demonstrating key Java concepts and functionalities.

Uploaded by

Hemanth Gowda
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)
2 views9 pages

Adv Java Ia1-Scheme

This document outlines the internal test scheme and solutions for the Advanced Java Programming course at B.N.M. Institute of Technology. It includes various programming tasks related to Java Collections Framework, Linked Lists, LinkedHashSets, Comparable and Comparator interfaces, and JSP applications. The test covers theoretical questions and practical programming assignments, with a focus on demonstrating key Java concepts and functionalities.

Uploaded by

Hemanth Gowda
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/ 9

B.N.M.

Institute of Technology
An Autonomous Institution under VTU
Department of Computer Science and Engineering
Internal Test –I
SCHEME AND SOLUTION

Sem: VI Sem Date: 04/03/2025


Sub: Advanced Java Programming Time: 11:15-12.30pm
Sub code: 22CSE1654 Max Marks: 30

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[]){

// Creating an Array of string type


ArrayList<String> al = new ArrayList<>();
05
// Addition
(4 Marks
// Adding elements to ArrayList at the end
Program+
al.add("Geeks");
1 Mark-
al.add("Geeks");
Output)
System.out.println("Orignal List : "+al);
// Adding Elements at the specific index
al.add(1, "For");
System.out.println("After Adding element at index 1 : "+ al);

// Updating Values Updating value at index 0


al.set(0, "GFG");

// Printing all the elements in an ArrayList


System.out.println("List after updation of value : "+al);
BNMIT/T/23-06 Page 1 of 9 REV: 3
}
}

Identify the advantages of Iterator.

1. The Iterator is a simple and easy-to-use interface that allows us to traverse


a collection without exposing its underlying implementation.
2. The Iterator is an efficient way to iterate over a collection, especially when 02
we have a large amount of data.
Any 2-02
3. The Iterator provides a safe way to remove elements from a collection Marks
during iteration without causing any concurrent modification exceptions.
4. The Iterator interface is implemented by all the collection classes in Java,
so we can use the same code to iterate

Develop Java program to demonstrate the following operations on Linked 05


list.
i) Access elements
ii) Change elements
i)

2.

2.5Marks

ii)

2.5Marks

BNMIT/T/23-06 Page 2 of 9 REV: 3


3. Develop a Java program to demonstrate different operations on 08
LinkedHashsets.
1. Addition
import java.io.*;
import java.util.*; 02 Marks
class Geeks {
public static void main(String[] args) {
// Creating an empty LinkedHashSet
LinkedHashSet<String> lh = new LinkedHashSet<String>();
// Adding elements to above Set using add() method
lh.add("Geek");
lh.add("For");
lh.add("Geeks");
System.out.println("LinkedHashSet : " + lh);
}
}
2. Removing
import java.io.*; 03 Marks
import java.util.*;
class Geeks {
public static void main(String[] args) {
// Creating an empty LinekdhashSet of string type
LinkedHashSet<String> lh = new LinkedHashSet<String>();
// Adding elements to above Set using add() method
lh.add("Geek");
lh.add("For");
lh.add("Geeks");
lh.add("A");
lh.add("B");
BNMIT/T/23-06 Page 3 of 9 REV: 3
lh.add("Z");
System.out.println("" + lh);
// Removing the element from above Set
lh.remove("B");
// Again removing the element
System.out.println("After removing element " + lh);
// Returning false if the element is not present
System.out.println(lh.remove("AC"));
}
}

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

// Sort by rating in descending order


return Double.compare(m2.getR(), m1.getR());
}
}

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

// Create an array of Number objects


Number[] n = { new Number(4), new Number(1),
new Number(7), new Number(2) };
System.out.println("Before Sorting: " + Arrays.toString(n));
// Sort the array
Arrays.sort(n);
BNMIT/T/23-06 Page 5 of 9 REV: 3
// Display numbers after sorting
System.out.println("After Sorting: " + Arrays.toString(n));
}
}

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

6. Develop a Java program to demonstrate the working of the following 07


Hashtable constructors.
i) Hashtable()
import java.io.*;
import java.util.*;
class AddElementsToHashtable 3.5Marks
{
public static void main(String args[])
{
// No need to mention the Generic type twice
Hashtable<Integer, String> ht1 = new Hashtable<>();
// Initialization of a Hashtable using Generics
Hashtable<Integer, String> ht2
= new Hashtable<Integer, String>();
// Inserting the Elements using put() method
ht1.put(1, "one");
ht1.put(2, "two");
ht1.put(3, "three");
ht2.put(4, "four");
ht2.put(5, "five");
ht2.put(6, "six");
// Print mappings to the console
System.out.println("Mappings of ht1 : " + ht1);
System.out.println("Mappings of ht2 : " + ht2);
}
}
ii) Hashtable(int , float)
BNMIT/T/23-06 Page 6 of 9 REV: 3
// Using Hashtable(int size, float fillRatio)
import java.io.*;
import java.util.*;
class GFG
{
public static void main(String args[]) 3.5Marks
{
// No need to mention the Generic type twice
Hashtable<Integer, String> ht1
= new Hashtable<>(4, 0.75f);
// Initialization of a Hashtable using Generics
Hashtable<Integer, String> ht2
= new Hashtable<Integer, String>(3, 0.5f);
// Inserting the Elements using put() method
ht1.put(1, "one");
ht1.put(2, "two");
ht1.put(3, "three");
ht2.put(4, "four");
ht2.put(5, "five");
ht2.put(6, "six");
// Print mappings to the console
System.out.println("Mappings of ht1 : " + ht1);
System.out.println("Mappings of ht2 : " + ht2);
}
}

7. List the advantages of JSP over Servlets. 03


1.JSP simplifies web development by combining the strengths of Java with the
flexibility of HTML. Any 3-03
2.JSP code is easier to manage than Servlets as it separates UI and business Marks
logic.
3.JSP minimizes the amount of code required for web applications.
4.Easily generates content dynamically in response to user interactions.
5.It provides access to the complete range of Java APIs for robust application
development.
6.JSP is suitable for applications with growing user bases.

Develop a JSP program that calculates factorial values for an integer 05


number, while the input is taken from an HTML form.
<html>
<body>
<form action="Factorial.jsp"> 02 Marks
Enter a value for n: <input type="text" name="val">
<input type="submit" value="Submit">
</form>
</body>
</html>

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>

8. With a neat diagram, build the architecture of JSP. 03


• JSP architecture gives a high-level view of the working of JSP.
• JSP architecture is a 3 tier architecture. 03 Marks
• It has a
• Client
• Web Server
• Database.

Develop a JSP program that calculates Powers of 2 for integers in the 05


range 0-10.
<html> 05 Marks
<head>
<title>Powers of 2</title>
</head>
<body>
BNMIT/T/23-06 Page 8 of 9 REV: 3
<center>
<table border="2" align="center">
<th>Exponent</th>
<th>2^Exponent</th>
<% for (int i=0; i<=10; i++) { //start for loop %>
<tr>
<td><%= i%></td>
<td><%= Math.pow(2, i) %></td>
</tr>
<% } //end for loop %>
</table>
</center>
</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:

Name: Prof. Manikantha K Name: Dr.Anitha N

BNMIT/T/23-06 Page 9 of 9 REV: 3

You might also like