Coding Bootcamp: Creating classes

Defining the term class

Class representation



Porting a 'logical' entity into a class


Class name

public class Circle {
    /* class body */
}

Instance variables

class Circle {
    /* Instance variables are defined here*/
    
    /* Point x coordinate */ 
    private int x;
    /* Point y coordinate */
    private int y;
    /* Circle's radius */
    private int radius;
}

Class methods

class Circle {

    /* Point x coordinate */ 
    private int x;
    /* Point y coordinate */
    private int y;
    /* Circle's radius */
    private int radius;

    /* Methods are defined here */

    /* Returns the x coordinate */
    public int getX() { return x; }
    /* Returns the y coordinate */
    public int getY() { return y; }
    /* Returns the radius */
    public int getRadius() { return radius; }
}

Access modifiers

Getters and setters methods

Getters and setters methods

The keyword this

class Circle {

    /* x and y coordinates and radius */
    private int x;
    private int y; 
    private int radius;

    public void setCoordinates(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

Anatomy of a method

public double getPerimeter(double pi) {
    double perimeter = 2*pi*this.radius;
    return perimeter; 
}

Non-returning methods

void methods perform an operation without returning a value


public void printPerimeter(double pi) {
    double perimeter = 2*pi*this.radius;
    System.out.println("Perimeter is " + perimeter); 
}

Overloading methods

Is the ability to have more than one methods with the same name but with different parameters. Different in sense of, type, number and order

/** valid overloaded methods **/
public void dummy() {}
public void dummy(String message) {} // different number of arguments
public void dummy(String message, int number) {} // different number of arguments
public void dummy(int number) {} // different number of arguments
public void dummy(int number, String message) {} // different order of arguments
public void dummy(float number) {} // different type of arguments
public int dummy(String message, double d) { return 0; } // different return type. The return type does not affect the overloading process
private void dummy(int[] array) {} // different type of arguments. Private does not affect the overloading process
public static void dummy(double real) {} // static modifier does no affect the overlaoding process
public final void dummy(float f1, float f2) {} // final modifier does no affect the overlaoding process

/** invalid overloaded methods **/
public void dummy(String text) {} // different argument name but same type
public void dummy(String message, int x) {} // the same as above

In other words, only the signature of the method affects its overloading ability

Constructor - a special method!

/* default constructor */
Circle() {
    this.x = this.y = 0; // you can omit this line. x and will be initialized to zero either way. 
    this.radius = 3;
}

Overloaded constructor

/* Overloaded constructor */
Circle(int x, int y) {
    this.x = x;
    this.y = y;
    this.radius = 3; //hard coded
}
/* Overloaded constructor */
Circle(int x, int y, int r) {
    this.x = x;
    this.y = y;
    this.radius = r;
}

Creating instances of type Circle:

    Circle c1 = new Circle();
    Circle c2 = new Circle(4,4);
    Circle c3 = new Circle(3,4,5);

Chaining constructors

Copying objects

Circle c1 = new Circle(5,2,5);
Circle c2 = new Circle(10,4,10);
c2 = c1;

Memory allocation


Circle c1 = new Circle(5,2,5);
Circle c2 = new Circle(10,4,10);
c2 = c1;

Copying objects (2)

c1.calculatePerimeter();
c2.calculatePerimeter();

Copying objects (3)

Circle c1 = new Circle(5,5);
Circle c2 = c1;
c2.setRadius(10);
c1.calculatePerimeter();
c2.calculatePerimeter();

Copying objects (4)

Circle c1 = new Circle(5,5);
Circle c2 = c1;
c2.setRadius(10);
c1.calculatePerimeter();
c2.calculatePerimeter();

Copy constructor

/* Regular Overloaded Constructor */
Circle(int x, int y, int r) {
    this.x = x;
    this.y = y;
    this.radius = r;
}

/* Copy Constructor */
public Circle(Circle original) {
    this(original.x, original.y, original.radius);
}

Garbage Collector and Destructor method

/** Circle destructor */
public void finalize() {
    System.out.println("Circle (" + 
        this.x +"," + this.y + "," + this.radius + ") deleted");
}

Static fields and methods

class Circle {
    /* counts the number of the created circles */
    public static int count;

    /* Constructor */
    Circle() {
        count += 1; // increase count by one after creating a new circle 
    }
}

Static fields and methods (2)

c1 count is 2
c2 count is 2

Memory allocation of static fields


Circle c1 = new Circle();
Circle c2 = new Circle();

The final modifier

The final modifier (continued)

Circle c1 = new Circle();
Circle c2 = new Circle();
Circle c3 = new Circle();

System.out.println("c1 count is " + Circle.getCount() +  ", id is " + c1.getId());
System.out.println("c2 count is " + Circle.getCount() +  ", id is " + c2.getId());
System.out.println("c3 count is " + Circle.getCount() +  ", id is " + c3.getId());

Singleton

class DBConnector
{
    /* unique instance of the class */
    private static DBConnector unique_db_connection_instnce;

    /* Private constructor of the class */
    private DBConnector()
    {
        ... 
    }

    /* method that creates (if not initialized)
    and returns the unique_db_connection_instnce of the class */
    public static  DBConnector getInstance()
    {
        if (unique_db_connection_instnce == null)
            unique_db_connection_instnce = new DBConnector();

        return unique_db_connection_instnce;
    }

    /* sample method */
    public void connect()
    {
        ... 
    }
}

Classes as custom types

class Laptop {
    /* The serial number of the product */
    private String serialNumber;
    /* Custom class representing the central processor unit */
    private Cpu cpu;
    /* Custom class representing the random access memory */ 
    private Memory memory;
    /* Custom class representing the Operating system license
       The license is bound to this Laptop object */
    private OSLicense osLicense;
}

Classes as custom types (continued)

class Laptop {
    private String serialNumber;
    private Cpu cpu;
    private Memory memory;
    private OSLicense osLicense;

    /* Constructor */
    Laptop () {
        this.serial_number = "XXXX-XXXX-XXXX-XXXX";
        this.cpu = new Cpu();
        this.memory = new Memory();
        //osLicense is not initialized
    }
}

Deep vs shallow copies

Packages


Relationship among classes


Simple dependency


Association/Aggregation


Composition


References

Exercise 1

Exercise 1 (continued)

Exercise 1 (continued)

Exercise 1 (continued)

Exercise 1 (continued)

Exercise 1 (continued)

Exercise 1 (continued)

Exercise 1 (continued)

Exercise 1 (continued)

Exercise 1 (continued)

Exercise 2

Exercise 2 (continued)

Exercise 3

You have the following relations between entities:

Exercise 3 (continued)

Exercise 3 (continued)

Exercise 3 (continued)

Exercise 3 (continued)

Exercise 3 (continued)

Exercise 3 (continued)

Exercise 3 (continued)

Exercise 3 (end)

The following books are available at the library for renting
Books available for renting:
    1. Book [title=Book1, author=Ruth, isbn=368777540-2, physicalCopies=10, availableCopies=2, timesRented=20]
    2. Book [title=Book2, author=Diane, isbn=963099898-2, physicalCopies=10, availableCopies=1, timesRented=22]
    3. Book [title=Book4, author=Rachel, isbn=538310208-2, physicalCopies=10, availableCopies=3, timesRented=24]
    4. Book [title=Book5, author=Joan, isbn=562448132-2, physicalCopies=10, availableCopies=4, timesRented=26]
    5. Book [title=Book6, author=Theresa, isbn=670364563-2, physicalCopies=10, availableCopies=2, timesRented=21]
    6. Book [title=Book7, author=Angela, isbn=466916869-2, physicalCopies=10, availableCopies=5, timesRented=17]
    7. Book [title=Book9, author=Lisa, isbn=052469721-2, physicalCopies=10, availableCopies=6, timesRented=17]
    8. Book [title=Book10, author=Ruth, isbn=609291817-2, physicalCopies=10, availableCopies=3, timesRented=13]
    9. Book [title=Book11, author=Diane, isbn=451378028-2, physicalCopies=10, availableCopies=8, timesRented=12]
    10. Book [title=Book12, author=Jacqueline, isbn=142352773-2, physicalCopies=10, availableCopies=6, timesRented=20]
    11. Book [title=Book14, author=Joan, isbn=631942468-2, physicalCopies=10, availableCopies=3, timesRented=20]
    12. Book [title=Book18, author=Ruth, isbn=549307784-2, physicalCopies=10, availableCopies=1, timesRented=20]
    13. Book [title=Book19, author=Ruth, isbn=368777230-2, physicalCopies=10, availableCopies=23, timesRented=20]
Book with title= 'Book3' found! Details: 
    Book [title=Book3, author=Jacqueline, isbn=005382097-2, physicalCopies=10, availableCopies=0, timesRented=23]
Book with author= 'Ruth' found! Details:
    Book [title=Book1, author=Ruth, isbn=368777540-2, physicalCopies=10, availableCopies=2, timesRented=20]
Book with author= 'Ruth' found! Details:
    Book [title=Book10, author=Ruth, isbn=609291817-2, physicalCopies=10, availableCopies=3, timesRented=13]
Book with author= 'Ruth' found! Details:
    Book [title=Book18, author=Ruth, isbn=549307784-2, physicalCopies=10, availableCopies=1, timesRented=20]
Book with author= 'Ruth' found! Details:
    Book [title=Book19, author=Ruth, isbn=368777230-2, physicalCopies=10, availableCopies=23, timesRented=20]
Book with author= 'Ruth' found! Details:
    Book [title=Book20, author=Ruth, isbn=793027213-2, physicalCopies=10, availableCopies=0, timesRented=20]
Most popular book: 
    Book [title=Book5, author=Joan, isbn=562448132-2, physicalCopies=10, availableCopies=4, timesRented=26]
Book with title:'Book0' not found
Book with author:'angor' not found

Exercise 4 (BONUS!)


Creative Commons Licence
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.