Skip to content

Latest commit

 

History

History
167 lines (134 loc) · 4.36 KB

File metadata and controls

167 lines (134 loc) · 4.36 KB

Notes

  • Tutorial 9 continues on the work of Tutorial 8 and Exercise in the Coursebook.
  • CHECK SUBMISSION BOX, TUTORIAL 9, CLASS A05 and B05 for source code.

Review on last week

  1. A OOP head space in java, usually, have the following contents:
/**
* @overview
* @attributes
* (name) (class wrapper) (data type of class wrapper in the PL)
* @object
* @abstract_properties
*/
  1. State space specification:
public class ClassName {
  @DomainConstraint()
  priavte (datatype) name ();
....
}
  1. The implementation (ah, it's what we talk about today).

Implementation

  • Last week, we've write the headspace and state space specification. It's method time this week.
  • An (unifinish) code of the program:
/**
(state space)
/*

Constructor class

  • Consider if we want to construct a Banh Chung object (WHYYYYY...?) (image)

  • So, in order to construct something, in our case, Banh Chung, it need to take in ingredients (state space specs), and return an instance.

  • A notable thing: This instance will have the same name as in our class.

  • An example (using the Person class in last week problem):

/**
* if a method is NOT optional -> put something inside the param.
* and also we need to add the description before. 
* ----------------------(remove this line and anything above it)
* @effects <pre>
* if id, name is valid
*   initialize this as <id, name>
* else 
*   throws NotPossibleExeception
* </pre>
*/
public Person (@int id, String name) (@AttrRef("id") int id, @AttrRef )
{
if 
  this.id = id; // to make sure that this is a value that the user input in
  this.name = name;
  
}

// it returns nothing; but instead of void, it'll return that Object.

Observer/Getter

  • Usually start with get.
  • The principle behind it is this: for each
/**
* since id is int, the get method data type is THE SAME as the type of data.
* ------ (ignore this line) --------- 
* @effects <pre>
*   return this.id
* </pre>
@DOpt (type= OptType.observer)
@AttrRef("id")
public int getId() {
  return this.id;

}

Mutator:

  • For each attribute which is mutable, creater a Setter (Mutator)
  • We use the class Person here (see Textbook for the original
/**
* @modifies this.name
* @effects <pre>
*   if newName is valid
*     set this.name = newName\
*     return true
*   else
*     return false
* </pre>

public boolean setName (string newName)
{
this.name = newName;
}
  • You can see how it plays out in the following picture: image

Helper

Validators

  • The idea is this, for each attribute, we will have a validator.
/**
* @effects <pre>
*   if id is valid
*     return true (hence the boolean)
*   else
*     return false
* </pre>
private boolean validateID(){
  // Since we've already tested most of the previous conditions
  // We will test something that is unique.
  // int data has a min or max. So, we will tets that.
  
 
  // Also we will modify the Constructor above: 
}
  • Some note about String (and null)
    • In some case

image

  • And the constructor located aboved can be modify to this: image

  • A more update version will look like this:
    image

RepOK

  • Using the Banh Chung example: This is to MAKE SURE WE DON'T GET FOOD POSIONING.

image

  • But what if something is an Object, like in our MobilePhone class?

  • And the end result: image

toString

  • A sample of toString can be founded here:
    image

  • And an overview of this program in the beginning here: image
## Some notes on Assignment 2:
- ```Set``` and ```PCProg``` are given.