• 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

Oliver_FF

New Member
Joined
Oct 15, 2006
Messages
544 (0.08/day)
Processor Intel q9400 @ stock
Motherboard Lanparty P45-T2RS
Cooling Zalman CNPS-9500
Memory 8GB OCZ PC2-6400
Video Card(s) BFG Nvidia GTX285 OC
Storage 1TB, 500GB, 500GB
Display(s) 20" Samsung T200HD
Case Antec Mini P180
Audio Device(s) Sound Blaster X-Fi Elite Pro
Power Supply 700w Hiper
Software Ubuntu x64 virtualising Vista
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

Once you've installed netbeans, open it up and go to File -> New Project.


Pick a new Java Application


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'.


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


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



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 ;)
 

Attachments

  • 1.jpg
    1.jpg
    82.7 KB · Views: 28,928
  • 2.jpg
    2.jpg
    84.2 KB · Views: 27,215
  • 3.jpg
    3.jpg
    80.4 KB · Views: 26,513
  • 4.jpg
    4.jpg
    86.9 KB · Views: 26,896
  • 5.jpg
    5.jpg
    87 KB · Views: 26,846
  • 6.jpg
    6.jpg
    73 KB · Views: 29,359

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
Nice job, I'm looking forward to your next installments !
 

Oliver_FF

New Member
Joined
Oct 15, 2006
Messages
544 (0.08/day)
Processor Intel q9400 @ stock
Motherboard Lanparty P45-T2RS
Cooling Zalman CNPS-9500
Memory 8GB OCZ PC2-6400
Video Card(s) BFG Nvidia GTX285 OC
Storage 1TB, 500GB, 500GB
Display(s) 20" Samsung T200HD
Case Antec Mini P180
Audio Device(s) Sound Blaster X-Fi Elite Pro
Power Supply 700w Hiper
Software Ubuntu x64 virtualising Vista
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:
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
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:
 
Top