Need some help practicing with basic array methods in Java. Given... public clas
ID: 3595843 • Letter: N
Question
Need some help practicing with basic array methods in Java.
Given...
public class Test {
private char[] characters;
public StringExample() {
characters = new char[5];
characters[0] = 'L';
characters[1] = 'u';
characters[2] = 'c';
characters[3] = 'k';
characters[4] = 'y';
}
public StringExample(char[] example) {
characters = new char[example.length];
for(int index = 0; index < example.length; index++) {
characters[index] = example[index];
}
}
public StringExample(String example) {
characters = new char[example.length()];
for(int index = 0; index < example.length(); index++) {
characters[index] = example.charAt(index);
}
}
}
- Write a method that converts this array into a normal string. (create an empty string then concatenate all of the characters of the array into it. then return that string)
- Write a method that concatenates the StringExample with another StringExample. (create a new array of characters thats big enough to hold both strings, copy all characters from them into it, use the TestExample constructor that takes an array parameter and returns the new object.)
- Write a method that returns a copy of the TestExample with all copies of one character replaced by another. Have it take two parameters(two chars, one for the old and one for the new) and have it return a copy of this with all copies of old chars replaced by new chars (essentially, Create a new array that copies characters into it, then create a new TestExample instance using that array)
- Write a method that returns part of this TestExample, from beginIndex up to but not including endIndex. It should have two int parameters for best the beginIndex and endIndex. It should return a string of characters from beginIndex up to but not including endIndex. (assume that endIndex will not be larger then length and that beginIndex is not less than 0 and also endIndex is not less than beginIndex)
-Write one last method that returns a copy of this in which lowercase characters have been replaced by uppercase ones.
Explanation / Answer
Please find my implementation.
public class StringExample {
private char[] characters;
public StringExample() {
characters = new char[5];
characters[0] = 'L';
characters[1] = 'u';
characters[2] = 'c';
characters[3] = 'k';
characters[4] = 'y';
}
public StringExample(char[] example) {
characters = new char[example.length];
for(int index = 0; index < example.length; index++) {
characters[index] = example[index];
}
}
public StringExample(String example) {
characters = new char[example.length()];
for(int index = 0; index < example.length(); index++) {
characters[index] = example.charAt(index);
}
}
public String getString() {
String str = "";
for(int i=0; i<characters.length; i++)
str = str + characters[i];
return str;
}
public void concatenate(StringExample obj) {
char newArr[] = new char[characters.length + obj.characters.length];
int k = 0;
for(int i=0; i<characters.length; i++)
newArr[k++] = characters[i];
for(int i=0; i<obj.characters.length; i++)
newArr[k++] = obj.characters[i];
characters = newArr;
}
public void replace(char old, char newChar) {
for(int i=0; i<characters.length; i++)
if(characters[i] == old)
characters[i] = newChar;
}
public String getParts(int beginIndex, int endIndex) {
String str = "";
for(int i=beginIndex; i<endIndex; i++)
str = str + characters[i];
return str;
}
public String replaceLowerToUpper() {
String str = "";
for(int i=0; i<characters.length; i++) {
if(Character.isLowerCase(characters[i]))
str = str + Character.toUpperCase(characters[i]);
else
str = str + characters[i];
}
return str;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.