diff --git a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task1.java b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task1.java index fb8a529..3edef45 100644 --- a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task1.java +++ b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task1.java @@ -2,6 +2,9 @@ public class Task1 { public static String makeTags(String tag, String text) { - return ""; + StringBuilder tagText = new StringBuilder(); + tagText.append("<").append(tag).append(">").append(text).append(""); + return tagText.toString(); + } } diff --git a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task10.java b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task10.java index 95ec7eb..e3696ac 100644 --- a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task10.java +++ b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task10.java @@ -1,7 +1,20 @@ package school.lemon.changerequest.java.introduction.hw2; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + public class Task10 { public static String trim(String text) { + if (text == null) { + return null; + } + String regExp = "(((?<=[\\t]\\s)[a-z,A-Z]+.*[^\\s\\t]+)|([A-Z]).*[^\\t\\s])"; + Pattern pattern = Pattern.compile(regExp); + Matcher matcher = pattern.matcher(text); + if (matcher.find()){ + return matcher.group(); + } return ""; + } } diff --git a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task2.java b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task2.java index fd4494e..0d8caa0 100644 --- a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task2.java +++ b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task2.java @@ -3,6 +3,17 @@ public class Task2 { public static String firstTwo(String s) { - return ""; + + if (s == null) { + return null; + } + + + if (s.length() < 2) { + return s; + } + else { + return s.substring(0, 2); + } } } diff --git a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task3.java b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task3.java index 4d2e987..dabcc7c 100644 --- a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task3.java +++ b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task3.java @@ -3,6 +3,20 @@ public class Task3 { public static String comboString(String s1, String s2) { - return ""; + if (s1 == null){ + s1 = String.valueOf(s1); + } + + if (s1.length() > s2.length()) + { + StringBuilder newstring = new StringBuilder(); + newstring.append(s2).append(s1).append(s2); + return newstring.toString(); + } + else { + StringBuilder newstring2 = new StringBuilder(); + newstring2.append(s1).append(s2).append(s1); + return newstring2.toString(); + } } } diff --git a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task4.java b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task4.java index 35edd98..ff51551 100644 --- a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task4.java +++ b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task4.java @@ -2,6 +2,24 @@ public class Task4 { public static String charAt(String s, int i) { - return ""; + + if (s == null) { + return null; + } + int len = s.length(); + if (len < 0) { + if (Math.abs(i) >= len) { + while (Math.abs(i) > len) { + i = Math.abs(i) - len; + } + } + return String.valueOf(s.charAt(len - Math.abs(i))); + } + else { + if (i >= len) { + while (i >= len) i-= len; + } + return String.valueOf(s.charAt(i)); + } } } diff --git a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task5.java b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task5.java index 5e42b95..b806a8f 100644 --- a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task5.java +++ b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task5.java @@ -2,6 +2,6 @@ public class Task5 { public static boolean commondEnd(int[] a, int[] b) { - return false; + return (a[0]==b[0] || a[a.length - 1]== b[b.length - 1] || a[0]==b[b.length - 1] || b[0]== a[a.length - 1]); } } diff --git a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task6.java b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task6.java index ee19236..1140540 100644 --- a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task6.java +++ b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task6.java @@ -1,8 +1,14 @@ package school.lemon.changerequest.java.introduction.hw2; - public class Task6 { public static int[] reverse(int[] arr) { - return null; + if (arr == null) { + return null; + } + int reverseArr[] = new int[arr.length]; + for (int i = arr.length-1; i >= 0; i--) { + reverseArr[arr.length - i - 1] = arr[i]; + } + return reverseArr; } } diff --git a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task7.java b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task7.java index f95de42..333ef10 100644 --- a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task7.java +++ b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task7.java @@ -2,6 +2,15 @@ public class Task7 { public static int countEvens(int[] arr) { - return 0; + if (arr == null) return 0; + + int evenNum = 0; + for (int anArr : arr) { + if (anArr % 2 == 0) { + evenNum++; + } + } + + return evenNum; } } diff --git a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task8.java b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task8.java index 271facb..662651f 100644 --- a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task8.java +++ b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task8.java @@ -3,6 +3,22 @@ public class Task8 { public static int[] evenOdd(int[] arr) { - return null; + if (arr == null) return null; + + int sortArr[] = new int[arr.length]; + int j = 0; + for (int anArr : arr) { + if (anArr % 2 == 0) + sortArr[j++] = arr[anArr]; + } + + int n = 0; + for (int newArr: arr) { + if (newArr % 2 != 0) { + sortArr[j + n++] = arr[newArr]; + } + } + + return sortArr; } } diff --git a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task9.java b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task9.java index 267a59d..c6166f9 100644 --- a/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task9.java +++ b/src/main/java/school/lemon/changerequest/java/introduction/hw2/Task9.java @@ -1,7 +1,27 @@ package school.lemon.changerequest.java.introduction.hw2; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + public class Task9 { public static String[] extractData(String URL) { - return new String[]{"", "", ""}; + + if (URL == null) { + + return new String[]{"", "", ""}; + } + Pattern pattern = Pattern.compile("(?:([a-z]+):\\/\\/)?([\\da-z._-]+)(?::(\\d+)?)?.*"); + //"(?:(\w+):\/\/)?([\d\w._-]+)(?::(\d+)?)?.*" + Matcher matcher = pattern.matcher(URL); + + + if (matcher.matches()) { + String scheme = matcher.group(1); + String host = matcher.group(2); + String port = matcher.group(3); + return new String[] {scheme != null ? scheme : "", host != null ? host : "", port != null ? port : ""}; + + } + return new String[] {"", "", ""}; } }