Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Client Protocol Server

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Client Protocol Server

    I'm making a system where the server asks questions to the client and the client responds. Then the marks out of 3 that the client had got is to be displayed. Everythign in my code works to the way i want it to, except the display of the marks. I have made arrays in which the final mark should relate to. But they always give the same output which is "You got 0/3 correct, Try Harder." instead of whichever one its meant to print out, someone help me, ive spent hours on this. I would also love to ass a password to the system.

    PROTOCOL

    import java.net.*;
    import java.io.*;
     
    public class MathsProtocol {
        private static final int FIRSTstate = 0;
        private static final int SECONDstate = 1;
    	private static final int THIRDstate = 2;
        private static final int FOURTHstate = 3;
    	private static final int FIFTHstate = 4;
    	private static final int SIXTHstate = 5;
    	 private static final int SEVENTHstate = 6;
     
        private static final int NUMques = 7;
     
        private int state = FIRSTstate;
        private int currentQues = 0;
    	private String[] clues = { "You got 0/3 correct, Try Harder.",
                                     "You got 1/3 correct, You could do better.",
                                     "You got 2/3 correct, Average.",
                                     "You got 3/3 correct, You smart pants.",};
     
     
        public String processInput(String theInput) {
            String theOutput = null;
    		int Q1Res = 0;
    		int Q2Res = 0;
    		int Q3Res = 0;
    		int TOTALRes = 0;
     
     
    		//QUESTION 1-----------------------------------------------------------------------------
            if (state == FIRSTstate) {
                theOutput = ("Q1: (A + B)*(A+B)" + " 1.A*A+B*B "+ " 2.A*A+A*B+B*B " + " 3.A*A+2*A*B+B*B ");
     
     
                state = SECONDstate;
            } else if (state == SECONDstate) {
                if (theInput.equalsIgnoreCase("3")) {
    			theOutput = "That is the correct answer";
    			Q1Res++;
     
                    state = THIRDstate;
                } else {
                    theOutput = " WRONG!! TRY AGAIN";
                }
    			// QUESTION 2-----------------------------------------------------------------------------
     
    			if (state == THIRDstate) {
    			theOutput = "CORRECT!! Q2: (A+B)*(A-B) =" + " 1) A*A+2*B*B" + " 2)  A*A-B*B " + " 3)  A*A-2*A*B+B*B";
    			 state = FOURTHstate;
            }
    		//----------
            } else if (state == FOURTHstate) {
     
                if (theInput.equalsIgnoreCase("2")) {
                    theOutput = "That is the correct answer";
     
                    state = FIFTHstate;
                } else {
                    theOutput =  " WRONG!!Try again ";
                    state = FOURTHstate;
                }
    			}
    			//QUESTION 3-----------------------------------------------------------------------------
    			if (state == FIFTHstate) {
                theOutput = ("CORRECT!! Q3: sin(x)*sin(x) + cos(x)*cos(x)  1?  2?  or  3?");
     
     
                state = SIXTHstate;
            } else if (state == SIXTHstate) {
                if (theInput.equalsIgnoreCase("1")) {
    			theOutput = "That is the correct answer, go again? (y/n)";
    			Q3Res++;
                    state = SEVENTHstate;
                } else {
                    theOutput = " WRONG!! TRY AGAIN";
    				state = SIXTHstate;
                }
    				TOTALRes = Q1Res;
    		theOutput = ("you got")+clues[Q1Res+Q2Res];
    			//---------------
            } else if (state == SEVENTHstate) {
                if (theInput.equalsIgnoreCase("y")) {
                    theOutput = "Q1: (A + B)*(A+B)" + " 1.A*A+B*B "+ " 2.A*A+A*B+B*B " + " 3.A*A+2*A*B+B*B ";
                    if (currentQues == (NUMques - 1))
                        currentQues = 0;
                    else
                        currentQues++;
                    state = SECONDstate;
                } else {
     
    				theOutput = "Bye.";
                    state = FIRSTstate;
                }
            }
            return theOutput;
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Client Protocol Server

    always give the same output
    Is that because the index to the array is always 0?
    What will change the value of the index? Where is the code that makes changes to the index?
    Is that code being executed?

    Do you understand how variables defined locally to method are created and used?
    Their values are not saved when the method exits.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Client Protocol Server

    the last output i get is "You got 0/3 correct, Try Harder." I compile a server and client code, but thats just to get it running which includes things like the port and sockets. The main code is this one i displayed.
    It's supposed to print out the correct array according to the questions that are right. I defined the Total Result globally.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Client Protocol Server

    Is there new code with class variables used instead of local variables so their values are saved?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. my encrypting simple client to server program unable to get the key from client
    By Paytheprice in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 3rd, 2013, 07:15 AM
  2. Replies: 0
    Last Post: May 31st, 2012, 05:35 PM
  3. [SOLVED] Server/client
    By lorider93p in forum Java Theory & Questions
    Replies: 1
    Last Post: February 8th, 2012, 01:36 PM
  4. server/client application fails when client closes
    By billykid in forum Java Networking
    Replies: 4
    Last Post: January 26th, 2012, 01:54 AM
  5. server and client
    By lanpan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 24th, 2011, 09:29 AM