Skip to content

Commit 221cbd1

Browse files
committed
#Modification 112
1 parent 6b43261 commit 221cbd1

12 files changed

Lines changed: 199 additions & 19 deletions

aoa_lab/BinarySearch.java

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package aoa_lab;
22

3+
import java.util.Arrays;
34
import java.util.Scanner;
45
import java.util.concurrent.ThreadLocalRandom;
56

@@ -12,33 +13,42 @@ public static int getRandomValue(int Min, int Max) {
1213
return ThreadLocalRandom.current().nextInt(Min, Max + 1);
1314
}
1415

15-
public static int search(int[] a, int n, int l, int r, int e){
16-
l = 0;
17-
r = n;
16+
int binarySearch(int[] arr, int l, int r, int x)
17+
{
18+
if (r >= l) {
19+
int mid = l + (r - l) / 2;
1820

19-
if(r >= l){
20-
int m = l + (r-l)/2;
21+
// If the element is present at the
22+
// middle itself
23+
if (arr[mid] == x){
24+
count++;
25+
return mid;
26+
}
2127

22-
if(a[m] == e)
23-
return m;
2428

25-
if(a[m] > e)
26-
search(a, n, l, m-1, e);
29+
// If element is smaller than mid, then it can only be present in left sub-array
30+
if (arr[mid] > x){
31+
count++;
32+
return binarySearch(arr, l, mid - 1, x);
33+
}
2734

28-
search(a, n, l, m+1, e);
35+
// Else the element can only be present in right sub-array
36+
return binarySearch(arr, mid + 1, r, x);
2937
}
30-
return 0;
38+
39+
System.out.println(count);
40+
41+
// We reach here when element is not present in array
42+
return -1;
3143
}
3244

3345
public static void main(String[] args) {
3446

47+
BinarySearch ob = new BinarySearch();
48+
3549
Scanner sc = new Scanner(System.in);
3650
int n = sc.nextInt();
37-
38-
int l = 0, r = n, e;
39-
40-
System.out.println("Enter the value to be found");
41-
e = sc.nextInt();
51+
int x;
4252

4353
System.out.println("Enter Min and Max range of random values");
4454
int Min = sc.nextInt(), Max = sc.nextInt();
@@ -49,6 +59,17 @@ public static void main(String[] args) {
4959
a[i] = getRandomValue(Min, Max);
5060
System.out.println(a[i] + " ");
5161
}
52-
search(a, n, l, r, e);
62+
63+
System.out.println("Enter the value to be found");
64+
x = sc.nextInt();
65+
66+
Arrays.sort(a);
67+
int result = ob.binarySearch(a, 0, n - 1, x);
68+
if (result == -1)
69+
System.out.println("Element not present");
70+
else{
71+
System.out.println("Element found at index " + result);
72+
}
73+
5374
}
5475
}

rmi_2/ApplicationClient.class

1.34 KB
Binary file not shown.

rmi_2/ApplicationClient.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//package rmi_2;
2+
3+
import java.net.MalformedURLException;
4+
import java.rmi.Naming;
5+
import java.rmi.NotBoundException;
6+
import java.rmi.RemoteException;
7+
import java.util.Scanner;
8+
import java.rmi.*;
9+
public class ApplicationClient {
10+
11+
public static void main(String[] args) {
12+
13+
Authentication authentication;
14+
15+
try {
16+
authentication = (Authentication)Naming.lookup("rmi://localhost:5000/AS");
17+
18+
Scanner scanner = new Scanner(System.in);
19+
20+
System.out.println("Enter the username : ");
21+
String userName = scanner.next();
22+
23+
System.out.println("Enter the password : ");
24+
String password = scanner.next();
25+
26+
boolean status = authentication.authenticate(userName, password);
27+
28+
if(status) {
29+
System.out.println("You're authorization is successful ...");
30+
} else {
31+
System.out.println("Unauthorized Login Attempt");
32+
}
33+
scanner.close();
34+
} catch (MalformedURLException | RemoteException | NotBoundException e) {
35+
System.out.println(e.getMessage());
36+
e.printStackTrace();
37+
}
38+
39+
}
40+
}

rmi_2/Authentication.class

250 Bytes
Binary file not shown.

rmi_2/Authentication.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package rmi_2;
2+
3+
import java.rmi.Remote;
4+
import java.rmi.RemoteException;
5+
6+
public interface Authentication extends Remote {
7+
8+
boolean authenticate(String userName, String password) throws RemoteException ;
9+
}

rmi_2/AuthenticationImpl.class

621 Bytes
Binary file not shown.

rmi_2/AuthenticationImpl.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package rmi_2;
2+
3+
import java.rmi.*;
4+
import java.rmi.server.*;
5+
6+
public class AuthenticationImpl extends UnicastRemoteObject implements Authentication {
7+
8+
protected AuthenticationImpl() throws RemoteException {
9+
super();
10+
}
11+
12+
public boolean authenticate(String userName, String password)
13+
throws RemoteException {
14+
15+
if ((userName != null && !userName.isEmpty())
16+
&& (password != null && !password.isEmpty())) {
17+
18+
return (userName.equalsIgnoreCase("admin"))
19+
&& (password.equalsIgnoreCase("admin"));
20+
}
21+
return false;
22+
}
23+
24+
}

rmi_2/Server.class

832 Bytes
Binary file not shown.

rmi_2/Server.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//package rmi_2;
2+
import java.rmi.*;
3+
import java.rmi.AlreadyBoundException;
4+
//import java.rmi.Naming;
5+
import java.rmi.RemoteException;
6+
import java.rmi.registry.LocateRegistry;
7+
import java.rmi.registry.Registry;
8+
9+
public class Server {
10+
//private static final int PORT = 5000;
11+
public static void main(String[] args) {
12+
13+
try {
14+
Authentication authentication = new AuthenticationImpl();
15+
16+
LocateRegistry.createRegistry(5000);
17+
18+
Naming.rebind("rmi://localhost:5000/AS", authentication);
19+
//Naming.rebind();
20+
21+
//System.out.println("Authentication Service running at " + PORT + " port...");
22+
23+
} catch (Exception e) {
24+
25+
System.out.println(e.getMessage());
26+
e.printStackTrace();
27+
}
28+
}
29+
}

searchingAlgorithms/BinarySearch.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public static void main(String[] args) {
7171

7272
else{
7373
count++;
74+
System.out.println("Comparisons => ");
75+
System.out.print(count);
7476
System.out.println("Element found ✔ at index => " + result);
7577
}
7678

0 commit comments

Comments
 (0)