Results 1 to 6 of 6
  1. #1
    Vysero is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Attempting to understand code. Homework question, mystery code.

    Hello everyone! So my teacher assigned us a block of Mystery code that we are suppose to try and figure out how it works. I have been looking at it for some time now here is the code:

    Java Code:
    package assignment0;
    import java.util.*;
    
    public class mystery {
    
        public static class Mystery{
        
            private List<Integer> makeData(int n){
            
                List<Integer> values = new ArrayList<>(); 
                for(int i = 0; i < n; i++){
                
                    values.add(i);
                }
                Collections.shuffle(values);
                return values;
            }
            
            private int[] guessResult(List<Integer> values){
                
                Random rand = new Random();
                int len = values.size();
                int[] guess = {
                
                    values.get(rand.nextInt(len)),
                    values.get(rand.nextInt(len)),
                };
                return guess;
            };
            
            private int[] mystery(List<Integer> values, int [] guess){
            
                if(values.isEmpty()){
                
                    return guess;
                }
                
                int v = values.get(0);
                int[] newGuess = {
                
                    (guess[0] < v) ? guess[0] : v,
                    (guess[1] > v) ? guess[1] : v,
                };
                return this.mystery(values.subList(1, values.size()), newGuess);
            }
            
            public int[] operation(int n){
            
                List<Integer> data = this.makeData(n);
                int[] guess = this.guessResult(data);
                int[] result = this.mystery(data, guess);
                return result;
            }
        }
        
        
        public static void main(String[] args) {
           
            Mystery mystery = new Mystery();
            int n = 99;
            int[] result = mystery.operation(n);
            for(int i = 0; i<2; i++){
            
                System.out.println(result[i]);
            }
        }
        
    }
    So I punched it into my NetBeans and tested it. It returns two values 0 and 98. So what I have been able to ascertain so far is that the code will always return 0 and a number wish is 1 less than n, that much is obvious. However, it does this by printing the contents of the array result. Those values are found by some sort of comparison between the array guess and the array data in the mystery method. I know that data is an array of random integers of size n and that guess is an array of size 2 who's contents are random integers. I am not sure how the mystery class is comparing the contents of the arrays in order figure out what to print.

    Is this a recursive method which uses a Boolean comparison to do some type of binary search on the array.. as you can see I am a bit lost.

    Side question is there a way I can use the debugger to figure this out? Like it would be nice if I could see the contents of data and guess without printing them out. Is there a way to step through the program as it runs whilst displaying that content?

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

    Default Re: Attempting to understand code. Homework question, mystery code.

    One approach to understanding code is to add comments to it describing what each section does. Multiple passes over the code adding more comments as understanding improves should eventually help you understand what the code is doing.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Jan 2017
    Posts
    2
    Rep Power
    0

    Default Re: Attempting to understand code. Homework question, mystery code.

    This is recursive function. So you must analyse code rather than debug 99 functions call.
    probably you can change n value to reduce number of steps.

    Function is simple.

    1. Base case empty list. function return 2 items guess array.

    2. function call itself without [0] item. (this means iteration trough array)

    *return this.mystery(values.subList(1, values.size()), newGuess);

    3. each time first item is compared with guess and select max and min values and store values
    in guess array
    Java Code:
        int[] newGuess = {
                 
                    (guess[0] < v) ? guess[0] : v,
                    (guess[1] > v) ? guess[1] : v,
                };
    Last edited by homeworkhelp4u; 01-23-2017 at 06:27 AM. Reason: added code tag

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

    Default Re: Attempting to understand code. Homework question, mystery code.

    The OP was supposed to figure this out for himself. I guarantee you that Norm could have provided the same answer. Please provide ways to help posters find answers without providing the details. As you are new to the forum you may also want to read the forum guidelines.

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

  5. #5
    Join Date
    Jan 2017
    Posts
    2
    Rep Power
    0

    Default Re: Attempting to understand code. Homework question, mystery code.

    Quote Originally Posted by jim829 View Post
    The OP was supposed to figure this out for himself.
    Jim
    I apologize. It's my fault. It was not a complete solution.

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

    Default Re: Attempting to understand code. Homework question, mystery code.

    No apology necessary. Just wanted to let you know. And sometimes syntax and examples are provided. Usually it is a judgement call. And welcome to the forum!

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

Similar Threads

  1. I don't understand the code help!
    By gomdohri in forum New To Java
    Replies: 3
    Last Post: 10-15-2011, 02:58 PM
  2. Trying to understand this code better
    By Kimomaru in forum New To Java
    Replies: 2
    Last Post: 09-28-2011, 10:44 PM
  3. Can any one help me to understand the Code
    By soomroimran in forum New To Java
    Replies: 2
    Last Post: 04-28-2011, 09:23 AM
  4. Help Testing My Homework Code
    By sehudson in forum New To Java
    Replies: 1
    Last Post: 03-01-2011, 07:23 AM
  5. understand the code
    By prof.deedee in forum New To Java
    Replies: 8
    Last Post: 11-11-2009, 02:43 AM

Posting Permissions

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