1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[Tutorial] Learn Java Programming

Discussion in 'Other Languages' started by buckyaustin, Oct 6, 2013.

  1. buckyaustin

    buckyaustin Newbie

    Joined:
    Aug 27, 2013
    Messages:
    32
    Likes Received:
    24
    Occupation:
    Web Developer
    Location:
    Ireland
    Welcome to buckyaustin's java tutorial. From the creators of Learn xHTML/HTML and CSS.


    One of the most complicated things to start learning is programming. So in this tutorial I hope to open a world of possibilities to you all. At the end of this tutorial you will be well versed in programming jargon. Cause that is all it really is jargon, nothing more nothing less. It isn't even that hard to do. You will notice that as you follow along.


    If you think this is difficult, think again, it is like learning to speak a foreign language. It takes time and practice. I only hope to provide you with the building blocks for this time and practice.


    In this first post I just want to get you set up and to learn the basics. I mean the very basics.
    Ok download eclipse IDE and the Java Development Kit (JDK). Install the JDK, restart your computer then install Eclipse. There is an additional step in Windows where you have to open Eclipse and point eclipse to the JDK. But I won't be explaining that because it is on the eclipse website.


    On Linux open up the command line and type dependent on distro;
    [Debian (.deb)]
    Code:
       
    
    sudo apt-get install openjdk-7-jre eclipse
    [password]
    
    

    [Red Hat (.rpm)]

    Code:
    su
     [password]
     yum install openjdk-7-jre eclipse
    
    

    Restart your computer. Open up eclipse, it will ask you for a workspace, Type in the location where you want to store your programs. I use ?\home\unknown\workspace\?. Eclipse will even create the folder if it doesn't exist.


    Now this is where I have to explain naming conventions. Projects start with an upper case. Packages come after this and start with a lower-case. Classes come after packages and start with upper case. Then after Classes there is variables which start with a lower-case. Any way notice that it is in the order upper, lower, upper, lower. This is called camel cassing and all programming languages use it. The reason for this is to allow the programmer to know what they are using, a package or a variable. Even thou you should know what you are using this does help.


    OK go to file, click new, click project. Name it anything you want. Right click on your project, new, package. Name the package conversion. Right click conversion. Click new and Class. Name the class CurrencyConverter.


    The reason you named the package conversion was because the package can hold many different converter programs, and you will want to make the name meaningful but at the same time abstract.
    The left side of eclipse is called the package explorer. The middle is the text editor. And the bottom is the console. The top is the toolbar.


    Ok go to the text editor and make sure you have this in your CurrencyConverter


    Code:
    [FONT=Times New Roman][SIZE=3]/* 
    Author:  buckyaustin
    Date:      06/10/2013[/SIZE][/FONT]
    [FONT=Times New Roman][SIZE=3]Purpose: To present user with a menu. And display them output[/SIZE][/FONT]
    [FONT=Times New Roman][SIZE=3]Version:  Version 1[/SIZE][/FONT]
    [FONT=Times New Roman][SIZE=3]*/[/SIZE][/FONT]
     
    [FONT=Times New Roman][SIZE=3]public class CurrencyConverter[/SIZE][/FONT]
    [FONT=Times New Roman][SIZE=3]{//start of class[/SIZE][/FONT][INDENT] [FONT=Times New Roman][SIZE=3]public static void main(String [] args) [/SIZE][/FONT] 
    [/INDENT]
    [INDENT][FONT=Times New Roman][SIZE=3]{//start of main method
    [/SIZE][/FONT]
    [/INDENT]
    [INDENT=2] [FONT=Times New Roman][SIZE=3]//lets display an attractive title to our program[/SIZE][/FONT][/INDENT]
    [INDENT]  [FONT=Times New Roman][SIZE=3]        System.[I]out[/I].println("\t\t Currency Converter!");
    
    [/SIZE][/FONT][/INDENT]
    [INDENT=2] [FONT=Times New Roman][SIZE=3]//Display the menu for the user to choose from.[/SIZE][/FONT]
    [/INDENT]
    [INDENT]  [FONT=Times New Roman][SIZE=3]        System.[I]out[/I].print("1: \t Convert to US Dollar." +[/SIZE][/FONT][/INDENT]
    [INDENT=3]             [FONT=Times New Roman][SIZE=3]"2: \t Convert to AUS Dollar." +[/SIZE][/FONT]
    [/INDENT]
    [INDENT=4]     [FONT=Times New Roman][SIZE=3]"3: \t Convert to STG. \n");
    
    [/SIZE][/FONT]
    [/INDENT]
    [INDENT][FONT=Times New Roman][SIZE=3]}//end of main method[/SIZE][/FONT]
    [/INDENT]
    [FONT=Times New Roman][SIZE=3]}//end of class[/SIZE][/FONT]
    [COLOR=#000000]
    [/COLOR]


    Breakdown;



    1. The '/*' and '*/' are the start and end of a block comment. In this block comment you give information about the program. Which version it is, what has been changed, creators name etc.
    2. The '//' is a single line comment anything that comes after this is just ignored, you add them there just for you or anyone else who is going to be working on the program. Make sure you add enough of these for future reference.
    3. '{' are the start of methods. After these I have added comments, that is because you can easily get confused about which curly bracket belongs where and how many are needed in your program. Each curly bracket gets a line to itself. Opening curly brackets mean on the next line you insert an extra tab, closing brackets mean on the next line you insert one less tab/indent.
    4. 'System.out.print' in any of it's forms is just a way to display on the console. '\t' is called an escape character, it escapes from 'Sytem.out.prints' normal rules and inserts a tab. '\n' is another but this time it means new line.
    5. 'System.out.print' displays anything within it's brackets, but just like math anything in the brackts has to be completed before the rest of the math can be done. So the '+' symbol adds/joins the Strings of output together so it displays the way we want.


    What's next?:


    Well in the next post we will be going onto variables, conditional statements and your first introduction into Object Orientated Programming (OO).​
     
    • Thanks Thanks x 6
    Last edited: Oct 6, 2013
  2. Junkfood00

    Junkfood00 Elite Member

    Joined:
    Sep 13, 2011
    Messages:
    1,949
    Likes Received:
    1,339
    Your post has color issues, rectify them to make the tutorial readable.
     
  3. buckyaustin

    buckyaustin Newbie

    Joined:
    Aug 27, 2013
    Messages:
    32
    Likes Received:
    24
    Occupation:
    Web Developer
    Location:
    Ireland
    Could you pm me the colours of the parts that are not readable to you?That would be much appreciated.
     
  4. Junkfood00

    Junkfood00 Elite Member

    Joined:
    Sep 13, 2011
    Messages:
    1,949
    Likes Received:
    1,339
    Ehh?? Just look yourself, it's the second half of your post.
     
  5. bartosimpsonio

    bartosimpsonio Jr. VIP Jr. VIP Premium Member

    Joined:
    Mar 21, 2013
    Messages:
    13,040
    Likes Received:
    11,688
    Occupation:
    CYPHERBARTO
    Location:
    $BHTC
    Home Page:
    The text on the textarea here is black, but once published it becomes white. If you invert it here, it becomes black on the main message. I hope this helps you correct that.
     
    • Thanks Thanks x 1
  6. TheeAriGrande

    TheeAriGrande Regular Member

    Joined:
    Jul 14, 2013
    Messages:
    270
    Likes Received:
    153
    Location:
    Candlestick Park
    Fix the "Breakdown", the text color is black. Change it to something other than black please.
     
    • Thanks Thanks x 1
  7. buckyaustin

    buckyaustin Newbie

    Joined:
    Aug 27, 2013
    Messages:
    32
    Likes Received:
    24
    Occupation:
    Web Developer
    Location:
    Ireland
    Thank you so much. I'll try to correct it now.
     
  8. buckyaustin

    buckyaustin Newbie

    Joined:
    Aug 27, 2013
    Messages:
    32
    Likes Received:
    24
    Occupation:
    Web Developer
    Location:
    Ireland
    Thank you, actually it's brown lol same thing thou, not readable
     
  9. buckyaustin

    buckyaustin Newbie

    Joined:
    Aug 27, 2013
    Messages:
    32
    Likes Received:
    24
    Occupation:
    Web Developer
    Location:
    Ireland
    I use the white theme of BHW it makes all posts have a higher contrast, meaning I can read the posts, all posts more rapidly. But yeah I'll check the original post in various themes to see the problems, that seems to be the best solution so far thank you.
     
  10. buckyaustin

    buckyaustin Newbie

    Joined:
    Aug 27, 2013
    Messages:
    32
    Likes Received:
    24
    Occupation:
    Web Developer
    Location:
    Ireland
    Variables and Conditional Statments

    I know I promised an introduction to OO programming in the last post, but that seems to be too much too fast, I believe that the fundamentals would be more important in this stage.


    Welcome to my second post about learning java. So what is a variable? Well a variable is just an allocation in RAM that stores information. There are different types of variables, 'int', 'double', 'char', 'string' and 'Boolean'. I will be going into each of these at some point and time.


    Conditional statements, now what the hell are they. Actually they are quite simple, for instance is it sunny outside is a conditional question, it has two answers yes or no, these are conditional statements. So using these two conditions we can for instance decide to cut the grass or decide to leave it for a sunny day.


    Open up eclipse again, this time go to package explorer and copy CurrencyConverter and paste it into the same package. Name it CurrencyConverterV2 for version two.


    Code:
    /* Author:   buckyaustin
        Date:      07/10/2013
        Purpose:    To present user with a menu. Respond to their choice accordingly
        Version:  Version 2
        Update Log: Added two variables, if else and switch statements.
    */
    
    
    //importing the Scanner class from java/util folder.
    import java.util.Scanner;
    
    
    public classCurrencyConverterV2 
    {//start of class
        public static void main(String[] args) 
        {//start of main method
            
            //creating a variable to store the users choice
            int choice = 0;
    
    
            //creating a variable of the amount the users wants converted
            double euroAmount = 0.0;
    
    
            //an instantiation of the Scanner class
            Scanner keyboardIn = newScanner(System.in);
    
    
            //lets display an attractive title to our program
            System.out.println("\t\tCurrency Converter!");
    
    
            
            //asking the user for the amount to convert
            System.out.print("How much do you want convert? ");
    
    
            //Storing this in euroamount using our new scanner class
            euroAmount =keyboardIn.nextDouble();
    
    
            
            //Display the menu for the user to choose from.
            System.out.print("1:\t Convert to US Dollar." +
                             "2: \tConvert to AUS Dollar." +
                             "3: \t Convertto STG. \n");
    
    
            //an if statement that looks at the users choice and acts dependent on that.
            if (choice == 1) 
            {//start of if
                System.out.println("USDollar conversion chosen");
            }//end of if
     
    
            else if(choice == 2) 
            {//start of else
                System.out.println("AUSDollar conversion chosen");
            }//end of else
    
    
            else if(choice == 3) 
            {//start of else
                System.out.println("STGconversion chosen");
            }//end of else
    
    
            else
            {//start of else
                System.out.println("Invalid Choice made");
            }//end of else
    
    
    
            
            //a switch statement that looks at the users choice and acts dependent on that
            switch (choice) 
            {//start of switch
    
    
            case1:    System.out.println("US Dollar conversion chosen");
                break;
            case 2:   System.out.println("AUS Dollar conversion chosen");
                break;
            case 3:   System.out.println("STG conversion chosen");
                break;
    
            default:  System.out.println("Invalid Choice made");
                break;
            }//end of switch
    
    
            
            /*notice how switches are smaller?
             * The switch and if else do the same thing.
             * They are called conditional statements.
             */
    
    
        }//end of main method
    }//end of class
    

    Breakdown:

    1. We update the block comment to include the new additions to the code.
    2. 'import java.util.Scanner' means use Scanner which is located in java/util/ in this class.
    3. 'int choice', means store a whole number in a box called choice.
    4. 'double euroAmount', means store a decimal number in a box called euroAmount.
    5. 'Scanner keyboardIn = new Scanner(System.in)' means create a new version of Scanner called keyboardIn. This is instantiation. Simple isn't it?
    6. The if, else if, and else statements are dependent on what the user enters as their choice. If they enter one go to if, if they enter 2 go else if. And if they enter something outside the range go to the only thing left else.
    7. The switch statement, does the exact same thing as if, just in my opinion is a lot easier to read and learn about. But instead of else we have default.


    Tip:Type "switch" and press [ctrl] + [space], this is called inteli-sense, eclipse is smart enough to give you the basic layout of a switch statement. This can save you a lot of heart ache, it works with everything, from variables the whole way up to packages.


    In the next post you will be learning about loops in all it's variations. This is where we start to see a more useful program,because this version still has a lot of problems. The main one right now is that it doesn't allow the user to choose when the program should end, it just ends. We will be fixing that.
     
    • Thanks Thanks x 2
    Last edited: Oct 7, 2013
  11. buckybrendan

    buckybrendan Registered Member

    Joined:
    Aug 6, 2013
    Messages:
    64
    Likes Received:
    72
    Occupation:
    Web Developer
    Location:
    Ireland
    Nice post austin. You fixed the color scheme, its readable now.

    I hope ppl read this tutorial and follow it, as java is a great language to learn first. It will help you begin with whatever language you wish to learn, or if you want to learn java this will be a good tutorial to start with.
     
  12. rob1977

    rob1977 Power Member

    Joined:
    Mar 25, 2013
    Messages:
    775
    Likes Received:
    677
    Cheers for this (I've just spotted it)

    I've been following Brendan html css tut's and thought they were great learned loads.

    Looking forward to having a bash at this to thanks very much
     
  13. buckyaustin

    buckyaustin Newbie

    Joined:
    Aug 27, 2013
    Messages:
    32
    Likes Received:
    24
    Occupation:
    Web Developer
    Location:
    Ireland
    Thanks for your kind words man, yeah were bro's me and Brendan. He was told to join BHW, and he informed me of how good BHW was.

    I hope people are noticing that this is process of creating a fully working program from the beginning to the finish. All explained. I am also trying to push it in a way to be more useful and relievant to the readers, in the exact same way a business want you to code.
     
  14. buckyaustin

    buckyaustin Newbie

    Joined:
    Aug 27, 2013
    Messages:
    32
    Likes Received:
    24
    Occupation:
    Web Developer
    Location:
    Ireland
     
    • Thanks Thanks x 1
  15. buckyaustin

    buckyaustin Newbie

    Joined:
    Aug 27, 2013
    Messages:
    32
    Likes Received:
    24
    Occupation:
    Web Developer
    Location:
    Ireland
     
    • Thanks Thanks x 1
  16. Gophering

    Gophering Junior Member

    Joined:
    Mar 21, 2013
    Messages:
    115
    Likes Received:
    279
    Occupation:
    Unemployed
    Location:
    EU
    Can't wait till we get to the factory factory factories!

    Just kidding. Good job OP.
     
  17. Chris22

    Chris22 Regular Member

    Joined:
    Sep 29, 2010
    Messages:
    400
    Likes Received:
    1,062
    I'm stoked for the IOC container for the factory factory factories, that's when it gets intense
     
  18. buckyaustin

    buckyaustin Newbie

    Joined:
    Aug 27, 2013
    Messages:
    32
    Likes Received:
    24
    Occupation:
    Web Developer
    Location:
    Ireland
    Thanks guys, I do actually get the jokes, but I really don't want anyone here to try and use what I am teaching to get jobs, I want the readers to make there own ways, I just hope to help them through that path.
     
  19. buckyaustin

    buckyaustin Newbie

    Joined:
    Aug 27, 2013
    Messages:
    32
    Likes Received:
    24
    Occupation:
    Web Developer
    Location:
    Ireland
     
    • Thanks Thanks x 1
  20. pxoxrxn

    pxoxrxn Supreme Member

    Joined:
    Dec 21, 2011
    Messages:
    1,398
    Likes Received:
    2,073
    Buckyaustin are you thenewboston?