1、4-1 class Y int a,b,y,i; int sum=0; public void f() while(i1) y=b*(i-1)+a; sum=sum+y; i-; System.out.println(“等差数列的和为 :“+sum); public class XIN41 public static void main(String args) Y y1=new Y(); y1.sum=0; java.util.Scanner sc =new java.util.Scanner(System.in); System.out.println(“请输入首项的值 a:“); y1.
2、a =sc.nextInt(); System.out.println(“请输入公差的值 b:“); y1.b = sc.nextInt(); System.out.println(“请输入项数 i:“); y1.i = sc.nextInt(); y1.f(); 4-2 class A double a, b; static double c; double root1, root2; boolean boo; public A(double a, double b, double c) this.a = a; this.b = b; A.c = c; if (a != 0) boo = t
3、rue; else boo = false; public void getRoots() if (boo) System.out.println(“是一元次方程 “); double disk = b * b - 4 * a * c; if (disk = 0) root1 = (-b + Math.sqrt(disk) / (2 * a); root2 = (-b - Math.sqrt(disk) / (2 * a); System.out.printf(“方程的根 :%f,%fn“, root1, root2); else System.out.printf(“方程没有实根 n“); else System.out.println(“不是一元次方程 “); public void B(double a, double b, double c) this.a = a; this.b = b; A.c = c; if (a != 0) boo = true; else boo = false; public class XIN42 public static void main(String args ) A a1 = new A(4, 4, 1); A a2 = new A(1, 2, 1); a1.getRoots(); a2.getRoots();