Wednesday, February 7, 2007

Notes from Week #4

Announcement:
  • Lab 3 is graded. Please see this blog if you have questions about grading. If you still have questions, please visit my office hours. For those whose functionality points were severely deducted, you may still be able to get some points back by visiting my office hours. Requests for regrading must be done in person, so you can show me that your program really works.
  • Lab 4 is due this Friday (2/9). Shaomei will be your grader.
  • Lab 5 is due next Friday (2/16). I will be your grader.

Recursive Programs: What does the following program print?


private int f(int x) {
if (x==0) return 1;
else return x * f(x-1);
}

private int a(int m, int n) {
if (m==0) return n+1;
if (n==0) return a(m-1, 1);
return a(m-1, a(m, n-1));
}

public static void main(String[] args) {
System.out.println(f(5));
System.out.println(a(3, 1));
}



Lab 4: Augmenting the SketchApp program in the text (Page 169). Please visit this blog often to see if there are any updates/hints. You should already know all the needed concepts. It's just a little (?) more work than Lab 3.

Lab 5:
A little animation involving the bouncing ball program (Chapter 7 of the text). You've already had everything needed. You just need to study the original program.

Good styles:

What is a good style?

  1. This is a good style that I personally like. It's the standard java style. Many companies also like this style:
    Code Conventions for the JavaTM Programming Language
    http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html

  2. The textbook is a good style.
  3. Many, many other styles...

What are important?
  • One class per file (... generally)
  • private/protected/public
    • The constructor should be public. The main method needs to be public. The accessors/mutators need to be public. The methods that other classes need to call may need to be public.
    • Make everything else as private as possible. If it can be private, make it private. If it cannot be private, maybe it can be protected (only subclasses can access)? Only when absolutely neccessary should you make an instance variable public.
  • Consistent indentation is very important for the readability of your program.
    • Different level of code should be at different depth of indents.
    • Same level of code should be at the same depth.
  • Line width should be limited to 80 charactors for good printing and viewing.

Suggestions:
  • Use spaces instead of tab. Set the tab stop to 2 or 4 spaces.
  • (Strongly suggested) Use an IDE or a good text editor. It can be painful to do good indentation in NotePad. I reccommend Eclipse, NetBeans, or Crimson Editor. Feel free to come to my office hours if you need help on setting up these programs.

No comments:

Contributors