Created
September 21, 2023 23:42
-
-
Save mfelleisen/2459ecfb0452a39be31f983e8e844fc9 to your computer and use it in GitHub Desktop.
lecture 8/checking arguments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
+----------------------+ || +--------------+ | |
| Client Module | --- program to ------> | ITemperature | | |
+----------------------+ || +--------------+ | |
|| ^ | |
|| | implements | |
|| | | |
|| +----------------+-------------+ | |
|| | | | | |
|| +------------+ +---------+ +--------+ | |
|| | Fahrenheit | | Celsius | | Kelvin | | |
|| +------------+ +---------+ +--------+ | |
*/ | |
class Client { | |
public static void main(String argv[]) { | |
ITemperature k = ITemperature.checked_builder(1,"K"); | |
ITemperature f = ITemperature.checked_builder(1,"C"); | |
System.out.println(k.less_than(f) + " should be true (K less F)"); | |
System.out.println(f.less_than(f) + " should be false (F less F)"); | |
} | |
} | |
// ============================================================================= | |
// ============================================================================= | |
interface ITemperature { | |
// is `this` temperature less than `t` | |
boolean less_than(ITemperature t); | |
static ITemperature checked_builder(double t, String s) { | |
if ( (s.equals("K")) && (t < 0)) | |
throw new RuntimeException("t K does not exist"); | |
if ( (s.equals("F")) && ((t - 32) / 1.79999999 + 273.15 < 0) ) | |
throw new RuntimeException("t F does not exist"); | |
return ATemperature.builder(t,s); | |
} | |
} | |
// ----------------------------------------------------------------------------- | |
abstract class ATemperature implements ITemperature { | |
// --------------------------------------------------------------------------- | |
// check assumptions first, then call workhorse | |
// --------------------------------------------------------------------------- | |
public static ITemperature builder(double t, String s) { | |
if (s.equals("K")) | |
return new Kelvin(t); | |
else if (s.equals("F")) | |
return new Fahrenheit(t); | |
else | |
throw new RuntimeException("bad temp. kind"); | |
} | |
public boolean less_than(ITemperature t) { | |
Kelvin this_normal = this.normalize(); | |
Kelvin t_normal = ((ATemperature)t).normalize(); | |
return this_normal.less_than_normal(t_normal); | |
} | |
protected abstract Kelvin normalize(); | |
protected boolean less_than_normal(Kelvin t) { | |
throw new RuntimeException("n/a"); | |
} | |
} | |
// ----------------------------------------------------------------------------- | |
class Fahrenheit extends ATemperature { | |
double f; | |
Fahrenheit(double f) { | |
this.f = f; | |
} | |
protected Kelvin normalize() { | |
double g = (this.f - 32) / 1.79999999 + 273.15; | |
return new Kelvin(g); | |
} | |
} | |
// ----------------------------------------------------------------------------- | |
class Kelvin extends ATemperature { | |
double k; | |
Kelvin(double k) { | |
this.k = k; | |
} | |
protected boolean less_than_normal(Kelvin t) { | |
return this.k < t.k; | |
} | |
protected Kelvin normalize() { | |
return this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment