1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Java Programming Tutorial Part2 - Netbeans, Classes and Sample Project

Discussion in 'Programming & Webmastering' started by Oliver_FF, Aug 16, 2008.

  1. Oliver_FF

    Oliver_FF New Member

    Joined:
    Oct 15, 2006
    Messages:
    546 (0.19/day)
    Thanks Received:
    65
    Netbeans
    Netbeans is a FREE java IDE that has loads of cool features (any many totally unnecessary, as with most IDE's these days).

    Netbeans is available for download here:
    http://download.netbeans.org/netbeans/6.1/final/
    You want any bundle as long as it contains Java SE - thats Java StandardEdition ;) The IDE also does loads of other stuff - if you're interested you can get the giant bundle, or just the Java SE bundle. Whatever floats your boat. :p


    Sample Project
    Ok, so i've been doing some thinking about some simple easy project thats not as boring as hell... I've come up with creating a small app that acts as like a forum. You can create users, post in threads in forums etc etc. There's plenty of scope for people to add to the project and learn new things should they want to.

    Desired functionality in v1:
    • Be able to create a number of users
    • Be able to create a number of threads
    • Be able to post in threads
    • Be able to view threads
    I will assume for the moment that there is only one subforum to post in.


    Creating our project
    [​IMG]
    Once you've installed netbeans, open it up and go to File -> New Project.

    [​IMG]
    Pick a new Java Application

    [​IMG]
    I'm going to call this "TPUForumExample" and click Finish.

    There's the basis for out project :)

    The First Step
    Ok, first up we're going to create a Class called "user" which will represent each person who uses our 'forum'.

    [​IMG]
    Right click on the top of the tree on the left and select New -> Java Class...

    [​IMG]
    In the new window, name our class "User" (with a capital U) and pick our projectname from the Package option. Click Finish.

    [​IMG]

    Data types in Java
    Whenever you want to store some data in Java you have to chose what data type you want to use for it. Here are some of the main data types and examples of data you put in them.
    Code:
    Name     | Descrption            | Examples
    -------------------------------------------------------------------------
    String    Series of characters    "Oliver_FF", "The Ninj4", "Bob", "12345"
    int       Whole numbers           1, 56, 9990, 374362, 3434333
    double    Decimal numbers         56.993, 573.222, 0.001, 6666992.2
    boolean   True/False              True, False
    char      Single character        'a', 'b', 'h', 'w', 'z', '1', '@'
    

    The structure of a Class
    Code:
    public class [Class Name]
    {
        [Fields]
    
        [Constructor]
    
        [Methods]
       
    }
    Fields:
    Fields are where you store data that is relevant to the class you are working in. In our User class for example, things like the user's name, their signature, post count etc are all data items that might be Fields.

    Declaring a Field:
    Code:
    private [type] [name];
    It is good practise to declare ALL fields as PRIVATE. I'll explain what another time.

    Adding Fields to our User class:
    Ok, so here's a list of a few things that I think we should store about each User:
    1. Their username
    2. Their password
    3. Their email address
    4. Their signature
    5. Their post count

    Now, we need to decide what data types to use. Numbers 1-4 are variable length series of characters, so we will use Strings. Number 5 will be whole number values, so we will use int.

    Code:
    private String userName;
    private String password;
    private String email;
    private String signature;
    private int postCount;

    Methods:
    Methods define how the objects in your program interact with eachother and themselves. You use them to pass data between objects or to process data - things like that. For out User class, we'll only be needing some very basic methods to access the data inside the User objects.

    Declaring a Method:
    Code:
    [public/private] [return-type] [name]([parameters?])
    {
        return [data];
    }

    Adding some methods to our class:
    Code:
    public String getUserName()
    {
       return userName;
    }
    This method will simply get the username contained inside a User object.

    Code:
    public void setUserName(String newUserName)
    {
       userName = newUserName;
    }
    This method takes a String parameter, and uses it to set the username of a User object.

    How methods are used and how they fit together will become apparent in the next part, when we create the Post class.


    The Constructor:
    The constructor is basically a method that is run when you first create an Object out of a class. For now we will not bother with one (Java will default to an empty constructor if none are provided).


    Next Time:
    Creation of the Post class.
    More data types.
    Stuff...

    Lots more will become apparent as soon as the second class is completed. Hopefully lightbulbs should start clicking soon ;)

    Attached Files:

    • 1.jpg
      1.jpg
      File size:
      82.7 KB
      Views:
      22,050
    • 2.jpg
      2.jpg
      File size:
      84.2 KB
      Views:
      21,641
    • 3.jpg
      3.jpg
      File size:
      80.4 KB
      Views:
      21,340
    • 4.jpg
      4.jpg
      File size:
      86.9 KB
      Views:
      21,438
    • 5.jpg
      5.jpg
      File size:
      87 KB
      Views:
      21,338
    • 6.jpg
      6.jpg
      File size:
      73 KB
      Views:
      22,342
    2pu200 says thanks.
  2. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (5.03/day)
    Thanks Received:
    5,615
    Location:
    Cheeseland (Wisconsin, USA)
    Nice job, I'm looking forward to your next installments !
  3. Oliver_FF

    Oliver_FF New Member

    Joined:
    Oct 15, 2006
    Messages:
    546 (0.19/day)
    Thanks Received:
    65
    It's actually well hard to get out the really basic concepts :eek:

    I'll try and clear everything up in part3... i got distracted by the bank holiday weekend and ended up doing nearly 300miles on my motorbike :laugh:
  4. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (5.03/day)
    Thanks Received:
    5,615
    Location:
    Cheeseland (Wisconsin, USA)
    I agree. Once someone undestands the concepts of OOP then learning the various languages is just a matter of learning the syntax and API calls. The modern OOP languages all accomplish the same thing with only variations in implimentation.

    I remember the good old days when the language you used was dictated by the function you wanted the program to perform (ie. if you were doing high level math, like FFTs, you used Fortran. Period.)

    It's still true to some extent today, but the lines have greatly blurred due to the inclusion of thousands of API modules and functions and the great job of optimization that the latest compilers do.

    Anyway, go ride your motorcycle all you want. No hurry on the tutorial :toast:
  5. bruwmac New Member

    Joined:
    Jun 20, 2010
    Messages:
    1 (0.00/day)
    Thanks Received:
    0
    Did you ever continue this tutorial?

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page