***********IN JAVA************* The String class is provided in the Java library
ID: 3872259 • Letter: #
Question
***********IN JAVA*************
The String class is provided in the Java library. Provide your own implementation for the following methods (name the new class MyString):
**DO NOT USE THE STRING OBJECT AND ITS METHODS. IMPLEMENT THEM ALL AGAIN YOURSELF!
**You can use char[] and Character object.
public MyString(char[] chars);
public char charAt(int index);
public int length();
public MyString substring(int begin, int end);
public MyString toLowerCase();
public boolean equals(MyString s);
public static MyString valueOf(int i);
public char[] toChars();
*******Use the following class driver with no modifications to it!********
import java.util.*;
import java.lang.*;
import java.io.*;
class DriverMain{
public static void main(String[] args) {
MyString s1 = new MyString(new char[] {'A', 'B', 'C'});
System.out.println(s1.length());
System.out.println(s1.charAt(1));
MyString s2 = s1.substring(0,2);
System.out.println(s2.toLowerCase().equals(new MyString(new char[] {'a', 'b'})));
char[] chars = MyString.valueOf(345).toChars();
for (int i = 0; i < chars.length; i++) {
System.out.print(chars[i]);
}
}
}
***********************************************************************************
---And use the following class that was refered to in main----
class MyString {
public MyString(char[] chars){
}
public char charAt(int index){
}
public int length(){
}
public MyString substring(int begin, int end){
}
public MyString toLowerCase(){
}
public boolean equals(MyString s){
}
public static MyString valueOf(int i){
}
public char[] toChars(){
}
}
***********************************************************************************
Leaving comments in the code would be much appreciated to help me understand the code more!
Explanation / Answer
public class MyString{
static char[] c;
static int length;
public MyString(char[] chars){
length=chars.length;
c = new char[length];
for(int i=0;i<length;i++){
c[i]=chars[i];
}
}
public char charAt(int index){
return c[index];
}
public int length(){
return length;
}
public MyString substring(int begin, int end){
char[] c1=new char[end-begin];
for(int i=0;i<(end-begin);i++){
c1[i]=c[i+begin];
}
MyString m = new MyString(c1);
return m;
}
public MyString toLowerCase(){
for(int i=0;i<length;i++){
c[i]=Character.toLowerCase(c[i]);
}
return this;
}
public boolean equals(MyString s){
if (length!=s.length)return false;
for(int i=0;i<length;i++){
if(c[i]!=s.charAt(i))return false;
}
return true;
}
public static MyString valueOf(int i){
int remainder[]=new remainder[100];
int index=0;
length=0;
while(i>0){
remainder[index]=i%10;
c[index]=Character.forDigit(remainder[index],10);
i=i/10;
length++;
index++;
}
//Reverse the array
for(int i=0;i<length/2;i++){
char ch=c[i];
c[i]=c[length-1-i];
c[length-1-i]=ch;
}
return new MyString(c);
}
public char[] toChars(){
return c;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.