Skip to content

Commit 105ee3e

Browse files
committed
Add Codes
1 parent 14d26f3 commit 105ee3e

28 files changed

+1938
-0
lines changed

Report/Outputs.pdf

5.46 MB
Binary file not shown.

Report/Questions.pdf

84.4 KB
Binary file not shown.

Solutions/Bonus/AppJDBC.java

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
import java.util.Scanner;
2+
3+
import java.sql.*;
4+
5+
class StudentVIT {
6+
String name;
7+
String password;
8+
String school;
9+
int marks;
10+
11+
void getDetails() {
12+
Scanner sc = new Scanner(System.in);
13+
System.out.print("Enter your name: ");
14+
name = sc.nextLine();
15+
System.out.print("Enter your password: ");
16+
password = sc.nextLine();
17+
System.out.println("Enter School: ");
18+
school = sc.nextLine();
19+
System.out.println("Enter marks");
20+
marks = sc.nextInt();
21+
}
22+
23+
void insertRecord() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
24+
try {
25+
DatabaseConn dbms = new DatabaseConn("jdbc://mysql://localhost:3306/vit", "root", "");
26+
Connection conn = dbms.getConnection();
27+
String sql = "INSERT INTO student VALUES (?,?,?,?)";
28+
PreparedStatement stmt = conn.prepareStatement(sql);
29+
stmt.setString(1, name);
30+
stmt.setString(2, password);
31+
stmt.setString(3, school);
32+
stmt.setInt(4, marks);
33+
stmt.execute();
34+
System.out.println("Record inserted successfully");
35+
dbms.closeConnection(conn, stmt);
36+
}
37+
catch(Exception e) {
38+
e.printStackTrace();
39+
}
40+
}
41+
42+
void updateRecord() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
43+
try {
44+
DatabaseConn dbms = new DatabaseConn("jdbc://mysql://localhost:3306/vit", "root", "");
45+
Connection conn = dbms.getConnection();
46+
System.out.println("Enter your name: ");
47+
Scanner sc = new Scanner(System.in);
48+
String inputname = sc.nextLine();
49+
System.out.println("Enter your new password: ");
50+
String inputpass = sc.nextLine();
51+
52+
String sql = "UPDATE student SET password = ? WHERE name = ?";
53+
PreparedStatement stmt = conn.prepareStatement(sql);
54+
stmt.setString(1, inputname);
55+
stmt.setString(2, inputpass);
56+
int i = stmt.executeUpdate();
57+
if(i>0) {
58+
System.out.println("Record updated successfully");
59+
}
60+
else {
61+
System.out.println("No such record found in the database");
62+
}
63+
dbms.closeConnection(conn, stmt);
64+
}
65+
catch(Exception e) {
66+
e.printStackTrace();
67+
}
68+
}
69+
70+
void deleteRecord() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
71+
try {
72+
DatabaseConn dbms = new DatabaseConn("jdbc://mysql://localhost:3306/vit", "root", "");
73+
Connection conn = dbms.getConnection();
74+
System.out.println("Enter name to be deleted: ");
75+
Scanner sc = new Scanner(System.in);
76+
String inputname = sc.nextLine();
77+
78+
String sql = "DELETE FROM student WHERE name = ?";
79+
PreparedStatement stmt = conn.prepareStatement(sql);
80+
stmt.setString(1, inputname);
81+
int i = stmt.executeUpdate();
82+
if(i>0) {
83+
System.out.println("Record deleted successfully");
84+
}
85+
else {
86+
System.out.println("No such record found in the database");
87+
}
88+
dbms.closeConnection(conn, stmt);
89+
}
90+
catch(Exception e) {
91+
e.printStackTrace();
92+
}
93+
}
94+
95+
void searchRecord() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
96+
try {
97+
DatabaseConn dbms = new DatabaseConn("jdbc://mysql://localhost:3306/vit", "root", "");
98+
Connection conn = dbms.getConnection();
99+
System.out.println("Enter name to be searched: ");
100+
Scanner sc = new Scanner(System.in);
101+
String inputname = sc.nextLine();
102+
103+
String sql = "SELECT * FROM student WHERE name = ?";
104+
PreparedStatement stmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
105+
stmt.setString(1, inputname);
106+
ResultSet rs = stmt.executeQuery();
107+
if(rs.next() == false) {
108+
System.out.println("No such record found in the database");
109+
}
110+
else {
111+
rs.previous();
112+
while(rs.next()) {
113+
System.out.println("Name: " + rs.getString(1));
114+
System.out.println("Password: " + rs.getString(2));
115+
System.out.println("School: " + rs.getString(3));
116+
System.out.println("Marks: " + rs.getString(4));
117+
}
118+
}
119+
dbms.closeConnection(conn, stmt);
120+
}
121+
catch(Exception e) {
122+
e.printStackTrace();
123+
}
124+
}
125+
126+
}
127+
128+
class DatabaseConn {
129+
String url;
130+
String username;
131+
String password;
132+
133+
DatabaseConn(String url, String username, String password) {
134+
this.url = url;
135+
this.username = username;
136+
this.password = password;
137+
}
138+
139+
public Connection getConnection() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
140+
Connection conn = null;
141+
Class.forName("com.mysql.kela.jdbc.Driver").newInstance();
142+
conn = DriverManager.getConnection(url, username, password);
143+
System.out.println("Connection is established");
144+
return conn;
145+
}
146+
147+
public void closeConnection(Connection conn, Statement stmt) throws SQLException {
148+
stmt.close();
149+
conn.close();
150+
}
151+
}
152+
153+
public class AppJDBC {
154+
public static void main(String[] args) {
155+
Scanner sc = new Scanner(System.in);
156+
int ch;
157+
try {
158+
do {
159+
System.out.println("Select a choice.\n1. Enter Student Details\n2. Update Password\n3. Delete Student\n4. Search Record\n5. Exit");
160+
ch = 0;
161+
StudentVIT s = new StudentVIT();
162+
System.out.print("Enter choice: ");
163+
ch = sc.nextInt();
164+
switch(ch)
165+
{
166+
case 1:
167+
s.getDetails();
168+
s.insertRecord();
169+
break;
170+
171+
case 2:
172+
s.updateRecord();
173+
break;
174+
175+
case 3:
176+
s.deleteRecord();
177+
break;
178+
179+
case 4:
180+
s.searchRecord();
181+
break;
182+
183+
case 5:
184+
System.out.println("Exiting..");
185+
break;
186+
187+
default:
188+
System.out.println("Please select the correct option.");
189+
break;
190+
}
191+
}while(ch!=5);
192+
}
193+
catch(Exception e) {
194+
e.printStackTrace();
195+
}
196+
}
197+
}

Solutions/Bonus/BONUS.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Bonus Question - JDBC Concept
2+
The code in the file `AppJDBC.java` is based on the JDBC concepts in Java Programming. JDBC stands for Java Database Connectivity. JDBC is a Java API to connect and execute the query with the database. It is a part of JavaSE (Java Standard Edition). JDBC API uses JDBC drivers to connect with the database.
3+
4+
This JDBC sample code is related CRUD operations using SQL database.
5+
It allows users to :
6+
* Get Input of students
7+
* Insert Records into the database
8+
* Update Records
9+
* Delete Records
10+
* Search Records
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import java.util.*;
2+
import java.io.*;
3+
import java.lang.*;
4+
5+
public class DistrictVaccine {
6+
public static void main(String[] args) {
7+
Scanner sc = new Scanner(System.in);
8+
int n,i,j,k,p;
9+
10+
System.out.println("Maharashtra COVID-19 Vaccination Drive");
11+
System.out.print("Enter the number of districts: ");
12+
n = sc.nextInt();
13+
14+
String[] district = new String[n];
15+
int[][] centres = new int[n][];
16+
boolean[] free = new boolean[n];
17+
int[] left = new int[n];
18+
System.out.println("\nEnter the details of each district: ");
19+
for(i=0;i<n;i++)
20+
{
21+
free[i] = false;
22+
System.out.println("\nDistrict " + (i+1));
23+
System.out.print("Enter Name: ");
24+
district[i] = sc.next();
25+
System.out.print("Enter number of vaccination centres: ");
26+
k = sc.nextInt();
27+
System.out.print("Enter number of people registered: ");
28+
p = sc.nextInt();
29+
30+
centres[i] = new int[k];
31+
for(j=0;j<k;j++)
32+
{
33+
if(p > 40) {
34+
centres[i][j] = 40;
35+
p -= 40;
36+
}
37+
else if (p > 0) {
38+
centres[i][j] = p;
39+
p -= p;
40+
}
41+
else {
42+
centres[i][j] = 0;
43+
free[i] = true;
44+
}
45+
}
46+
left[i] = p;
47+
}
48+
49+
// Traversing the jagged array using foreach loop
50+
System.out.println();
51+
k = 0;
52+
for (int[] cv : centres) {
53+
System.out.print(district[k++] + ": ");
54+
for (int num : cv) {
55+
System.out.print(num + " ");
56+
}
57+
System.out.println();
58+
}
59+
60+
// Printing the districts where all people were not able to vaccinate
61+
k=0;
62+
for (int[] cv : centres) {
63+
if(left[k] != 0) {
64+
System.out.println("Only " + 40*cv.length + " people were able to vaccinate in " + district[k] + " and " + left[k] + " people were left.");
65+
}
66+
k++;
67+
}
68+
69+
// Districts with free centres
70+
k=0;
71+
for (int[] cv : centres) {
72+
int cnt = 0;
73+
if(free[k]) {
74+
for (int num : cv) {
75+
if(num != 0) {
76+
cnt++;
77+
}
78+
}
79+
System.out.println(district[k] + " had " + cnt + " centres free on that day.");
80+
}
81+
k++;
82+
}
83+
}
84+
}

Solutions/Question 10/Generics.java

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import java.io.*;
2+
import java.util.*;
3+
import java.lang.*;
4+
5+
public class Generics {
6+
public static < E > boolean isPalindrome(E str) {
7+
int i;
8+
String s = str.toString();
9+
10+
StringBuilder sb = new StringBuilder();
11+
sb.append(s);
12+
sb.reverse();
13+
if(sb.toString().equals(s)) {
14+
return true;
15+
}
16+
return false;
17+
}
18+
19+
public static < E > boolean isPrime(E str) {
20+
int i;
21+
int n = Integer.parseInt(str.toString());
22+
for(i = 2; i < n; i++) {
23+
if(n%i == 0) {
24+
return false;
25+
}
26+
}
27+
return true;
28+
}
29+
30+
public static < E > E[] changeArray(E[] arr) {
31+
int n = arr.length, i;
32+
33+
if(arr instanceof String[])
34+
{
35+
System.out.println("\nReversing all the non - palindrome strings in array: ");
36+
for(i = 0; i < n; i++)
37+
{
38+
if(!isPalindrome(arr[i])) {
39+
StringBuilder sb = new StringBuilder();
40+
sb.append(arr[i].toString());
41+
sb.reverse();
42+
arr[i] = (E)sb.toString();
43+
}
44+
else {
45+
StringBuilder sb = new StringBuilder();
46+
sb.append(arr[i].toString());
47+
sb.append("*");
48+
arr[i] = (E)sb.toString();
49+
}
50+
}
51+
return arr;
52+
}
53+
if(arr instanceof Integer[])
54+
{
55+
System.out.println("\nReplacing all prime numbers in the array with their squares: ");
56+
for(i = 0; i < n; i++)
57+
{
58+
if(isPrime(arr[i])) {
59+
int m = Integer.parseInt(arr[i].toString());
60+
Integer sq = new Integer((int)Math.pow(m,2));
61+
arr[i] = (E)sq;
62+
}
63+
}
64+
return arr;
65+
}
66+
return arr;
67+
}
68+
public static void main(String[] args) {
69+
System.out.println("GENERIC METHODS");
70+
71+
Integer[] intArray = {20, 11, 78, 23, 89};
72+
String[] strArray = {"rushabh", "wow", "abba", "kela"};
73+
74+
System.out.println("Original Integer Array: ");
75+
for(int i : intArray) {
76+
System.out.print(i + " ");
77+
}
78+
Integer[] changedIntArray = changeArray(intArray);
79+
System.out.println("Changed Integer Array: ");
80+
for(int i : changedIntArray) {
81+
System.out.print(i + " ");
82+
}
83+
System.out.println("\n");
84+
85+
System.out.println("Original String Array: ");
86+
for(String str : strArray) {
87+
System.out.print(str + " ");
88+
}
89+
String[] changedStrArray = changeArray(strArray);
90+
System.out.println("Changed String Array: ");
91+
for(String str : changedStrArray) {
92+
System.out.print(str + " ");
93+
}
94+
System.out.println("\n");
95+
}
96+
}

0 commit comments

Comments
 (0)