Tuts
11-04-2012, 03:18 PM
-----------------------------
Java IDE
First thing's first, you need to set up an IDE. I'll let Wikipedia explain what an IDE is if you've never come across the term.
An integrated development environment (IDE) is a software application that provides comprehensive facilities to computer programmers for software development
http://en.wikipedia.org/wiki/Integrated_development_environment
Here is a list of Java IDEs:
Eclipse
Netbeans
BlueJ
IntelliJ IDEA
For the purpose of this tutorial, we will be using BlueJ. BlueJ is a fantastic IDE for new Java programmers, it can tell you where things go wrong which is always a plus.
Here's the direct download link - http://www.bluej.org/download/download.html
-----------------------------
Tutorial Notation
Mutator - A mutator is designed to modify existing fields, see type VOID.
public void editAge()
Accessors - Accessors are used to return, display or call values from fields, types include boolean, String, int, double and more - These are data types!
public String getName()
Parameters - Parameters are passed through the method, they're added inside of the brackets. See code below.
public void editAge(int age)
What this does is, when the editAge method is called you will be prompted to add an age.
Constructor - Say your class is called Tutorial, here's the constructor.
public class Tutorial
{
// The constructor is below
public Tutorial()
{
}
//End constructor
}
So there's the notation.
-----------------------------
Your first script - Your information. AFTER EACH STEP, COMPILE (press Compile button).
Hopefully you're now eager to write your first small script, and so we shall.
1. On your BlueJ document, click New Class and set the Class Name to Me. Once it's created you'll see a small yellow box on your window, double click it and delete everything in there. It's all nonsense.
2. So you should have a blank document, we need to declare our class and constructor. Here's our code.
public class Me
{
public Me()
}
3. At present our script does nothing, so let's create some field declarations. Here's a brief overview of what we want our script to hold:
First Name
Surname
Age
School
Year
So, we've got our list, it's probably best we assign relevant data types to our fields.
String - Text that can include numbers e.g. 22 Carb Street.
int - Whole numbers only, also supports a limited range.
double - Whole and decimal numbers e.g. £2.50 (minus the £).
boolean - Returns true or false, nothing else.
In order to declare our fields, we merely add private 'datatype' 'fieldname';
For example, First Name would be private String firstName; - I've named it firstName for ease, it can be anything.
It is imperative you remember the semi-colon at the end of the lines, bar the method title.
public class Me
{
private String firstName;
public Me()
{
}
}
Add all of your fields, try not to peak at mine, you're here to learn.
public class Me
{
private String firstName;
private String surname;
private int age;
private String school;
private int year;
public Me()
{
}
}
4. Now we need to add data to those fields using parameters, remember those?
Declare appropriate parameters in your constructor. You can call your parameter variables anything and you must assign them a datatype.
...
public Me(String myName, String lastName, int myAge, String mySchool, int myYear)
{
}
..
5. Next, we assign our parameters to our private fields. We simply do that by adding..
firstName = myName
Match your private fields with your parameter values. Your whole code should look like this:
public class Me
{
private String firstName;
private String surname;
private int age;
private String school;
private int year;
public Me(String myName, String lastName, int myAge, String mySchool, int myYear)
{
firstName = myName;
surname = lastName;
age = myAge;
school = mySchool;
year = myYear;
}
}
What does the above do? - Good question, assuming you've compiled your code. Go onto BlueJ, right click and then click new Me(String myName, String lastName...)
Once that has appeared, you're prompted to enter values for your parameters. Where you have to enter a String you must wrap it in speech marks, so beside String myName you must put "Tuts" instead of Tuts.
6. Writing Methods (I've put it in bold because Methods are cool)
INCREMEN
Say it's your birthday tomorrow, and in our script your age is 20, you'll want to update it to 21 right? Let's right a method for that. What we want to do is increment (add) 1 to the age every time the method is called.
public void oneYear()
{
age = age + 1;
}
Here's how it works, we're using our mutator because we're changing data or existing fields.
The age = age + 1 simply grabs what's currently inside the age variable and adds 1 to it.
(Note: Remember we set age = myAge in our constructor?). Similarly you could use the shorthand form
age += 1;
RETURN AGE
You'll want to see if your age has changed right? Let's create another method. This time we're using accessors because we're not changing fields.
public int getAge()
{
return age;
}
You're merely returning what's in the age variable, simples.
So now your code should be similar to this:
public class Me
{
private String firstName;
private String surname;
private int age;
private String school;
private int year;
public Me(String myName, String lastName, int myAge, String mySchool, int myYear)
{
firstName = myName;
surname = lastName;
age = myAge;
school = mySchool;
year = myYear;
}
public void oneYear()
{
age = age + 1;
}
public int getAge()
{
return age;
}
}
Let's print all of this out in the format of text.
Declare a mutator called getInformation.
Inside your mutator add:
System.out.println("Hello my name is " + firstName + " " + surname + ". I am " + age + " years old. I attend " + school + " and I'm in year " + year);
Your document should look like this:
public class Me
{
private String firstName;
private String surname;
private int age;
private String school;
private int year;
public Me(String myName, String lastName, int myAge, String mySchool, int myYear)
{
firstName = myName;
surname = lastName;
age = myAge;
school = mySchool;
year = myYear;
}
public void oneYear()
{
age = age + 1;
}
public int getAge()
{
return age;
}
public void getInformation()
{
System.out.println("Hello my name is " + firstName + " " + surname + ". I am " + age + " years old. I attend " + school + " and I'm in year " + year);
}
}
In order to run it, through BlueJ, compile it, right click and then do new Me(String myName, String lastName...)
Input your relevant information, you should see a red box appear at the bottom, right click it and you'll see your three (or more if you wrote your own) methods int getAge(), int oneYear(), void getInformation().
Sorry for any vagueness, grammatical errors and what not. I've got work in a minute so I tried to get through it quickly. It took me 50 minutes to write out such a small tutorial. When I return I'll go through ArrayLists, Arrays and we will implement a text based interface for our small script we've just written.
Thread moved by Chris (Forum Super Moderator): From "Designing & Development". Great guide, thanks for posting! :)
Java IDE
First thing's first, you need to set up an IDE. I'll let Wikipedia explain what an IDE is if you've never come across the term.
An integrated development environment (IDE) is a software application that provides comprehensive facilities to computer programmers for software development
http://en.wikipedia.org/wiki/Integrated_development_environment
Here is a list of Java IDEs:
Eclipse
Netbeans
BlueJ
IntelliJ IDEA
For the purpose of this tutorial, we will be using BlueJ. BlueJ is a fantastic IDE for new Java programmers, it can tell you where things go wrong which is always a plus.
Here's the direct download link - http://www.bluej.org/download/download.html
-----------------------------
Tutorial Notation
Mutator - A mutator is designed to modify existing fields, see type VOID.
public void editAge()
Accessors - Accessors are used to return, display or call values from fields, types include boolean, String, int, double and more - These are data types!
public String getName()
Parameters - Parameters are passed through the method, they're added inside of the brackets. See code below.
public void editAge(int age)
What this does is, when the editAge method is called you will be prompted to add an age.
Constructor - Say your class is called Tutorial, here's the constructor.
public class Tutorial
{
// The constructor is below
public Tutorial()
{
}
//End constructor
}
So there's the notation.
-----------------------------
Your first script - Your information. AFTER EACH STEP, COMPILE (press Compile button).
Hopefully you're now eager to write your first small script, and so we shall.
1. On your BlueJ document, click New Class and set the Class Name to Me. Once it's created you'll see a small yellow box on your window, double click it and delete everything in there. It's all nonsense.
2. So you should have a blank document, we need to declare our class and constructor. Here's our code.
public class Me
{
public Me()
}
3. At present our script does nothing, so let's create some field declarations. Here's a brief overview of what we want our script to hold:
First Name
Surname
Age
School
Year
So, we've got our list, it's probably best we assign relevant data types to our fields.
String - Text that can include numbers e.g. 22 Carb Street.
int - Whole numbers only, also supports a limited range.
double - Whole and decimal numbers e.g. £2.50 (minus the £).
boolean - Returns true or false, nothing else.
In order to declare our fields, we merely add private 'datatype' 'fieldname';
For example, First Name would be private String firstName; - I've named it firstName for ease, it can be anything.
It is imperative you remember the semi-colon at the end of the lines, bar the method title.
public class Me
{
private String firstName;
public Me()
{
}
}
Add all of your fields, try not to peak at mine, you're here to learn.
public class Me
{
private String firstName;
private String surname;
private int age;
private String school;
private int year;
public Me()
{
}
}
4. Now we need to add data to those fields using parameters, remember those?
Declare appropriate parameters in your constructor. You can call your parameter variables anything and you must assign them a datatype.
...
public Me(String myName, String lastName, int myAge, String mySchool, int myYear)
{
}
..
5. Next, we assign our parameters to our private fields. We simply do that by adding..
firstName = myName
Match your private fields with your parameter values. Your whole code should look like this:
public class Me
{
private String firstName;
private String surname;
private int age;
private String school;
private int year;
public Me(String myName, String lastName, int myAge, String mySchool, int myYear)
{
firstName = myName;
surname = lastName;
age = myAge;
school = mySchool;
year = myYear;
}
}
What does the above do? - Good question, assuming you've compiled your code. Go onto BlueJ, right click and then click new Me(String myName, String lastName...)
Once that has appeared, you're prompted to enter values for your parameters. Where you have to enter a String you must wrap it in speech marks, so beside String myName you must put "Tuts" instead of Tuts.
6. Writing Methods (I've put it in bold because Methods are cool)
INCREMEN
Say it's your birthday tomorrow, and in our script your age is 20, you'll want to update it to 21 right? Let's right a method for that. What we want to do is increment (add) 1 to the age every time the method is called.
public void oneYear()
{
age = age + 1;
}
Here's how it works, we're using our mutator because we're changing data or existing fields.
The age = age + 1 simply grabs what's currently inside the age variable and adds 1 to it.
(Note: Remember we set age = myAge in our constructor?). Similarly you could use the shorthand form
age += 1;
RETURN AGE
You'll want to see if your age has changed right? Let's create another method. This time we're using accessors because we're not changing fields.
public int getAge()
{
return age;
}
You're merely returning what's in the age variable, simples.
So now your code should be similar to this:
public class Me
{
private String firstName;
private String surname;
private int age;
private String school;
private int year;
public Me(String myName, String lastName, int myAge, String mySchool, int myYear)
{
firstName = myName;
surname = lastName;
age = myAge;
school = mySchool;
year = myYear;
}
public void oneYear()
{
age = age + 1;
}
public int getAge()
{
return age;
}
}
Let's print all of this out in the format of text.
Declare a mutator called getInformation.
Inside your mutator add:
System.out.println("Hello my name is " + firstName + " " + surname + ". I am " + age + " years old. I attend " + school + " and I'm in year " + year);
Your document should look like this:
public class Me
{
private String firstName;
private String surname;
private int age;
private String school;
private int year;
public Me(String myName, String lastName, int myAge, String mySchool, int myYear)
{
firstName = myName;
surname = lastName;
age = myAge;
school = mySchool;
year = myYear;
}
public void oneYear()
{
age = age + 1;
}
public int getAge()
{
return age;
}
public void getInformation()
{
System.out.println("Hello my name is " + firstName + " " + surname + ". I am " + age + " years old. I attend " + school + " and I'm in year " + year);
}
}
In order to run it, through BlueJ, compile it, right click and then do new Me(String myName, String lastName...)
Input your relevant information, you should see a red box appear at the bottom, right click it and you'll see your three (or more if you wrote your own) methods int getAge(), int oneYear(), void getInformation().
Sorry for any vagueness, grammatical errors and what not. I've got work in a minute so I tried to get through it quickly. It took me 50 minutes to write out such a small tutorial. When I return I'll go through ArrayLists, Arrays and we will implement a text based interface for our small script we've just written.
Thread moved by Chris (Forum Super Moderator): From "Designing & Development". Great guide, thanks for posting! :)