CS 145 Lecture 5
September 23, 2011 by Chris Johnson. Filed under cs145, fall 2011, lectures.
Agenda
- revisiting method anatomy
- put yourself in the method’s shoes:
- what am I trying to do?
- what do I need to give back?
- what outside information do I need in order to perform my job?
- how arguments are passed
- method improv
Code
GeometryFun.java
package lecture;
import java.util.Scanner;
public class GeometryFun {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter radius: ");
double value = in.nextDouble();
double area = getAreaOfCircle(value);
System.out.println("Area: " + area);
// getAreaOfCircle("t");
System.out.println(getAreaOfAnnulus(6, 8));
}
public static double getAreaOfCircle(double radius) {
double area = radius * radius * Math.PI;
// radius = 6.0; // this has absolutely no effect on anything at all
return area;
}
// meaningful name?
// return type?
// outside information?
public static double getAreaOfAnnulus(double insideRadius, double outsideRadius) {
double insideArea = getAreaOfCircle(insideRadius);
double outsideArea = getAreaOfCircle(outsideRadius);
double totalArea = outsideArea - insideArea;
return totalArea;
}
public static double f(double t) {
return 1 / (1 + Math.sqrt(t));
}
//
// public static double getEdgeOfCube(double diagonalLength) {
// // find diagonal of bottom face
// //
// }
}
TODO
- Work on homework 1.
- Read section 2.3 in your book.
Haiku
dragon before princess
bread before peanut butter
thinking before code
show