// Program for Monday of week of 6/9/04 import java.awt.*; import javabook.*; class Poly { int c0,c1,c2; // the function eval returns the value of the y co-ordinate public int Eval(int x) { return (c0*x*x + c1*x + c2); } public void Plot(Graphics g, Color j) { int i,y; g.setColor(j); // 100 values taken intead of 10 for a visible output for(i=0;i<=100;i++) { y=Eval(i); g.drawOval(i,y,2,2); } } } class MaxPoly { Poly p1 = new Poly(); Poly p2 = new Poly(); MaxPoly(int c00,int c01,int c02,int c10,int c11,int c12) { p1.c0 = c00; p1.c1 = c01; p1.c2 = c02; p2.c0 = c10; p2.c1 = c11; p2.c2 = c12; } MaxPoly(Poly p, Poly q) { p1 = p; p2 = q; } // this function will plot the max of the two fuctions // the fuction max can be used instead of the if condition public void Plot(Graphics g, Color j) { int i,y; g.setColor(j); for(i=0;i<=100;i++) { if(p1.Eval(i) > p2.Eval(i)) y = p1.Eval(i); else y = p2.Eval(i); g.drawOval(i,y,2,2); } } // the function uses the trapeziodal rule to calculate the area under the curve // here the value of h =1 // area of each portion = 1/2(f(x) +f(x+h)) public float Integrate() { float sum = 0; float previous=0; int i,y; if(p1.Eval(0) > p2.Eval(0)) y = p1.Eval(0); else y = p2.Eval(0); previous = y; for(i=1;i<=100;i++) { if(p1.Eval(i) > p2.Eval(i)) y = p1.Eval(i); else y = p2.Eval(i); sum = sum + (previous + y) / 2; previous = y; } return sum; } } class Test { public static void main(String[] args) { Poly p1 = new Poly(); Poly p2 = new Poly(); int c0,c1,c2,c3,c4,c5; MainWindow mainWindow = new MainWindow("hi"); mainWindow.setVisible(true); Graphics graphics = mainWindow.getGraphics(); InputBox inputBox = new InputBox(mainWindow); OutputBox outputBox = new OutputBox(mainWindow); c0 = inputBox.getInteger("Enter value of c0"); c1 = inputBox.getInteger("Enter value of c1"); c2 = inputBox.getInteger("Enter value of c2"); c3 = inputBox.getInteger("Enter value of c3"); c4 = inputBox.getInteger("Enter value of c4"); c5 = inputBox.getInteger("Enter value of c5"); p1.c0 = c0; p1.c1 = c1; p1.c2 = c2; p2.c0 = c3; p2.c1 = c4; p2.c2 = c5; p1.Plot(graphics, Color.red); p2.Plot(graphics, Color.blue); MaxPoly mp1 = new MaxPoly (p1, p2); mp1.Plot(graphics,Color.green); float area1 = mp1.Integrate(); System.out.println("area1 is" + area1); MaxPoly mp2 = new MaxPoly (c0,c1,c2,c3,c4,c5); mp2.Plot(graphics,Color.green); float area2 = mp2.Integrate(); System.out.println("area2 is" + area2); } }