Loading ...
Sorry, an error occurred while loading the content.

Re: [XP] Interview Problems

Expand Messages
  • Kevin Lawrence
    Last one and I think I am done ;-) I tried creating a value object that formats itself. I like how it came out but I imagine that this version would lose me
    Message 1 of 94 , Jun 26, 2007
    • 0 Attachment
      Last one and I think I am done ;-)

      I tried creating a value object that formats itself. I like how it came out
      but I imagine that this version would lose me more interviews than it wins.
      The tests changed only a little:

      @Test
      public void shouldWriteNumbersDivisibleByFiveAsBuzz(){
      assertEquals("Buzz", new FizzBuzzValue(5).toString());
      }

      but my implementation is quite different.

      public class FizzBuzzValue {
      public final int number;
      private String formattedValue;

      public FizzBuzzValue(int number) {
      this.number = number;
      this.formattedValue = Integer.toString(number);
      }

      @Override
      public String toString() {
      StringBuilder buffer = new StringBuilder();

      if(hasMagicValue(3)) {
      buffer.append("Fizz");
      }
      if(hasMagicValue(5)) {
      buffer.append("Buzz");
      }

      if(buffer.length() == 0){
      buffer.append(formattedValue);
      }

      return buffer.toString();
      }

      public boolean hasMagicValue(int magicValue) {
      return isDivisibleBy(magicValue) ||
      containsDigit(Character.forDigit(magicValue,
      10));
      }

      public boolean containsDigit(char digit) {
      return formattedValue.indexOf(digit) >= 0;
      }

      public boolean isDivisibleBy(int magicValue) {
      return number % magicValue == 0;
      }

      public static void printNumbersFromOneToOneHundred(PrintStream out) {
      for(int i = 1; i <= 100; i++){
      out.println(new FizzBuzzValue(i));
      }
      }

      public static void main(String[] args) {
      FizzBuzzValue.printNumbersFromOneToOneHundred(System.out);
      }
      }

      It's a cool little example and might become my new favorite 'demo TDD in 10
      minutes' example. There is a lot of richness and plenty of room for
      discussion about the trade-offs between alternative implementations.


      --
      Kevin

      http://www.junitfactory.com
      You send us code. We send you tests. For free.


      [Non-text portions of this message have been removed]
    • Kevin Lawrence
      Last one and I think I am done ;-) I tried creating a value object that formats itself. I like how it came out but I imagine that this version would lose me
      Message 94 of 94 , Jun 26, 2007
      • 0 Attachment
        Last one and I think I am done ;-)

        I tried creating a value object that formats itself. I like how it came out
        but I imagine that this version would lose me more interviews than it wins.
        The tests changed only a little:

        @Test
        public void shouldWriteNumbersDivisibleByFiveAsBuzz(){
        assertEquals("Buzz", new FizzBuzzValue(5).toString());
        }

        but my implementation is quite different.

        public class FizzBuzzValue {
        public final int number;
        private String formattedValue;

        public FizzBuzzValue(int number) {
        this.number = number;
        this.formattedValue = Integer.toString(number);
        }

        @Override
        public String toString() {
        StringBuilder buffer = new StringBuilder();

        if(hasMagicValue(3)) {
        buffer.append("Fizz");
        }
        if(hasMagicValue(5)) {
        buffer.append("Buzz");
        }

        if(buffer.length() == 0){
        buffer.append(formattedValue);
        }

        return buffer.toString();
        }

        public boolean hasMagicValue(int magicValue) {
        return isDivisibleBy(magicValue) ||
        containsDigit(Character.forDigit(magicValue,
        10));
        }

        public boolean containsDigit(char digit) {
        return formattedValue.indexOf(digit) >= 0;
        }

        public boolean isDivisibleBy(int magicValue) {
        return number % magicValue == 0;
        }

        public static void printNumbersFromOneToOneHundred(PrintStream out) {
        for(int i = 1; i <= 100; i++){
        out.println(new FizzBuzzValue(i));
        }
        }

        public static void main(String[] args) {
        FizzBuzzValue.printNumbersFromOneToOneHundred(System.out);
        }
        }

        It's a cool little example and might become my new favorite 'demo TDD in 10
        minutes' example. There is a lot of richness and plenty of room for
        discussion about the trade-offs between alternative implementations.


        --
        Kevin

        http://www.junitfactory.com
        You send us code. We send you tests. For free.


        [Non-text portions of this message have been removed]
      Your message has been successfully submitted and would be delivered to recipients shortly.