Results 1 to 15 of 15
  1. #1
    alfaprototype is offline Member
    Join Date
    Nov 2014
    Posts
    5
    Rep Power
    0

    Lightbulb Method creation - Help

    How can I write a method that takes a string with duplicates letters and returns the same string which does not contain duplicates. For example, if you pass it radar, it will return rad. Also i would like to know how can I Write a method that takes as parameters the secret word and the good guesses and returns a string that is the secretword but has dashes in the places where the player has not yet guessed that letter. For example, if the secret word is radar and the player has already guessed the good guesses letters r and d, the method will return r-d-r.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Method creation - Help

    What have you tried?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    alfaprototype is offline Member
    Join Date
    Nov 2014
    Posts
    5
    Rep Power
    0

    Default Re: Method creation - Help

    Quote Originally Posted by Norm View Post
    What have you tried?
    the most i can do is create a method to check if the input matches the secretword, I am new in java so thats why I am having trouble understanding method

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Method creation - Help

    I am having trouble understanding method
    Take a look at the tutorial: Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Method creation - Help

    Quote Originally Posted by alfaprototype View Post
    How can I write a method that takes a string with duplicates letters and returns the same string which does not contain duplicates. For example, if you pass it radar, it will return rad. Also i would like to know how can I Write a method that takes as parameters the secret word and the good guesses and returns a string that is the secretword but has dashes in the places where the player has not yet guessed that letter. For example, if the secret word is radar and the player has already guessed the good guesses letters r and d, the method will return r-d-r.
    Is this a complicated way of telling us that you have to implement 'hang man'? Just say so; here it is without any further comments or explanation because it's a complete spoiler and you have to learn something:

    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class HangMan {
    	
    	private BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
    	private String correct, used, word;
    	private int turns;
    	
    	private void setup(String w, int turns) {
    
    		correct= "-"; used= ""; word= w;
    		this.turns= turns;
    	}
    	
    	private String word(String prompt) {
    		
    		String word= this.word.replaceAll("[^"+correct+"]", "-");
    		System.out.println(prompt+": "+word);
    		return word;
    	}
    	
    	private boolean wins(String word) {
    		
    		boolean wins= word.indexOf('-') < 0;
    		
    		if (wins)
    			System.out.println("You guessed the word");
    		return wins;
    	}
    	
    	private String guess(char x) {
    		
    		if (used.indexOf(x) >= 0) 
    			return word("Character '"+x+"' already used");
    		
    		used+= x;
    		
    		if (word.indexOf(x) < 0) {
    			turns--;
    			return word("Character '"+x+"' is incorrect");
    		}
    		
    		correct+= x;
    		
    		return word("Character '"+x+"' is correct");
    	}
    	
    	private char input() {
    		
    		String s= "";
    		try {
    			for (; (s= s.trim()).length() == 0; s= br.readLine()) 
    				System.out.print("Guess a character: ");
    		} catch (IOException ioe) { System.exit(1); }
    		
    		return s.charAt(0);
    	}
    	
    	public boolean play(String word) {
    		
    		for (setup(word, word.length());; System.out.println("turns: "+turns)) 
    			if (wins(guess(input()))) 
    				return true;
    			else if (turns == 0) {
    				System.out.println("You lose");
    				return false;
    			}
    	}
    	
    	public static void main(String[] args) {
    		
    		new HangMan().play("defenestration");
    	}
    }
    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Method creation - Help

    Quote Originally Posted by JosAH View Post
    here it is without any further comments or explanation because it's a complete spoiler and you have to learn something:
    Hmm, I believe this could snowball into others doing the same using the same rationale.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Method creation - Help

    I'm also confused. It looks like spoonfeeding.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Method creation - Help

    Well I'll get in that line, I also had a bit of a "What, really!?" moment :)

    I wonder if this bit:

    Java Code:
    new HangMan().play("defenestration"); // <-- why specifically that word of all words to choose from?
    is some kind of silent hint.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Method creation - Help

    Quote Originally Posted by gimbal2 View Post
    Well I'll get in that line, I also had a bit of a "What, really!?" moment :)

    I wonder if this bit:

    Java Code:
    new HangMan().play("defenestration"); // <-- why specifically that word of all words to choose from?
    is some kind of silent hint.
    That is just an arbitrary normal word; it is just an example. For the folks who are afraid that I've been spoonfeeding, the OP needs to answer the following questions if he wants to turn it in if it were his own code:

    1) there are no visible loops in the code; except in the input() and play( ... ) methods; explain why the code works.
    2) this class can only play the game once; how to change the code so that it can play multiple games.
    3) explain how regular expressions work.
    4) why is this code better/worse than other code in terms of size/efficiency of the code.
    5) what does each method do?

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  10. #10
    alfaprototype is offline Member
    Join Date
    Nov 2014
    Posts
    5
    Rep Power
    0

    Default Re: Method creation - Help

    i was working on this code this morning, with this i am able to only print out the letter than are not duplicate in a string.
    take a look at it.

    Java Code:
    public static String removeDuplicate(String data){
    
    		   String remove="";
    		   for(char a:data.toCharArray())
    		   {
    		       if(!remove.contains(""+a))
    		       {
    		          remove+=a;
    		       }
    		   }
    		   return remove;        
    		}

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Method creation - Help

    Do you have a question about the method? Does it do what you want? If not please explain.
    Post some print out that shows what the code does and add some comments saying what is wrong.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    alfaprototype is offline Member
    Join Date
    Nov 2014
    Posts
    5
    Rep Power
    0

    Default Re: Method creation - Help

    Yes, it does what i want. I just wanted to show you that i was able to do it

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Method creation - Help

    Ok, glad you solved that one.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    alfaprototype is offline Member
    Join Date
    Nov 2014
    Posts
    5
    Rep Power
    0

    Default Re: Method creation - Help

    Now, what do you think i can do to solve the second one?

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Method creation - Help

    What have you tried for the second one?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 7
    Last Post: 04-11-2013, 05:31 AM
  2. Basic method creation
    By alexix in forum New To Java
    Replies: 5
    Last Post: 02-26-2013, 08:32 PM
  3. Replies: 0
    Last Post: 05-18-2012, 01:29 PM
  4. Accessing instance outside of creation method
    By meringue in forum New To Java
    Replies: 6
    Last Post: 03-22-2009, 01:36 AM
  5. Replies: 3
    Last Post: 10-06-2008, 11:03 PM

Tags for this Thread

Posting Permissions

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