# Representaing information with Objects

• Representing information inside the computer

• We can represent most things in the real world inside the computer by using numbers

• Examples:

• How old you are by

an integer variable named age that represents the number of years you have lived.

• How happy you are by

a floating point variable named happiness that contains the "level" of happiness (that's what happens in the popular game "Sims")

• And so on

• Real-life objects are represented inside the computer by representing only the essential properties

• Examples:

• A BankAccount can be represented by

• a variable accountNumber that represents the number of the account
• a variable accountHolder that represents the name of the account holder
• a variable balance that represents the balance in the account

• A Car can be represented by

• a variable fuelCapacity that represents the amount of gas that the tank can hold
• a variable fuelLevel that represents the amount of gas that is in the tank right now.
• a variable fuelEfficiency that represents the number of miles that the car can travel per gallon of fuel
• a variable odometer that represents the number of miles that the car has been driven

• So how are objects represented inside the computer and how are they "created" ???

• We will use a simple object - a Rectangle - to illustrate these concepts.

• Representing rectangle objects inside the computer

• Rectangle objects is a simple polygone

Polygone objects are commonly used in video games:

The more polygones are used in computer graphics, the more smoother the object will appear.

• In a video game, the polygones objects are placed in a 3D coordinate system.

We will not use such a complicated system to illustrate the point of how rectangles are represented.

We will use a 2D coordinate system

• The commonly used 2D coordinate system in computer program is as follows:

You may have noticed from your Math classes that the y-axis is flipped

But then again, you are not doing a Math course... and when in Rome, do as Romans do...

• The following figure shows 3 rectangles in the coordinate system:

• The \$64,000 question is:

 How can we represent a rectangle shape in a computer ???     I.e.: what information is needed to reconstruct the rectangle ???

 You can use one of these sets of information to re-construct a rectangle: The coordinates of lower left corner and the coordinates of upper right corner of the rectangle The coordinates of lower right corner and the coordinates of upper left corner of the rectangle The coordinates of any one corner point and the dimensions (length and width) of the rectangle

• Java's library's Rectangle class

• The Java library provides a Rectangle that you can use to represent rectangles.

• The representation choice used by Java is:

 A rectangle is represented by the coordinates of upper left corner point and the dimensions (length and width) of the rectangle

• Now take a look at the Java's documentation on the Rectangle class: click here

Look for the section "Field Summary" (this section describes the variables (or fields) contained in the class)

 int height The height of the Rectangle. int width The width of the Rectangle. int x The x coordinate of the Rectangle. int y The y coordinate of the Rectangle.

• A Rectangle object in Java is represented 4 integer variables

The 4 integer variables are used as follows:

• int x                         (for the x-coordinate of the top left corner),
• int y                         (for the y-coordinate of the top left corner),
• int width               (for the width of the rectangle) and
• int height             (for the height of the rectangle)

• As you know, the computer memory is used to store information:

• Here is an example how the 3 rectangles are represented inside the computer:

• Each rectangle is represented as a set of 4 numbers stored in consecutive memory location

• As such, you only need to remember the location of the first value to find all the 4 numbers (the address location of the first value is highlighted in red)

• NOTE: in the above figure, I depicted that one number is stored inside one memory cell. In reality, several memory cells are used to store one double precision number - this detail has been omitted for simplicity.

• Creating Rectangle objects

• As you have already know, we use the new operation in Java to create/instantiate a new object.

• Here, I want to elaborate on the new operation.

• In general, there can be many ways to create objects in a class.

 The different ways to create objects of a class is defined inside the definition of the class.

• Therefore, to learn how (i.e., what information you need to provide) to create objects of a class, you must read the documentation of the class

• So, if you want to learn how to create Rectangle objects, you must read the documentation for the Rectangle class: click here )

Look for the section "Constructor Summary"

• From the documentation, we see that there are 7 ways to create a new rectangle (see "Constructor Summary"):

 Rectangle() Constructs a new Rectangle whose top-left corner is at (0, 0) in the coordinate space, and whose width and height are both zero. Rectangle(Dimension d) Constructs a new Rectangle whose top left corner is (0, 0) and whose width and height are specified by the Dimension argument. Rectangle(int width, int height) Constructs a new Rectangle whose top-left corner is at (0, 0) in the coordinate space, and whose width and height are specified by the arguments of the same name. Rectangle(int x, int y, int width, int height) Constructs a new Rectangle whose top-left corner is specified as (x, y) and whose width and height are specified by the arguments of the same name. Etc. etc.

• From the descriptions (and what we know about the new operation), we can tell how to create Rectangle objects.

• Example 1: create a Rectangle with top left corner location at (0,0) and dimensions length = 0 and height = 0.

1. From the documentation of Rectangle , we see that we can use:

 Rectangle()

2. We know that we need to use the new operation to create objects, so the command to create a Rectangle with top left corner location at (0,0) and dimensions length = 0 and height = 0 is:

 new Rectangle()

3. Finally, we need to remember the location of memory where the representation of the newly created Rectangle is stored.

We use a Rectangle variable for this purpose:

 Rectangle x; x = new Rectangle();

Example 2: create a Rectangle with location of top left corner at (3, 4) and dimension width = 10 and height =20.

1. From the documentation of Rectangle , we see that we can use:

 Rectangle(int x, int y, int width, int height)

This tells use that we need to provide the following information to the Rectangle() method:

1. First, an int value that will be the "x" coordinate (BTW, the variable name "x" is chosen for our convenience - Java could have picked some very weird name)

2. Second, an int value that will be the "y" coordinate (Again, the variable name "y" is chosen for our convenience)

3. Third, an int value that will be the "width" of the Rectangle() (Again, the variable name "width" is chosen for our convenience)

4. Finally, an int value that will be the "height" of the Rectangle() (Again, the variable name "height" is chosen for our convenience)

We provide these value as parameters to the method as follows:

 Rectangle(3, 4, 10, 20)

2. We know that we need to use the new operation to create objects, so the command to create a Rectangle with top left corner location at (3,4) and dimensions length = 10 and height = 20 is:

 new Rectangle(3, 4, 10, 20)

3. Finally, we need to remember the location of memory where the representation of the newly created Rectangle is stored.

We use a Rectangle variable for this purpose:

 Rectangle x; x = new Rectangle(3, 4, 10, 20);

• Technical details on creating Rectangle objects (the new operation)

• Introductory question:

Using what you have learned about representing rectangles, can you tell what the new operation needs to do in order to create the representation of the following rectangle:

• Reserve computer memory to store 4 integer numbers

• Store 10 in the first memory location (x)

• Store 20 in the second memory location (y)

• Store 40 in the third memory location (width)

• Store 20 in the fourthmemory location (heigh)

• Last, but not least (Very important):

Return the address of the FIRST location of the memory segment

This address is stored away so you can find the representation of the rectangle !!!

• OK, we have enough information to understand how computer create Rectangle objects.

Now, let's look at the language constructs to tell the computer to do so...

Technical Material

• The effect of the new operation

• I want to take you behind the scenes and show you what happens when an object is created by the new operation.

Hopefully, this will demystify some of the things you see in the computer program.

• Effect of the new operation:

 The new operator reserves a block of memory to store the representation of a ClassName object This is popularly known as "creating" or "instantiating" a ClassName object We will see later that classses have variables. The new operation will reserve space to store the variables of a class Often, parameters are provided which Java can use to initialize the reserved memory space of the newly created object. Finally, the new operator return the location of memory block (so that the program can find the values back !!!)

• NOTE:

• the new operation returns an address which is an integer number.

• In other words, the entire expression

 new Rectangle(.....)

look and feel like an address

This address is stored away in variable "x" when we write:

 Rectangle x; x = new Rectangle(3, 4, 10, 20);

We will now look at some examples...

• Example of the new operation: Creating a Rectangle object

Suppose we execute the follow new operation:

 Rectangle R1; R1 = new Rectangle();

1. First, the following will happens inside the computer when the computer executes

 Rectangle R1; // define variable R1

• A block of memory is reserved that can store an address value

• The computer identifies this memory location with the name R1 and can also use the name R1 to retrieve information from this memory location.

Graphically:

2. Next, the computer executes

 new Rectangle();

This will create a rectangle by reserving memory for 4 integer variables:

Graphically:

• When the new operation terminates, it will return the address of the reserved memory

• In this example, the new operation will return the value 2000

3. Finally, the computer executes the assignment statement:

 R1 = new Rectangle(); or: R1 = 2000; // Because new returns 2000

Graphically:

You can see that we can obtain the rectangle representation through the address stored in the variable R1