Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
583
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.52 KB | None | 0 0
  1. import tester.Tester;
  2.  
  3. interface IFunc<X, Y> {
  4.   Y apply(X x);
  5. }
  6.  
  7. // A function from Int -> Int that can be plotted on the cartesian plane
  8. abstract class Plottable implements IFunc<Integer, Integer> {
  9.  
  10.   // Does this function cover the origin? (does f(0) = 0?)
  11.   boolean coversOrigin() {
  12.     return ...;
  13.   }
  14. }
  15.  
  16. // f(x) = x
  17. class Identity extends Plottable {
  18.   public Integer apply(Integer x) {
  19.     return x;
  20.   }
  21. }
  22. // f(x) = ax^2 + bx + c
  23. class Quadratic extends Plottable {
  24.   int a;
  25.   int b;
  26.   int c;
  27.  
  28.   Quadratic(int a, int b, int c) {
  29.     this.a = a;
  30.     this.b = b;
  31.     this.c = c;
  32.   }
  33.  
  34.   public Integer apply(Integer x) {
  35.     return this.a * x * x + this.b * x + this.c;
  36.   }
  37. }
  38.  
  39. class ExamplesPlottable {
  40.   Plottable identity = new Identity();
  41.   Plottable xSquared = new Quadratic(1, 0, 0);
  42.   Plottable xSquaredPlusOne = new Quadratic(1, 0, 1);
  43.  
  44.   boolean testIdentity(Tester t) {
  45.     return t.checkExpect(identity.apply(0), ?)
  46.         && t.checkExpect(identity.apply(5), ?);
  47.   }
  48.  
  49.   boolean testXSquared(Tester t) {
  50.     return t.checkExpect(xSquared.apply(0), ?)
  51.         && t.checkExpect(xSquared.apply(5), ?);
  52.   }
  53.  
  54.   boolean testXSquaredPlusOne(Tester t) {
  55.     return t.checkExpect(xSquaredPlusOne.apply(0), ?)
  56.         && t.checkExpect(xSquaredPlusOne.apply(5), ?);
  57.   }
  58.  
  59.   boolean testCoversOrigin(Tester t) {
  60.     return t.checkExpect(identity.coversOrigin(), ?)
  61.         && t.checkExpect(xSquared.coversOrigin(), ?)
  62.         && t.checkExpect(xSquaredPlusOne.coversOrigin(), ?);
  63.   }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement