Monday, August 30, 2010

FizzBuzz Program

The FizzBuzz program is a simple test that is used often by employers to root out those who can actually program and those who can't.  On paper, I was able to implement FizzBuzz in about 3 minutes without proper documentation and comments.  Using Eclipse, it took me about 10 minutes as I fiddled around in my new IDE.

/**
 * Prints out all numbers from 1 to 100, except "Fizz" is printed when it is a   
 * multiple of 3, "Buzz" is printed when it is a multiple of 5, and "FizzBuzz" 
 * when it is a multiple of both 3 and 5.
 *
 * @author David Lin
 * @date August 30, 2010
 */
public class FizzBuzz {
  /*
   * @param args ignored
   */
  public static void main(String[] args) {
    for (int i = 1; i < 101; i++) {
      if (i % 15 == 0) {
        System.out.println("FizzBuzz");
      }
      else if (i % 3 == 0) {
        System.out.println("Fizz");
      }
      else if (i % 5 == 0) {
        System.out.println("Buzz");
      }
      else {
        System.out.println(i);
      }
    }
  }
}

The first problem I encountered was that Eclipse defaulted to tabs for indentation.  The standard for this Software Engineering class is 2 spaces.  I used the search function in Eclipse's help library, but it was fruitless, so I searched "Eclipse how to change indentation" on Google.  I found a posting on stackoverflow.com that indicated the location of this setting, which is Windows > Preferences > Java > Code Style > Formatter > Edit > Indentation.  I also learned of a quick way to have Eclipse automatically reformat my code with the new settings, which is Ctrl-Shift-F.

The second problem I encountered was formatting my FizzBuzz code to show up nicely in this blog.  A quick and easy way to retain coloring and formatting is to
  1. Copy the code from Eclipse
  2. Paste into Word
  3. Copy from Word and paste
This experience has taught me that standards (indentation), clarity (documentation, formatting), and resourcefulness (problem-solving) are important in software engineering and this class in general.

1 comment:

  1. bằng nhau, mà Hàn Giang Đào và một số trưởng lão cũng bất đắc dĩ, bởi vì bọn họ nhìn thấy ba người vẫn còn đứng đó, không tiến lên khiêu chiến.
    đồng tâm
    game mu
    cho thuê phòng trọ
    cho thuê phòng trọ
    nhac san cuc manh
    tư vấn pháp luật qua điện thoại
    văn phòng luật
    số điện thoại tư vấn luật
    dịch vụ thành lập doanh nghiệp
    Sau khi cuộc tranh tài kết thúc, mười người bài danh đầu tên chỉ có vị trí thứ mười và thứ tám là có biến hóa, những người khác đều bảo trì được vị trí của mình.

    Trận đấu kết thúc, một trăm người chiến thắng ngày mai có thể tiến vào lớp học viên cũ, hơn nữa còn được phần thưởng thẻ Linh.

    Nhạc Thành quay lại gian phòng của mình, sau đố vội vàng giải trừ cấm chế, phát hiện Thác Ni Tư và Trần Bưu đã đột phá.

    - Đại nhân, chủ nhân…

    Thác Ni Tư và Khiếu Thiên Hổ đều hành lễ với Nhạc Thành. Về phần Khiếu Thiên Hổ và Tử Điện mãng, hôm qua Nhạc Thành rời đi đã để bọn họ trong cấm chế, nhìn thấy hai người đột phá Nhạc Thành vô cùng hài lòng.

    ReplyDelete