福建师大福清分校计算机实验报告(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;
a1 = a - x.a;
a2 = b - x.b;
return new Complex(a1,a2);
}
void print()
{
System.out.printf("实部%.2f,虚部%.2f\n",a,b);
}
}
说明:
系统实施:(程序流程图、重要过程说明、参数设置、变量的说明等)
测试报告:(调试输入数据、结果数据或状态。
给出一、两个不同的输入和结果)结果分析:(碰到的问题、如何解决、有何体会)
附录:(主要的源程序代码)。