Results 1 to 4 of 4
  1. #1
    sourlemons is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default Need HW help on java homework

    Hello,

    I am a bit new to Java and need help on a homework problem.to sart off, I am not sure what the problem is asking when it says "// FIX ME if you see the following string is not on the same line" in the first fix me, and I am not sure what to add inside the while (true) {}

    thanks

    Below is the homework problem

    Java Code:
    import java.io.*;
    import java.util.Arrays;
    /**
     *
     * @author EDUCPHA
     */
    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
    
    
    
        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[]
        }
    
        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;
        }
    }
    Last edited by sourlemons; 10-16-2010 at 03:27 AM.

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    please use code tags, I am not, and nobody else will read that code( unless they are a sadist :D)

    to use code tags use the # symbol or [code...] [/code...] and omit the ... in each i put them there so code tags would not actually show

  3. #3
    sourlemons is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default

    I am stuck on the method section of the problem, second code tag:

    Java Code:
    class MyStringBuffer {
        //TODO explain: why you would need these data members.
        private char[] chars; //character storage.
        private int length;   //number of characters used
    
    
    
        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;
        }
    Java Code:
     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[]
        }
    So basically, I have to construct a string from the charas data member. How do I do this? I mean how do I make a new String from char[]. I checked the api site, which didn't help.

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    String has a constructor to do it:

    Java Code:
    char[]myChars=new char[33];
    for(int i=0;i<myChars.length;i++){ my[i]='*';}
    
    // consructor takes char array		
    String mystr=new String(my);
    System.out.println(mystr);

Similar Threads

  1. Need help with my java homework.
    By nightrise420 in forum New To Java
    Replies: 6
    Last Post: 09-24-2010, 07:23 PM
  2. java homework help
    By jenniferrlie in forum New To Java
    Replies: 5
    Last Post: 09-22-2009, 08:12 PM
  3. Java homework please
    By Indulgence in forum New To Java
    Replies: 1
    Last Post: 11-03-2008, 02:48 AM
  4. LF: Homework help with Java
    By excurssion in forum New To Java
    Replies: 2
    Last Post: 10-17-2008, 06:00 AM
  5. Help with my java servlet homework
    By jellyfish888 in forum Java Servlet
    Replies: 2
    Last Post: 12-21-2007, 05:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •