I need a Java Oriented where there is a Driver class that reads a line of text f
ID: 3865008 • Letter: I
Question
I need a Java Oriented where there is a Driver class that reads a line of text from the user. Then test each of the methods you have written. Be sure to print a clear explanation of the test and the results. A MyString Class where the code is written for the driver to run through and test results.The fields should be the original String and an array of chars. Write a constructor method that accepts a String as a parameter and initializes the two fields.Write a method for each of the API String methods listed below. Do not use the String API except as noted below.
What I need in the MyString.java is a written method in the Java String the methods are not used I need them written. The only methods that are only allowed are:
From String Class, Scanner class, constructors, toCharArray() and to String.
The following methods must be implemented:
MyString(String str)
char charAt(int index)
int compareTo(MyString anotherMyString)
int compareToIgnoreCase(MyString anotherMyString)
MyString concat(MyString str)
boolean endsWith(MyString suffix)
boolean equals(MyString anotherMyString)
boolean equalsIgnoreCase(MyString anotherMyString)
int indexOf(char ch)
int indexOf(char ch, int fromIndex)
int lastIndexOf(char ch)
int length( )
MyString replace(char oldChar, char newChar)
boolean startsWithString(MyString prefix)
MyString substring(int beginIndex)
MyString substring(int beginIndex, int endIndex)
MyString toLowerCase( )
MyString toUpperCase( )
String toString( )
MyString replaceAll(MyString str, MyString replacement)
int indexOf(MyString str)
int indexOf(MyString str, int fromIndex)
boolean contains(MyString s)
Output for each test should look like the following:
Original string is: test
Result from call to toUpperCase: TEST
Explanation / Answer
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.*;
import java.io.*;
class MyString
{
private char [] charArray;
private int length;
public MyString ()
{}
public MyString (String s) //constructor method that accepts a String as a parameter and initializes the two fields.length an char Array
{
length = s.length();
charArray = new char [length];
for (int i = 0; i < length; i++)
charArray[i] = s.charAt(i);
}
public int length ()
{
return length;
}
public char charAt(int index)//charAt
{
if ((index < 0) || (index >= charArray.length))
throw new StringIndexOutOfBoundsException(index);
return charArray[index];
}
public int compareTo(MyString anotherMyString)
{
//handling different sized arrays consistant with String
if (this.length != anotherMyString.length)
return this.length - anotherMyString.length;
//the two MyStrings have the same length
for (int i = 0; i < length; i++)
if (this.charArray[i] != anotherMyString.charArray[i])
return this.charArray[i] - anotherMyString.charArray[i];
return 0;
}
public int compareToIgnoreCase(MyString anotherMyString)
{
return this.toLowerCase().compareTo(anotherMyString.toLowerCase());
}
public int indexOf (int ch)
{
return indexOf(ch, 0);
}
public int indexOf (int ch, int fromIndex)
{
//the handling of an out of bounds index is consistant with the String class
if (fromIndex < 0)
fromIndex = 0;
else if (fromIndex >= length)
return -1;
for (int i = fromIndex; i < length; i++)
if (charArray[i] == ch)
return i;
return -1;
}
public int indexOf (MyString str)
{
return indexOf (str, 0);
}
public int indexOf(MyString str, int fromIndex)
{
//the handling of an out of bounds index is consistant with the String class
if (fromIndex < 0)
fromIndex = 0;
else if (fromIndex >= length)
return -1;
for (int i = fromIndex; i < length; i++)
{
if (charArray[i] == str.charAt(0))
{
if (str.length() == 1)
return i;
int offset = 1;
while (offset < str.length() //tests for end of string
&&
((i + offset) < length) //tests end of character array
&&
((str.charAt (offset)) == (charArray[i + offset]))) //test still matching
{
if (offset == str.length() - 1)
return i;
offset++;
}
}
}
return -1;
}
//the next two work with a String parameter
public int indexOf (String str)
{
return indexOf (str, 0);
}
//notice the few changes I made here. Could I have reused the MyString version ? (see below)
public int indexOf(String str, int fromIndex)
{
if (fromIndex < 0)
fromIndex = 0;
else if (fromIndex >= length)
return -1;
for (int i = fromIndex; i < length; i++)
{
if (charArray[i] == str.charAt(0))
{
if (str.length() == 1)
return i;
int offset = 1;
while (offset < str.length() //tests for end of string
&&
((i + offset) < length) //tests end of character array
&&
((str.charAt (offset)) == (charArray[i + offset]))) //test still matching
{
if (offset == str.length() - 1)
return i;
offset++;
}
}
}
return -1;
}
public int lastIndexOf (int ch)
{
return lastIndexOf(ch, length - 1);
}
public int lastIndexOf (int ch, int fromIndex)
{
if (fromIndex >= length)
fromIndex = length -1;
//if (fromIndex < 0) -1 is returned
for (int i = fromIndex; i >= 0; i--)
if (charArray[i] == ch)
return i;
return -1;
}
public int lastIndexOf (String str)
{
return lastIndexOf(str, length - 1);
}
public int lastIndexOf(String str, int fromIndex)
{
if (fromIndex >= length)
fromIndex = length -1;
//if (fromIndex < 0) -1 is returned
for (int i = fromIndex ; i >= 0; i--)
{
if (charArray[i] == str.charAt(0))
{
if (str.length() == 1)
return i;
int offset = 1;
while (offset < str.length() //tests for end of string
&&
((i + offset) < length) //tests end of character array
&&
((str.charAt (offset)) == (charArray[i + offset]))) //test still matching
{
if (offset == str.length() - 1)
return i;
offset++;
}
}
}
return -1;
}
public boolean equalsIgnoreCase (MyString other)
{
if (this.length != other.length())
return false;
int i = 0;
char thisTemp,otherTemp;
while (i < this.length)
{
if (this.charArray[i] >= 'A' && this.charArray[i] <= 'Z')
thisTemp = (char)(this.charArray[i] - 'A' + 'a');
else
thisTemp = this.charArray[i];
if (other.charAt(i) >= 'A' && other.charAt(i) <= 'Z')
otherTemp = (char)(other.charAt(i) - 'A' + 'a');
else
otherTemp = other.charAt(i);
if (thisTemp != otherTemp)
return false;
i++;
}
return true;
}
public MyString toLowerCase ()
{
//note: we must return a new MyString object -- see how we create it
MyString newMyString = new MyString();
newMyString.length = this.length;
newMyString.charArray = new char [this.length];
for (int i = 0; i < this.length; i++)
{
if ((this.charArray[i] >= 'A') && (this.charArray[i] <= 'Z'))
newMyString.charArray[i] = (char)(this.charArray[i] + 32);
else
newMyString.charArray[i] = this.charArray[i];
}
return newMyString;
}
public MyString toUpperCase ()
{
MyString newMyString = new MyString();
newMyString.length = this.length;
newMyString.charArray = new char [this.length];
for (int i = 0; i < this.length; i++)
{
if ((this.charArray[i] >= 'a') && (this.charArray[i] <= 'z'))
newMyString.charArray[i] = (char)(this.charArray[i] - 32);
else
newMyString.charArray[i] = this.charArray[i];
}
return newMyString;
}
public String toString ()
{
return new String(charArray);
}
public static void main(String[] args) {
System.out.println("Enter string ");
Scanner sc=new Scanner(System.in);
String st=sc.nextLine();
MyString str = new MyString(st);
System.out.println("Enter another string to compare");
String strw=sc.nextLine();
MyString strc = new MyString(strw);
System.out.println("CharAt :"+str.charAt(2));
System.out.println("compareTo -Returns 0 if true ,else negative integer:"+str.compareTo(strc));
System.out.println("compareTo -Returns integer 0 if true,else negative integer:"+str.compareToIgnoreCase(strc));
System.out.println("indexOf(char ch):"+str.indexOf('o'));
System.out.println("toLowerCase( ):"+str.toLowerCase( ));
System.out.println("toLowerCase( ):"+str.toUpperCase( ));
System.out.println("Equals( ):"+str.equals(strc ));
System.out.println("equalsIgnoreCase( ):"+str.equalsIgnoreCase(strc));
System.out.println("Length( ):"+str.length());
System.out.println("indexOf(char ch,int fromIndex):"+str.indexOf('h',0));
System.out.println("indexOf(char ch,int fromIndex,Returns index if the char is present fro the given index,else return -1):"+str.indexOf('e',0));
System.out.println("lastIndexOf"+str.lastIndexOf(0));
}
}
Output:
Enter string
HeLLo
Enter another string to compare
hello
CharAt :L
compareTo -Returns 0 if true ,else negative integer:-32
compareTo -Returns integer 0 if true,else negative integer:0
indexOf(char ch):4
toLowerCase( ):hello
toLowerCase( ):HELLO
Equals( ):false
equalsIgnoreCase( ):true
Length( ):5
indexOf(char ch,int fromIndex):-1
indexOf(char ch,int fromIndex,Returns index if the char is present fro the given index,else return -1):1
lastIndexOf-1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.