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 9 of 9

Thread: LOOPS

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    3
    My Mood
    Happy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default LOOPS

    Need some hints and ideas to code the following problems. I have done question 5. it's just for the reference for question 6.Thanks

    4) The Evil E is back and it brought back some friends! Write a method that will acknowledge that the letter E is evil. Each letter E will eliminate the characters around it. But the Evil Es will not eliminate its friends. So it will not eliminate the letters e or E.

    evilEs("HELLO") --> " E LO"
    evilEs("dEep") --> " Eep"
    evilEs("Evil") --> "E il"
    evilEs("bean") --> "bean"
    evilEs("AbEEmgmEeodE") --> "A EE g Eeo E"
    evilEs("ELEPHANT") --> "E E HANT"
    evilEs("HomepagE") --> "Homepa E"

    5) Write a method that will encode a given string in the following format.

    encode("Hello") -> "leHlo"
    encode("abcdef") -> "fdbace"
    encode("ILoveJava") -> "vJvLIoeaa"
    encode("mnopqrstuv") -> "vtrpnmoqsu"


    6) Write a method that will undo the encoding in question 5. So this method will decode a String that is returned in the format of question 5.
    (This one is much more complicated than question 5)

    decode("leHlo") -> "Hello"
    decode("fdbace") -> "abcdef"
    decode("vJvLIoeaa") -> "ILoveJava"


  2. #2
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: LOOPS

    What have you tried so far? Are you stuck on a particular part of the problem?
    Post what you have done/where you are having issues - people can help you more
    when they know exactly what the problem your having is.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    3
    My Mood
    Happy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: LOOPS

    here's my code for question 5, but i don't see any pattern for question 6 and for that reason i don't know what to start with.
    public static String encode(String str)
    {
    int len = str.length();
    String output1 = "";
    String output2 = "";
    if(len % 2 != 0)
    {

    for(int i = len-2; i >= 0; i-=2)
    {
    output1 = output1 + str.charAt(i);
    }
    for(int k = 0; k < len; k+=2)
    {
    output2 = output2 + str.charAt(k);
    }

    }
    else
    {
    for(int i = len-1; i >= 0; i-=2)
    {
    output1 = output1 + str.charAt(i);
    }
    for(int k = 0; k < len; k+=2)
    {
    output2 = output2 + str.charAt(k);
    }
    }
    return output1 + output2;
    }

    --- Update ---

    ohhh by the way, my code for Question 5 worked for every scenarios I have tried .
    Last edited by master_java; June 4th, 2014 at 03:00 PM. Reason: forgot braces

  4. #4
    Junior Member
    Join Date
    May 2014
    Location
    Inverness, United Kingdom
    Posts
    11
    My Mood
    Cheerful
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: LOOPS

    You can format Java Code this way.
    Last edited by GregBrannon; June 5th, 2014 at 03:25 AM. Reason: Quoted code removed

  5. The Following User Says Thank You to BlueEyesWhiteDragon For This Useful Post:

    master_java (June 6th, 2014)

  6. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: LOOPS

    @Abhilash: please review The Problem with Spoon-Feeding and comply.

    Thank you.

  7. #6
    Junior Member
    Join Date
    Jun 2014
    Posts
    3
    My Mood
    Happy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: LOOPS

    I had no idea what spoon-feeding is until I have clicked to your spoon feeding link. But do not worry guys, I didn't just copy and paste; I have understood the whole problem.
    Your help and comments are much appreciated @Abhilash, Greg and Blue Eyes

    --- Update ---

    Here's my next question
    3) Take 2 Strings and compare each char. If the chars are the same it will concatenate a y if they are different it will concatenate an n. It will then return the new concatenated String. The length of the returned string will always be the length of the longest string.

    sameYN(“gold”, “bold”)  “nyyy”
    sameYN(“abcd”, “abcd”)  “yyyy”
    sameYN(“copy”, “copies”)  “yyynnn”
    sameYN(“abcxyz”, “mnoxyzabc”)  “nnnyyynnn”

    The following is the solution I came up with. But one of my friend(he won't help) keeps on telling me there's one more scenario to be taken care of.
    public static String sameYN(String str1, String str2)
        {
            String output = "";
            int len1 = str1.length();
            int len2 = str2.length();
            if(len1 > len2)
            {
                for(int i = 0; i < len2; i++)
                {
                    if(str1.charAt(i) == str2.charAt(i))
                        output = output+"y";
                    else 
                        output = output+"n";
                }
            }
            else if(len2 > len1)
            {
                for(int i = 0; i < len1; i++)
                {
                    if(str1.charAt(i) == str2.charAt(i))
                        output = output+"y";
                    else 
                        output = output+"n";
                }
            }
            else 
            {
                for(int i = 0; i < len1; i++)
                {
                    if(str1.charAt(i) == str2.charAt(i))
                        output = output+"y";
                    else 
                        output = output+"n";
                }
            }
            for(int k = 0; k < Math.abs(len1 - len2); k++)
            {
                output = output +  "n";
            }
     
            return output;
        }

  8. #7
    Member Abhilash's Avatar
    Join Date
    Jun 2014
    Location
    Kolkata, West Bengal, INDIA
    Posts
    108
    My Mood
    Busy
    Thanks
    5
    Thanked 10 Times in 10 Posts

    Default Re: LOOPS

    Hey! no problem. If you have any more problems, I'll help anytime, but this time I will explain instead of giving the full code. So that you do the program your way and understand it.

  9. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: LOOPS

    The following is the solution I came up with. But one of my friend(he won't help) keeps on telling me there's one more scenario to be taken care of.
    Read the assignment and check that you've satisfied every requirement. If you find that you've missed one and can't solve it, then come back and let us know specifically what you need help with.

  10. #9
    Member Abhilash's Avatar
    Join Date
    Jun 2014
    Location
    Kolkata, West Bengal, INDIA
    Posts
    108
    My Mood
    Busy
    Thanks
    5
    Thanked 10 Times in 10 Posts

    Default Re: LOOPS

    I don't know if I'm right but I guess the "Scenario" involves cases

    Eg:-
    sameYN(“BOLD”, “bold”) returns "nnnn" instead of "yyyy"

Similar Threads

  1. Replies: 6
    Last Post: March 12th, 2014, 12:43 PM
  2. For vs. For Each loops?
    By jean28 in forum Java Theory & Questions
    Replies: 6
    Last Post: February 5th, 2013, 06:27 PM
  3. For Loops
    By Shyamz1 in forum Loops & Control Statements
    Replies: 3
    Last Post: September 27th, 2011, 11:54 AM
  4. For loops
    By JCTexas in forum Loops & Control Statements
    Replies: 4
    Last Post: September 21st, 2011, 05:43 PM
  5. help with loops... :(
    By Macgrubber in forum Loops & Control Statements
    Replies: 2
    Last Post: November 2nd, 2010, 12:38 PM