Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Apply UML design on user defined class Write overloaded constructors of a class

ID: 3774212 • Letter: A

Question

Apply UML design on user defined class Write overloaded constructors of a class Write customer methods of the class (Other than get and set methods) Problem description: The String class is provided by java library. Write your own String class based on the following UML diagram. No built-in String, StringBuffer related classes are allowed. MyString -data: char[] +MyString(chars: char[]) +charAt(i: int): char +length(): int +substring(begin: int, end: int): MyString +toLowerCase(): MyString +equals(other: MyString): Boolean +valueOf(i: int): MyString All methods have the same functionality as the corresponding methods in String class. You shall provide a main function to test all methods in MyString class.

Explanation / Answer

ANS:

import java.util.Arrays;
class MyString
{
   private char[] data;
   MyString(){}
  
   MyString(char chars[]){
       data=new char[chars.length];
   for(int i=0;i<chars.length;i++)
       data[i]=chars[i];
   }
public char charAt(int i)
   {
       if(i>=data.length)
       {
           return '';
       }
       else
           return data[i];
   }
   public int length()
   {
       return data.length;
   }

public char[] substring(int begin,int end)
   {
       char[] newa=new char[data.length];
       for(int i=begin,j=0;i<=end;i++,j++)
newa[j]=data[i];
       return newa;
   }

public char[] toLowerCase(){
char[] newa=new char[data.length];
for(int i=0;i<data.length;i++)
   {
   newa[i]=Character.toLowerCase(data[i]);
   }
   return newa;
}
public boolean equals(char newa[]){
   return Arrays.equals(data,newa);
   }

public Character valueOf(int i){
   Character c=Character.valueOf((char)i);
   return c;
}
  
public static void main(String[] args)
   {
       char c[]={'a','b','c','d','e','f'};
       MyString p=new MyString(c);
System.out.println("Character At 3rd index: "+p.charAt(3));
       System.out.println("Lenth= "+p.length());
       char res[]=p.substring(2,4);
System.out.print("Substring= ");
       for(int i=0;i<res.length;i++)
           System.out.print(res[i]);
       System.out.println();
       char res1[]=p.toLowerCase();
       System.out.print("toLowerCase= ");
for(int i=0;i<res.length;i++)
           System.out.print(res1[i]);
       System.out.println();
      
       char b[]={'A','B','C','D','E','F'};
       System.out.println("equals()= "+p.equals(b));
System.out.println("ValueOf()= "+p.valueOf(65));
       }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote