public class FunctionManager { private static final int Y_START = -10; private static final int Y_STOP = 10; private static final int Y_STEP = 1; private static final int X_START = -10; private static final int X_STOP = 10; private static final int X_STEP = 1; static int aSquare; static int bSquare; static int cSquare; static int aLinear; static int bLinear; public FunctionManager(int aSquare, int bSquare, int cSquare, int aLinear, int bLinear) { this.aSquare = aSquare; this.bSquare = bSquare; this.cSquare = cSquare; this.aLinear = aLinear; this.bLinear = bLinear; printFunctions(); } public static void main(String[] args) { new FunctionManager(1,-5,6,2,-6); } private static void printFunctions() { for (int y = Y_START;y<=Y_STOP;y+=Y_STEP){ buildRow(y); System.out.println(); } printXAxis(); } private static void printXAxis() { System.out.print(" "); for (int x = X_START; x<= X_STOP; x+= X_STEP){ System.out.print(String.format("%-5s",x)); } } private static void buildRow(int y) { printYAxis(y); for (int x = X_START; x<= X_STOP; x+= X_STEP){ buildPoint(x,y); } } private static void buildPoint(int x, int y) { boolean isSquareHit = testSquareFunctionInPoint(x,y); boolean isLinearHit = testLinearFunctionInPoint(x,y); if (isSquareHit && isLinearHit){ System.out.print(" ~ "); } else if (isSquareHit && (!isLinearHit)){ System.out.print(" * "); } else if ((!isSquareHit) && isLinearHit){ System.out.print(" - "); } else { System.out.print(" "); } } private static boolean testLinearFunctionInPoint(int x, int y) { return g(aLinear, bLinear, x) == y; } private static boolean testSquareFunctionInPoint(int x, int y) { return f(aSquare,bSquare,cSquare,x) == y; } private static double f(double a, double b, double c, int x){ return a * x * x + b * x + c; } private static double g(double a, double b, int x){ return a * x + b; } private static void printYAxis(int y) { System.out.print(String.format("%-5s",y)); } }