Question

Restrictions: You are not allowed to use anything from the String, StringBuilder, or Wrapper classes. In general, you may notThis method shall take a CSString object as input, This method shall return true if the source string ends with the exact cha

language is java

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

//class CSString
class CSString
{
   //data fields
   char[] text;
  
   //constructor
   public CSString(char[] text)
   {
       this.text = new char[text.length];
       System.arraycopy(text, 0, this.text, 0, text.length);
   }
  
   //method to return a character at index
   public char charAt(int index)
   {
       if(index<0 || index>text.length-1)
       {
           System.out.println ("Invalid index");
           return '\u0000';
}
       return text[index];
   }
  
   //method to concat two CSString objects
   public CSString concat(CSString other)
   {
       char s[] = new char[text.length + other.length()];
       int i;
       for(i=0; i<text.length; i++)
       {
           s[i] = text[i];
       }
       for(int j=0; j<other.text.length; j++, i++)
       {
           s[i] = other.text[j];
       }
       CSString string = new CSString(s);
       return string;
   }
  
   //method checks the CSString object ended with the suffix
   public boolean endsWith(CSString suffix)
   {
       int n = text.length - suffix.text.length;
       for(int i=0; i<suffix.text.length; i++)
       {
           if(text[n+i]!=suffix.text[i])
               return false;
       }
       return true;
   }
  
   //method checks two CSString objects are equal or not
   //if equal return true otherwise false
   public boolean equals(CSString other)
   {
       for(int i=0; i<text.length; i++)
       {
           if(text[i]!=other.text[i])
               return false;
       }
       return true;
   }
  
   //method checks two CSString objects are equal or not ignoring case
   //if equal return true otherwise false
   public boolean equalsIgnoreCase(CSString other)
   {
       for(int i=0; i<text.length; i++)
       {
           char c1 = Character.toLowerCase(text[i]);
           char c2 = Character.toLowerCase(other.text[i]);
          
           if(c1!=c2)
               return false;
       }
       return true;
   }
  
   //method to return the length of the CSString
   public int length()
   {
       return text.length;
   }
  
   //method to create a substring of CSString object from begin
   public CSString substring(int begin)
   {
       if(begin<0 || begin>text.length-1)
       {
           System.out.println ("Invalid index");
           return null;
       }
          
       char s[] = new char[text.length - begin];
       for(int i=0; i<text.length - begin; i++)
       {
           s[i] = text[begin+i];
       }
      
       CSString string = new CSString(s);
       return string;
   }
  
   //method to create a substring of CSString object from begin to end-1
   public CSString substring(int begin, int end)
   {
       if(begin<0 || begin>text.length-1 || end<0 || end>text.length-1)
       {
           System.out.println ("Invalid index");
           return null;
       }
          
       char s[] = new char[end - begin];
       for(int i=0; i<end-begin; i++)
       {
           s[i] = text[begin+i];
       }
      
       CSString string = new CSString(s);
       return string;
   }
  
   //method to create a new CSString object with swapping cases
   public CSString swapCase()
   {
       CSString string = new CSString(text);
      
       for(int i=0; i<text.length; i++)
       {
           if(Character.isLowerCase(text[i]))
               string.text[i] = Character.toUpperCase(text[i]);
           else
               string.text[i] = Character.toLowerCase(text[i]);
       }
      
       return string;
   }
  
   //method to create a new CSString object with title cases
   public CSString titleCase()
   {
       CSString string = new CSString(text);
      
       string.text[0] = Character.toUpperCase(text[0]);
       for(int i=1; i<text.length; i++)
       {
           if(text[i-1]==' ' && text[i]!=' ')
               string.text[i] = Character.toUpperCase(text[i]);
       }
       return string;
   }
  
   //method to create a new CSString object with lower cases
   public CSString toLowerCase()
   {
       CSString string = new CSString(text);
      
       for(int i=0; i<text.length; i++)
       {
           string.text[i] = Character.toLowerCase(text[i]);
       }
      
       return string;
   }
  
   //method to create a new CSString object with upper cases
   public CSString toUpperCase()
   {
       CSString string = new CSString(text);

       for(int i=0; i<text.length; i++)
       {
           string.text[i] = Character.toUpperCase(text[i]);
       }
       return string;
   }
  
   //method to convert CSString object to String object and return
   public String toString()
   {
       String string = "";
      
       for(int i=0; i<text.length; i++)
       {
           string = string + text[i];
       }
       return string;
   }
}

//Demo class to testing
class Demo
{
   //main method
   public static void main (String[] args)
   {
       char s[] = {'a','b','c','d','e', 'f','g'};
       char s2[] = {'p','q','r','s','t'};
      
       //create two objects of CSString class
       CSString string = new CSString(s);
       CSString string2 = new CSString(s2);
      
       /******* Testing *******/
       System.out.println ("Length = " + string.length());
      
       System.out.println ("Char at 3: " + string.charAt(3));
      
       System.out.println ("The string is: "+ string);
      
       System.out.println ("Touppercase: " + string.toUpperCase());
      
       System.out.println ("Touppercase: " + string.toLowerCase());
      
       System.out.println ("Titlecase: " + string.titleCase());
      
       System.out.println ("Substring from 3: " + string.substring(3));
      
       System.out.println ("Substring from 3 to 6: " + string.substring(3, 6));
      
       System.out.println ("Concat of two string: " + string.concat(string2));
      
       System.out.println ("Is equal? " + string.equals(string2));
      
       System.out.println ("Is equal? " + string.equalsIgnoreCase(string2));
      
       System.out.println ("EndsWith? " + string.endsWith(string2));
   }
}

Output:

Length = 7
Char at 3: d
The string is: abcdefg
Touppercase: ABCDEFG
Touppercase: abcdefg
Titlecase: Abcdefg
Substring from 3: defg
Substring from 3 to 6: def
Concat of two string: abcdefgpqrst
Is equal? false
Is equal? false
EndsWith? false

N.B. Whether you face any problem then share with me in the comment section, I'll happy to help you. I expect positive feedback from your end.

Add a comment
Know the answer?
Add Answer to:
language is java Restrictions: You are not allowed to use anything from the String, StringBuilder, or...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • USING JAVA Consider the following methods: StringBuilder has a method append(). If we run: StringBuilder s...

    USING JAVA Consider the following methods: StringBuilder has a method append(). If we run: StringBuilder s = new StringBuilder(); s.append("abc"); The text in the StringBuilder is now "abc" Character has static methods toUpperCase() and to LowerCase(), which convert characters to upper or lower case. If we run Character x = Character.toUpperCase('c');, x is 'C'. Character also has a static isAlphabetic() method, which returns true if a character is an alphabetic character, otherwise returns false. You will also need String's charAt()...

  • Prompt the user to enter two character strings. The program will then create a new string...

    Prompt the user to enter two character strings. The program will then create a new string which has the string that appears second, alphabetically, appended to the end of the string that shows up first alphabetically. Use a dash (-') to separate the two strings. You must implement the function string appendStrings(string, string), where the return value is the combined string. Display the newly created string. Your program must be able to handle both uppercase and lowercase characters. You are...

  • Write Java Programs to perform the following: 1. To create a new String object 2. To...

    Write Java Programs to perform the following: 1. To create a new String object 2. To find the length of the string 3. Concatenation operator 4. To display the content in String array 5. A complete Java Program that incorporates the following String methods taught in class a. toUpperCase()) b. to LowerCase()) C. replace('0','A')) d. s1.length() e. compareTo(s1)) methods taught in class a. toUpperCase()) b. to LowerCase()) C. replace('0','A')) d. s1.length()) e. compareTo(s1)) f. 51.substring(4)) g. s2.substring(2,10)) h. s1.indexOf('g')) i....

  • Programming project in Java: You are allowed to use the following methods from the Java API:...

    Programming project in Java: You are allowed to use the following methods from the Java API: class String length charAt class StringBuilder length charAt append toString class Character any method Create a class called HW2 that contains the following methods: 1. isAlphabeticalOrder takes a String as input and returns a boolean: The method returns true if all the letters of the input string are in alphabetical order, regardless of case. The method returns false otherwise. Do not use arrays to...

  • 10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string...

    10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string object arguments. Let's call them string1, string2, and string3. It should search string for all occurrences of string2. When it finds an occurrence of Programming Challenges string2, it should replace it with string. For example, suppose the three arguments have the following values: stringt: "the dog jumped over the fence" string 2 "the" string3: "that" With these three arguments, the function would return a...

  • package week_3; /** Write a method called countUppercase that takes a String array argument. You can...

    package week_3; /** Write a method called countUppercase that takes a String array argument. You can assume that every element in the array is a one-letter String, for example String[] test = { "a", "B", "c", "D", "e"}; This method will count the number of uppercase letters from the set A through Z in the array, and return that number. So for the example array above, your method will return 2. You will need to use some Java library methods....

  • TASK Your task is to build a palindrome from an input string. A palindrome is a...

    TASK Your task is to build a palindrome from an input string. A palindrome is a word that reads the same backward or forward. Your code will take the first 5 characters of the user input, and create a 9- character palindrome from it. Words shorter than 5 characters will result in a runtime error when you run your code. This is acceptable for this exercise – we will cover input validation in a later class. Some examples of input...

  • In the first task, you will write a Java program that accepts a string of the...

    In the first task, you will write a Java program that accepts a string of the format specified next and calculate the answer based on the user input. The input string should be of the format dddxxdddxx*, where d represents a digit and x represents any character and asterisk at the end represents that the string can have any number of characters at the end. 1. Prompt the user to enter a string with the specific format (dddxxdddxx*) 2. Read...

  • Given java code is below, please use it! import java.util.Scanner; public class LA2a {      ...

    Given java code is below, please use it! import java.util.Scanner; public class LA2a {       /**    * Number of digits in a valid value sequence    */    public static final int SEQ_DIGITS = 10;       /**    * Error for an invalid sequence    * (not correct number of characters    * or not made only of digits)    */    public static final String ERR_SEQ = "Invalid sequence";       /**    * Error for...

  • ​​​​​​public static int countCharacter(String str, char c) { // This recursive method takes a String and...

    ​​​​​​public static int countCharacter(String str, char c) { // This recursive method takes a String and a char as parameters and // returns the number of times the char appears in the String. You may // use the function charAt(int i) described below to test if a single // character of the input String matches the input char. // For example, countCharacter(“bobbie”, ‘b’) would return back 3, while // countCharacter(“xyzzy”, ‘y’) would return back 2. // Must be a RECURSIVE...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT