Download presentation
Presentation is loading. Please wait.
1
Implementing Non-Static Features
Ahmed Sabbir Arif
2
Utilities vs. Non-Utilities
A special kind of class: Attributes are associated with the class itself rather than with its instances. Can’t have different instances with different states. Most classes are non-utility: Hence, we usually drop the “non-utility” qualifier.
3
Non-Utilities In this lecture we focus on classes with not static features. Remember that when a method is declared static, it can be used without having to create a object first. We need to instantiate a class before using it: Its client must begin by creating an instance.
4
The Rectangle Class
5
Example 1 Rectangle empty = new Rectangle(); // def. constructor
2 output.println(empty); 3 4 int width = 0; 5 int height = 1; 6 Rectangle rectangle = new Rectangle(width, height); 7 output.println(rectangle); 8 9 width = 2; 10 rectangle.setWidth(width); // mutator 11 output.println(rectangle);
6
UML Diagram Drop the <<utility>> stereotype.
Here, the attribute box is empty, and The return of void methods is left blank.
7
Memory Diagram Implicit parameter Explicit parameter Invocation block
1 Rectangle empty = new Rectangle(); 2 output.println(empty); 3 4 int width = 0; 5 int height = 1; Rectangle rectangle = new Rectangle(width, height); 7 output.println(rectangle); 8 9 width = 2; 10 rectangle.setWidth(width); 11 output.println(rectangle); Implicit parameter Explicit parameter Invocation block
8
Class Structure The 3 sections of the class definition can appear in any order. If a class implements one/more interface then its header must declare this fact by using the keyword implements. 1 // any needed package statement 2 // any needed import statements 3 4 public class SomeName 5 { 6 // the attribute section 7 // the constructor section 8 // the method section 9 }
9
The Attribute Section Make sure the attribute meets the specification stated in the API of the class. The final outcome may not be unique: However, all implementations are correct. access type name; access final type name;
10
The Attribute Section, cont.
The access can be either public: If it is a field. Or private: Means it is not visible to the client. Keep all non-final attributes private: Forces the client to use a mutator. Simplifies the API.
11
The Attribute Section, cont.
Use final if the attribute is a constant. Constant per object, not per class! type specifies the type of this attribute: Primitive or non-primitive.
12
The Rectangle Class
13
The Attribute Section 1 public class Rectangle 2 { 3 private int width; 4 private int height; 5 ... 6 } Initialize static attributes as you declare them and leave the initialization of non-static ones to the constructor section. The scope of an attribute is the entire class. this.a refers to the attribute a of the (non static) object on which the method is invoked.
14
The Constructor Section
A class constructor is invoked when the class is instantiated (the new operator). The default constructor of the Rectangle class: access signature; 1 public Rectangle() 2 { 3 this.width = 0; 4 this.height = 0; 5 }
15
The Constructor Section, cont.
Example: A two-parameter constructor: Remember the client code fragment from the first example? 1 public Rectangle(int width, int height) 2 { 3 this.width = width; 4 this.height = height; 5 }
16
Example 1 Rectangle empty = new Rectangle(); // def. constructor
2 output.println(empty); 3 4 int width = 0; 5 int height = 1; 6 Rectangle rectangle = new Rectangle(width, height); 7 output.println(rectangle); 8 9 width = 2; 10 rectangle.setWidth(width); // mutator 11 output.println(rectangle);
17
Another Example 1 int width = 1; 2 int height = 2;
3 Rectangle rectangle; rectangle = new Rectangle(width, height);
18
The Method Section Contains one definition per method.
The type is either void or the actual type of the return. The signature consists of its name followed by its parameter list. access type signature;
19
The Method Section, cont.
Common themes of methods: State-related methods, i.e. accessors, mutators, Obligatory methods that override those of the Object class, i.e. toString, equals, and hashcode, Interface-related methods, i.e. compareTo, and Class-specific methods, i.e. getArea and scale.
20
Accessors Allows the client to gain access to the value of one of the attributes of an instance of the class. 1 public int getWidth() 2 { 3 return this.width; 4 }
21
Mutators Allows the client to modify the value of one of the attributes of an instance of the class. 1 public void setWidth(int width) 2 { 3 this.width = width; 4 }
22
Obligatory Methods Java expects certain methods, such as equals, hashCode and toString, to be in all or almost all classes. The purpose of equals, a boolean valued method, is to compare two objects of the class to see if they satisfy the notion of "being equal“ Note: You cannot use == to compare objects public boolean equals(ClassName objectName) The purpose of the toString method is to return a String value that represents the data in the object public String toString() The purpose of the hashCode method is to return an integer that can serve a hash code for the object – it should be the same for objects that are equals public int hashCode()
23
Testing a Class Different from testing a utility class:
Need to check if the constructors behave according to their specifications. May have multiple constructors. A test case not only test the values for the parameters but also the value for the implicit parameter this.
24
Example 1 Random random = new Random(); 2 final int MAX = 100;
3 int width = random.nextInt(MAX + 1); 4 int height = random.nextInt(MAX + 1); 5 6 Rectangle rectangle = new Rectangle(width, height); 7 8 if (rectangle.getWidth() != width) 9 { output.print("Either the two-parameter constructor or the method getWidth failed the test case: "); 11 output.println("width = " + width + ", height = " + height); 12 } 13 if (rectangle.getHeight() != height) 14 { getHeight failed the test case: "); 16 output.println("width = " + width + ", height = " + height); 17 }
25
Further Reading How to avoid code duplication? Immutable Objects
Attribute Caching Class Invariants Validation and the Implementer
26
Lab Exercises Due: before 12am on Friday Grades: Code (50%)
Syntax (25%) Testing (25%)
27
Lectures Tuesday and Thursday: Labs: 2:30pm – 4:00pm 5:00pm – 6:30pm
I will check if I can create another (lab) section.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.