实验3-类与对象

时间:2020-11-29 11:08:12 浏览量:

 XX大学实验报告

 ---(3)类与对象 学生姓名:

  学

 号:

 专业班级:

 网工班

 实验类型:■ 验证 □ 综合 □ 设计 □ 创新

  实验日期:

  实验成绩:

  一、实验目的 本实验的目的用于指导读者掌握Java语言面向对象的程序设计方法,理解对象的封装等概念,要求读者在调试实例程序后,总结出面向对象的事务定义以及以对象的形式进行封装等内容。

 二、实验内容

  1、课本P221,8-6 和 课本P237,8.1题 2、编写一个名为“复数”的类,包含复数的实部和虚部(数据成员),以及复数之间的基本算术运算:加、减(方法成员),并要求复数加减运算,程序运行中能对给定的复数进行运算,并打印运算的结果。

 3、用类来描述游戏角色的交手次数、经验值、生命值之间的关系,并断定角色决斗的胜负。

 三、实验要求 1、加减运算能够接收不同类型的参数既可以实现复数与实数的加减、复数与复数的加减运算。

 2、两游戏角色决斗。角色1交手次数+1,生命值-1,经验值+2;角色2交手次数+1,生命值-2,经验值+3。经验值每增加50时,生命值+1;生命值<0判为负。生命值初始为1000,经验值初始为0。

  3、给定二个不同的角色,判定交手的胜负关系。

 4、实验报告给出决斗结果和交手次数 5、实验报告给出所有源代码。

  四、实验环境 1、PC微机; 2、DOS操作系统或 Windows 操作系统; 3、Eclipse程序集成环境。

  五、实验步骤

 内容一:

 1、创建“复数”类Complex,定义实部、虚部成员 2、定义构造函数接收二个double参数用来初始化数据成员 3、定义二个复数运算函数plus()以及minus()各接收一个复数类型以实现复数与复数的加减运算。

 4、定义二个复数运算函数plus()以及minus()各接收一个double类型以实现复数与与实数的加减运算。

 4、定义一个打印方法。

 5、在main()方法中创建复数对象并调用相关方法来验证。

 内容二:

 1、建立角色类Role,给出相应的成员,并能以生命值、经验值初始化角色对象。

 2、在角色类中建立fight方法,接收一个角色类型的参数并与之“战斗”,返回胜者信息。

 3、在主函数中初始化二个角色,调用fight方法。

   六、测试数据

  略 七、实验报告(学生完成) 实验报告应包括以下几个部分:

 1、 程序流程图; 鉴于复数类Complex没有复杂的流程,所以没有画流程图。

 角色类的流程图,我分了三个部分:

  2、 程序的数据结构设计; 复数类Complex包含:

 成员:

 double类型的real和imaginary,分别代表实部和虚部

 方法:

 public Complex plus(Complex a); public Complex minus(Complex a); public Complex plus(double a); public Complex minus(double a); 分别进行复数与复数或实数的加减法运算

  public String print()//打印复数类的信息 角色类Role包含:

 成员:

  private int life; // 生命值

 private int exp; // 经验值 private int lifeSub; //每次战斗生命值减少的量

 private int expAdd;//每次战斗经验值增加的量

 public static int num; // 交手的次数

 private int expBase; // 经验值增加50的基准 方法:

  私有成员exp和life的set和get方法。

  public boolean fight(Role other)//参数为Role的fight方法。

  public void fight()//不带参数的fight方法。

 3、 程序的源代码及相关注释 复数类源码:

 package shiyan3;

 public class Complex {

 private double real;

 private double imaginary;

  public Complex plus(Complex a) {

  double x = this.real + a.getReal();

  double y = this.imaginary + a.getImaginary();

  Complex tmp = new Complex(x, y);

  return tmp;

 }

  public Complex minus(Complex a) {

  double x = this.real - a.getReal();

  double y = this.imaginary - a.getImaginary();

  Complex tmp = new Complex(x, y);

  return tmp;

 }

  public Complex plus(double a) {

  double x = this.real + a;

  double y = this.imaginary;

  Complex tmp = new Complex(x, y);

  return tmp;

 }

  public Complex minus(double a) {

  double x = this.real - a;

  double y = this.imaginary;

  Complex tmp = new Complex(x, y);

  return tmp;

 }

  public String print() {

  if (this.imaginary > 0)

 return this.real + “+“ + this.imaginary + “i“;

  else if (this.imaginary < 0)

 return this.real + ““ + this.imaginary + “i“;

  else

 return this.real + ““;

 }

  public double getReal() {

  return real;

 }

  public void setReal(double real) {

  this.real = real;

 }

  public double getImaginary() {

  return imaginary;

 }

  public void setImaginary(double imaginary) {

  this.imaginary = imaginary;

 }

  public Complex() {

 };//无参数的构造方法。

 public Complex(double real, double imaginary) {

  this.real = real;

  this.imaginary = imaginary;

 }

  public static void main(String[] args) {

  Complex a = new Complex(1, 2);

  Complex b = new Complex(4, 5);

  Complex c = new Complex(2, 3);

  System.out.println(a.print() + “ 和“ + b.print() + “相加等于 “ + a.plus(b).print());

  System.out.println(a.print() + “ 和“ + c.print() + “相减等于 “ + a.minus(c).print());

  System.out.println(a.print() + “ 和“ + 12 + “相加等于 “ + a.plus(12).print());

  System.out.println(a.print() + “ 和“ + 10 + “相减等于 “ + a.minus(10).print());

 } } 角色类Role源码:

 package shiyan3;

 public class Role {

 // 生命值

 private int life;

 // 经验值

 private int exp;

 // 交手的次数

 private int lifeSub;

 private int expAdd;

 public static int num;

 // 经验值增加50的基准

 private int expBase;

  public int getLife() {

  return life;

 }

  public void setLife(int life) {

  this.life = life;

 }

  public int getExp() {

  return exp;

 }

  public void setExp(int exp) {

  this.exp = exp;

 }

  public void fight() {

  life -= lifeSub;

  exp += expAdd;

  if ((exp - expBase) >= 50) {

 life++;

 expBase += 50;

  }

 }

  public boolean fight(Role other) {

  num++;

  this.fight();

  other.fight();

  System.out.println(“第“ + num + “次战斗开始“);

  if (life <= 0 && other.getLife() <= 0) {

 System.out.println(“战斗结束,两人打平“);

 return true;

  } else if (life <= 0) {

 System.out.println(“战斗结束,角色2获胜“);

 return true;

  } else if (other.getLife() <= 0) {

 System.out.println(“战斗结束,角色1获胜“);

 return true;

  } else {

 System.out.println(“

 角色1的生命值为“ + life + “,角色2的生命值为“

 + other.getLife());

 System.out.println(“

 角色1的经验值为“ + exp + “,角色2的经验值为“

 + other.getExp());

 return false;

  }

 }

  public Role(int life, int exp, int lifeSub, int expAdd) {

  super();

  this.life = life;

  this.exp = exp;

  this.expBase = exp;

  this.lifeSub = lifeSub;

  this.expAdd = expAdd;

 }

  public static void main(String[] args) {

  Role a = new Role(1000, 0, 1, 2);

  Role b = new Role(1000, 0, 2, 3);

  // 死循环,两人战斗,直到分出胜负为止

  while (!a.fight(b)) {

  }

 } } 4、程序运行结果的分析。

 复数类的运行结果:

  角色类程序的部分运行结果:

  八、思考题(学生完成) 1.定义一个点类“Point”。Point 类的属性有x 轴和y 轴坐标,可以通过方法setX()设置x轴坐标,方法setY()设置y 轴坐标,方法getX()获得x 轴坐标,方法getY()获得y 轴坐标。编写一个测试类PointTest 来创建Point 类的对象,测试该类。

 2.编程定义一个栈类,它封装了栈数组,判断栈空方法、判断栈满方法以及进栈和出栈。

  Point类:

 package shiyan3; class Point{

 private double x;

 private double y;

 public double getX() {

  return x;

 }

 public void setX(double x) {

  this.x = x;

 }

 public double getY() {

  return y;

 }

 public void setY(double y) {

  this.y = y;

 } } public class PointTest {

  public static void main(String[] args) {

  // TODO Auto-generated method stub

  Point a = new Point();

  a.setX(10);

  a.setY(20);

  System.out.println(“该点的坐标为(“+a.getX()+“ , “+a.getY()+“)“);

  a.setX(30.5);

  a.setY(55.3);

  System.out.println(“该点的坐标为(“+a.getX()+“ , “+a.getY()+“)“);

 }

 } Stack类:

 package shiyan3;

 import java.util.Scanner;

 public class Stack {

 int stacksize=10;

 private int[] elem;

 public Stack(int stacksize) {

  elem = new int[stacksize];

  this.stacksize = stacksize;

 }

 //当前栈中元素的个数

 private int count;

 public boolean isEmpty(){

  return count==0?true:false;

 }

 public boolean isFull(){

  return count==stacksize?true:false;

 }

 public void push(int var){

  if(this.isFull()){

 System.out.println(“栈已满,不能进栈!!!“);

 return;

  }

  elem[count++]=var;

 }

 public void

 pop(){

  if(this.isEmpty()){

 System.out.println(“栈已空,不能进栈!!!“);

 return ;

  }

  --count;

  return;

 }

 public void print(){

  System.out.println(“当前栈中的元素为:“);

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

 System.out.print(elem[i]+“ “);

  System.out.println();

 }

 public static void main(String[] args) {

  // TODO Auto-generated method stub

  System.out.println(“请输入栈的大小“);

  Scanner sc = new Scanner(System.in);

  int size = sc.nextInt();

  Stack s = new Stack(size);

  int op;

  while(true){

 System.out.println(“请选择要进行的操作:1 进栈;2 出栈 “);

 op=sc.nextInt();

 if(op==1){

  System.out.print(“请输入要进栈的数:“);

  int var=sc.nextInt();

  s.push(var);

  s.print();

 }

 else if(op==2){

  s.pop();

  s.print();

 }

 else{

  System.out.println(“输入有误,请重新输入。“);

 }

  }

  }

 }

 

推荐访问:对象 实验

《实验3-类与对象.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:

文档为doc格式

一键复制全文 下载 投诉