A1: Specifying and Using Abstraction
CPSC 210
Learning Goals
- Specify the methods in a data abstraction using REQUIRES, MODIFIES, EFFECTS clauses
-
Once the methods of a data abstraction have been specified, demonstrate how those methods could be used
- i.e. write examples of calls to those methods
Abstraction Concepts
Specification
// REQUIRES
// MODIFIES
// EFFECTS
What assumptions does the method make?
-
Does the object itself change? (this)
-
Does some other object change? (name the object)
-
No internal implementation details
- Externally visible effects
- No internal details or variable names
- No description for accomplishing these effects
- No description of algorithm in detail
Parts of a Method
private void makeOtherDogBark(Dog otherDog) {
System.out.println("Woof Woof Woof Woof Woof"):
}
Visibility
Return Type
Method Name
Parameter List
Method body
Visibility
Objects of a different class (type) can access/use it
Only objects of the same class (type) can use/access it
Private
Public
public class Dog {
private String name;
public Dog(String name) { this.name = name; }
public void bark() {
System.out.println("BAARRRRK");
}
public void barkAndMakeOtherDogBark(Dog otherDog) {
this.bark();
this.makeOtherDogBark(otherDog);
}
private void makeOtherDogBark(Dog otherDog) {
otherDog.bark();
}
}
public class Main {
public static void main(String[] args) {
Dog peter = new Dog("Peter");
Dog paul = new Dog("Paul");
peter.bark();
peter.barkAndMakeOtherDogBark(paul);
peter.makeOtherDogBark(paul);
}
}
Visibility
Pod Activity (Do in Groups of 3-5)
- Which of the following methods should have specifications (in general) ? Why or Why Not?
Method | Needs specifications? | Why ? |
---|---|---|
Private Methods | ... | |
Public Methods | ... | |
Constructors | ||
Getters | ||
Setters | ||
Tests |
Lecture Ticket Review
public class Person {
private int age;
private String status;
private String name;
public Person() {
this.status = "young";
this.age = 0;
this.name = "Unnamed Person";
}
public void setName(String name) {
this.name = name;
}
public void getOlderByYears(int years) {
this.age = this.age + years;
if (age > 10) { this.status = "old"; }
}
...
}
// EFFECTS: constructs a person with age 0,
// status "young" and name "Unnamed Person"
// MODIFIES: this
// EFFECTS: sets the name of this person
// REQUIRES: years >= 0
// MODIFIES: this
// EFFECTS: increases the age of this person by years and
// sets their status to "old" if the increased age is
// greater than 10
Pod Activity (Do in Groups of 3-5)
- Convert the following English specification into its code equivalent:
REQUIRES Clause | "Code" Equivalent |
---|---|
The target funding amount is between 500 and 50,000 |
500 < targetAmount < 50,000 |
The car's speed must be either less than 30 km/h or greater than 70 km/h | |
Amount is bigger than 0 and is less than the targetAmount minus the amount just funded | |
The student's grade must be between 0.0 and 4.0 | |
The x,y coordinates of the Point are larger in size than the x,y coordinates of the shape |
🏗️ Lecture Lab
A1: Specifying and Using Abstraction
The End - Thank You!
A1 Specifying and Using Abstraction
By firas_moosvi
A1 Specifying and Using Abstraction
- 81