Algorithms and OOD (CSC 207 2014F) : EBoards

CSC 207: Extra Session 4 (Thursday, 25 Sept. 2014)


Planned Topics

Additional Topics

Debugging and Style

                    if (num == 0 || num == 1 || num == 2 || num == 3 || num == 4
                                                            || num == 5 || num == 6 || num == 7) {

    if ((num >= 0) && (num < NUM_REGISTERS))

String comparison in C

    char *s1;
    char *s2;
       ...
    if (s1 == s2)
      {
        ...
      } // same location
    if (strcmp(s1,s2) == 0)
      {
        ...
      } // same string

String comparison in Java

    String s1;
    String s2;
    ...
    if (s1 == s2)
      {
        ...
      } // same location
    if (s1.equals(s2))
      {
        ...
      } // same string

Polymorphism

Basic idea: You have an object. You know that it provides a certain set of capabilities. (In Java: either it implements an interface or it extends a class). You can use that object anywhere that you need something with those capabilities.

Concrete version 1: If A extends B, then

Concrete version 2: If O implements I, then

Yes, super/sub feels backwards from math, but it matches when you think about the set as being "things that can do all of this" rather than "the set of capabilities"

Example: How to get to McNallys

getToMcNallysFromScience3821(Walker w)
{
  w.walkTo("door marked exit");
  w.open("door marked exit");
  w.lookFor(theLegendaryCSTeleportationChamber);
  w.enter(theLegendaryCSTeleportationChamber);
  w.type("McNallys", theLegendaryCSTeleportationChamber.getKeyboard());
  w.click(theLegendaryCSTeleporationChamber.teleportButton());
  w.pray();
}

    public interface Walker
    {
      public void walkTo(String) throws Exception;
      public void open(String);
      public void lookFor(String);
      public void enter(EnterableObject obj);
      public void type(String, Keyboard);
      public void click(button);
    }

    public class Student implements Walker
    {
      public void walkTo(String)
        throws Exception
      {
        location = identifyWhereToFind(String);
        orientSelfTo(location);
        ...
      }

    } //

    public class StudentInWheelchair extends Student
    {
      @Override
      public void walkTo(String)
        throws Exception
      {
        this.wheelchair.orientTo(...);
        arms.pushOnWheels();
        ...
      }
    } //

The TextBlock Example

We did not get to this topic.