我正在嘗試輸出一個數學函數,但我不知道如何調用我的類來繪制/計算該函數。在主課中,我嘗試了以下public class GraphingProgram {public static void main(String[] args) { Applet program = new Applet(); program.setSize(300, 400); program.setName("Graphing Program"); GraphApplet testFunction = new GraphApplet(); program.add(testFunction); program.setVisible(true); } 班級代碼public class GraphApplet extends Applet{ double f(double x) { return (Math.cos(x/5) + Math.sin(x/7) + 2) * getSize().height/ 4; } public void paint (Graphics g) { for(int x = 0; x< getSize().width; x++) g.drawLine(x, (int) f(x), x+1, (int) f(x+1)); } public String getAppletInfo() { return "Draw a function graph"; } }執行程序時,我們應該期望看到類的函數圖。例如,我應該能夠在給定的間隔上輸出圖形 f(x) = cos(x/5) + sin(x/7) + 2,如下所示!https://i.imgur.com/przHRk6.png
添加回答
舉報
0/150
提交
取消