Java Program easy points! Most of code written A split method also needs to be a
ID: 641866 • Letter: J
Question
Java Program easy points! Most of code written
A split method also needs to be added of the form:
split( class delimeter, mystring s)
HERE IS THE CODE I ALREADY HAVE AND IS CORRECT JUST METHODS NEED TO BE ADDED
package mystring;
public class MyString {
//Character array to hold string
private char[] chars;
//myString constructor
public MyString(char[] chars) {
this.chars = new char[chars.length];
System.arraycopy(chars, 0, this.chars, 0, chars.length);
}
//Return the string
public char[] toChars() {
return chars;
}
//Return the string length
public int length() {
return chars.length;
}
//Return character at index
public char charAt(int index) {
return chars.charAt();
}
//Return substring
public MyString substring(int begin, int end) {
return myString.substring(start, Math.min(start + length, myString.length());
return new MyString(sub);
}
//Return string in lower case
public MyString toLowerCase() {
return new MyString(lower);
}
}
package mystring;
public class TestString {
public static void main(String[] args) {
MyString s = new MyString(new char[] {'T', 'e', 's', 't'});
System.out.println(s.toChars());
System.out.println("Test length is " + s.length());
System.out.println("The character at position 2 is " + s.charAt(2));
System.out.println("Test lower case method:");
MyString t = s.toLowerCase();
System.out.println(t.toChars());
System.out.println("Test substring method:");
MyString u = t.substring(2, 4);
System.out.println(u.toChars());
}
}
CODE NEEDS TO BE FULL AND COMPLETE FOR FULL POINTS PLEASE!
Explanation / Answer
package mystring;
public class MyString
{
//Character array to hold string
private char[] chars;
//myString constructor
public MyString(char[] chars)
{
this.chars = new char[chars.length];
System.arraycopy(chars, 0, this.chars, 0, chars.length);
}
//Return the string
public char[] toChars()
{
return chars;
}
//Return the string length
public int length()
{
return chars.length;
}
//Return character at index
public char charAt(int index)
{
//Get character at index
return chars[index-1];
}
//Return substring
public MyString substring(int begin, int end)
{
//Convert the character array to string
String str=String.valueOf(chars);
//Get substring from str
String substring=str.substring(begin, end);
//convert the str back to character array
char[] charArray=substring.toCharArray();
return new MyString(charArray);
}
//Return string in lower case
public MyString toLowerCase()
{
//Convert the character array to string
String tempString=String.valueOf(chars);
//Call toLowerCase on tempString
String lowerCase=tempString.toLowerCase();
//convert the str back to character array
char[] lowerCaseArray=lowerCase.toCharArray();
return new MyString(lowerCaseArray);
}
}
---------------------------------------------------------------------------------------------------------
package mystring;
public class TestString {
public static void main(String[] args) {
MyString s = new MyString(new char[] {'T', 'e', 's', 't'});
System.out.println(s.toChars());
System.out.println("Test length is " + s.length());
System.out.println("The character at position 2 is " + s.charAt(2));
System.out.println("Test lower case method:");
MyString t = s.toLowerCase();
System.out.println(t.toChars());
System.out.println("Test substring method:");
MyString u = t.substring(2, 4);
System.out.println(u.toChars());
}
}
---------------------------------------------------------------------------------------------------------
Sample run:
Test
Test length is 4
The character at position 2 is e
Test lower case method:
test
Test substring method:
st
Hope this helps you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.