Ah, ****-it, i'm board so i thought id give this a go.
This tutorial is going to quickly try and get you to grasps with the basics of Java . Experience in any other languages (javascript, php, visual basic or whatever else you like) will be pretty useful to you when reading this.
First off before you start, im going to suggest usage of a nice little java IDE (Integrated Development Environment) - blueJ and the very quickly go over the basic functionary.
My tutorial will probably make reference to its features, though with a little common sense you will likely be able to do almost all the stuff in any other ide you wish to use.
You can download blueJ from here: http://www.bluej.org/download/download.html
One downloaded, install and run it. You may find you need to install the java JDK* if you haven't already
* i think that's the right one, an error message will tell you though, if you need it.
BlueJ should open up with a a dialog asking weather you want to open an existing file or create a new one. Create a new one, and save it as testingjava . (note: there is no extension, the actual file blueJ uses is actually a folder, including a number of files)
Now select the "New Class" button, and call your new class "pie".
Now you will notice a box called pie will appear in the main area. This is the Pie class, which we are going to be creating. The pie class will contain all the information needed to define a specific pie.
[- Image to be added - ]
Now on to the code, or more so the java rather than just blueJ. Double click the pie object, and the code pad should open. You'll notice that blueJ has actually already set out a basic class for you.
Although this dummy class doesn't do what we want it to, it does give the basic idea of how a class should be set out.Code:/** * Write a description of class pie here. * * @author (your name) * @version (a version number or a date) */ public class pie { // instance variables - replace the example below with your own private int x; /** * Constructor for objects of class pie */ public pie() { // initialise instance variables x = 0; } /** * An example of a method - replace this comment with your own * * @param y a sample parameter for a method * @return the sum of x and y */ public int sampleMethod(int y) { // put your code here return x + y; } }
Just to explain this a little, heres a differnt commented version
Now, to continue with detail ill just outline the basic structure for a few more things.Code:public class pie { // Set up fields. These are variables that are stored and kept in this object. private int x; /** * The method ( maybe better know to you as a function) which has the same name as * the class becomes its constrcutor. This means this method is run * everytime a class is created. */ public pie() { // it sets are fields value. x = 0; } /** * An example of a method - replace this comment with your own * * as it already said, this is just a basic method. */ public int sampleMethod(int y) { // put your code here return x + y; } }
----------------------
Fields:
Private int number;
private String text;
<WhocanUse> <DateType> <NameOfVariable>
WhoCanUse, is basically saying who can use it, if its private (it should almost always be this) then only the class your in. if its public, any class can.
The DataType is the type of data its storing.
String = normal text
int = an integer or whole number
boolean = a true of false value
These are just a few, but probably the most commonly used. Most DataTypes are actually just another object used to store data, for example a string is just an object someone else has written which stores an array of characters.
nameOfVarible, is a quite obvious one: Its just the name of the variable your creating. It can really be what you like, unlike php no $ is needed.
----------------------
A Method:
The above is a really simple method and follows a similar structure to fields in some ways:Code:public void myMethod() { //Does nothing }
<WhocanUse> <DateType> <NameOfMethod>( <paramiters> ){
//Contents
}
Whocanuse again says who can access, is the method internal to the class or should other classes be able to call with it, so as to interact with this object?
DataType is the type of date this method will return when called, If its not going to return any data directly, then set its value to "void",
NameOfMethod, is just what you call the methods.
Parameters are extra bits of information passed to a method. Like with php there seperated by commas, but in java they require datatypes.
(int tempirture, String name, boolean containsNuts)
(String text)
----------------------
The constructor is the same as the above, except it cant return anything hence doesn't need any datatype there.
----------------------
OK, now we have been nicely side tracked and hopefully got a few basics down, lets try and actually do somthing about creating are pie.
First of we need to know what info we need to store about are pie, its best if you deviate from what i do a bit, so u can get to play around as much as possible.
My list:
name - as a string
size - as a number
filling - as a string
temperature - as a number
containsNuts - as a boolean
Most this stuff we want set at the very beginning hence we need to do two things.
1:
Create all the fields needed to contain these values.
2:
Get there values in the constructor, aka add them as paramiters and use the vaules provided to set the feilds.
Temperature will be set at zero rather than taken from a parameter though.
#
Note: before you start, remove the method at the bottom or you will get errors because the field x has been removed
#
Have a go yourself, the see how it compares to my version's if you like? (it doesn't have to be exactly the same)
My fields look like this:
And my constructor looks like this:Code:// set up feilds private String name; private int size; private String filling; private int temperature; private boolean containsNuts;
Note: if a parameter and field your trying to set have the same name, you must put this. before the fields name or the parameter will set itself instead.Code:public pie(String pieName, int pieSize, String pieFilling, boolean pieContainsNuts) { // Set the stuff we got to are pies feilds. name = pieName; size = pieSize; filling = pieFilling; containsNuts = pieContainsNuts; //And tempirture temperature = 0; }
this.name = name
Ok, now before i show you the complete code, click the compile button on the blueJ main window. Hopefully it will come up error free, but if your unlucky there could be an error in there ( check for missing {} and
My completed code if your struck here is this for comprasion
Now, to test out it all works (your version hopefully): To do this, right click the pie object and select the top option: new pie( ..... )Code:public class pie { // set up feilds private String name; private int size; private String filling; private int temperature; private boolean containsNuts; /** * Constructor for objects of class pie */ public pie(String pieName, int pieSize, String pieFilling, boolean pieContainsNuts) { // Set the stuff we got to are pies feilds. name = pieName; size = pieSize; filling = pieFilling; containsNuts = pieContainsNuts; //And tempirture temperature = 0; } }
A dialogue will open asking you to input all the parameters you asked for in your constructor. Add them in (remember: strings require " " around them)
With luck that went successfully. You'll notice an instance of your pie has appeared in the bottom panel of the blueJ window. Right click this and select inspect to see if all your values are stored.
Now to actually start doing something: what we want to do is create a few methods in there. (make them public also)
1) heat pie (will at 10 to temperature)
2) cool pie (will remove 10 from temperatures)
3) Set temperature (will allow the user to set the pies temperature)
Now i recommend you have a go at doing this before looking at my solution, there not as hard as you might think. Remember the basic structure for a method:
-----------------------Code:public void myMethod(){ // do somthing }
Ok, ill assume you ether had a go or were lazy: the first two classes initially require very little code:
The 3rd and final one requires a parameter:Code:public void heatPie(){ temperature = temperature + 10; } public void coolPie(){ temperature = temperature - 10; }
Hopefully that wasn't to hard. But lets just check it works. Again create an instance of pie (right click and select new pie) fill in the required data with what ever you like, and it will again appear at the bottom.Code:public void setTemp(int newTemp){ temperature = newTemp; }
Now when you right click it you will notice the methods you created (coolPie, heatPie etc, are listed there as well. Select them to test if they work. Remember to inspect the object to see if the changes made appear.
To make this easier you could create a method that will return the temperature to you. Remember, instead of void you will need a number data type (int) and you will have to use return (basicly u write return before the value you want the method to give back:
-----------
Had a go? give it a test.
If it doesnt work try comparing it to this to see if you can figure out whats wrong.
Code:public int getTemp(){ return temperature; }
Your next challenge though is to stop cool pie allowing the temperatures to get below zero.
Then try the same thing with setTemp.
To do this you will need to use an if statement. Which is much like its php equivalent.
[code]if(myvarible > 10){ // variable is bigger than
// do this
}
if(thisVarible == 4){ // variable is equal too
//do this
}
if(yetanotherVar != 10){ // variable is not equal to
// do this
}
Have a go, then test to see if they work.
------------------
Remember, other solutions will work just as well, but my current ones, to look at if ya stuck
Code:public void coolPie(){ if(temperature != 0){ temperature = temperature - 10; } }Code:public void setTemp(int newTemp){ if(newTemp < 0) { temperature = 0; } else { temperature = newTemp; } }
---------------------
Sorry that's all i got time for today: I plan to keep going with this tutorial, by adding a number of more complex methods, and eventually working up to getting 2 or 3 interacting classes and useing a few more complex data types. In addition i will also hopefully work to refine the current tutoral parts, fix errors, make stuff clearer, better explain bits and bobs, and possibly restructure alot of it.
Any comments, ideas, suggestions would be great at this point so i know what to add in to future versions.
Disclaimer: this thing was written straight in to this box so may have bugs: the contents of the tutorial are copyrighted to myself and may not be reposted else where without my explicit permission.









Reply With Quote






