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

Java Sorting, Regex, and SHA-256 Examples

The document contains three Java programming examples: a sorting algorithm using a custom comparator for players based on their scores and names, a pattern syntax checker that validates regular expressions, and a SHA-256 hashing implementation. Each example includes sample input and output to demonstrate functionality. The code snippets illustrate key programming concepts such as sorting, regex validation, and cryptographic hashing.

Uploaded by

vvce22ise0095
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Java Sorting, Regex, and SHA-256 Examples

The document contains three Java programming examples: a sorting algorithm using a custom comparator for players based on their scores and names, a pattern syntax checker that validates regular expressions, and a SHA-256 hashing implementation. Each example includes sample input and output to demonstrate functionality. The code snippets illustrate key programming concepts such as sorting, regex validation, and cryptographic hashing.

Uploaded by

vvce22ise0095
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Sorting: Comparator
import [Link].*;

class Checker implements Comparator<Player> {


// complete this method
public int compare(Player p1, Player p2) {

return [Link] != [Link] ? ([Link] - [Link]) : [Link]([Link]);

}
}

class Player {
String name;
int score;

Player(String name, int score) {


[Link] = name;
[Link] = score;
}
}

public class Solution {

public static void main(String[] args) {


Scanner scan = new Scanner([Link]);
int n = [Link]();

Player[] player = new Player[n];


Checker checker = new Checker();

for(int i = 0; i < n; i++){


player[i] = new Player([Link](), [Link]()
);
}
[Link]();

[Link](player, checker);
for(int i = 0; i < [Link]; i++){
[Link]("%s %s\n", player[i].name, playe
r[i].score);
}
}
}
Sample Input
5
amy 100
david 100
heraldo 50
aakansha 75
aleksa 150

Sample Output
aleksa 150
amy 100
david 100
aakansha 75
heraldo 50

2. Pattern-syntax-checker
import [Link];
import [Link].*;

class Solution{
public static void main(String[] args){
Scanner in = new Scanner([Link]);
int testCases = [Link]([Link]());
while(testCases>0){
String pattern = [Link]();
try{
Pattern pat=[Link](pattern);
[Link]("Valid");
}catch(Exception e){[Link]("Invalid");}
testCases--;
}
}
}

Sample Input

3
([A-Z])(.+)
[AZ[a-z](a-z)
batcatpat(nat

Sample Output

Valid
Invalid
Invalid

3. Java SHA-256
import [Link].*;
import [Link].*;
import [Link].*;

public class Solution {

public static void main(String[] args) {


/* Enter your code here. Read input from STDIN. Print out
put to STDOUT. Your class should be named Solution. */
Scanner scanner = new Scanner([Link]);
String key = [Link]();
try{
MessageDigest md = [Link]("SHA-256");
[Link]([Link]());

byte[] digest = [Link]();

StringBuffer stringbuffer = new StringBuffer();


for (byte b: digest)
{ // needed to print it in hexadecimal format
[Link]([Link]("%02x", b));
}

[Link]([Link]());

}
catch (NoSuchAlgorithmException exception)
{
[Link](exception);
}

}
}

Sample Input

HelloWorld
Sample Output

872e4e50ce9990d8b041330c47c9ddd11bec6b503ae9386a99da8584e9bb12c4

You might also like