teaching machines

CS 145 Lecture 5 – Methods

February 8, 2012 by . Filed under cs145, lectures, spring 2012.

Agenda

Code

CharacterArithmetic.java

package preexam1;

public class CharacterArithmetic {
  public static void main(String[] args) {
    char c = 'm';
    
    System.out.print(c + 5);
    System.out.println(" <- the sum");
    
    c = (char) (c + 5);
    System.out.println(c);
    
    char avgLetter = ('a' + 'z') / 2;
    System.out.println(avgLetter);
  }
}

Mathods.java

package preexam1;

public class Mathods {
  public static void main(String[] args) {
    double debraOfThirteen = Mathods.getDebraValue(13);
    System.out.println(debraOfThirteen);
  }
  
  public static double getDebraValue(double x) {
    double answer = -1.0 / x;
    return answer;
  }
}

CircleMaker.java

package preexam1;

import java.util.Scanner;

public class CircleMaker {
  public static void main(String[] args) {
    
    Scanner in = new Scanner(System.in);
    
    // Use that data to make them their awesome circle.
    System.out.println("<html>");
    System.out.println("<body>");
    System.out.println("<h1>My first SVG</h1>");
    System.out.println("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">");

    handleCircle(in);
    handleCircle(in);
    handleCircle(in);
    handleCircle(in);
    handleCircle(in);
    handleCircle(in);
    handleCircle(in);
    handleCircle(in);
    handleCircle(in);
    handleCircle(in);
    handleCircle(in);
    handleCircle(in);
    handleCircle(in);
    handleCircle(in);
    handleCircle(in);

    System.out.println("</svg>");

    System.out.println("</body>");
    System.out.println("</html>");

  }
  
  public static void handleCircle(Scanner in) {
    // Get some circle data from the user.
    System.out.println("Where? ");
    int x = in.nextInt();
    int y = in.nextInt();
    System.out.println("What color? ");
    String color = in.next();
    System.out.println("How big? ");
    int radius = in.nextInt();
    
    System.out.println("<circle cx=\"" + x + "\" cy=\"" + y + "\" r=\"" + radius + "\" stroke=\"black\"");
    System.out.println("stroke-width=\"2\" fill=\"" + color + "\" />");
  }
}

Haiku

Grandpa’s stories change.
Each time, the Nazis double.
Grandpa has a bug.