Islamic University of Gaza Faculty of Engineering Computer Engineering Department Computer Programming Lab (ECOM 2114) Lab 11 Object Oriented Programming Eng. Mohammed Alokshiya December 16, 2014
Object-oriented programming (OOP) involves programming using objects. An object represents an entity in the real world that can be distinctly identified. For example, a student, a desk, a circle, a button, and even a loan can all be viewed as objects. An object has a unique identity, state, and behavior. The state of an object (also known as its properties or attributes) is represented by data fields with their current values. A circle object, for example, has a data field radius, which is the property that characterizes a circle. A rectangle object has the data fields width and height, which are the properties that characterize a rectangle. The behavior of an object (also known as its actions) is defined by methods. To invoke a method on an object is to ask the object to perform an action. For example, you may define methods named getarea() and getperimeter() for circle objects. A circle object may invoke getarea() to return its area and getperimeter() to return its perimeter. You may also define the setradius(radius) method. A circle object can invoke this method to change its radius. Objects of the same type are defined using a common class. A class is a template, blueprint, or contract that defines what an object s data fields and methods will be. An object is an instance of a class. You can create many instances of a class. Creating an instance is referred to as instantiation. The terms object and instance are often interchangeable. The relationship between classes and objects is analogous to that between an apple-pie recipe and apple pies: You can make as many apple pies as you want from a single recipe. A Java class uses variables to define data fields and methods to define actions. Additionally, a class provides methods of a special type, known as constructors, which are invoked to create a new object. A constructor can perform any action, but constructors are designed to perform initializing actions, such as initializing the data fields of objects. 2
Class Name: Person Data Fields: fname is lname is age is ID is Methods: getfullname A class Template Person Object 1 Data Fields: fname is Emad lname is Mostafa age is 21 ID is 120130000 Person Object 2 Data Fields: fname is Hamza lname is Emad age is 21 ID is 120130001 Two objects of the Person class A class is a construct that defines objects of the same type. Here is a java code to define the above class Person : class Person { // Fields String fname; String lname; int age; int ID; class Person // A Constructor public Person() { // Another Constructor public Person(String fname, String lname, int age, int ID) { this.fname = fname; this.lname = lname; this.age = age; this.id = ID; // A method public String getfullname() { String fullname = fname + " " + lname; return fullname; 3
Then you can create an instance p1 of the class Person, using the new operator, and assign values to its fields using. operator: class Main public class Main { public static void main(string[] args) { // Create object p1 using the first constructor (new Person()) Person p1 = new Person(); p1.fname = "Emad"; p1.lname = "Mostafa"; p1.age = 21; p1.id = 120130000; System.out.println(p1.getFullName()); // Create object p1 using the // second constructor (new Person(fName, lname, age, id)) Person p2 = new Person("Hamza", "Emad", 21, 120130001); System.out.println(p2.getFullName()); 4
Constructors Constructors are a special kind of methods that invoked to create an object using the new operator. They have three peculiarities: A constructor must have the same name as the class itself. Constructors do not have a return type not even void. Constructors are invoked using the new operator when an object is created. Constructors play the role of initializing objects. The constructor has exactly the same name as its defining class. Like regular methods, constructors can be overloaded (i.e., multiple constructors can have the same name but different signatures), making it easy to construct objects with different initial data values. Constructors are used to construct objects. To construct an object from a class, invoke a constructor of the class using the new operator, as follows: new ClassName(arguments); Note: A class may be defined without constructors. In this case, a public noarg constructor with an empty body is implicitly defined in the class. This constructor, called a default constructor, is provided automatically only if no constructors are explicitly defined in the class. The this Reference The this keyword is the name of a reference that an object can use to refer to itself. You can use the this keyword to reference the object s instance members. It can also be used inside a constructor to invoke another constructor of the same class. 5
Example: Design a class named Car to represent a car. The class contains: Two String data fields named model and color that specify the model and color of the car. Three int data fields named capacity, maxspeed and manufacturingyear that specify the capacity, maximum speed and manufacturing year of the car. The default value of capacity is 4. A no-arg constructor that creates a default car. A constructor that creates a car with the specified capacity, maxspeed, manufacturingyear, model and color. A method named tostring() that returns all information about the car as string. Then create two instances of the Car class, with the following info: 1. Model >> I30, color >> Silver, capacity >> 4, maxspeed >> 240, manufacturingyear >> 2010. 2. Model >> Mercedes 250, color >> Red, capacity >> 4, maxspeed >> 260, manufacturingyear >> 2000. Use tostring() method to print the information of a car. public class Main { Code public static void main(string[] args) { Car c1 = new Car("I30", "Silver", 4, 240, 2010); Car c2 = new Car("Mercedes 250", "Red", 4, 260, 2000); System.out.println(c1.toString()); System.out.println(); System.out.println(c2.toString()); class Car { String model; String color; int capacity; int maxspeed; int manufacturingyear; 6
public Car() { public Car(String model, String color, int capacity, int maxspeed, int manufacturingyear) { this.model = model; this.color = color; this.capacity = capacity; this.maxspeed = maxspeed; this.manufacturingyear = manufacturingyear; @Override public String tostring() { String s = String.format("%-20s%s\n%-20s%s\n%-20s%s\n%-20s%s\n%-20s%s", "Model:", model, "Color:", color, "Capacity:", capacity, "Maximum Speed:", maxspeed, "Manufacturing Year:", manufacturingyear); return s; Model: I30 Color: Silver Capacity: 4 Maximum Speed: 240 Manufacturing Year: 2010 Model: Mercedes 250 Color: Red Capacity: 4 Maximum Speed: 260 Manufacturing Year: 2000 Output 7