Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By Tolls

Thread: Project Help

  1. #1
    Dillon112244 is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Project Help

    Hello everyone! I'm fairly new to Java, and just wanted to see if anyone could help figure out what I'm doing wrong.

    Here's the directions:http://cs.uga.edu/~cs1301/files/Project2.pdf (Part 5 is the only part I'm having trouble with)
    Here's the API: PirateDuelInsults (You don't have to look through this but it may help).
    Here's one of the 2 classes: http://cs.uga.edu/~cs1301/files/PirateDuelInsults.java (getCorrectComeback is the most important thing in this)

    I've pretty much done the entire project, but there's one thing that I just can't seem to figure out. The place where I'm having my problems is at part 5 from the directions.

    Here's part of what I have so far:


    PHP Code:
    import java.util.Scanner;
    public class PirateDuel {
    
    
        public static void main (String [] args)
        {
            Scanner keyboard = new Scanner(System.in);
             
            String insult, comeback1, comeback2, comeback3, comeback4, correctComeback;
            //Create a new instance of PirateDuelInsults.
            // Your program must have one, and only one, instance of PirateDuelInsults
            PirateDuelInsults pdi = new PirateDuelInsults();
            //Get a random insult. This method can only be called 3 times.
            // This method will setup the four comeback methods below, and put the
            // correct comeback in one of them.
            insult = pdi.getInsult();
            //Get the randomly shuffled comebacks to display to the player
            // These methods should be called right after the getInsult() is called
            comeback1 = pdi.getFirstComeback();
            comeback2 = pdi.getSecondComeback();
            comeback3 = pdi.getThirdComeback();
            comeback4 = pdi.getFourthComeback();
            //Get the correct comeback for the insult argument
            correctComeback = pdi.getCorrectComeback(insult);
            
            int playerAnswer;
            int playerStrikes = 0;
            
            
            
            //The following lines are the opening lines of the game.
            System.out.println("A software pirate challenges you to a sword duel.");
            System.out.println("Only wits as sharp as Occam's razor will prevail.");
            System.out.println("En Garde!");
            System.out.println();
            
            
            System.out.println("The software pirate yells, " + pdi.getInsult());
            System.out.println("You ponder some clever comebacks...");
            
            
            
                //The following lines display the possible comebacks the player can choose.
                System.out.println("  1)  " + pdi.getFirstComeback());
                
                System.out.println("  2)  " + pdi.getSecondComeback());
            
                System.out.println("  3)  " + pdi.getThirdComeback());
                
                System.out.println("  4)  " + pdi.getFourthComeback());
                
                
            //Prompts the player to choose a comeback.
            System.out.println("Enter your clever comeback:        ");
            
            
            playerAnswer = keyboard.nextInt();
          
            switch(playerAnswer)
            {
            case 1:
                if (pdi.getFirstComeback().equals(pdi.getCorrectComeback(insult)))
                        {
                    System.out.println("The software pirate looks stunned.");
                    System.out.println("Your sword strikes the software pirate.");
                    playerStrikes++;
                        }


    I've done more but this is the important stuff.

    The part I'm having difficulty with is right during my if statement, right after my initial switch statement (and all the other if statements within this switch statement):
    if (pdi.getFirstComeback().equals(pdi.getCorrectComeb ack(insult)))

    This if statement always ends up being false even if getFirstComeback is the correct response. Is the .equals method the right method to use or is there another problem.

    I'm trying to make where if the getFirstComeback() is the correct comeback then it rewards the player, but for some reason I can't do it. getFirstComeback and getCorrectComeback for some reason never equal each other (the same is true with getSecondComeback and so on). I've tested what the output is when I have System.out.println(pdi.getCorrectComeback);, but it never actually gives the right comeback.

    My teacher said we are not allowed to alter the PirateDuelInsults page.

    I'm sorry for how convoluted this may be, but I would greatly appreciate any help! Thanks in advance!
    Last edited by Dillon112244; 02-09-2012 at 06:02 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Project Help

    You have posted a lot of material to go through. More than a lot of volunteers will want to go through.
    How are you trying to debug your program?
    If you don't have an interactive debugger, you will need to use the println statement to print out the values of variables as they change and to show the execution flow of the program. If you understand what the program is supposed to do, the print out should show you where the program is not doing what you want it to do.

  3. #3
    Dillon112244 is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Re: Project Help

    I know it's a lot of information, I'll try to take some unneeded information out to make it easier to go through.

    I've actually done a println statement of each of the strings.

    The strings getFirstComeback, getSecondComeback, getThirdComeback, and getFourthComeback all work correctly. But the string getCorrectComeback never actually displays the correct comeback and this is a problem because my teacher said we can't change this. I know I probably just made a small error somewhere, but I just don't know where.

    Thanks for the info

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Project Help

    the string getCorrectComeback never actually displays the correct comeback
    What does the method getCorrectComeback return? Did you print out the String that it returned?
    What was it?
    When you print it be sure to delimit it so you can see if there are any extra characters:
    println("getCorrectComeback=" + pdi.getCorrectComeback(insult) + "<");

  5. #5
    Dillon112244 is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Re: Project Help

    Sorry, I didn't say what it actually returned. I've printed the string getCorrectComeback quite a few times, and it always prints one of the many, many comebacks there is to choose from, but never the right one. All the other methods seem to be working exactly how they should be, but for some reason getCorrectComeback just doesn't seem to be doing what it should.

    Once again, thanks for the help!

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Project Help

    getCorrectComeback()
    Is that the method the instructor gave you, that you are not allowed to change?

  7. #7
    Dillon112244 is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Re: Project Help

    Yes, it's actually getCorrectComeback(insult), and many of the other methods (getFirstComeback, etc) we can't change as well.

    My instructor also told us to put lines 9-24 into our code as, and that we cannot change those lines either.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Project Help

    You'll have to ask the instructor about your problem if the part he provided does not work as you expect.

  9. #9
    Dillon112244 is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Re: Project Help

    Alright, thanks for the help

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Project Help

    Java Code:
            //Get the randomly shuffled comebacks to display to the player
             // These methods should be called right after the getInsult() is called
             comeback1 = pdi.getFirstComeback();
             comeback2 = pdi.getSecondComeback();
             comeback3 = pdi.getThirdComeback();
             comeback4 = pdi.getFourthComeback();
    You then (in your if):
    Java Code:
            case 1:
                 if (pdi.getFirstComeback().equals(pdi.getCorrectComeback(insult)))
    Now that seems to go against the comments in the first bit above.
    I suspect calling getFirstComeback() will get a random comeback (if any).

    You've already got the comeback saved in the 4 comeback variables.
    Norm likes this.

  11. #11
    Dillon112244 is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Re: Project Help

    getFirstComeback, getSecondComeback2, etc gets one of the random comebacks from the third link in my original post. One of these get...Comebacks will always be the correct comeback, but it's random which one it will be. Every time the getInsult method is used, the getFirstComeback,getSecondComeback, etc will change, but after getInsult is used these methods will stay the same (until getInsult is used again). So when every I print the string getFirstComeback it is always consistent with what it should be, but this is not the case with getCorrectComeback. Sorry, I didn't make this clearer in the original post.

    Thanks for the help

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Project Help

    No.
    That stuff in the section of code you are not supposed to change says quite clearly you should get the values from those 4 methods straight after calling getInsult().
    It stores them in 4 variables (comeback 1 - 4) presumably because you cannto rely on get<1-4>Comeback() returning the same value each time.
    That can be the only reason for that.
    So change your if statement to compare against the comeback1 variable and don't call getFirstComeback() (or equivalent).

    If you don't believe me then print the value of comeback1 and getFirstComeback just prior to doing the if.
    Java Code:
    System.out.println(comeback1);
    System.out.println(pdi.getFirstComeback());
    if (pdi.getFirstComeback().equals(pdi.getCorrectComeback(insult))) ... etc etc
    In fact, similarly use the correctComeback variable as well, though I suspect that's simply doing a lookup into a Map.

    Your tutor is clearly a Monkey Island fan...is one of the replies "I am rubber you are glue..."?

  13. #13
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default Re: Project Help

    I quickly looked at your logic and it seemed right to me. So, I thought there may be an issue with the API, so I tested it. The API works fine as shown by the below program. When I test the comebacks to the correct comebacks it is finding a correct answer every time. What does the rest of your code look like?

    Java Code:
    	public static void main(String[] args){
    		PirateDuelInsults pdi = new PirateDuelInsults();
    		String insult = pdi.getInsult();
    		//print insult
    		System.out.println("Insult: " + insult);
    		//print Comeback
    		System.out.println("1: " + pdi.getFirstComeback());
    		System.out.println("2: " + pdi.getSecondComeback());
    		System.out.println("3: " + pdi.getThirdComeback());
    		System.out.println("4: " + pdi.getFourthComeback());
    		//print correct comeback
    		System.out.println("Correct: "  + pdi.getCorrectComeback(insult));
    		//compare correct comeback to each comeback option and print true for match false for no match
    		System.out.println("1? -- " + pdi.getFirstComeback().equals(pdi.getCorrectComeback(insult)));
    		System.out.println("2? -- " + pdi.getSecondComeback().equals(pdi.getCorrectComeback(insult)));
    		System.out.println("3? -- " + pdi.getThirdComeback().equals(pdi.getCorrectComeback(insult)));
    		System.out.println("4? -- " + pdi.getFourthComeback().equals(pdi.getCorrectComeback(insult)));
    	}
    OUTPUT
    Insult: My name is feared in every dirty corner of this island!
    1: So you got that job as janitor, after all.
    2: A computer once beat me at chess, but it was no match for me at kick boxing.
    3: If at first you don't succeed, you must be a programmer.
    4: Ninjas pwn Pirates!
    Correct: So you got that job as janitor, after all.
    1? -- true
    2? -- false
    3? -- false
    4? -- false

  14. #14
    Dillon112244 is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Re: Project Help

    I figured it out! Thanks Tolls, your post helped me figure out where I went wrong.

    In line 38 of my code I wrote

    PHP Code:
    System.out.println("The software pirate yells, " + pdi.getInsult());
    I should have written insult instead of pdi.getInsult().

    Once again thanks everyone who posted and helped out! I greatly appreciate it.

Similar Threads

  1. Creating a project in eclipse from existing project
    By Suraiya in forum New To Java
    Replies: 1
    Last Post: 10-08-2011, 09:14 AM
  2. How to convert from Ant project to maven project?
    By jiapei100 in forum New To Java
    Replies: 0
    Last Post: 06-08-2011, 10:01 AM
  3. Adding a project to an existing project
    By Seijuro in forum NetBeans
    Replies: 4
    Last Post: 08-08-2010, 10:15 AM
  4. open existing project project ..
    By itaipee in forum Eclipse
    Replies: 1
    Last Post: 12-28-2008, 08:12 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
  •