Help with console program with 3 classes...
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Help with console program with 3 classes...

Hybrid View

  1. #1
    Join Date
    May 2012
    Posts
    2

    Unhappy Help with console program with 3 classes...

    Hello everyone,

    I've been stuck on this homework assignment for a Java class I am taking and I don't even know where to begin. I want to learn it, not to have someone figure it out for me. I guess really, I just need the assignment dumbed down to where I can understand. I am newer to java and "supposedly" this is a intro level java class.

    The assignment is :
    Programming Assignment 3


    Write a program that consists of three classes. The first class will be the actual program.

    The second class will simply convert a string to lower case.

    The third class will have three methods:

    public static String trimmed(String str)

    and

    public static String trimmed(String str, int len)

    and

    public static String squeeze(String str)

    The 1st trimmed method will return str without leading or trailing whitespace and will return an empty string if str is null.

    The 2nd trimmed method will return the first len characters after trimming the string. The 2nd method must make use of the 1st method.

    The squeeze method will replace all sequences of 2 spaces with 1 space.

    The program will read a file containing the data below (cut and paste it into notepad and save it), and will display each line:

    as entered but trimmed and squeezed.
    as entered but trimmed, squeezed, and shortened to 10 characters
    as entered but trimmed, squeezed, converted to lower case, and shortened to 20 characters.

    Data (copy after this line down to (not including) the line that says end data, you will have 5 lines):
    This is a test, this is only a test!

    This test is a test of your Java programming skills!
    xyz
    ABCDEFGHIJKLMONPQRSTUVWXYZ1234567890-=_+ !
    end data

    Grading Notes:

    You can do this program however you like (console, GUI, AWT, Swing, Applet, etc.).
    If you use an inner classes for the second and third classes, you will receive up to 5 extra points, and if the third class is anonymous, you will receive up to five additional points.
    If you make the program graphical, you will receive up to 10 extra points (total won't exceed 100).
    You will lose points for poor formatting, poor commenting, not following instructions, program that don't work, etc.
    In any case, your total cannot exceed 100 points.
    Since this is the 3rd assignment, you are expected to get this right before you turn it in. It will be graded much more rigorously than the earlier assignments and in most cases, you won't get a second chance! If something is confusing in the instructions, ask before submitting!

    Now, I don't care for it to be fancy, so I'm working console.

    I drew out what I need on paper. I asked the professor for help, and I was even more confused.

    Basically the three methods i use to manip. a string should be passed a string and return the modified string.
    The printing should be don in the main loop.
    The loop should :
    Read a line of data, process the input string as described, and then output the modified string (either after each required modification) or, if I save the 3 modified strings, output them at the end. So basically, i get what that is saying, I'd prefer to write them all at the end which would save them in memory correct?
    The loop should continue to read & process lines of data until there is no more data to read (end of file).

    Now, i understand how to do the inner classes, and im not really sure how an anonymous inner class would work here for the 3rd class because other than having no name, its used only once in a program and is defined right at the spot where it is used? I dont see how this would fit in ?

    Any assistance is greatly appreciated...

    I am also willing to have someone help me via GTalk, if they want to PM me. I would love to have a "tutor" of sorts on this Java class. My class is online and it's just a huge pain for me to understand at times.

    Regards,

    Patrick

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Help with console program with 3 classes...

    You should be able to write separate 3 skeleton classes as per the instructions. To start with, code the second and third class methods to just return the string passed into them unmodified.

    Then write your main class. It needs to read the file and for each line it reads in, pass the string to the combination of classes 2 & 3 as per the instructions.

    Once you can compile and run the program (which will produce an output of each input line printed 3 times) then start work on each of the modification methods do one at a time and don't start the next until you are sure the one you are working on is correct.

    Finally, once it's all working look at moving the classes 2 & 3 to inner classes etc.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    May 2012
    Posts
    2

    Re: Help with console program with 3 classes...

    Quote Originally Posted by keang View Post
    You should be able to write separate 3 skeleton classes as per the instructions. To start with, code the second and third class methods to just return the string passed into them unmodified.

    Then write your main class. It needs to read the file and for each line it reads in, pass the string to the combination of classes 2 & 3 as per the instructions.

    Once you can compile and run the program (which will produce an output of each input line printed 3 times) then start work on each of the modification methods do one at a time and don't start the next until you are sure the one you are working on is correct.

    Finally, once it's all working look at moving the classes 2 & 3 to inner classes etc.
    Ok, well here is what i have so far. its VERY broken, at least, i think it is. im trying to figure out how to get BufferReader to read the input.txt file i have with the stuff i need to manip.. but heres my code :

    Code:
     
     import java.io.*;
       
     public class A3BE2300780 
     {
       
    BufferedReader in = getReader ("input.txt");
    
           private static class LowerCase {
        public static String convertToLowerCase(String input) {
        if (input==null) return "";
        return input.toLowerCase();
        }
        }
           
           public static class ThirdStringManip{
         //This method will trim the white space from the 
         //beginning and end of the string
            public static String trimmed(String s){
                if (s == null){
                    return "";
                }
                return s.trim();
            }
            //This method will return a trimmed string of size len
            public static String trimmed(String s, int len){
                String retVal = ThirdStringManip.trimmed(s);
                if (len > retVal.length()){
                    len = retVal.length();
                }
                return retVal.substring(0,len);
            }
            //This method will convert all double spaces to a single space
            public static String squeeze(String s){
                return s.replace("  ", " ");
            }
        }
           
            //This method will read strings from the input file 
        //and perform manipulations on each string. The results of the 
        //manipulations are displayed in the text area
        private void displayManipulatedStrings() throws Exception{
       
           
            while(loop)
            {
             //Get the next line in the file
                String curString = s.nextLine();
                //Trim and Squeeze
                System.out.print ( "Trim & Squeeze: " + ThirdStringManip.squeeze(ThirdStringManip.trimmed(curString)) + "\n");
                //Trim, Squeeze, and Shorten to 10 characters
                System.out.print ( "Trim, Squeeze, Shorten to 10: " + ThirdStringManip.trimmed(ThirdStringManip.squeeze(ThirdStringManip.trimmed(curString)),10) + "\n");
                //Trim, Squeeze, lower-case and shorten to 20 characters
                System.out.print ( "Trim, Squeeze, Shorten to 20, lower-case: " + toLower(ThirdStringManip.trimmed(ThirdStringManip.squeeze(ThirdStringManip.trimmed(curString)),20)) + "\n");
                System.out.print ( "\n");
            }
        }
     }
    Like I said, i think im on right track now, ish. Im really still noobish at this and i am taking somewhat blind stabs at this based on my book. :S

  4. #4
    Join Date
    Jun 2012
    Posts
    1

    Re: Help with console program with 3 classes...

    airforcegeek,

    did you ever figure it out? i think i have a similar assignment like yours maybe you can hook a fellow military out

    Thanks,
    Thone26
    USN

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Help with console program with 3 classes...

    did you ever figure it out? i think i have a similar assignment like yours maybe you can hook a fellow military out
    I hope you're not asking airforcegeek to give you the answer.

    Have a go yourself and if you get stuck post a question and we will help.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  6. #6
    Join Date
    Jul 2013
    Posts
    1

    Re: Help with console program with 3 classes...

    I am working on the same program. Shot in the dark but can you assist me, particularly with the while loop?

  7. #7
    Join Date
    Jun 2014
    Posts
    1

    Re: Help with console program with 3 classes...

    AirForceGeek,

    I've got questions about your post. Send me a message, when able.
    Thanks

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center