Advertise here.

Saturday, 25 August 2012

Java programming lesson #8 - Inheritance

Inheritance is the ability of objects in java to inherit properties and methods from other objects. When an object inherits from another object it can add its own properties and methods. The object that is being inherited from is called the parent or base class and the class that is inheriting from the parent is called the child or derived class. How to use inheritance We will first create a parent class called Person. We will then create a child class called Student. Then we will create a program to use the Student class. The following is...

Java programming lesson #7 - Object-Oriented Programming(OOP)

Object oriented programming or OOP is a way of writing programs using objects. An object is a data structure in memory that has attributes and methods. The attributes of an object are the same as variables and the methods of an object are the same as functions or procedures. The reason for using objects instead of the old procedural method of programming is because objects group the variables and methods about something together instead of keeping them all apart as in procedural programming. There are many other reasons why OOP is better which...

Java programming lesson #6 - Arrays

An array is group of variables that is given only one name. Each variable that makes up the array is given a number instead of a name which makes it easier to work with using loops among other things. Declaring an array Declaring an array is the same as declaring a normal variable except that you must put a set of square brackets after the variable type. Here is an example of how to declare an array of integers called a. public class Array{   public static void main(String[] args)   {      int[]...

Java programming tutorial #5 - Data input and type conversions

Until now we have set the values of variables in the programs. Now we will learn how to get input from the user so that our programs are a bit more interesting and useful. Reading single characters To read a single character from the user you must use the System.in.read() method. This will return and integer value of the key pressed which must be converted to a character. We use (char) in front of System.in.read() to convert the integer to a character. After the user has pressed the key they must press enter and we must put a second System.in.read()...

Java programming lesson #4 - Loops

A loop is a way of repeating lines of code. If you want to for example print Hello on the screen 10 times then you can either use System.out.println 10 times or you can put 1 System.out.println in a loop that runs 10 times which takes a lot less lines of code. For loop The for loop goes from a number you give it to a second number you give it. It uses a loop counter variable to store the current loop number. Here is the format for a for loop: for (set starting value; end condition; increment loop variable) You first set the loop counter...

Java programming lesson #3 - Decisions

A decision is a way of allowing a program to choose which code to run. If a condition is met then one section of code will be run and if it is not then another section will be run. The if decision structure The if statement evaluates a condition and if the result is true then it runs the line of code that follows it. Here is an example of how to test if the value of a variable called i is equal to 5: public class Decisions{   public static void main(String[] args)   {      int i...

Java programming lesson #2 - Variables and constants

Variables are places in the computer's memory where you store the data for a program. Each variable is given a unique name which you refer to it with. Variable types There are 3 different types of data that can be stored in variables. The first type is the numeric type which stores numbers. There are 2 types of numeric variables which are integer and real. Integers are whole numbers such as 1,2,3,... Real numbers have a decimal point in them such as 1.89 or 0.12. Character variables store only 1 letter of the alphabet. You must always put...

Java programming lesson #1 First Program

What is Java? Java is an object-oriented programming language which was developed by Sun Microsystems. Java programs are platform independant which means they can be run on any operating system with any type of processor as long as the Java interpreter is available on that system. What you will need You will need the Java software development kit from Sun's Java site. Follow the instructions on Sun's website to install it. Make sure that...