Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package school.lemon.changerequest.java.introduction.hw2;

public class Task1 {
public static String makeTags(String tag, String text) {
return "";
public static String makeTags(String tag, String text)
{
return String.format("%1$s%2$s%3$s%4$s%5$s%2$s%3$s", "<", tag, ">", text, "</", tag, ">");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can print any constant values in pattern, for example:
String.format("<%1$s>", "tag") will print "<tag>".

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@

public class Task2 {
public static String firstTwo(String s) {
return "";
if (s == null)
return null;
else if (s.equals(""))
return "";
else
if (s.length() <= 2) {
return s;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If s will be equal to "" than it's length will be less than 2 - so there is no need for such check.

return s.substring(0, 2);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also you if clauses could be combined into one:

if (s == null || s.length <=2){
return s;
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But, if string equals null, we must return null , not s

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if s will be equal to null - you'll return value of s - null. There will be no difference between returning s(that is equal to null) and null.
For example:

public class Test(){
   public static void main(String[] args){
      String nullValueString = null;
      System.out.println(null); // will print 'null'
      System.out.println(nullValueString); // will print 'null'
      System.out.println(nullValueString == null); // will print 'true'
   }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@


public class Task3 {
public static String comboString(String s1, String s2) {
return "";
public static String comboString(String s1, String s2)
{
if(s1 == null)
return s1+s2+s1;
else
if(s2 == null)
return s2 + s1 + s2;
else
if(s1.length() < s2.length())
return s1+s2+s1;

return s2+s1+s2;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's OK, but format your code before submitting.
In IDEA you can use CTRL+ALT+L shortcut.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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] || a[a.length-1] == b[0]);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@

public class Task6 {
public static int[] reverse(int[] arr) {
return null;
if (arr == null) {
return null;
}
int[]a = new int[arr.length];
for(int i = 0;i < arr.length;i++)
{
a[arr.length-1-i] = arr[i];

}
return a;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

public class Task7 {
public static int countEvens(int[] arr) {
return 0;
int count=0;
if(arr == null)
return 0;
for (int anArr : arr) {
boolean b = anArr % 2 == 0;
if (b)
count++;

}
return count;
}
}