views:

46

answers:

1
 package homework5;


import java.io.*;
import java.util.Arrays;
public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException {
    MyStringBuffer strTest = new MyStringBuffer();
    // FIX ME if you see the following string is not on the same line
    System.out.println("stringTest is initilized - capacity=" + strTest.capacity() + " length=" + strTest.length());
    BufferedReader stdin = new BufferedReader(
                         new InputStreamReader( System.in ) );
    System.out.print("Enter a string:");
    String myString = stdin.readLine();
    strTest.append(myString); //TOTEST: test your append (String str)
    printStrTest(strTest);
    while (true) {
        // FIX ME if you see the following string is not on the same line
        System.out.println("Enter 1 of 4 options: ac (append a char), as (append a string), i (insert), d (delete), r (reverse), q (quit)");
        String opt = stdin.readLine();
        if (opt.equals("ac")) {
            System.out.print("Append a char:");
            char c = stdin.readLine().charAt(0);
            strTest.append(c); //TOTEST: test your append (char a) function
            printStrTest(strTest);
        } else if (opt.equals("as")) {
            System.out.print("Append a string:");
            String aStr = stdin.readLine();
            strTest.append(aStr); //TOTEST: test append with expandation
            printStrTest(strTest);
        } else if (opt.equals("i")) {
            System.out.print("Insert a char:");
            char c = stdin.readLine().charAt(0);
            System.out.print("Location:");
            int loc = Integer.parseInt(stdin.readLine());
            strTest.insert(loc, c); //TOTEST: test your insert
            printStrTest(strTest);
        } else if (opt.equals("d")) {
            System.out.print("Delete at location:"); // TOTEST delete
            int loc = Integer.parseInt(stdin.readLine());
            strTest.deleteCharAt(loc);
            printStrTest(strTest);
        } else if (opt.equals("r")) {
            strTest.reverse(); //TOTEST reverse
            printStrTest(strTest);
        } else if (opt.equals("q")) {
            System.out.println("Goodbye!!!");
            break;
        } else {
            System.out.println("Error option entered:" + opt);
        }
    }
}

static void printStrTest(MyStringBuffer strTest){
    // FIX ME if you see the following string is not on the same line
    System.out.println("New string:" + strTest.toString()+ ",cap=" + strTest.capacity() + " len=" + strTest.length());
}
}

class MyStringBuffer {
//TODO explain: why you would need these data members.
private char[] chars; //character storage. 
private int length;   //number of characters used.  efficient

public MyStringBuffer(){
    chars = new char[16]; //Default storage is 16
    length  = 0; // No char
}

public int capacity(){
    return chars.length;
}

//Expanse the capcity of the chars storage
void expandCapacity(int minimumCapacity) {
int newCapacity = (chars.length + 1) * 2;
    if (newCapacity < 0) {
        newCapacity = Integer.MAX_VALUE;
    } else if (minimumCapacity > newCapacity) {
    newCapacity = minimumCapacity;
}
    chars = Arrays.copyOf(chars, newCapacity);
}

public int length(){
    return length;
}

public String toString(){
    //TODO

    //Hint: just construct a new String from the ‘chars’ data member
    //and return this new String – See API online for how create
    //new String from char[]

String result = new String(chars, 0, length);
return result;
}

public MyStringBuffer append (char c){

    //TODO
    //You will need to call the expandCapacity if necessary

    return this;
}

public MyStringBuffer append (String str){
    //TODO
    //You will need to call the expandCapacity if necessary

    return this;
}

public MyStringBuffer insert (int offset, char c){
    //TODO
    //You will need to call the expandCapacity if necessary

    return this;
}

public MyStringBuffer deleteCharAt(int index) {
    //TODO

    return this;
}

public MyStringBuffer reverse (){
    //TODO

    return this;
}
}

Hi, I read this http://www.roseindia.net/java/beginners/StringBuffer.shtml, and I understand how to append things in those situations, but I don't know how to apply it in this one. What am I suppose to reference? I think I am suppose to do something like this: strbuf.append("Hello"); but what do I put instead of strbuf? I tried a few difference references but they keep saying variable not found. Can someone show me? I'm pretty sure I can do the rest. Or at least I hope I can. I thought it was myString but that didn't work.

Thanks :)

A: 

In this homework the goal isn't to use a StringBuffer but instead create your own based on a char[].

For example with append(char c), you're supposed to write a code which will add the given char at the end of the array chars and increment the length (because you just added a char).

This assignment is about creating your own implementation of StringBuffer and understand how it work instead of using it directly.

Colin Hebert
@Colin Hebert I'm sorry but did I misunderstood this?You will implement MyStringBuffer class which is a simplified version of StringBuffer class (see Lecture note: cis36L052). The framework is already provided. You will need to implement 6 functions: append (char c), append (String str), insert (int offset, char c), deleteCharAt(int index), reverse(), and toString().You have to debug, fix errors if you can not build the program.
CuriousStudent
The framework is provided for you. You need to copy the following code into your new program. Whenever you see //FIX ME or //TODO comments, you will need to fix the code or add your new codes. You have to debug, fix errors if you can not build the program.
CuriousStudent
@CuriousStudent, It says that you'll **implement** a simplified version of `StringBuffer`, not use `StringBuffer`.
Colin Hebert
oh wow, damn so basically all i have to do is this: http://www.roseindia.net/java/beginners/StringBuffer.shtml ?
CuriousStudent
@CuriousStudent, well, this is again a class which uses `StringBuffer`. You don't have to (in fact you have to not) use the class `StringBuffer` but create one of your own
Colin Hebert
@colin Hebert Yeah you were right. He extended this homework till next week since most people in the class couldn't do it. I understand now, I am suppose to write a program that will do the action required. My question is, do I have to make a "new" for each and every one? something like StringBuffer str1 = new StringBuffer(); for every function.
CuriousStudent
You're not supposed to use `StringBuffer` at all
Colin Hebert