/** This class contains method possessive that we started in class. In class,
    we showed how we used stepwise refinement in developing the method. We
    wrote a sequence of statements, in English or Java, that would accomplish the
    task. The key is almost to forget about Java code, but knowing that you
    need to work with variables, and write a high-level sequence of instructions
    to accomplish the task. Then, refine each of those high-level statements
    further, into English or Java. Keep going until everything is in Java.
    
    Precise statement are important. To say, "Find the first blank" is vague. What
    do you do when you have found it? Better is: "Store in j the index of the
    first blank". */

public class Demo {
    // substring(h,k)  substring(h) indexOf(s) lastIndexOf(s)
    
    /** s may contain an occurrence of "'s" as in
     "Perhaps dog's leg was hurt". If so, change it to:
     "Perhaps leg of dog was hurt". Another e.g.: change
     "I think John's father was sick" to
     "I think the father of John was sick".
     Precondition: If "'s" appears in s, it is only in
     such situations. Also, each of the noun phrases involved (e.g.
     "dog" and "leg" consist only of a single noun, with no adjectives. */
    public static String possessive(String s) {
        // Store in j the index of first 's (-1 if it not there)
           int j=  s.indexOf("'s");
        
        if (j == -1)
               return s;
           
        // { j is ³ 0 }
        // Store in first the part preceding the "'s "
           String first= s.substring(0,j);
           System.out.println("first: \"" + first + "\"");
           
        // Store the possessive word in first into pos1 and
        // remove it from first. If there is a blank, leave it
        // it at the end of first.
        int k= first.lastIndexOf(" ");
        String pos1= s.substring(k+1,j);
        first= first.substring(0,k+1);
        System.out.println("pos1: \"" + pos1 + "\"");
        System.out.println("first: \"" + first  + "\"");
           
        // Store in rest the part following the "'s "
           String rest= s.substring(j+3);
           System.out.println("rest: " + rest);
        
        // Store into pos2 the word at the beginning of rest
        // and remove it from rest.
        int t= rest.indexOf(" ");
        String pos2;
        if (t >= 0) { 
            pos2= rest.substring(0,t);
            rest= rest.substring(t+1);
        }
        else {
            pos2= rest.substring(0);
            rest= "";
        }
        System.out.println("pos2: \"" + pos2 + "\"");
        System.out.println("rest: \"" + rest  + "\"");
       
        
        
        return first + pos2 + " of " + pos1 + " " + rest;
    }
    
}