Algorithms and OOD (CSC 207 2013F) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] [FAQ] [IRC] [Teaching & Learning]
Current: [Assignment] [EBoard] [Lab] [Outline] [Partners] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Partners] [Readings]
Reference: [Java 7 API] [Java Code Conventions]
Related Courses: [CSC 152 2006S (Rebelsky)] [CSC 207 2013S (Walker)] [CSC 207 2011S (Weinman)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] [Issue Tracker (Course)] [Issue Tracker (Textbook)]
Overview
Admin
Questions on HW2
git commit -m "*Message*"
provides a faster way to commit. /**
* Compute x^n
* @pre
* x^n < Integer.MAX_VALUE
* x^n > Integer.MIN_VALUE
*/
int expt(int x, int n)
One test: 9 == expt(3,2)
assertEquals("D's Test", 9, expt(3,2));
Another test: 1 == expt(posint,0)
for (int i = 0; i < 10000; i++) {
assertEquals(i + "^0", 1, expt(i,0));
} // for
Negative tests
assertEquals("Negative", 1, expt(-2,0));
assertEquals("Negative", -2, expt(-2,1));
assertEquals("Negative", 4, expt(-2,2));
assertEquals("Negative", -8, expt(-2,3));
A for loop to do a lot of tests
for (int base = 2; base < 500; base++) {
assertEquals(base + " squared", base*base, expt(base, 2));
assertEquals(base + " cubed", base*base*base, expt(base, 3));
} // for
A for loop for a power of 1
Detour: Java specifies that integers are 32 bits, excess 2^m-1 notation 32 bits: If only positive, largest 2^32-1; if you allow negative, it's even smaller (maybe 2^31-1, if I remember the notation)
Nest for loops: Different bases, different powers
for (int power = 1; power < 6; power++) {
assertEquals(3 + "^" + power, ...,expt (3, power));
} // for
Something complicated that lets us get back to the original number
Skipped
Insufficient time. Finish up on your own.
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] [FAQ] [IRC] [Teaching & Learning]
Current: [Assignment] [EBoard] [Lab] [Outline] [Partners] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Partners] [Readings]
Reference: [Java 7 API] [Java Code Conventions]
Related Courses: [CSC 152 2006S (Rebelsky)] [CSC 207 2013S (Walker)] [CSC 207 2011S (Weinman)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] [Issue Tracker (Course)] [Issue Tracker (Textbook)]
Copyright (c) 2013 Samuel A. Rebelsky.
This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this
license, visit http://creativecommons.org/licenses/by/3.0/
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.