Learning Curve

Discussion in 'Programming General' started by SexayMistahBee, Jan 20, 2014.

Learning Curve
  1. Unread #1 - Jan 20, 2014 at 11:49 AM
  2. SexayMistahBee
    Joined:
    Feb 28, 2006
    Posts:
    2,410
    Referrals:
    0
    Sythe Gold:
    27
    Discord Username:
    SexayMistahBee

    SexayMistahBee Sexiest Bee On Earth
    $50 USD Donor New

    Learning Curve

    So how do you guys study programming exactly?

    I'm learning through a book, and I got the basics down. Then I came across a problem asking me to create a method in Java that shuffles cards, using the method Math.random().

    the answer was this:


    Now that I've seen it, I can understand how this method shuffles a deck of cards.
    In fact, this method was mentioned really early in the book.
    However, it would have taken me forever to write the method unless I had memorized it.

    Judging by the contents of the book, it seems that the writer assumes that you have the math skills to write that method without looking at the answer sheet. This, I don't have.

    In the future, will I come across situations where I have to write methods like this often? Is math absolutely necessary?

    I really should start studying math, shouldn't I? lol
     
  3. Unread #2 - Jan 20, 2014 at 9:45 PM
  4. MineMarkPT
    Joined:
    Jun 19, 2012
    Posts:
    477
    Referrals:
    0
    Sythe Gold:
    0

    MineMarkPT Forum Addict
    Banned

    Learning Curve

    That has nothing to do with Maths. Its just pure logic, put into form of code.
    Lets break down the act of shuffling a deck of cards to a simpler version.
    Since you are studying Java i'm assuming that you are familiar with arrays and how they work.

    In order to show you how easy this can be, and this is how everyone starts in programming (by doing stuff that can be done quite simply, with pretty complicated ways), and how elementary it can be done.

    Lets imagine you have an array with a length of 52, or an array with 52 elements inside, however you prefer. Now lets imagine a that this is your deck of cards, and each of the elements is a card.

    Now, lets imagine you have another array with a length of 52, and that has none of the 52 indexes ocupied, which means this array has a length of 52, however all the "seats" in it are empty. This would be your deck box, where the shuffled deck will be stored.

    The most elementary way to shuffle a deck of cards, is to remove a random card from the deck and put it in the box.
    You are now finished with the logical and thinking part of it. Moving on to the code.

    Lets first create both the variables needed:

    Code:
        static int[] deck = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52};
        int[] box = new int[52];
    
    After this, we are going to need to write a loop that will iterate through the deck and put a random card from that deck into the box.

    Code:
    for(int i = 0; i < deck.length; i++) {
    int random = (int) (Math.random() * 51)
    box[i] = deck[random];
    }
    
    What this code does is, it generates a random number between 0 and 51, convert it to an int, because we are going to use it to refer to indexes in arrays which needs to be an integer and then, in the i position of the box, it stores the card that is put in the random index of the deck. This will repeat this process 52 times.

    You can then print, in the end, the final result that will be stored in "box" by using

    Code:
    System.out.println(Arrays.toString(box));
    
    You might need to import the Arrays class.

    Problems you will find by using this simple process:
    1 - Because you are generating a random number for each iteration of the for cicle, you will eventually generate the same number, and you will have duplicated entries in the box array;

    I will leave this process to be solved by you, as grinding this issue will train your mentality to look for these types of errors, and investigate for code on how to fix and create this sort of simple applications. If you want help solving this issue you can PM me and we'll talk on skype.

    I hope I answered your question and helped you at your issue.

    Cheers.
     
  5. Unread #3 - Jan 21, 2014 at 12:52 AM
  6. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    Learning Curve

    You're confusing math and logic. Programming LOGIC takes getting used to. There's no real way to actually learn it apart from to observe, break down and understand. So basically, TRY to do these exercises you're given, give them a good hour or two (treat them as homework). If you can't get it, try googling around for code snippets, and if you still don't understand after Google, THEN look at the answer.

    Math on the other hand can be as simple or as complicated as you want it to be in your own software. There are some applications (especially with calculus) that may be more advanced to put in to programming, but for every application that I have had to write in this regard I have actually done it in a scripting program called MATLAB, which is a scripting language and suite written purely for math purposes. I've even done a bunch of time-based math calculations in something called Simulink (part of MATLAB) which allows drag-and-drop of blocks that represent different math operations.

    For reference, here's what Simulink looks like:
    [​IMG]

    So you can see, programming MATH and programming LOGIC are quite a different category from each other. It IS a challenge to get used to the logic, and math DOES help, but that is because math is purely logical expression itself. The opposite is also true: trying to understand logic in programming can actually help you with your math.

    Look. Listen. Watch. Observe. Learn. Be patient. Sooner or later it will click with you. There's really nothing more you can do.
     
  7. Unread #4 - Jan 21, 2014 at 7:19 AM
  8. SexayMistahBee
    Joined:
    Feb 28, 2006
    Posts:
    2,410
    Referrals:
    0
    Sythe Gold:
    27
    Discord Username:
    SexayMistahBee

    SexayMistahBee Sexiest Bee On Earth
    $50 USD Donor New

    Learning Curve


    Thanks a lot guys, everybody who I asked told me the same.
    I guess that I'll get back to where I've left off now.

    Once again, thanks
    kudos++
     
  9. Unread #5 - Jan 23, 2014 at 4:32 AM
  10. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    Learning Curve

    Just as a general aside to show you what math oriented programming CAN look like, here's a MATLAB script I wrote to find the intersection point of two lines in 3D space as represented by vectors

    Code:
    function P = IntersectionPoint(v0, vDirection, u0, uDirection)
        syms t s
        % Vector equations of lines
        v = v0 - t*vDirection;
        u = u0 - s*uDirection;
        
        % Get x and y components of vector equations
        Vx = v(1);
        Vy = v(2);
        Ux = u(1);
        Uy = u(2);
        
        % Solve each x1==x2 and y1==y2 for the 't' parametre
        tx = solve(Vx == Ux, t);
        ty = solve(Vy == Uy, t);
        
        % Solve tx-ty = 0, and find the value of the 's' parameter
        sValue = solve(tx-ty, s);
        
        % Get x, y, z components of the vector equation for u
        %   and use them as mathematical functions of s in the code.
        P1(s) = u(1);
        P2(s) = u(2);
        P3(s) = u(3);
        
        % Substitute the solved value of the 's' parameter in to
        %   the above functions to find the intersection point
        P = [P1(sValue), P2(sValue), P3(sValue)];
    end
    On the other hand, calculus based math programming is usually very iterative, which means you use loops to crunch numbers over and over again until you get a solution. The smaller the iteration (difference in numbers) the more accurate the answer is.

    Nothing you should really be bothered about I guess, but I figured considering I mentioned MATLAB I might as well show a script for it so you might get an idea of what you do with it.
     
  11. Unread #6 - Jan 27, 2014 at 11:49 AM
  12. SexayMistahBee
    Joined:
    Feb 28, 2006
    Posts:
    2,410
    Referrals:
    0
    Sythe Gold:
    27
    Discord Username:
    SexayMistahBee

    SexayMistahBee Sexiest Bee On Earth
    $50 USD Donor New

    Learning Curve

    So I'm guessing that in MATLAB, % is used for comments?
    Thanks for the info, I'll look it up. Maybe MATLAB will make calculus less boring for me; I'm having fun with programming lol

    MineMarkPT:
    I couldn't come up with an answer to that problem myself, but kinda stumbled across on while solving array-related problems.



    I guess that this is a nice solution to the problem. lol

    I thought of another way to shuffle cards, and I'm going to write the codes for it.
    I don't think that it'd be very efficient, but I guess that it's a start... I guess that I'll post it later.
     
  13. Unread #7 - Jan 27, 2014 at 7:03 PM
  14. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    Learning Curve

    If you intend on going to university and studying anything that has anything to do with math (engineering, sciences, etc.) then there exists a big chance that you'll have something to do with MATLAB.

    It isn't a programming language per se, but it is a scripting language. There's a main window where you can input individual commands, and a scripting interface so you can execute multiple commands without having to type them again. All variables in MATLAB are global and not strictly typed (int, string etc aren't defined. For a variable you literally just type "x = 10") and yeah, comments are %.

    It's actually really good for checking your answers if you know how to use it. In terms of calculus, MATLAB has the capability to integrate or derive most of the functions you can throw at it, so if I wanted to find the derivative of x^2 / ln (x) I would just put in:

    Code:
    % Syms is the command used to define a "symbolic" variable, rather than a numeric one, so instead of x = 1 or whatever you just see "x" like you would if you solved the equation by hand.
    syms x
    y = x^2 / log(x);
    diff(y)
    It also has comprehensive functionality for matrices and vectors (the MAT actually stands for MATRIX in MATLAB).

    Again, I'd recommend learning the basics if you're doing anything strictly math related. It's not very good at making "useful" applications but it sure as hell can crunch numbers for you.
     
  15. Unread #8 - Mar 5, 2014 at 11:18 PM
  16. Oculus
    Joined:
    Jul 16, 2013
    Posts:
    71
    Referrals:
    0
    Sythe Gold:
    0

    Oculus Member
    Banned

    Learning Curve

    Back to your real question,

    How do I study programming?

    Well its different for me I guess because I started young. Sort of just playing with coding I guess. However, now that I'm in college for Software Engineering I can see why people think this is a challenge. So many people who have never coded are in these classes hoping to get big money jobs. The truth is, studying and memorizing is not going to make anything easier for you. Unless you can constantly reference the material, you will never learn this way.

    That having been said, the BEST way to learn programming is through practice... It sounds dumb but its called a programming LANGUAGE. You are now an infant learning new words, if you want the words to stick and become natural you must practice them... Over and over and over... A simple trick I tell people to do in the tutoring lab: take one method, class, etc. that you are trying to commit to memory, and make endless programs using it.

    For example, if you were learning something simple like the Scanner class in Java. Do MANY different programs using it. I'll start you off with the example:
    Code:
    public static void main (String[] args)
    {
         String userIn;
         Scanner scan = new Scanner(System.in);
         
         System.out.print("Please say something to the computer:\t");
         userIn = scan.next();
    
         System.out.print("You typed:\n" + userIn");
    }
    Now you learn something else you can do with a user input (anything really). Then you repeat the above but add in what you just learned. Then do the same with the new material, just adding on to it each time. Eventually the first thing you learned will become second nature to you.
     
< Java help | >

Users viewing this thread
1 guest


 
 
Adblock breaks this site