[Go to site: main page, start]

0% found this document useful (0 votes)
3 views23 pages

Java Lab 1

The document contains multiple Java programs addressing various tasks such as converting numbers to words, finding second maximum and minimum values using autoboxing, managing an ArrayList with a menu-driven interface, manipulating strings, and creating servlets for voting eligibility and file downloading. Each program includes user input handling, data processing, and output display. Additionally, there are JDBC operations for managing student records in a database.

Uploaded by

berlin7san
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)
3 views23 pages

Java Lab 1

The document contains multiple Java programs addressing various tasks such as converting numbers to words, finding second maximum and minimum values using autoboxing, managing an ArrayList with a menu-driven interface, manipulating strings, and creating servlets for voting eligibility and file downloading. Each program includes user input handling, data processing, and output display. Additionally, there are JDBC operations for managing student records in a database.

Uploaded by

berlin7san
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

PartA\a1_NumToWords.

java

1
2 * Question: Write a program to convert numbers into words using Enumerations
3 * with constructors, methods and instance variables. (INPUT RANGE-0 TO 99999)
4
5 import [Link];
6
7 enum NumToWordsEnum {
8 ZERO(0), ONE(1), TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9),
9 TEN(10), ELEVEN(11), TWELVE(12), THIRTEEN(13), FOURTEEN(14), FIFTEEN(15), SIXTEEN(16),
10 SEVENTEEN(17), EIGHTEEN(18), NINETEEN(19), TWENTY(20), THIRTY(30), FORTY(40), FIFTY(50),
11 SIXTY(60), SEVENTY(70), EIGHTY(80), NINETY(90);
12
13 private final int value;
14 NumToWordsEnum(int v) { [Link] = v; }
15 public int getVal() { return value; }
16
17 public static String findWord(int num) {
18 for (NumToWordsEnum n : [Link]()) {
19 if ([Link]() num) return [Link]();
20 }
21 return "";
22 }
23 }
24
25 public class NumToWords {
26 static String convert(int num) {
27 if (num 0) return [Link]();
28 String words = "";
29 if (num / 1000 > 0) {
30 words += convert(num / 1000) + " THOUSAND ";
31 num %= 1000;
32 }
33 if (num / 100 > 0) {
34 words += [Link](num / 100) + " HUNDRED ";
35 num %= 100;
36 }
37 if (num > 0) {
38 if (num < 20) {
39 words += [Link](num);
40 } else {
41 int tens = (num / 10) * 10;
42 int % 10;
43 words += [Link](tens) + " ";
44 if (ones > 0) words += [Link](ones);
45 }
46 }
47 return [Link]();
48 }
49
50 public static void main(String[] args) {
51 Scanner sc = new Scanner([Link]);
52 [Link]("Enter a number (0-99999): ");
53 int number = [Link]();
54 if (number < 0 number > 99999) {
55 [Link]("Out of supported range");
56 } else {
57 [Link](number + " in words is: " + convert(number));
58 }
59 [Link]();
60 }
61 }
PartA\a2_AutoboxingUnboxing.java

1
2 * Question: Find the second maximum and second minimum in a set of numbers
3 * using autoboxing and unboxing.
4
5 import [Link].*;
6
7 public class AutoboxingUnboxing {
8 public static void main(String[] args) {
9 Scanner sc = new Scanner([Link]);
10 [Link]("Enter the number of elements: ");
11 int n = [Link]();
12
13 if (n < 2) {
14 [Link]("Minimum and maximum cannot be found. Insufficient
elements.");
15 return;
16 }
17
18 Autoboxing: primitive 'int' to wrapper 'Integer'
19 ArrayList<Integer> numbers = new ArrayList ();
20 [Link]("Enter the elements: ");
21 for (int i = 0; i < n; i ) {
22 [Link]([Link]());
23 }
24 [Link]();
25
26 [Link](numbers);
27
28 Unboxing happens implicitly during comparisons
29 Integer min1 = [Link](0);
30 Integer min2 = null;
31 for (int i = 1; i < n; i ) {
32 if (![Link](i).equals(min1)) {
33 min2 = [Link](i);
34 break;
35 }
36 }
37
38 Integer max1 = [Link](n - 1);
39 Integer max2 = null;
40 for (int i = n - 2; i 0; i ) {
41 if (![Link](i).equals(max1)) {
42 max2 = [Link](i);
43 break;
44 }
45 }
46
47 if (min2 null max2 null) {
48 [Link]("Not enough distinct values to find second minimum and second
maximum.");
49 } else {
50 [Link]("Second minimum: " + min2);
51 [Link]("Second maximum: " + max2);
52 }
53 }
54 }
PartA\a3_ArrayListDemo.java

1
2 * Question: Write a menu driven program to create an Arraylist and perform the following
operations
3 * i) Adding elements ii) Sorting elements iii) Replace an element with another
4 * iv) Removing an element v) Displaying all the elements vi) Adding an element between two
elements
5
6 import [Link].*;
7
8 public class ArrayListDemo {
9 public static void main(String[] args) {
10 Scanner sc = new Scanner([Link]);
11 ArrayList<Integer> al = new ArrayList ();
12
13 while (true) {
14 [Link]("\n Menu ");
15 [Link]("1. Add elements");
16 [Link]("2. Sort elements");
17 [Link]("3. Replace element");
18 [Link]("4. Remove an element");
19 [Link]("5. Display all elements");
20 [Link]("6. Adding an element b/w 2 elements");
21 [Link]("7. Exit");
22 [Link]("Enter your choice: ");
23 int choice = [Link]();
24
25 switch (choice) {
26 case 1:
27 [Link]("Enter the number of elements to add: ");
28 int n = [Link]();
29 [Link]("Enter the elements:");
30 for (int i = 0; i < n; i ) [Link]([Link]());
31 break;
32 case 2:
33 if (![Link]()) {
34 [Link](al);
35 [Link]("Elements sorted.");
36 }
37 break;
38 case 3:
39 if (![Link]()) {
40 [Link]("Enter index to replace: ");
41 int idx = [Link]();
42 [Link]("Enter new element: ");
43 int ele = [Link]();
44 if (idx 0 idx < [Link]()) {
45 [Link](idx, ele);
46 [Link]("Element replaced.");
47 } else [Link]("Invalid Index.");
48 }
49 break;
50 case 4:
51 if (![Link]()) {
52 [Link]("Enter index to remove: ");
53 int remIdx = [Link]();
54 if (remIdx 0 remIdx < [Link]()) {
55 [Link](remIdx);
56 [Link]("Element removed.");
57 } else [Link]("Invalid Index.");
58 }
59 break;
60 case 5:
61 [Link]("Elements: " + al);
62 break;
63 case 6:
64 [Link]("Enter index to add at: ");
65 int addIdx = [Link]();
66 [Link]("Enter element: ");
67 int addEle = [Link]();
68 if (addIdx 0 addIdx [Link]()) {
69 [Link](addIdx, addEle);
70 [Link]("Element added.");
71 } else [Link]("Invalid Index.");
72 break;
73 case 7:
74 [Link]("Exiting ");
75 [Link]();
76 [Link](0);
77 default:
78 [Link]("Invalid choice!");
79 }
80 }
81 }
82 }
PartA\a4_StringManipulation.java

1
2 * Question: Write a java program to find words with even number of characters in a string,
3 * then swap the pair of characters in those words and also toggle the characters in a given
string.
4
5 import [Link];
6
7 public class StringManipulation {
8 public static void main(String[] args) {
9 Scanner sc = new Scanner([Link]);
10 [Link]("Enter a string: ");
11 String str = [Link]();
12 [Link]();
13
14 String[] words = [Link]().split("\\s+");
15 StringBuilder swapRes = new StringBuilder();
16 StringBuilder togRes = new StringBuilder();
17
18 for (String w : words) {
19 Toggling characters
20 StringBuilder togWord = new StringBuilder();
21 for (char ch : [Link]()) {
22 if ([Link](ch)) {
23 [Link]([Link](ch));
24 } else {
25 [Link]([Link](ch));
26 }
27 }
28 [Link](togWord).append(" ");
29
30 Swapping logic for even length words
31 if ([Link]() % 2 0) {
32 StringBuilder sb = new StringBuilder(w);
33 for (int j = 1; j < [Link](); j += 2) {
34 char temp = [Link](j);
35 [Link](j, [Link](j - 1));
36 [Link](j - 1, temp);
37 }
38 [Link](sb).append(" ");
39 } else {
40 [Link](w).append(" ");
41 }
42 }
43
44 [Link]("Swapped Output (Even words): " + [Link]().trim());
45 [Link]("Toggled Output: " + [Link]().trim());
46 }
47 }
Selected files
2 printable files

PartA\a5\[Link]
PartA\a5\[Link]

PartA\a5\[Link]

1
2 Question: Write a Servlet program that accepts the age and name
3 and displays if the user is eligible for voting or not.
4
5 [Link]
6 <!DOCTYPE html>
7 html lang "en"
8 head
9 meta charset "UTF-8"
10 title Voter App title
11 head
12 body
13 form action "VotingServlet" method "get"
14 table
15 tr td Name td td input type "text" name "name" required td tr
16 tr td Age td td input type "text" name "age" required td tr
17 tr td colspan "2" input type "submit" value "Check Voting Eligibility" td
tr
18 table
19 form
20 body
21 html

PartA\a5\[Link]

1 [Link]
2 import [Link].*;
3 import [Link].*;
4 import [Link].*;
5
6 public class VotingServlet extends HttpServlet {
7 public void doGet(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException {
8 [Link]("text/html");
9 PrintWriter pw = [Link]();
10
11 String name = [Link]("name");
12 int age = [Link]([Link]("age"));
13
14 if (age 18) {
15 [Link]("<font color='green' size='4'>" + name + ", you are eligible to vote
font>");
16 } else {
17 [Link]("<font color='red' size='4'>" + name + ", you are not eligible to
vote font>");
18 }
19 [Link]("<br><br><a href='[Link]'>Home a>");
20 [Link]();
21 }
22 }
PartA\a6_fibonacci_prime.java

1 <%
2 Question: Write a JSP program to print first 10 Fibonacci and 10 prime numbers.
3 %>
4 <%@ page contentType="text/html" pageEncoding="UTF-8"%>
5 <!DOCTYPE html>
6 <html>
7 <head>
8 <title>JSP Page title>
9 head>
10 <body>
11 <h4>Fibonacci Series h4>
12 <%
13 int f1 = -1, f2 = 1, f3;
14 for (int i = 1; i 10; i ) {
15 f3 = f1 + f2;
16 [Link](f3 + "&nbsp;&nbsp;");
17 f1 = f2;
18 f2 = f3;
19 }
20 %>
21
22 <h4>Prime Numbers h4>
23 <%
24 int p = 2, count = 1;
25 while (count 10) {
26 int i;
27 for (i = 2; i p / 2; i ) {
28 if (p % i 0) break;
29 }
30 if (i > p / 2) {
31 [Link](p + "&nbsp;&nbsp;");
32 count ;
33 }
34 p ;
35 }
36 %>
37 body>
38 html>
Selected files
2 printable files

PartA\a7\[Link]
PartA\a7\[Link]

PartA\a7\[Link]

1
2 Question: Write a java Servlet program to Download a file and display it on the screen
3 (A link has to be provided in HTML, when link is clicked corresponding file should be
displayed).
4
5 [Link]
6 <!DOCTYPE html>
7 html
8 head
9 title File Download title
10 head
11 body
12 h2 Click below to download the file h2
13 a href "DownloadServlet?filename=[Link]" Download File a
14 body
15 html

PartA\a7\[Link]

1 [Link]
2 import [Link].*;
3 import [Link].*;
4 import [Link].*;
5
6 public class DownloadServlet extends HttpServlet {
7 public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
8 String fileName = [Link]("filename");
9 if (fileName null) {
10 [Link]().println("Filename not provided");
11 return;
12 }
13
14 [Link]("text/plain");
15 [Link]("Content-Disposition", "attachment; filename=\"" + fileName +
"\"");
16
17 Ensure "[Link]" exists in your "D:\" drive or update path
18 FileInputStream inStream = new FileInputStream("D:\\" + fileName);
19 OutputStream outStream = [Link]();
20
21 int i;
22 while ((i = [Link]()) -1) {
23 [Link](i);
24 }
25 [Link]();
26 [Link]();
27 }
28 }
PartB\b1_StudentJDBC.java

1
2 * Question: Write a menu driven JDBC program to perform basic operations with Student Table.
3 * 1) Add new Student 2) Delete a specified students Record 3) Update Students Address
4 * 4) Search for a particular Student 5) Exit
5
6 import [Link].*;
7 import [Link];
8
9 public class StudentJDBC {
10 public static void main(String[] args) {
11 Scanner in = new Scanner([Link]);
12 try {
13 NOTE: Update driver and URL according to your database (Derby/MySQL)
14 [Link]("[Link]");
15 Connection con = [Link]("jdbc:derby: localhost:1527/
Studentdb", "userbca", "Studentbca");
16 Statement stat = [Link]();
17
18 int choice;
19 do {
20 [Link]("\n Menu ");
21 [Link]("1. Add Student");
22 [Link]("2. Delete Student");
23 [Link]("3. Update Student");
24 [Link]("4. Search Student");
25 [Link]("5. Exit");
26 [Link]("Enter your choice: ");
27 choice = [Link]();
28
29 switch (choice) {
30 case 1:
31 [Link]("Enter Reg No: ");
32 int sregno = [Link](); [Link]();
33 [Link]("Enter Name: ");
34 String sname = [Link]();
35 [Link]("Enter Date: ");
36 String sdob = [Link]();
37 [Link]("Enter Address: ");
38 String sadd = [Link]();
39 [Link]("Enter Class: ");
40 String sclass = [Link]();
41 [Link]("Enter Course: ");
42 String scourse = [Link]();
43
44 String q1 = "INSERT INTO Student VALUES(" + sregno + ",'" + sname +
"','" + sdob + "','" + sadd + "','" + sclass + "','" + scourse + "')";
45 if ([Link](q1) 1) [Link]("Student Details
Saved");
46 break;
47 case 2:
48 [Link]("Enter Reg No to delete: ");
49 sregno = [Link]();
50 if ([Link]("DELETE FROM Student WHERE regno=" + sregno)
1)
51 [Link]("Record Deleted");
52 else
53 [Link]("Student Record Not Found");
54 break;
55 case 3:
56 [Link]("Enter Reg No to update: ");
57 sregno = [Link](); [Link]();
58 [Link]("Enter New Address: ");
59 String newadd = [Link]();
60 if ([Link]("UPDATE Student SET address='" + newadd + "'
WHERE regno=" + sregno) 1)
61 [Link]("Address updated");
62 else
63 [Link]("Student Record not found");
64 break;
65 case 4:
66 [Link]("Enter Reg No to search: ");
67 sregno = [Link]();
68 ResultSet rs = [Link]("SELECT * FROM Student WHERE regno="
+ sregno);
69 if ([Link]()) {
70 [Link]("Reg No: " + [Link](1));
71 [Link]("Name: " + [Link](2));
72 [Link]("Address: " + [Link](4));
73 } else {
74 [Link]("Student record not found");
75 }
76 break;
77 case 5:
78 [Link]("Exit ");
79 break;
80 }
81 } while (choice 5);
82 [Link]();
83 } catch (Exception ex) {
84 [Link]();
85 }
86 }
87 }
PartB\b2_BankJDBC.java

1
2 * Question: Write a menu driven JDBC program to perform basic operations with Bank Table.
3 * 1) Add new Account Holder 2) Amount Deposit 3) Amount Withdrawal (Maintain min balance
500)
4 * 4) Display all information 5) Exit
5
6 import [Link].*;
7 import [Link];
8
9 public class BankJDBC {
10 public static void main(String[] args) {
11 Scanner in = new Scanner([Link]);
12 try {
13 NOTE: Update driver and URL according to your database
14 [Link]("[Link]");
15 Connection con = [Link]("jdbc:derby: localhost:1527/
bankdb", "bank", "bank123");
16 Statement stat = [Link]();
17
18 int ch;
19 do {
20 [Link]("\n Transaction Menu ");
21 [Link]("1. Add Account");
22 [Link]("2. Deposit");
23 [Link]("3. Withdraw");
24 [Link]("4. Display");
25 [Link]("5. Exit");
26 [Link]("Enter your choice: ");
27 ch = [Link]();
28
29 switch (ch) {
30 case 1:
31 [Link]("Enter Acc Number: ");
32 int accno = [Link](); [Link]();
33 [Link]("Enter Name: ");
34 String name = [Link]();
35 [Link]("Enter Address: ");
36 String address = [Link]();
37 [Link]("Enter Initial Amount: ");
38 double bal = [Link]();
39 if (bal < 500) {
40 [Link]("Minimum balance is Rs 500");
41 break;
42 }
43 String q1 = "INSERT INTO Bank VALUES(" + accno + ",'" + name + "','"
+ address + "'," + bal + ")";
44 [Link](q1);
45 [Link]("Record Inserted");
46 break;
47 case 2:
48 [Link]("Enter Acc Number: ");
49 accno = [Link]();
50 [Link]("Enter Deposit Amount: ");
51 double dep = [Link]();
52 if (dep 0) break;
53 [Link]("UPDATE Bank SET balance=balance+" + dep + " WHERE
accno=" + accno);
54 [Link]("Balance Updated");
55 break;
56 case 3:
57 [Link]("Enter Acc Number: ");
58 accno = [Link]();
59 [Link]("Enter Withdraw Amount: ");
60 double wit = [Link]();
61 ResultSet rs = [Link]("SELECT balance FROM Bank WHERE
accno=" + accno);
62 if ([Link]()) {
63 double curBal = [Link](1);
64 if (curBal - wit < 500) {
65 [Link]("Amount cannot be withdrawn Pls
maintain minimum balance 500!");
66 } else {
67 [Link]("UPDATE Bank SET balance=balance-" + wit +
" WHERE accno=" + accno);
68 [Link]("Amount Withdrawn Successfully");
69 }
70 } else {
71 [Link]("Account not found!");
72 }
73 break;
74 case 4:
75 ResultSet rsAll = [Link]("SELECT * FROM Bank");
76 while ([Link]()) {
77 [Link]([Link](1) + "\t" + [Link](2) +
"\t" + [Link](4));
78 }
79 break;
80 case 5:
81 [Link]("Exit ");
82 break;
83 }
84 } while (ch 5);
85 [Link]();
86 } catch (Exception e) {
87 [Link]();
88 }
89 }
90 }
PartB\b3_all4files.java

1
2 * Question: Write a Java class called Tax with methods for calculating Income Tax.
3 * Have this class as a servant and create a server program and register in the rmiregistry.
4 * Write a client program to invoke these remote methods of the servant and do the
calculations.
5
6
7 1. [Link]
8 import [Link].*;
9 public interface TaxIntf extends Remote {
10 public double cal_tax(double a) throws RemoteException;
11 }
12
13 2. [Link]
14 import [Link].*;
15 import [Link].*;
16 public class TaxImpl extends UnicastRemoteObject implements TaxIntf {
17 public TaxImpl() throws RemoteException {}
18 public double cal_tax(double amt) throws RemoteException {
19 double t = 0;
20 if (amt < 300000) t = 0;
21 else if (amt 600000) t = 0.05 * amt;
22 else if (amt 900000) t = 0.10 * amt;
23 else if (amt 1200000) t = 0.15 * amt;
24 else if (amt 1500000) t = 0.20 * amt;
25 else t = 0.30 * amt;
26 return t;
27 }
28 }
29
30 3. [Link]
31 import [Link].*;
32 import [Link].*;
33 public class TaxServer {
34 public static void main(String args[]) {
35 try {
36 TaxImpl taximp1 = new TaxImpl();
37 [Link]("TaxServer", taximp1);
38 [Link]("Server is Running");
39 } catch(Exception e) {
40 [Link]("Error");
41 }
42 }
43 }
44
45 4. [Link]
46 import [Link].*;
47 import [Link].*;
48 public class TaxClient {
49 public static void main(String args[]) {
50 try {
51 String TaxURL = "rmi: localhost/TaxServer";
52 TaxIntf taxintf = (TaxIntf) [Link](TaxURL);
53 BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
54 [Link]("Enter the amount: ");
55 String amt = [Link]();
56 double a = [Link](amt);
57 double t = taxintf.cal_tax(a);
58 [Link]("Calculated Tax Amount is: %.2f\n", t);
59 } catch(Exception e) {
60 [Link]("Error");
61 }
62 }
63 }
PartB\b4_allfiles.java

1
2 * Question: Write a Java class called SimpleInterest with methods for calculating simple
interest.
3 * Have this class as a servant and create a server program and register in the rmiregistry.
4 * Write a client program to invoke these remote methods. Accept inputs at command prompt.
5
6
7 1. [Link]
8 import [Link].*;
9 public interface SimpleIntf extends Remote {
10 public double cal_simple(double p, int t, float r) throws RemoteException;
11 }
12
13 2. [Link]
14 import [Link].*;
15 import [Link].*;
16 public class SimpleImpl extends UnicastRemoteObject implements SimpleIntf {
17 public SimpleImpl() throws RemoteException {}
18 public double cal_simple(double p, int t, float r) throws RemoteException {
19 return (p * t * r) / 100;
20 }
21 }
22
23 3. [Link]
24 import [Link].*;
25 import [Link].*;
26 public class SimpleServer {
27 public static void main(String[] args) {
28 try {
29 SimpleImpl simpleimpl = new SimpleImpl();
30 [Link]("SimpleServer", simpleimpl);
31 [Link]("Server is running");
32 } catch (Exception e) {
33 [Link](e);
34 }
35 }
36 }
37
38 4. [Link]
39 import [Link].*;
40 import [Link].*;
41 public class SimpleClient {
42 public static void main(String[] args) {
43 try {
44 if ([Link] < 3) {
45 [Link]("Usage: java SimpleClient <principal> <time> <rate>");
46 return;
47 }
48 String SmpURL = "rmi: localhost/SimpleServer";
49 SimpleIntf smpintf = (SimpleIntf) [Link](SmpURL);
50
51 double p = [Link](args[0]);
52 int t = [Link](args[1]);
53 float r = [Link](args[2]);
54
55 [Link]("Principal amount is: " + p);
56 [Link]("Time is: " + t);
57 [Link]("Rate is: " + r);
58 double si = smpintf.cal_simple(p, t, r);
59 [Link]("Simple Interest: " + si);
60 } catch(Exception e) {
61 [Link]("Error" + e);
62 }
63 }
64 }
Selected files
3 printable files

PartB\b5\[Link]
PartB\b5\[Link]
PartB\b5\[Link]

PartB\b5\[Link]

1 <%
2 Question: Write a java JSP program to get student information through a HTML
3 and create a JAVA Bean Class, populate Bean and Display the same information through
another JSP.
4 %>
5 1. [Link]
6 <!DOCTYPE html>
7 html
8 body
9 h3 Student Registration Information h3
10 form method "POST" action "[Link]"
11 table border "5"
12 tr td Enter Reg No td td input type "text" name "srno" td tr
13 tr td Enter Name td td input type "text" name "sname" td tr
14 tr td Enter Address td td input type "text" name "sadd" td tr
15 tr td Enter Programme td td input type "text" name "sprog" td tr
16 tr td Enter Marks td td input type "text" name "smarks" td tr
17 tr td colspan "2" align "center" input type "submit" value "Register" td
tr
18 table
19 form
20 br a href "[Link]" View Student Information a
21 body
22 html

PartB\b5\[Link]

1 2. [Link] (Must be placed in package 'com')


2 package com;
3 public class Student {
4 private String srno, sname, sadd, sprog, smarks;
5 public Student() {}
6 public void setSrno(String srno) { [Link] = srno; }
7 public void setSname(String sname) { [Link] = sname; }
8 public void setSadd(String sadd) { [Link] = sadd; }
9 public void setSprog(String sprog) { [Link] = sprog; }
10 public void setSmarks(String smarks) { [Link] = smarks; }
11
12 public String getSrno() { return srno; }
13 public String getSname() { return sname; }
14 public String getSadd() { return sadd; }
15 public String getSprog() { return sprog; }
16 public String getSmarks() { return smarks; }
17 }

PartB\b5\[Link]

1 3. [Link]
2 <%@ page import="[Link]" %>
3 jsp:useBean id "s" scope "session" class "[Link]"
4 jsp:setProperty name "s" property "srno" param "srno"
5 jsp:setProperty name "s" property "sname" param "sname"
6 jsp:setProperty name "s" property "sadd" param "sadd"
7 jsp:setProperty name "s" property "sprog" param "sprog"
8 jsp:setProperty name "s" property "smarks" param "smarks"
9 html
10 body
11 h3 Student Information Saved h3
12 br a href "[Link]" View Student Information a
13 body
14 html
15
16 4. [Link]
17 <%@ taglib prefix="c" uri="http: [Link]/jsp/jstl/core" %>
18 html
19 body
20 h1 Student Registration Details h1
21 table border "5"
22 tr td Reg No td td c:out value "${[Link]}" td tr
23 tr td Name td td c:out value "${[Link]}" td tr
24 tr td Address td td c:out value "${[Link]}" td tr
25 tr td Programme td td c:out value "${[Link]}" td tr
26 tr td Marks td td c:out value "${[Link]}" td tr
27 table
28 body
29 html
PartB\b6_LinkedListDemo.java

1
2 * Question: Write a menu driven program to create a linked list and perform the following
operations.
3 * a. Insert Elements at Specified Position b. swap two elements c. Iterate in Reverse Order
4 * d. Compare Two LinkedList e. Convert a LinkedList to ArrayList
5
6 import [Link].*;
7
8 public class LinkedListDemo {
9 public static void main(String[] args) {
10 LinkedList<Integer> flist = new LinkedList ();
11 LinkedList<Integer> slist = new LinkedList ();
12 Scanner in = new Scanner([Link]);
13 char choice;
14
15 do {
16 [Link]("\n MENU ");
17 [Link]("a. Insert elements at specified position");
18 [Link]("b. Swap two elements in linked list");
19 [Link]("c. Iterate linked list in reverse order");
20 [Link]("d. Compare two linked lists");
21 [Link]("e. Convert linked list to arraylist");
22 [Link]("x. Exit");
23 [Link]("Enter your choice: ");
24 choice = [Link]().toLowerCase().charAt(0);
25
26 switch (choice) {
27 case 'a':
28 [Link]("Current list: " + flist);
29 [Link]("Enter the position to insert (1-based index): ");
30 int pos = [Link]();
31 if (pos 0 pos > [Link]() + 1) {
32 [Link]("Invalid position");
33 } else {
34 [Link]("Enter the number to insert: ");
35 int num = [Link]();
36 [Link](pos - 1, num);
37 [Link]("Element " + num + " inserted at position " +
pos);
38 }
39 break;
40 case 'b':
41 [Link]("Original list: " + flist);
42 [Link]("Enter the first position to swap: ");
43 int fpos = [Link]();
44 [Link]("Enter the second position to swap: ");
45 int spos = [Link]();
46 if (fpos 0 spos 0 fpos > [Link]() spos > [Link]())
{
47 [Link]("Invalid position(s)!");
48 } else {
49 int n1 = [Link](fpos - 1);
50 int n2 = [Link](spos - 1);
51 [Link](fpos - 1, n2);
52 [Link](spos - 1, n1);
53 [Link]("Elements swapped successfully!");
54 [Link]("Updated list: " + flist);
55 }
56 break;
57 case 'c':
58 [Link]("Original list: " + flist);
59 [Link]("Reversed list: [ ");
60 Iterator<Integer> it = [Link]();
61 while ([Link]()) {
62 [Link]([Link]() + " ");
63 }
64 [Link]("]");
65 break;
66 case 'd':
67 [Link]("Enter the number of elements for second list: ");
68 int r = [Link]();
69 [Link]();
70 for (int i = 0; i < r; i ) {
71 [Link]("Enter element " + (i + 1) + ": ");
72 [Link]([Link]());
73 }
74 [Link]("First list: " + flist);
75 [Link]("Second list: " + slist);
76 if ([Link](slist)) [Link]("Lists are equal!");
77 else [Link]("Lists are not equal!");
78 break;
79 case 'e':
80 ArrayList<Integer> alist = new ArrayList (flist);
81 [Link]("Converted Arraylist: " + alist);
82 break;
83 case 'x':
84 [Link]("Exiting ");
85 break;
86 default:
87 [Link]("Invalid choice. Please try again.");
88 }
89 } while (choice 'x');
90 [Link]();
91 }
92 }
PartB\b7_allfiles.java

1
2 * Question: Implement a java application based on the MVC design pattern. Input student
Rolno, name,
3 * marks in 3 subject calculate result and grade and display the result in neat format.
4
5
6 1. [Link]
7 package StudentMVC;
8 public class Studentmodel {
9 private String rollno;
10 private String name;
11 private int m1, m2, m3;
12
13 public Studentmodel(String rollno, String name, int m1, int m2, int m3) {
14 [Link] = rollno;
15 [Link] = name;
16 this.m1 = m1;
17 this.m2 = m2;
18 this.m3 = m3;
19 }
20
21 public String getRollno() { return rollno; }
22 public String getName() { return name; }
23 public int getM1() { return m1; }
24 public int getM2() { return m2; }
25 public int getM3() { return m3; }
26
27 public String getResult() {
28 if (m1 < 35 m2 < 35 m3 < 35) return "Fail";
29 double per = (m1 + m2 + m3) / 3.0;
30 if (per 75) return "Distinction";
31 else if (per 60) return "First Class";
32 else if (per 50) return "Second Class";
33 else return "Pass Class";
34 }
35
36 public String getGrade() {
37 double per = (m1 + m2 + m3) / 3.0;
38 if (per 90) return "A";
39 else if (per 80) return "B";
40 else if (per 70) return "C";
41 else if (per 60) return "D";
42 else return "E";
43 }
44 }
45
46 2. [Link]
47 package StudentMVC;
48 public class Studentview {
49 public void display(String rno, String name, int m1, int m2, int m3, String result,
String grade) {
50 [Link]("-------------------------");
51 [Link]("Roll NO\tName\tMarks1\tMarks2\tMarks3\tResult\tGrade");
52 [Link]("-------------------------");
53 [Link](rno + "\t" + name + "\t" + m1 + "\t" + m2 + "\t" + m3 + "\t" +
result + "\t" + grade);
54 [Link]("-------------------------");
55 }
56 }
57
58 3. [Link]
59 package StudentMVC;
60 public class Studentcontroller {
61 private Studentmodel model;
62 private Studentview view;
63
64 public Studentcontroller(Studentmodel model, Studentview view) {
65 [Link] = model;
66 [Link] = view;
67 }
68
69 public void updateView() {
70 [Link]([Link](), [Link](), model.getM1(), model.getM2(),
71 model.getM3(), [Link](), [Link]());
72 }
73 }
74
75 4. [Link] (Main class)
76 package StudentMVC;
77 import [Link];
78 public class StudentMVC {
79 public static void main(String[] args) {
80 Scanner sc = new Scanner([Link]);
81 [Link]("Enter Roll NO: ");
82 String rollno = [Link]();
83 [Link]("Enter Name: ");
84 String name = [Link]();
85 [Link]("Enter marks in Subject 1: ");
86 int m1 = [Link]();
87 [Link]("Enter marks in Subject 2: ");
88 int m2 = [Link]();
89 [Link]("Enter marks in Subject 3: ");
90 int m3 = [Link]();
91 [Link]();
92
93 Studentmodel model = new Studentmodel(rollno, name, m1, m2, m3);
94 Studentview view = new Studentview();
95 Studentcontroller controller = new Studentcontroller(model, view);
96
97 [Link]();
98 }
99 }

You might also like