Create a new NetBeans project (using Ant) titled YourNameExam2P3.
2. Pretend you are a malware author and you are trying to come up with random-looking web domain names for your command-and-control viruses, e.g. “RDf-45dlfjxg7.com”.
3. First, you need to come up with some original criteria that define your domain name pattern. For example, “it should be 13 characters long, include 3 integers only, no lower case letters, and possibly end in .net or .edu”. You should come up with your own ORIGINAL pattern, of course.
4. Then, write an algorithm that is able to generate a list of random domain names based on your pattern. Make sure you are able to create at least 10 unique domain names.
5. Next, come up with the regex that fits your pattern. When you check the domain names created by your algorithm, they should all be matched by your regex.
6. For the output of your program, you should test your regex against 10 domain names that DO NOT match, as well as 10 domains that were generated by your algorithm and DO match. We should see labels of “Valid” and “Invalid” accordingly, next to each sample domain name.
7. The pattern you come up with is up to you, but at a minimum: a. It should end with at least two possible dot-somethings, e.g. “.com” and “.net”. b. Before the dot, your domain should have at least 10 characters. c. Must include letters, numbers, and possibly dashes, proportion, and pattern up to you.
8. Again, fully comment on your code, and it should be obvious what your program does from the output.
I need my program in Java Programming with fully commented.
Short Summary:
Source Code:
YourNameExam2P3.java
//This class generates a random domain ans validates it against
a regex
public class YourNameExam2P3 {
public static void main(String[] args)
{
//Initialize an end String
array
String [] endStr=
{".com",".net",".org"};
//Set no of characters before DOT
as 12
int n = 12;
// Form a string with alphabets,
numbers and a hyphen
String sampleStr = "0123456789" +
"-"
+ "abcdefghijklmnopqrstuvxyz";
String[]
str=sampleStr.split("");
// create StringBuffer
StringBuilder sb = null;
int noOfDomains=20;
int validCount=0;
int invalidCount=0;
for(int
count=0;count<noOfDomains;count++)
{
sb = new
StringBuilder(n);
for (int i = 0;
i < n; i++) {
// generate a random number between
int index = (int)(sampleStr.length() *
Math.random());
//Below condition to make sure hyphen is present
only once
if(sb.toString().contains("-")&&str[index].equals("-"))
{
//decrease i count to make
sure another iteration goes to meet string length
i--;
}
else
{
sb.append(str[index]);
}
}
//This is to
choose a random string like .com,.org,.net at last
int num=(int)
(endStr.length*Math.random());
String
generatedDomain=sb.append(endStr[num]).toString();
//Call to
validate domain name against regex
boolean valid=
isValidDomain(generatedDomain);
//The below
if checks are to get 10 valid and invalid domains to display
if(valid
&& validCount<10)
{
validCount++;
System.out.println("The domain
"+generatedDomain+" is a valid domain"+"\n");
}
if(!valid
&& invalidCount<10)
{
invalidCount++;
System.out.println("The domain
"+generatedDomain+" is an invalid domain"+"\n");
}
if(count==noOfDomains-1 )
{
//If valid and invalid domains count are less
than 10, then increase iterations by 5
if(validCount<10 || invalidCount<10)
noOfDomains=noOfDomains+5;
}
}
}
//This method checks if the domainNAme matches the
regex
static boolean isValidDomain(String domainName)
{
//Atleast one digit, atleast one
alphabet, length should be atleast 14 and max of 25
String regex = "^.*[a-zA-Z0-9]"+"(?=\\S+$).{14,25}$";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(regex);
//check if regex matches, hyphen is present atleast once and first character is an alphabet
if(m.matches() &&
domainName.contains("-") &&
String.valueOf(domainName.charAt(0)).matches("^[a-zA-Z]"))
return true;
return false;
}
}
Code Screenshot:
Output:
**************Please do upvote to appreciate our time. Thank you!******************
Get Answers For Free
Most questions answered within 1 hours.