Thursday, 1 September 2016

CORE JAVA PMR 3

Question-1  Write a java program to check the URL.


Answer:-

import java.util.Scanner;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

class UrlChecker

 {

        public static void main(String[] args) {

        Pattern Mypattern = Pattern.compile("^((https?|ftp)://|(www|ftp)\\.)[a-z0-9-]+(\\.[a-z0-9-]+[a-z]+)+([/?].*)?$");

        Scanner input = new Scanner(System.in);

        System.out.println("Enter the Url to be checked: ");

        String name = input.nextLine();

        Matcher Mymatcher = Mypattern.matcher(name);

        Boolean Myboolean = Mymatcher.matches();

        if (Myboolean == true) 

{

            System.out.println("Url is correct");

       

 }

 else 

{

            System.out.println("Url is incorrect");

         }

}

}



Question-2  Write a program for the shirt dealer.


Answer:-

public class Shirt{
String name;
Integer id;
public static void main(String name,Integer id ){
this.name=name;
this.id=id;
}
public String toString(){
return "\n"+id+" "+name;
}
}
import java.util.Comparator;
public class Namesort implements Comparator<Shirt>{
@Override
public int Compare(Shirt o1,Shirt o2){
return o1.name.compareTo(o2.name);
}
}
import java.util.Comparator;
public class idsort implements Comparator<Shirt>{
@Override
public int Compare(Shirt o1,Shirt o2){
return o1.id.compareTo(o2.id);
}
}
import java.util.Arrays;
public class Test{
public static void main(String[] as){
Shirt st[]={new Shirt("PEPE",1),
new Shirt("PeterEngland",3),
new Shirt("Denim",2)};
Array.sort(st,new Namesort());
Array.sort(st,new idsort());
}
}

1 comment: