Algorithms and OOD (CSC 207 2014F) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [Learning Outcomes] [FAQ] [Teaching & Learning] [Grading] [Rubric] - [Calendar]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Readings]
Reference: [Student-Curated Resources] [Java 8 API] [Java 8 Tutorials] [Code Conventions]
Related Courses: [CSC 152 2006S (Rebelsky)] [CSC 207 2014S (Rebelsky)] [CSC 207 2014F (Walker)] [CSC 207 2011S (Weinman)]
Misc: [Submit Questions] - [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Issue Tracker (Course)] [Issue Tracker (Textbook)]
CSC 207.01 2014F: Extra Session 7 (Thursday, 16 October 2014)
Topics to discuss
Open a terminal window.
$ cd Desktop
$ sftp username@ftp.cs.grinnell.edu
Password: MathLAN Password
sftp> cd /home/rebelsky/share/
sftp> get CSC207.xml$a
sftp> bye
Open Eclipse and Import
average?Long.MAX_VALUE.)Strategy: Think back to how you solved the "average two numbers correctly" How do we safely average a and b, assuming that they are both positive?
a/2 + b/2
Check! Average 3 and 3. 3/2 + 3/2 = 1 + 1 = 2.
What else might we do?
a + (b-a)/2
Only works if the are both positive. Need to check that it rounds down. It doesn't: Suppose a is 1 and b is 0. The average is 0. But 1 + (-1/2) = 1 + 0 = 1
Let's go back to the first strategy
a/2 + b/2
Issue: If both are odd and positive we are off by one
if ((a % 2 == 1) && (b % 2 == 1) && (a >= 0) && (b >= 0))
return a/2 + b/2 + 1
else
return a/2 + b/2;
Also need to think about other special cases. It will be long and ugly, but sometimes it's best to start with code that's correct but ugly.
Slightly generalize: What if we have three elements?
a/3 + b/3 + c/3
Example (average 4, 3, and 8):
4/3 + 3/3 + 8/3 = 1 + 1 + 2 = 4; off by one
4/3 + 4/3 + 7/3 = 1 + 1 + 2 = 4; off by one
3/3 + 3/3 + 3/3 = 1 + 1 + 1 = 3; correct
5/3 + 5/3 + 7/3 = 1 + 1 + 2 = 4; should be 5; off by 1
5/3 + 5/3 + 8/3 = 1 + 1 + 2 = 4; should be 6; off by 2
I really mean "how do I make a lot of random arrays"?
We could look at the sorting code.
"A lot" probably means I should use a loop
Random generator = new Random();
for (size = 3; size < 10000; size = size + generator.nextInt(100))
{
int[] arr = new int[size];
for (int i = 0; i < size; i++)
{
arr[i] = generator.nextInt();
} // inner for
} // outer for
Comparators.
Java suggests that when you have multiple "things" that are similar and you might want to substitute one for the other, you build an interface.
public interface StringComparator
{
/**
* Compare str1 to str2. Return a negative number if
* str1 comes before str2, zero if they are equal, and
* a positive number if str1 comes after str2.
*/
public int compare(String str1, String str2);
} // interface StringComparator
We want to generalize this.
public interface THINGComparator
{
/**
* Compare str1 to str2. Return a negative number if
* str1 comes before str2, zero if they are equal, and
* a positive number if str1 comes after str2.
*/
public int compare(THING str1, THING str2);
} // interface THINGComparator
Use language feature: Generics!
public interface Comparator<T>
{
/**
* Compare val1 to val2. Return a negative number if
* val1 comes before val2, zero if they are equal, and
* a positive number if val1 comes after val2.
*/
public int compare(T val1, T val2);
} // interface Comparator<T>
We want to build one of these things. Let's say an IntegerComparator
public class StandardIntegerComparator
implements Comparator<Integer>
{
public int compare(Integer val1, Integer val2)
{
if (val1 < val2)
return -1;
else if (val1 == val2)
return 0;
else
return 1;
} // compare(Integer, Integer)
} // class StandardIntegerComparator
Alternative
public class StandardIntegerComparator
implements Comparator<Integer>
{
public int compare(Integer val1, Integer val2)
{
return val1.compareTo(val2);
} // compare(Integer, Integer)
} // class StandardIntegerComparator
Later
Comparator<Integer> comp = new StandardIntegerComparator();
sort(stuff, comp);
Make the programmer more efficient (anonymous inner classes)
Comparator<Integer> comp = new Comparator<Integer>()
{
public int compare(Integer val1, Integer val2)
{
return val1.compareTo(val2);
} // compare(Integer, Integer)
};
sort(stuff, comp);
Make the programmer even more efficient (anonymous functions)
Comparator<Integer> comp =
(val1,val2) -> { return val1.compareTo(val2); }
sort(stuff, comp);
And more
(val1,val2) -> val1.compareTo(val2);
Issues: Problems are getting hard -> Programmers screw up.