-
November 22nd, 2011, 03:24 PM
#1
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;
}
}
-
November 22nd, 2011, 04:41 PM
#2
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.
-
November 22nd, 2011, 05:58 PM
#3
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 06:03 PM.
-
November 23rd, 2011, 06:42 AM
#4
Re: Questions about syntax/meaning
These are very basic Java questions, read a Java tutorial or google for the answers.
-
November 24th, 2011, 12:47 AM
#5
Re: Questions about syntax/meaning
I've never seen it used with a string literal before.
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.
-
November 24th, 2011, 05:33 AM
#6
Re: Questions about syntax/meaning
Well done. Have you worked out why is socket initialized to socket?
-
November 24th, 2011, 10:03 AM
#7
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 10:07 AM.
-
November 24th, 2011, 01:40 PM
#8
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 Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|