高分网 > 答案大全 > 作业答案 > 课后作业答案 >

java课后答案(2)

时间: 炎婷2 课后作业答案

  this.d=d;  }

  int getSum(int n){

  int sum=0,i=1;

  while(i<=n){

  sum=sum+start;

  start=start+d;

  i++; }

  return sum; } }

  public class ZuoYe04_1

  {public static void main (String args[ ])

  {DengCha shulie=new DengCha(2,3);

  System.out.println(shulie.getSum(100));

  shulie.setStart(10);

  shulie.setD(5);

  System.out.println(shulie.getSum(9));

  DengCha arithmetical=new DengCha();

  Scanner Reader=new Scanner(System.in);

  System.out.println("请输入等差数列开始值");

  arithmetical.start=Reader.nextInt();

  System.out.println("请输入等差数列的差值");

  arithmetical.d=Reader.nextInt();

  System.out.println("请输入等差数列求和的个数"); int n=Reader.nextInt();

  System.out.println(n+"个数的和是"+arithmetical.getSum(n));  }  }

  3.编写一个类,该类封装了一元二次方程的属性和功能,即该类有刻画方程系数的3个成员变量和求根的方法。要求:该类所有对象共享常数项。

  class SquareEquation{

  double a,b;

  static double c;

  double root1,root2;

  boolean boo;

  public SquareEquation(double a,double b,double c){

  this.a=a;

  this.b=b;

  SquareEquation.c=c;

  if(a!=0){

  boo=true;  }

  else{

  boo=false;  }  }

  public void getRoots(){

  if(boo){

  System.out.println("是一元2次方程");

  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,%f\n",root1,root2);  }

  else{

  System.out.printf("方程没有实根\n");  }  }

  else{

  System.out.println("不是一元2次方程");  }  }

  public void setCoefficient(double a,double b,double c){

  this.a=a;

  this.b=b;

  SquareEquation.c=c;

  if(a!=0){

  boo=true;  }

  else{

  boo=false;  }  }  }

  public class ZuoYe4_3{

  public static void main(String args[ ]){

  SquareEquation equation1=new SquareEquation(4,5,1);

  SquareEquation equation2=new SquareEquation(3,5,-7);

  equation1.getRoots();

  equation2.getRoots();  }  }

  4.程序源文件Example.java如果使用了包语句,例如:

  package tom.jiafei;

  并且有c:\1000\tom\jiafei的目录结构, 并且将源文件编译后得到的全部字节码文件拷贝到目录c:\1000\tom\jiafei中。问在哪个目录中运行此文件,运行命令是什么?

  答:运行程序时必须到tom\jiafei的上一层目录1000中来运行,如:

  c:\1000\java tom.jiafei. Example

47754