java习题文档格式.docx
- 文档编号:15840699
- 上传时间:2022-11-16
- 格式:DOCX
- 页数:41
- 大小:25.12KB
java习题文档格式.docx
《java习题文档格式.docx》由会员分享,可在线阅读,更多相关《java习题文档格式.docx(41页珍藏版)》请在冰豆网上搜索。
ButtoncaculateBtn[]=newButton[16];
StringbtnLabel[]={"
1"
"
2"
3"
+"
4"
5"
6"
-"
"
7"
8"
9"
*"
0"
C"
="
/"
};
TextFieldinOutTfd=newTextField(50);
PanelbtnPanel=newPanel();
publicvoidinit()
{
btnPanel.setLayout(newGridLayout(4,4));
for(inti=0;
i<
16;
i++)
caculateBtn[i]=newButton(btnLabel[i]);
caculateBtn[i].addActionListener(this);
btnPanel.add(caculateBtn[i]);
}
setLayout(newBorderLayout());
add("
North"
inOutTfd);
Center"
btnPanel);
inOutTfd.setText("
);
caculateStatus=0;
publicvoidactionPerformed(ActionEventae)
if(ae.getSource()instanceofButton)
charch=((Button)(ae.getSource())).getLabel().charAt(0);
//System.out.println(ch+"
status:
+caculateStatus);
switch(ch)
case'
C'
:
firstOperant=secondOperant=0;
break;
='
if(caculateStatus==0)
caculateStatus=3;
elseif(caculateStatus==2)
oneStepCaculate();
//System.out.println(firstOperant+"
+secondOperant);
+'
-'
*'
/'
if(caculateStatus==0||caculateStatus==3)
secondOperant=0;
caculateStatus=1;
elseif(caculateStatus==1)
operator=ch;
default:
//数字键
inttemp=(int)firstOperant;
temp=temp*10+ch-'
0'
;
inOutTfd.setText(Integer.toString(temp));
firstOperant=temp;
secondOperant=ch-'
inOutTfd.setText(Integer.toString((int)secondOperant));
caculateStatus=2;
inttemp=(int)secondOperant;
secondOperant=temp;
elseif(caculateStatus==3)
firstOperant=ch-'
inOutTfd.setText(Integer.toString((int)firstOperant));
}//switch
publicvoidoneStepCaculate()
switch(operator)
firstOperant+=secondOperant;
firstOperant-=secondOperant;
:
firstOperant*=secondOperant;
firstOperant/=secondOperant;
inOutTfd.setText(Double.toString(firstOperant));
}}
2.根据下面的要求编程实现复数类ComplexNumber。
(1)复数类的属性包括实数部分和虚数部分
(2)复数类的方法有加法add,减法subtract
并编程验证以上的复数类。
接收用户输入的复数的实部和虚部,计算复数与复数、复数与实数的加、减的操作。
publicclasstest1
publicstaticvoidmain(Stringargs[])
ComplexNumbercNumber_1=
newComplexNumber(3,-5);
ComplexNumbercNumber_2=
newComplexNumber(2,2);
doubled=10.0;
System.out.println(cNumber_1.toString()+"
加"
+cNumber_2.toString()+"
等于"
+cNumber_plexAdd(cNumber_2).toString());
+d+"
+cNumber_plexAdd(d).toString());
减"
+cNumber_plexMinus(cNumber_2).toString());
+cNumber_plexMinus(d).toString());
乘"
+cNumber_plexMulti(cNumber_2).toString());
+cNumber_plexMulti(d).toString());
}
classComplexNumber
//域
privatedoublem_dRealPart;
privatedoublem_dImaginPart;
//构造函数
ComplexNumber()
m_dRealPart=0.0;
m_dImaginPart=0.0;
ComplexNumber(doubler,doublei)
m_dRealPart=r;
m_dImaginPart=i;
ComplexNumber(ComplexNumberc)
m_dRealPart=c.getRealPart();
m_dImaginPart=c.getImaginaryPart();
//get,set方法
doublegetRealPart()
returnm_dRealPart;
doublegetImaginaryPart()
returnm_dImaginPart;
voidsetRealPart(doubled)
m_dRealPart=d;
voidsetImaginaryPart(doubled)
m_dImaginPart=d;
//复数运算方法
ComplexNumbercomplexAdd(ComplexNumberc)
returnnewComplexNumber(
this.m_dRealPart+c.getRealPart(),
this.m_dImaginPart+c.getImaginaryPart());
ComplexNumbercomplexAdd(doublec)
this.m_dRealPart+c,this.m_dImaginPart);
ComplexNumbercomplexMinus(ComplexNumberc)
this.m_dRealPart-c.getRealPart(),
this.m_dImaginPart-c.getImaginaryPart());
ComplexNumbercomplexMinus
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 习题