using System; using edu.neu.ccs.demeterf; using Fields = edu.neu.ccs.demeterf.control.Fields; using System.IO; // shape.java public abstract class shape{ public abstract double area(); public shape add(shape s){ return new pair(s,this); } public static void Main(String[] a){ shape s = new circle(5) .add(new square(5)) .add(new circle(2)) .add(new rect(4,9)); Console.WriteLine(" s == "+s); Console.WriteLine(" s.area() == "+s.area()); Console.WriteLine(" area(s) == "+Area.area(s)); Console.WriteLine(" area(s) == "+Count.count(s)); Console.WriteLine(" c->s(s) == "+Circ2sqr.circ2sqr(s)); Console.WriteLine(" scale(s) == "+Scale.scale(s)); Console.WriteLine("depthScl(s) == "+DepthScl.scale(s)); Console.WriteLine(" leftScl(s) == "+LeftScl.scale(s)); shape[] ss = {s,Circ2sqr.circ2sqr(s),Scale.scale(s), DepthScl.scale(s),LeftScl.scale(s)}; string[] fs = {"shape.svg","circ2sqr.svg","scale.svg", "depth.svg","left.svg"}; for(int i = 0; i < fs.Length; i++){ Console.WriteLine(" Do["+i+"]: "+fs[i]); try{ StreamWriter o = new StreamWriter(new FileStream(fs[i],FileMode.Create)); o.WriteLine(Display.display(ss[i])); o.Close(); }catch(IOException e){ Console.WriteLine("BAD: "+e); } } } public override string ToString() { return new Traversal(new Str()).traverse(this); } } class circle : shape{ int radius; public circle(int r){ radius = r; } public override double area(){ return Math.PI*radius*radius; } } class square : shape{ int side; public square(int s){ side = s; } public override double area(){ return side*side; } } class pair : shape{ shape left, right; public pair(shape l, shape r){ left = l; right = r; } public override double area(){ return left.area()+right.area(); } public class leftF : Fields.any{} public class rightF : Fields.any{} } class rect : shape{ int width, height; public rect(int w, int h){ width = w; height = h; } public override double area(){ return width*height; } } // Area function class class Area : ID{ double combine(circle c, int r){ return Math.PI*r*r; } double combine(square s, int d){ return d*d; } double combine(rect r, int w, int h){ return w*h; } double combine(pair p, double l, double r){ return l+r; } public static double area(shape s){ return new Traversal(new Area()).traverse(s); } } // Count function class class Count : ID{ int combine(shape c){ return 1; } int combine(pair p, int l, int r){ return l+r; } public static int count(shape s){ return new Traversal(new Count()).traverse(s); } } // circle -> square function class class Circ2sqr : Bc{ shape combine(circle c, int r){ return new square(r+r); } public static shape circ2sqr(shape s){ return new Traversal(new Circ2sqr()).traverse(s); } } class Str : ID{ string combine(circle c, int r){ return "circle("+r+")"; } string combine(square s, int d){ return "square("+d+")"; } string combine(rect r, int w, int h) { return "rect("+w+", "+h+")"; } string combine(pair p, string l, string r) { return "pair("+l+", "+r+")"; } } // scale function class class Scale : Bc{ int combine(int i){ return i*2; } public static shape scale(shape s){ return new Traversal(new Scale()).traverse(s); } } // DepthScl function class class DepthScl : Bc{ int update(pair p, Fields.any f, int d){ return d+1; } int combine(int i, int d){ return i*d; } public static shape scale(shape s){ return new Traversal(new DepthScl()).traverse(s,1); } } // LeftScl function class class LeftScl : DepthScl{ // Don't increase the depth for 'right' children int update(pair p, pair.rightF f, int d){ return d; } public static shape scale(shape s){ return new Traversal(new LeftScl()).traverse(s,1); } } class Display : ID{ static int y = 50; int update(pair p, pair.rightF f, int x){ return x+45; } string combine(pair p, string l, string r){ return l+r; } int combine(int i){ return 3*i; } string combine(circle c, int r, int x) { return svg.circle(x,y,r); } string combine(square s, int sd, int x) { return svg.rect(x,y,sd,sd); } string combine(rect r, int w, int h, int x) { return svg.rect(x,y,w,h); } public static string display(shape s){ return svg.image(240, 100, new Traversal(new Display()) .traverse(s,40)); } } class svg{ public static string head(int w, int h){ round = 0; return ""+ ""+ "\n"; } public static string foot(){ return "\n"; } public static string end(){ return " fill='"+col()+"' fill-opacity='0.5' />\n"; } public static string[] colors = {"red","green","blue","orange","purple"}; public static int round = 0; public static string col(){ return colors[round++]; } public static string circle(int x, int y, int r){ return ("