In this IB Computer Science lesson you will be learning about:
In the last lesson you looked at creating an object with only data. You could refer to this data as the attributes or characteristics of the object.
Sometimes the objects you want to create might also have behaviors associated with them. In object oriented programming you encapsulate (combine and hide) the attributes and behaviours together in one class.
Example: Fraction Objects
Suppose you wanted to create a new data type to represent a rational number (a fraction)
Attributes:
Behaviors:
Example: Phone Book Objects
Suppose you wanted to create a new data type to a phone book
Example: Space Ship in a Game
Suppose you were making a game and created a space ship for the player to control
Just as with basic data objects, the attributes are represented as variables, but the behaviours will be represented as methods in your class.
There are different types of methods that “could” be contained in your Class.
Constructors
Accessor and Mutator Methods
Helper Methods
In the Rest of this note we will be looking at the following example about a Bank
You have been given a task by a Bank to help write some software to create bank accounts for their customers. The bank needs to be able to create accounts with the customers name and an initial balance. Bank employees will need to be able to deposit money into the account, withdraw money from the account, and display the current balance of the account
What fields are necessary?
What Constructors are necessary?
What Behaviors are necessary?
Deposit Method
Withdraw Method
Display Method
The fields can be declared in the same way they were for data objects, except for one thing. You need to ask yourself as the programmer of the class if another programmer using your class should be able to access those fields directly using object.name notation or should they be hidden from that programmer and only allow access through methods where you have control over what values are acceptable. Generally speaking you should always hide your fields from other users. To accomplish this task you should declare them as private variables instead of public variables.
In Our bank example the code would be written as follows
A constructor is a way to tell the user of the class how to create objects.
In our bank example the constructor will accept 2 parameters as inputs, which represent the owner and the initial balance of the bank account. So inside the method you will have to assign the field values to the input parameters.
Here are two possible ways of doing this in Java
There isn’t any real difference between them except with the first method you use the keyword “this” in front of the field name.
You might ask why bother to do that? Well if the first method I used the variables in the parameters as the same variable name as the fields in the class. This was done for readability sake. So I couldn’t make a statement like owner = owner as the program wouldn’t know that the first owner referred to the class variable and the second owner referred to the parameter variable. Adding the “this” notation clears it up for the compiler. In the second method I just avoided having the same variable name in my parameters. Its not ideal as it might not be clear what I’m actually trying to pass from the constructor.
Now that you have defined in the class how Account objects will be created, here would be an example of how to use them in another program
This code will create two different bank accounts:
If you tried to create a new account as follows, then an error message would be generated for the bank employee and it would still create the account but with an opening balance of $0.
It is possible to define more than one Constructor for your class, that allows objects to be created in different ways, but we will revisit that concept in a later lesson.
The Behaviour Methods are created just like any other method in Java except you don’t need to add the keyword static anymore. That is because methods don’t need to be static when being called by objects.
To call methods from objects we use the objectName.methodName notation.
You are writing some code to control a simple robot as it goes on an adventure.
Details of the Robot:
Let’s create a class to represent this robot and then write a test program that gets the robot to move. There are multiple ways to design the class, here is one potential solution
Fields
Constructor:
Behaviors
Move Right
Move Left
Change Speed
Display Location
Here is what that completed class might look like
Here is a program that tests the Robot class we just created
Output:
You should check the math to make sure that this output makes sense.
Suppose you were asked to write a program that could find the area and perimeter of triangle if you were given the vertices of that triangle
Design:
The vertices are Points in Two Dimensions so we can design a Point Class and then use those Point objects as inputs to a Triangle Class
Point Class
Fields:
• x and y values to represent the 2D coordinates (use int to keep things simple)
Methods:
• Fields are private so we might need Getter Methods for the fields
Triangle Class
Fields
• 3 Point objects to represent the Vertices
Methods
• Area Method -> no parameters just return the area as a double (A,B,C) are vertices in the formula
• Perimeter Method -> no parameters just return the perimeter as a double
• Side Length -> private Helper method that calculates the side lengths for the perimeter
Testing
In Physics there are physical quantities that have just a size associated with them like time, mass, and volume. However there are also quantities that have a direction associated with them. Things like position, velocity, acceleration, Force. Quantities that have both a size and a direction are called vectors. It should also be noted that with a vector the size is often called its magnitude
It is important in science to be able to know how to add numbers that have directions associated with them. For example here are some one dimensional vector math equations
The easiest way to think about the calculations above are to think as all vectors that are pointing to the right as positive numbers and all vectors that are pointing to the left as negative numbers.
We are going to design a Vector object that can add and subtract vector quantities in 1 dimension.
Fields
Constructor
Methods
Test Program
The program below will produce the output given in the example at the start of the section
1. Create a Class to represent a Circle and write a test program to test to see if it works. Don’t worry about input validation in any of these practice programs. Assume the user will only input the right data to the method
Fields:
• Radius
Constructor:
• Accept the radius as a parameter
Methods:
• getArea -> returns the area of the circle
• getCircumference -> returns the circumference of the circle
2. Create a Class to represent a Rectangle and write a test program that reads 10 rectangles from a data file and outputs the Area and Perimeter of each rectangle. Design the Fields, Constructor, Methods yourself for the Rectangle Class. Don’t worry about input validation in any of these practice programs. Assume the user will only input the right data to the method
3. Create a Date class to represent a Date object. Check input validation and write a test program.
Fields:
• Day, month, year
Constructor:
• Accept all the fields as a parameters
Methods:
• getDay – returns day
• getMonth – returns the month
• getYear – returns the year
• setDay – sets the month
• setMonth – sets the day
• setYear – sets the Year
• setDate – sets all fields
• display – use format dd/mm/yyyy
4. Write a new Circle class called GeometricCircle that still the center point and the radius as fields. Accept two points in the constructor, the first point is the center of the circle and the second point is any other point on the circle. It should be able to calculate the area and perimeter of the circle. Test Your program.
Check out our programing in python courses that focus on high school level coding.
These courses are complete with interactive coding lessons, teacher led videos, and more practice questions with complete solutions
Return To International Baccalaureate Computer Science Main Page