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

Warm Up 2

The document defines 12 methods that perform string and array operations: 1. stringTimes repeats a given string n times and returns the result 2. frontTimes returns the first 3 characters of a string repeated n times or the full string if it is less than 3 characters 3. countXX counts the number of occurrences of "xx" in a given string

Uploaded by

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

Warm Up 2

The document defines 12 methods that perform string and array operations: 1. stringTimes repeats a given string n times and returns the result 2. frontTimes returns the first 3 characters of a string repeated n times or the full string if it is less than 3 characters 3. countXX counts the number of occurrences of "xx" in a given string

Uploaded by

kurir010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

stringTimes:

public String stringTimes(String str, int n) {


String result = "";
for (int i = 0; i < n; i++)
result += str;
return result;
}

2. frontTimes:

public String frontTimes(String str, int n) {


String result = "";
if (str.length() <= 3)
for (int i = 0; i < n; i++)
result += str;
else
for (int i = 0; i < n; i++)
result += str.substring(0, 3);
return result;
}

3. countXX:

int countXX(String str) {


int count = 0;
for (int i = 0; i < str.length() - 1; i++)
if (str.substring(i, i + 2).equals("xx"))
count++;
return count;
}

4. doubleX:

boolean doubleX(String str) {


int firstX = str.indexOf('x');
if (firstX < str.length() - 1)
return str.charAt(firstX + 1) == 'x';
return false;
}

5. stringBits:

public String stringBits(String str) {


String result = "";
for (int i = 0; i < str.length(); i++) {
if (i % 2 == 0)
result += str.charAt(i);
}
return result;
}
6. stringSplosion:

public String stringSplosion(String str) {


String result = "";
for (int i = 0; i <= str.length(); i++) {
result += str.substring(0, i);
}
return result;
}

7. last2:

public int last2(String str) {


int count = 0;
for (int i = 0; i < str.length() - 2; i++) {
if (str.substring(i, i + 2).equals(
str.substring(str.length() - 2)))
count++;
}
return count;
}

8. arrayCount9:

public int arrayCount9(int[] nums) {


int result = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 9)
result++;
}
return result;
}

9. arrayFront9:

public boolean arrayFront9(int[] nums) {


int check = (nums.length < 4) ? nums.length : 4;
for (int i = 0; i < check; i++) {
if (nums[i] == 9) return true;
}
return false;
}

10. array123:

public boolean array123(int[] nums) {


for (int i = 0; i < nums.length - 2; i++)
if (nums[i] == 1 && nums[i + 1] == 2 && nums[i + 2] == 3)
return true;
return false;
}

11. stringMatch:

public int stringMatch(String a, String b) {


int count = 0;
int minLength = Math.min(a.length(), b.length());
for (int i = 0; i < minLength - 1; i++) {
if (a.substring(i, i + 2).equals(b.substring(i, i + 2)))
count++;
}
return count;
}

12. stringX:

public String stringX(String str) {


String result = "";
if (str.length() <= 2) return str;

result += str.charAt(0);
for (int i = 1; i < str.length() - 1; i++) {
if (str.charAt(i) != 'x') result += str.charAt(i);
}
result += str.charAt(str.length() - 1);
return result;
}

You might also like