Java Working With Class
Written by Ian Elliot   
Monday, 30 July 2012
Article Index
Java Working With Class
Hands-on example
Why class and objects?
MathExample

 

A Simple Example

To see how it all works, we need an example that not only demonstrates how the Java language works to let you create classes and objects, but something that demonstrates why you might go to the trouble of using it.

Consider for the moment that you need to perform some complicated calculations. You could just write a set of functions, one for each calculation, but in many cases it would be better to group them together so that they could be used as properties and methods of an object.

For example, suppose you needed to work with circles drawn on a graphic display. Then it makes sense to create a class that encapsulate all of the things you might want to do with a circle.

Start a new project called MathExample. Add a new class called Circle. The basic properties of a circle are its position x,y and its radius r - so let's add these as properties to the Circle class:

 public class Circle{
    public double x;
    public double y;
    public double r;

 

They are public and so accessible from outside of the class. The data type double is a double precision number.

We also need to use the value of the constant Pi and there is no reason why we should make this available to the outside world so it can be private:

private double Pi=3.14159;

As well as storing the size and position of the circle it would be useful to have the ability to discover the circumference and so we add a public circumference method:

public double circumference(){
        return 2*Pi*r;
    }

In the same way it would be nice to discover the area of the circle - so lets add an area method. But to do this we need to compute r squared. Let's add a private squared function that simply returns the square of any number:

private double square(double a) {
        return a * a;
    }

 

You could argue that square was sufficiently useful to make public but in the spirit of hiding how the calculations are performed we can also chose to make it private. Finally the area function is:

public double area() {
        return Pi * square(r);
    }

 

The complete list for the class is:

public class Circle {
    public double x;
    public double y;
    public double r;
    private double Pi = 3.14159;

    public double circumference() {
        return 2 * Pi * r;
    }
    private double square(double a) {
        return a * a;
    }

    public double area() {
        return Pi * square(r);
    }
}

 

Now let's write a main function that makes use of the class:

 

public static void main(String[] args) {
    Circle circle = new Circle();
    circle.r = 10.5;
    circle.x = 0;
    circle.y = 0;
    double circ = circle.circumference();
    double area = circle.area();
    System.out.println(circ);
    System.out.println(area);
 }

This starts off by creating a circle object from the class. Notice that we have the problem of finding a suitable name for the class and the object - they can't both be called Circle. This is a standard problem and there are many ways around it. One is to always start a class name with an uppercase letter and use lowercase for objects. So in our example the class is Circle and the object is circle - remember Java is case sensitive. In practice this name problem isn't as big as it is in examples because you generally have more specific names ready to use for instances of a class - bigcircle, bluecircle, offsetcircle and so on.

After creating the circle object we set its radius and position and then use its methods to get the circumference and area which are displayed in the output window.

Notice you can create any number of circles now you have the Circle class and each one is independent of the others and will compute its own area and circumference.

This is just one of the reasons object-oriented programming is a good idea - there are many more more sophisticated reasons for using it.

Before ending this chapter, I should add that our example doesn't do things in the best possible way. There are better ways to do things. For example, instead of using public variables as properties it is better to use methods to acces private variables. There's still a lot to learn  about classes and objects, including constructors, inheritance and more. These ideas are the subject of a later chapter.

 

Modern Java Contents

  1. Getting started with Java
  2. Introducing Java - Swing Objects
  3. Writing Java Code - Methods
  4. Command Line Programs
  5. The Java User Interface - More Swing
  6. Working With Class
  7. Class Inheritance
  8. Building a Java GUI - Containers

 

To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin,  or sign up for our weekly newsletter.

 

blog comments powered by Disqus



Last Updated ( Wednesday, 07 November 2012 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.