Java实验报告(w5-w6)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

福建师大福清分校计算机实验报告(W5,W6)

院/系:数学与计算机科学系课程名称:Java面向对象程序设计日期:

析注意非法字符

2

class Letter{

char start = 'A';

void printLetter()

{

for(int i = 0;i<25;i++)

{

char c = '0';

c = (char)(i+start);

System.out.printf("%c ",c);

if(i%7==0&&i!=0) System.out.printf("\n");

}

}

}

class Letter2{

public static void main(String[] args){

Letter p = new Letter();

p.printLetter();

}

}

3.

import java.util.*;

public class DengCha3{

public static void main(String[] args){

DengCha shulie = new DengCha();

shulie.setStart(6);

shulie.setD(7);

System.out.printf("首项为6,公差为7的等差数列的前5项和为%d",shulie.getSum(5));

}

}

class DengCha{

int s;

int d;

int sum ;

DengCha(){}

void setStart(int s){this.s = s;}

void setD(int d){this.d = d;}

int getSum(int n)

{

sum = n*s+n*(n-1)*d/2;

return sum;

}

}

4.

import ng.Math;

class SquareEquation{

double a;

double b;

static double c;

double x1,x2;

SquareEquation(double a,double b,double c)

{

this.a = a;

this.b = b;

SquareEquation.c = c;

}

void getRoots()

{

double temp = b*b-4*a*c;

if(temp<0) System.out.println("方程无根\n");

else if(temp != 0)

{

x1 = (-b+Math.sqrt(temp))/(2.0*a);

x2 = (-b-Math.sqrt(temp))/(2.0*a);

System.out.printf("方程有两个不同的实根其中x1=%.2f,x2=%.2f\n",x1,x2);

}

else

{

x1 = -b/(2.0*a);

System.out.printf("方程有两个相同的实根,值为%f\n",x1);

}

}

}

public class SquareEquation4{

public static void main(String[] args){

SquareEquation yi = new SquareEquation(1,2,3);

System.out.println("方程式一为x*x+2x+3=0");

yi.getRoots();

SquareEquation er = new SquareEquation(4,10,1);

System.out.println("方程式二为4x^2+10x=0");

er.getRoots();

System.out.printf("方程一的常数项为%.2f,方程二的常数项为%.2f",yi.c,er.c);

}

}

5.

public class Shiyan_5{

public static void main(String args[]){

Complex x = new Complex(4.0,2.1);

Complex y = new Complex(2.5,5.0);

Complex t1 = new Complex( );

Complex t2 = new Complex( );

t1 =x.add(y);

t2 =x.sub(y);

t1.print( );

t2.print( );

}

}

class Complex{

double a;

double b;

Complex(){}

Complex(double a1,double b1){a =a1;b=b1;} Complex add(Complex x){

double a1,a2;

a1 = a + x.a;

a2 = b + x.b;

return new Complex(a1,a2);

}

Complex sub(Complex x){

double a1,a2;

相关文档
最新文档