Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 11 of 11

Thread: What does this code do?

  1. #1

    What does this code do?

    Hello everyone. I'm filling out this questionnaire and I'm being asked these questions about the below snippet of code. I'm a pretty seasoned web developer, but I'm not sure what language this is in. It looks like maybe Java or C++, but I'm not too sure.

    So this is what I need to answer:

    a) Explain what the code is supposed to do.
    b) Identify any errors in the code.
    c) If there are errors, please fix them.

    Here is the code:

    Code:
      
    public String mystery(String num) {
          if (num == null) {
            return "N/A";
          }
          int len = num.length();
          int c = 0;
          char[] sb = new char[len];
          for (int i = 0; i < len; i++) {
            sb[c++] = num.charAt(i);
            if ((len - 1 - i) % 3 == 0 && i != len - 1) {
              sb[c++] = ',';
            }
          }
          return new String(sb);
        }
    I can identify some errors, but I'm not really sure what it is supposed to do. Thanks!

  2. #2
    It's c++.

    a) it's a member function that is supposed to take a string and return a new string with the same content as the old string but with a comma inserted between every 3 letters.
    b) it does not correctly allocate enough storage for the new string.
    c) char[] sb = new char[len + (len / 3)];
    If you notice this notice you will notice that this notice is not worth noticing.

    "Are you doing anything tonight? If not, how about me?"

    Opera Sucks! - FIX IT
    Oliver Zheng

  3. #3
    Awesome. Thanks MTsoul. I definitely do not know C++. You rock! Oh, I might have another one for you.

  4. #4
    Here you go:

    Assume you are given two fixed lists of numbers (arbitrary size, but you can assume the lists are large):

    a) Write a method that can efficiently find out if there is any element in the 2nd list that is an element of the first list.

    b) Describe some of the tradeoffs you can make with regards to memory and complexity.

  5. #5
    Are you asking us to do your homework for you?

    Great, now even Kirupa is { facebooking | twittering }

  6. #6
    Quote Originally Posted by kirupa View Post
    Are you asking us to do your homework for you?
    He said filling out a questionnaire… More likely his college application. :O
    If you notice this notice you will notice this notice was inspired by the noticing of another notice.

  7. #7
    It's not C++, it's Java. In C++ the string type is called std::string, not String, and NULL is used to check pointer values, not whole strings. Also, String.charAt is a typical Java method. Also, in C++ public methods are grouped under a public header, they do not each have a public modifier.

    Also, do your own homework.
    Wait, what?

  8. #8
    Quote Originally Posted by Voetsjoeba View Post
    It's not C++, it's Java. In C++ the string type is called std::string, not String, and NULL is used to check pointer values, not whole strings. Also, String.charAt is a typical Java method. Also, in C++ public methods are grouped under a public header, they do not each have a public modifier.

    Also, do your own homework.
    But but but, he could have used the std namespace... and then made a custom class of string that mimics String, and overloaded the == operator to compare with booleans/integers... Then would it be C++?
    If you notice this notice you will notice that this notice is not worth noticing.

    "Are you doing anything tonight? If not, how about me?"

    Opera Sucks! - FIX IT
    Oliver Zheng

  9. #9
    it is indeed java. the use of public is not how you use it in c++
    Code:
    public String mystery(String num) {

  10. #10
    Omg i suck at c++.
    If you notice this notice you will notice that this notice is not worth noticing.

    "Are you doing anything tonight? If not, how about me?"

    Opera Sucks! - FIX IT
    Oliver Zheng

  11. #11
    I think it's JAVA too!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012