Introduction to Programming (CPSC 124)
—Hobart & William Smith Colleges, Spring 2015
Thursday Lab #5
Home | Syllabus | Calendar | Class Notes | Labs and Projects | General Notes

Due by 12:19 pm on February 27, 2015

# Overview

The goal of this lab is to build your experience in making reusable code, through method definitions. You are presented with several working program examples, which do not include any method definitions (other than main). Your job is to make methods with comparable functionality to these programs.

# Working Programs

Here are some working programs, representing a few familiar examples from the previous weeks:

```public class Windchill2 {
public static void main(String[] args) {
double t = Double.parseDouble(args[0]);
double w = Double.parseDouble(args[1]);

double v2 = Math.pow(w,0.16);
double windchill = 35.74 + 0.6215*t
+ (0.4275 * t  - 35.75) * v2;

System.out.println("Windchill: "  + windchill);
}```

```public class cels2Fahr {
public static void main (String[] args) {
double cent = Double.parseDouble(args[0]);

double fahr =  cent * (9.0/5.0) + 32;

System.out.println(fahr);
}
}
```

```public class Sqrt {
public static void main(String[] args) {
double x = Double.parseDouble(args[0]);

double EPSILON = 0.0000000001;
double sqX = 22.3; // "guess":  we can start with almost anything!

while (!(Math.abs(sqX*sqX - x) < EPSILON)) {
sqX = (sqX + (x / sqX)) / 2.0;
}

System.out.println(sqX);
}
}
```

```public class Fact {
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
int nF = 1;

int i = 1;
while (i <= n) {
nF = nF * i;
i = i + 1;
}
System.out.println(nF);
}
}
```

```public class Exp {
public static void main(String[] args) {
double EPSILON = 0.000000000000001;

double x = Double.parseDouble(args[0]) ;

double eX = 1.0;  // This will store our answer
double term = 1.0;   // This will store the current term to add to eX

int i = 1;  // counter variable
while (Math.abs(eX - (eX + term)) > EPSILON) {
// Compute the ith term of the series, and add it to the answer:
term = term * (x / i);
eX = eX + term;

i = i + 1;
} // while loop

System.out.println(eX);
}
}```

Convert each of the five programs above into equivalent methods. The first two should be defined in a library class called Weather. The last three should be defined in a library class called uMath. By "equivalent", I mean that the variables used to store command line arguments should become the parameters of the corresponding method. The value printed at the end of each program should become the value that is returned by the method.

As for the names? Those can be determined from the following two test driver programs. Put these two source files in a folder with the source files for your new libraries. You should be able to compile and run them (almost) without modifying any of the code from TestWeather or TestuMath.

Almost without modification. Unless you do the extra credit, you'll have to comment out the lines that refer to Weather.fahr2Cels() and uMath.choose().

```import java.util.Scanner;

public class TestWeather {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
boolean done = false;

while (! done) {
System.out.print("observed temperature:  ");
double t = kbd.nextDouble();
boolean isFahr = true;
double temp = t;

System.out.print("Is that in degrees Fahrenheit? [y/n]");
if (! kbd.next().equals("y") ) {
isFahr = false;
temp = Weather.cels2Fahr(t);
}

System.out.print("observed wind speed: ");
double v = kbd.nextDouble();

boolean isMPH = true;
double wind = v;

System.out.print("Is that in miles per hour? [y/n]");
if (! kbd.next().equals("y") ) {
isMPH = false;
wind = v * 0.621; // kilometers to miles
}

if (temp < 50 && wind > 3.0) {
double w = Weather.windchill(temp,wind);

System.out.print("Effective widnchill is ");
if (isFahr && isMPH) {
System.out.println(w + " degrees fahrenheit");
} else {
}
}
System.out.print("More?  [y/n]");
if (!kbd.next().equals("y")) {
done = true;
}
} // while

System.out.println("Goodbye.");
} // main
}```

```import java.util.Scanner;

public class TestuMath {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
boolean done = false;

while (! done) {
System.out.println("*** testing sqrt() ***");
System.out.print("value for x (must be >= 0):  ");
double x = kbd.nextDouble();

if (x >= 0) {
System.out.println("sqrt(x) == " + uMath.sqrt(x));
System.out.println("Math.sqrt(x) == " + Math.sqrt(x));
} else {
System.out.println("NaN");
}

System.out.println("*** testing exp() ***");
System.out.print("value for x:  ");
x = kbd.nextDouble();

System.out.println("e^x == " + uMath.exp(x));
System.out.println("Math.exp(x) == " + Math.exp(x));

System.out.println("*** testing fact() ***");
System.out.print("value for n (an integer):  ");
int n = kbd.nextInt();

System.out.println("n! == " + uMath.fact(n));

System.out.println("*** testing choose() ***");
System.out.print("number of pizza toppings:  ");
int p = kbd.nextInt();
System.out.print("number you can afford:  ");
int a = kbd.nextInt();

System.out.println("You have " + uMath.choose(p,a) + " choices");

System.out.print("More?  [y/n]");
if (!kbd.next().equals("y")) {
done = true;
}
} // while

System.out.println("Goodbye.");
} // main
}```

## Extra Credit (1 point each)

• Add to your Weather library a method, fahr2Cels, which takes a single floating point argument, representing a temperature in degrees centigrade. The method should return the equivalent temperature in degrees Fahrenheit.

• Add to your uMath library a method, choose, which takes two integer arguments, n and k. The method should return the value of "n choose k", which is the number of ways to choose k elements from a set of n things. This can be calculated from the following ratio:

# Expectations

As with all programming problems, your source code must be free of compile-time errors. If not, there is no credit for that part of the assignment. On the other hand, partial credit is alway awarded for partially-correct solutions (so long as they are legal programs).

# Turn In

• A paper copy of your source code files, Weather.java and uMath.java.

• Submit an electronic copy of the final versions of source code files. Remember to copy this to your turn in folder:

`/classes/cs124/<your last name>/lab5`

John H. E. Lasseter