Algorithms and OOD (CSC 207 2014F) : EBoards

CSC207.01 2014F, Class 22: Analyzing Algorithms


Overview

Preliminaries

Admin

Upcoming Work

Cool Things Coming to Campus

Extra Credit Opportunities

Academic

Peer Support

Miscellaneous

Questions

Why have anonymous inner classes?

Before there were anonymous functions, it was the best way to get "one off" functions or objects into your program. "I want a button that does X".

You can write classes that reference a field of an object without having to deal with significant protection issues. "I want a button that changes my state in this way."

It requires fewer characters.

It might make it easier for someone to read your code; everything is near where you need it.

Do you want a GUI or a TUI for problem 4 on HW7?

I haven't taught you anything about GUI's, so I would assume that you will write a textual user interface. I'm fine if it's relatively primitive: Use a numeric menu to select a type of criterion and then prompt for info on that criterion. Probably at least three different type of criterion. Okay to have only one predicate, though.

If you have extra time and energy, you might permit compound criteria (using and, or, and not) or a syntax for expressing criteria.

How should I compute distance using latitude and longitude?

Ideally, you would look up that information online. For example, a reasonable formula seems to appear at http://www.movable-type.co.uk/scripts/latlong.html

When are you getting grades to us?

I sent out the first two sets of grades from our grader this morning. I expect to have the next set this coming weekend. I also hope to have some detailed comments this weekend.

The grading that I have to do is going to take a bit longer. I spend a long time on administrative paperwork this weekend as well as getting the algorithms reading written and exams sketched.

Part E of HW6 asks you to think about four ways of determining whether an Item has a particular name

Option one: Look at fields. Conceptually easy. Practically hard (Java protection is your friend.) May be impossible if the classes you're working with have private fields.

Option two: getName(). Traditionally we write getters, so it's what we should be doing anyway. But ... need to change the interface and write N similar methods.

Option three: boolean equals(String name). Helpful for the remove method, in which the name matches, won't be helpful elsewhere. "Equals" has a particular implication - they are effectively the same object. Ambiguous - at least one person wanted to use the result of toString in the comparison.

Option four: boolean hasName(String name). Better version of option three. Reveals less than option two, and perhaps more general.

Summary

Activity One: Iterative Analysis

What's the running time of the following algorithm? Why? (Discuss with your partner and be prepared to share your analysis with this class.) (The algorithm is expressed in something close to Java, but not actual Java.)

/**
 * Count all of the values in l for which predicate p holds.
 */
public int countValues(List l, Predicate p)
{
  int count = 0;                            // 1 step
  for (int i = 0; i < l.length(); i++)      // n repetitions
    {                                       // l.length() takes 1, i++ takes 1
      if (p.test(l.get(i)))                 // test takes 1, get takes 1
        count += 1;                         // increment takes 1
    } // for
  return count;                             // 1 step
} // countValues

Challenge to that analysis

/**
 * Count all of the values in l for which predicate p holds.
 */
public int countValues(List l, Predicate p)
{
  int count = 0;                            // 1 step
  for (int i = 0; i < l.length(); i++)      // n repetitions
    {                                       // l.length() takes c*n, i++ takes 1
      if (p.test(l.get(i)))                 // test takes 1, get takes 1
        count += 1;                         // increment takes 1
    } // for
  return count;                             // 1 step
} // countValues

How about this slight variant?

/**
 * Count all of the values in l for which predicate p holds.
 */
public int countValues(List l, Predicate p)
{
  int count = 0;                    // 1
  int len = i.length();             // n steps
  for (int i = 0; i < len; i++)     // n repetitions
    {
      if (p.test(l.get(i)))
        count += 1;
    } // for
  return count;
} // countValues

Everything after this point was not covered; just planned!

How about this one? Why?

/**
 * Make a string that contains n copies of str.
 */
public String replicate(String str, int n)
{
  String result;
  for (int i = 0; i < n; i++)
    {
      result = result.append(str);
    } // for
  return result;
} // replicate

Activity Two: Recursive Analysis

Let's try to solve each of these recurrence relations

Activity Three: Proving Things About Big O

You can drop constant multipliers

Big-O is transitive

You can drop lower-order terms