(3)类象
学生姓名: 学 号: 专业班级: 网工班
实验类型:■ 验证 □ 综合 □ 设计 □ 创新 实验日期: 实验成绩:
实验目
实验目指导读者掌握Java语言面象程序设计方法理解象封装等概念求读者调试实例程序总结出面象事务定义象形式进行封装等容
二实验容
1课P22186 课P23781题
2编写名复数类包含复数实部虚部(数成员)复数间基算术运算:加减(方法成员)求复数加减运算程序运行中定复数进行运算印运算结果
3类描述游戏角色交手次数验值生命值间关系断定角色决斗胜负
三实验求
1加减运算够接收类型参数实现复数实数加减复数复数加减运算
2两游戏角色决斗角色1交手次数+1生命值-1验值+2角色2交手次数+1生命值-2验值+3验值增加50时生命值+1生命值<0判负生命值初始1000验值初始0
3定二角色判定交手胜负关系
4实验报告出决斗结果交手次数
5实验报告出源代码
四实验环境
1PC微机
2DOS操作系统 Windows 操作系统
3Eclipse程序集成环境
五实验步骤
容:
1创建复数类Complex定义实部虚部成员
2定义构造函数接收二double参数初始化数成员
3定义二复数运算函数plus()minus()接收复数类型实现复数复数加减运算
4定义二复数运算函数plus()minus()接收double类型实现复数实数加减运算
4定义印方法
5main()方法中创建复数象调相关方法验证
容二:
1建立角色类Role出相应成员生命值验值初始化角色象
2角色类中建立fight方法接收角色类型参数战斗返回胜者信息
3函数中初始化二角色调fight方法
六测试数
略
七实验报告(学生完成)
实验报告应包括部分:
1 程序流程图
鉴复数类Complex没复杂流程没画流程图
角色类流程图分三部分:
2 程序数结构设计
复数类Complex包含:
成员:
double类型realimaginary分代表实部虚部
方法:
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基准
方法:
私成员explifesetget方法
public boolean fight(Role other)参数Rolefight方法
public void fight()带参数fight方法
3 程序源代码相关注释
复数类源码:
package shiyan3
public class Complex {
private double real
private double imaginary
public Complex plus(Complex a) {
double x thisreal + agetReal()
double y thisimaginary + agetImaginary()
Complex tmp new Complex(x y)
return tmp
}
public Complex minus(Complex a) {
double x thisreal agetReal()
double y thisimaginary agetImaginary()
Complex tmp new Complex(x y)
return tmp
}
public Complex plus(double a) {
double x thisreal + a
double y thisimaginary
Complex tmp new Complex(x y)
return tmp
}
public Complex minus(double a) {
double x thisreal a
double y thisimaginary
Complex tmp new Complex(x y)
return tmp
}
public String print() {
if (thisimaginary > 0)
return thisreal + + + thisimaginary + i
else if (thisimaginary < 0)
return thisreal + + thisimaginary + i
else
return thisreal +
}
public double getReal() {
return real
}
public void setReal(double real) {
thisreal real
}
public double getImaginary() {
return imaginary
}
public void setImaginary(double imaginary) {
thisimaginary imaginary
}
public Complex() {
}参数构造方法
public Complex(double real double imaginary) {
thisreal real
thisimaginary 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)
Systemoutprintln(aprint() + + bprint() + 相加等 + aplus(b)print())
Systemoutprintln(aprint() + + cprint() + 相减等 + aminus(c)print())
Systemoutprintln(aprint() + + 12 + 相加等 + aplus(12)print())
Systemoutprintln(aprint() + + 10 + 相减等 + aminus(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) {
thislife life
}
public int getExp() {
return exp
}
public void setExp(int exp) {
thisexp exp
}
public void fight() {
life lifeSub
exp + expAdd
if ((exp expBase) > 50) {
life++
expBase + 50
}
}
public boolean fight(Role other) {
num++
thisfight()
otherfight()
Systemoutprintln(第 + num + 次战斗开始)
if (life < 0 && othergetLife() < 0) {
Systemoutprintln(战斗结束两)
return true
} else if (life < 0) {
Systemoutprintln(战斗结束角色2获胜)
return true
} else if (othergetLife() < 0) {
Systemoutprintln(战斗结束角色1获胜)
return true
} else {
Systemoutprintln( 角色1生命值 + life + 角色2生命值
+ othergetLife())
Systemoutprintln( 角色1验值 + exp + 角色2验值
+ othergetExp())
return false
}
}
public Role(int life int exp int lifeSub int expAdd) {
super()
thislife life
thisexp exp
thisexpBase exp
thislifeSub lifeSub
thisexpAdd expAdd
}
public static void main(String[] args) {
Role a new Role(1000 0 1 2)
Role b new Role(1000 0 2 3)
死循环两战斗直分出胜负止
while (afight(b)) {
}
}
}
4程序运行结果分析
复数类运行结果:
角色类程序部分运行结果:
八思考题(学生完成)
1定义点类PointPoint 类属性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) {
thisx x
}
public double getY() {
return y
}
public void setY(double y) {
thisy y
}
}
public class PointTest {
public static void main(String[] args) {
TODO Autogenerated method stub
Point a new Point()
asetX(10)
asetY(20)
Systemoutprintln(该点坐标(+agetX()+ +agetY()+))
asetX(305)
asetY(553)
Systemoutprintln(该点坐标(+agetX()+ +agetY()+))
}
}
Stack类:
package shiyan3
import javautilScanner
public class Stack {
int stacksize10
private int[] elem
public Stack(int stacksize) {
elem new int[stacksize]
thisstacksize stacksize
}
前栈中元素数
private int count
public boolean isEmpty(){
return count0truefalse
}
public boolean isFull(){
return countstacksizetruefalse
}
public void push(int var){
if(thisisFull()){
Systemoutprintln(栈已满进栈)
return
}
elem[count++]var
}
public void pop(){
if(thisisEmpty()){
Systemoutprintln(栈已空进栈)
return
}
count
return
}
public void print(){
Systemoutprintln(前栈中元素:)
for(int i0i
Systemoutprintln()
}
public static void main(String[] args) {
TODO Autogenerated method stub
Systemoutprintln(请输入栈)
Scanner sc new Scanner(Systemin)
int size scnextInt()
Stack s new Stack(size)
int op
while(true){
Systemoutprintln(请选择进行操作:1 进栈2 出栈 )
opscnextInt()
if(op1){
Systemoutprint(请输入进栈数)
int varscnextInt()
spush(var)
sprint()
}
else if(op2){
spop()
sprint()
}
else{
Systemoutprintln(输入误请重新输入)
}
}
}
}
文档香网(httpswwwxiangdangnet)户传
《香当网》用户分享的内容,不代表《香当网》观点或立场,请自行判断内容的真实性和可靠性!
该内容是文档的文本内容,更好的格式请下载文档