Questions about syntax/meaning
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Questions about syntax/meaning

  1. #1
    Join Date
    Sep 2011
    Posts
    197

    Questions about syntax/meaning

    why do they assign a null parameter here:
    Code:
     outputLine = kkp.processInput(null);


    Code:
    import java.net.*; 
    import java.io.*; 
      
    public class KKMultiServerThread extends Thread { 
        private Socket socket = null; 
      
        public KKMultiServerThread(Socket socket) { 
        super("KKMultiServerThread"); 
        this.socket = socket; 
        } 
      
        public void run() { 
      
        try { 
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true); 
            BufferedReader in = new BufferedReader( 
                        new InputStreamReader( 
                        socket.getInputStream())); 
      
            String inputLine, outputLine; 
            KnockKnockProtocol kkp = new KnockKnockProtocol(); 
            outputLine = kkp.processInput(null); 
            out.println(outputLine); 
      
            while ((inputLine = in.readLine()) != null) { 
            outputLine = kkp.processInput(inputLine); 
            out.println(outputLine); 
            if (outputLine.equals("Bye")) 
                break; 
            } 
            out.close(); 
            in.close(); 
            socket.close(); 
      
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
        }
    also, why in this next one do they add the minus one. That would make it 4.

    Code:
     if (currentJoke == (NUMJOKES - 1))
    Code:
    import java.net.*;
    import java.io.*;
    
    public class KnockKnockProtocol {
        private static final int WAITING = 0;
        private static final int SENTKNOCKKNOCK = 1;
        private static final int SENTCLUE = 2;
        private static final int ANOTHER = 3;
    
        private static final int NUMJOKES = 5;
    
        private int state = WAITING;
        private int currentJoke = 0;
    
        private String[] clues = { "Turnip", "yodel", "Ach", "who", "who" };
        private String[] answers = { "Turnip the heat, it's cold in here!",
                                     "I didn't know you could yodel!",
                                     "Bless you!",
                                     "Is there an owl in here?",
                                     "Is there an echo in here?" };
    
        public String processInput(String theInput) {
            String theOutput = null;
    
            if (state == WAITING) {
                theOutput = "Knock! Knock!";
                state = SENTKNOCKKNOCK;
            } else if (state == SENTKNOCKKNOCK) {
                if (theInput.equalsIgnoreCase("Who's there?")) {
                    theOutput = clues[currentJoke];
                    state = SENTCLUE;
                } else {
                    theOutput = "You're supposed to say \"Who's there?\"! " +
    			    "Try again. Knock! Knock!";
                }
            } else if (state == SENTCLUE) {
                if (theInput.equalsIgnoreCase(clues[currentJoke] + " who?")) {
                    theOutput = answers[currentJoke] + " Want another? (y/n)";
                    state = ANOTHER;
                } else {
                    theOutput = "You're supposed to say \"" + 
    			    clues[currentJoke] + 
    			    " who?\"" + 
    			    "! Try again. Knock! Knock!";
                    state = SENTKNOCKKNOCK;
                }
            } else if (state == ANOTHER) {
                if (theInput.equalsIgnoreCase("y")) {
                    theOutput = "Knock! Knock!";
                    if (currentJoke == (NUMJOKES - 1))
                        currentJoke = 0;
                    else
                        currentJoke++;
                    state = SENTKNOCKKNOCK;
                } else {
                    theOutput = "Bye.";
                    state = WAITING;
                }
            }
            return theOutput;
        }
    }

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

    Re: Questions about syntax/meaning

    why do they assign a null parameter here:
    No idea, what does the processInput() method do.
    also, why in this next one do they add the minus one. That would make it 4.
    Because currentJoke is an index into the joke arrays and so it is 0 based ie with 5 jokes the index is 0-4. When you want another joke, currentJoke is incremented but when it is 4 (ie the last joke) it has to be reset to 0.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Sep 2011
    Posts
    197

    Re: Questions about syntax/meaning

    what does the listening variable do, and in what class is it defined?

    Code:
        public KKMultiServerThread(Socket socket) {     
        super("KKMultiServerThread");     
    this.socket = socket;     }
    In this construct what does the super() method do?

    Why is socket initiliazed to socket? Is socket a variable in the extended class thread?
    Last edited by kolt007; November 22nd, 2011 at 05:03 PM.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Questions about syntax/meaning

    These are very basic Java questions, read a Java tutorial or google for the answers.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Sep 2011
    Posts
    197

    Re: Questions about syntax/meaning

    I've never seen it used with a string literal before.

    Code:
    super.PrintData();
    use's printData method from the superclass. but, what does it do when it's a string literal?

    *solved*
    calls the super construct with the literal as it's parameters.

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Questions about syntax/meaning

    *solved*
    Well done. Have you worked out why is socket initialized to socket?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #7
    Join Date
    Sep 2011
    Posts
    197

    Re: Questions about syntax/meaning

    All the this keyword is doing is telling that the paremeter is equal to the variable socket. I feel stupid for not knowing that, it's just I've never used the this keyword just got around it the long way.
    Last edited by kolt007; November 24th, 2011 at 09:07 AM.

  8. #8
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Questions about syntax/meaning

    I think you understand what it means but haven't expressed yourself very well.

    this.socket means the instance variable called socket. You need to use 'this' because you have two variables with different scope but with the same name and unless you explicitly state which one you want to use the compiler will use the most local ie the parameter variable called socket.

    So, in your code, the assignment will be to set the instance variable called socket to the reference in the parameter variable called socket.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

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