Question
please answer question
(5 points) Below is the skeleton for a simplified StringBuilder class as done in the Closed Lab this semester. As in the lab, the class has an ArrayList of Character to hold the individual characters of the StringBuilder object. Write the instance method indexOf as given below that returns the index of the first occurrence of the character c in the SimplestringBuilder. If there is no index for that character (i.e. the character is not in the String represented by the SimplestringBuilder 4. object) then the method must return -1. public class SimpleStringBuilder ( * A private member variable used to hold the internal state of the * SimplestringBuilder. This ArrayList holds the characters of the * String that will be built. So the String Hello would be represented by an ArrayList containing the characters private ArrayList<Character> list: // Rest of class omitted * Acts exactly like the indexOf method for String. Returns the * index of the first occurrence of the character c in the * SimpleStringBuilder representation public int indexOf (char c)
0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.ArrayList;

class SimpleStringBuilder{
    private ArrayList<Character> list;

    SimpleStringBuilder(){
        list = new ArrayList<>();
    }

    public int indexOf(char c){
        int ans = 0;
        for(int i=0;i<list.size();i++){
            if(list.get(i)==c){
                return i;
            }
        }
        return -1;
    }

    public void add(char c){
        list.add(c);
    }

    @Override
    public String toString(){
        return list.toString();
    }
}

class Tester{
    public static void main(String[] args) {
        SimpleStringBuilder s = new SimpleStringBuilder();
        s.add('H');
        s.add('e');
        s.add('l');
        s.add('l');
        s.add('o');
        System.out.println(s);
        System.out.println("Index of 'l' : "+s.indexOf('l'));
        System.out.println("Index of 'k' : "+s.indexOf('k'));
    }
}

OUTPUT :

Tester C: IProgram Files lJata Index of l : 2 Index of k :-1 Process finished with exit code 0

Add a comment
Know the answer?
Add Answer to:
please answer question (5 points) Below is the skeleton for a simplified StringBuilder class as done...
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
  • Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file...

    Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file with the appropriate name. public class BasicJava4 { Add four methods to the class that return a default value. public static boolean isAlphabetic(char aChar) public static int round(double num) public static boolean useSameChars(String str1, String str2) public static int reverse(int num) Implement the methods. public static boolean isAlphabetic(char aChar): Returns true if the argument is an alphabetic character, return false otherwise. Do NOT use...

  • Java StringNode Case Study: Rewrite the following methods in the StringNode class shown below. Leave all...

    Java StringNode Case Study: Rewrite the following methods in the StringNode class shown below. Leave all others intact and follow similar guidelines. The methods that need to be changed are in the code below. - Rewrite the indexOf() method. Remove the existing recursive implementation of the method, and replace it with one that uses iteration instead. - Rewrite the isPrefix() method so that it uses iteration. Remove the existing recursive implementation of the method, and replace it with one that...

  • The following is for java programming. the classes money date and array list are so I are are pre...

    The following is for java programming. the classes money date and array list are so I are are pre made to help with the coding so you can resuse them where applicable Question 3. (10 marks) Here are three incomplete Java classes that model students, staff, and faculty members at a university class Student [ private String lastName; private String firstName; private Address address; private String degreeProgram; private IDNumber studentNumber; // Constructors and methods omitted. class Staff private String lastName;...

  • DIRECTIONS FOR THE WHOLE PROJECT BELOW BigInt class The purpose of the BigInt class is to...

    DIRECTIONS FOR THE WHOLE PROJECT BELOW BigInt class The purpose of the BigInt class is to solve the problem using short methods that work together to solve the operations of add, subtract multiply and divide.   A constructor can call a method called setSignAndRemoveItIfItIsThere(). It receives the string that was sent to the constructor and sets a boolean variable positive to true or false and then returns a string without the sign that can then be processed by the constructor to...

  • Objectives Problem solving using arrays and ArrayLists. Abstraction. Overview The diagram below illustrates a banner constructed...

    Objectives Problem solving using arrays and ArrayLists. Abstraction. Overview The diagram below illustrates a banner constructed from block-letters of size 7. Each block-letter is composed of 7 horizontal line-segments of width 7 (spaces included): SOLID                        as in block-letters F, I, U, M, A, S and the blurb RThere are six distinct line-segment types: TRIPLE                      as in block-letter M DOUBLE                   as in block-letters U, M, A LEFT_DOT                as in block-letters F, S CENTER_DOT          as in block-letter...

  • Hi I really need help with the methods for this lab for a computer science class....

    Hi I really need help with the methods for this lab for a computer science class. Thanks Main (WordTester - the application) is complete and only requires uncommenting to test the Word and Words classes as they are completed. The needed imports, class headings, method headings, and block comments are provided for the remaining classes. Javadocs are also provided for the Word and Words classes. Word The Word class represents a single word. It is immutable. You have been provided...

  • language is java Restrictions: You are not allowed to use anything from the String, StringBuilder, or...

    language is java Restrictions: You are not allowed to use anything from the String, StringBuilder, or Wrapper classes. In general, you may not use anything from any other Java classes, unless otherwise specified. You are not allowed to use String literals in your code ("this is a string literal"). You are not allowed to use String objects in your code. The methods must be implemented by manipulating the data field array, The CSString Class: NOTE: Pay very careful attention to...

  • Using loops with the String and Character classes. You can also use the StringBuilder class to...

    Using loops with the String and Character classes. You can also use the StringBuilder class to concatenate all the error messages. Floating point literals can be expressed as digits with one decimal point or using scientific notation. 1 The only valid characters are digits (0 through 9) At most one decimal point. At most one occurrence of the letter 'E' At most two positive or negative signs. examples of valid expressions: 3.14159 -2.54 2.453E3 I 66.3E-5 Write a class definition...

  • 1. Given in part the str class interface below, implement the overloaded assignment operator class str...

    1. Given in part the str class interface below, implement the overloaded assignment operator class str public: str& operator (str &rhs); // Postcondition: contents of rhs (right-hand-side object) İs assigned to the calling object without memory leak private: int length; // # of characters of a C-string not including the NULL character "O' pointed to by p char *p; points to dynamic memory used to store sting of characters 1;

  • ISu a The paradox d I ata fields (or mputers. Program 3 Assume the existence of a class called Vo...

    ISu a The paradox d I ata fields (or mputers. Program 3 Assume the existence of a class called Voter which defines two instance variables: Add 10 user to the ArrayList. private String name; rivate char party: 'D'for Democrat or 'R' for Republican Assume that this class implements these two methods: public char getParty() public String tostring) // name and party affiliation Now assume you're in the main method in another class called VoterDriver. Write a Java statement that instantiates...

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