java实验Word文档下载推荐.docx
- 文档编号:5831045
- 上传时间:2023-05-05
- 格式:DOCX
- 页数:29
- 大小:189.98KB
java实验Word文档下载推荐.docx
《java实验Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《java实验Word文档下载推荐.docx(29页珍藏版)》请在冰点文库上搜索。
+getGirth()+"
面积是:
+getGirth();
}
publicclassMyEllipseextendsShape{
publicMyEllipse(intwidth,intheight){
this.y=85;
return(float)(Math.PI*(width+height)/4*(width+height)/4);
return(float)((float)2*Math.PI*(width+height)/2);
这是一个椭圆,周长是:
publicclassMyRectangleextendsShape{
publicMyRectangle(intwidth,intheight){
this.y=5;
returnwidth*height;
return2*width*height;
这是矩形,周长是:
publicclassMyTriangleextendsShape{
publicMyTriangle(intwidth,intheight){
this.y=125;
return(float)(width+height+Math.sqrt(width*width+height*height));
returnwidth*height*1/2;
这个是三角形,周长是:
publicabstractclassShape{
publicintwidth;
publicintheight;
publicintx;
publicinty;
publicShape(intwidth,intheight){
this.width=width;
this.height=height;
publicabstractfloatgetArea();
publicabstractfloatgetGirth();
importjava.awt.Graphics;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
publicclassTestextendsJPanel{
privateShapecircle;
privateShapeellipse;
privateShapetriangle;
privateShaperectangle;
publicTest(){
JFramef=newJFrame("
形状家族"
);
circle=newMyCircle(10,10);
ellipse=newMyEllipse(10,10);
triangle=newMyTriangle(10,10);
rectangle=newMyRectangle(10,10);
f.setVisible(true);
f.add(this);
f.setSize(600,600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
newTest();
protectedvoidpaintComponent(Graphicsg){
int[]xPoints=newint[]{triangle.x,triangle.x+20,triangle.x};
int[]yPoints=newint[]{triangle.y,triangle.y,triangle.y-20};
g.drawOval(circle.x,circle.y,circle.width,circle.height);
g.drawString(circle.toString(),circle.x+25,circle.y+9);
g.drawOval(ellipse.x,ellipse.y,ellipse.width,ellipse.height);
g.drawString(ellipse.toString(),ellipse.x+25,ellipse.y+9);
g.drawPolygon(xPoints,yPoints,3);
g.drawString(triangle.toString(),triangle.x+25,triangle.y+9);
g.drawRect(rectangle.x,rectangle.y,rectangle.width,rectangle.height);
g.drawString(rectangle.toString(),rectangle.x+25,rectangle.y+9);
运行结果:
练习2:
packageex.mao;
publicclassEmployee{
privateStringnumber;
privateStringname;
privateStringaddress;
privateStringphone;
privateStringemail;
publicStringgetName(){
returnname;
publicvoidsetName(Stringname){
this.name=name;
*Employee的默认构造函数
*结果:
初始化工号为空,姓名为xxx,地址为xx学府大道999号xxx,电话为138xxxxxxxx,邮件为1234@
publicEmployee(){
this("
"
xxx"
xx学府大道999号xxx"
138xxxxxxxx"
1234@"
*Employee的带形参的构造函数
*前提:
参数必须都为String类型
初始化以下参数
*@paramnumber
*@paramname
*@paramaddress
*@paramphone
*@paramemail
publicEmployee(Stringnumber,Stringname,Stringaddress,Stringphone,Stringemail){
this.number=number;
this.name=name;
this.address=address;
this.phone=phone;
this.email=email;
*计算奖金:
出勤天数/一年的有效出勤天数x10000x岗位级别
以下参数都必须是int类型
返回奖金,类型为double
*@paramattendDay
*@paramusefulDay
*@paramgrade
publicDoublegetSalary(intattendDay,intusefulDay,intgrade){
return(double)10000*grade*attendDay/usefulDay;
返回该对象信息
身份为雇员,工号为"
+number+"
,姓名为"
+name+"
,地址为"
+address+"
电话为"
+phone+"
,邮件为"
+email;
importjava.awt.BorderLayout;
importjava.awt.FlowLayout;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JComboBox;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;
importjavax.swing.JTextArea;
importjavax.swing.JTextField;
publicclassTestextendsJFrame{
privatestaticfinallongserialVersionUID=7570144998140883101L;
privateJLabeljlb1;
privateJTextFieldjtf1;
privateJTextAreajta;
privateJLabeljlb2;
privateJTextFieldjtf2;
privateJLabeljlb3;
privateJTextFieldjtf3;
privateJButtonjbn;
privateJButtonjbn2;
privateJLabeljlb4;
privateJTextFieldjtf4;
privateJComboBox<
String>
jcb;
initGUI();
*@return出勤天数,类型为int
publicintgetAttendDay(){
returnInteger.parseInt(jtf2.getText());
*@return有效出勤天数,类型为int
publicintgetUsefulDay(){
returnInteger.parseInt(jtf4.getText());
*@return岗位级别,类型为int
publicintgetGrade(){
returnjcb.getSelectedIndex()+1;
publicvoidinitGUI(){
Stringstr=newString("
姓名职称/职位岗位级别出勤天数奖金"
jlb2=newJLabel();
jtf2=newJTextField(12);
jlb3=newJLabel();
jtf3=newJTextField(12);
jlb4=newJLabel("
有效出勤天数:
jtf4=newJTextField(12);
jbn2=newJButton("
提交"
finalJPaneljp1=newJPanel();
jp1.setLayout(newFlowLayout());
finalJPaneljp2=newJPanel();
jp2.setLayout(newBorderLayout());
finalJPaneljp3=newJPanel();
finalJPaneljp4=newJPanel();
JPaneljp5=newJPanel();
JPaneljp6=newJPanel();
JPaneljp7=newJPanel();
jp5.setLayout(newFlowLayout());
jp6.setLayout(newFlowLayout());
jp7.setLayout(newFlowLayout());
jp3.setLayout(newGridLayout(4,1));
jp5.add(jlb2);
jp5.add(jtf2);
jp6.add(jlb3);
jp6.add(jtf3);
jp7.add(jlb4);
jp7.add(jtf4);
jp3.add(jp5);
jp3.add(jp6);
jp3.add(jp7);
jlb1=newJLabel("
工号:
jtf1=newJTextField(12);
jbn=newJButton("
确认"
jbn2.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
Employeeem=newEmployee();
intattendDay=getAttendDay();
//出勤天数
intusefulday=getUsefulDay();
//有效出勤天数
intgrade=getGrade();
//岗位级别
Doublesalary=em.getSalary(attendDay,usefulday,grade);
jta.append("
\n"
+"
"
+em.getName()+"
\t"
+jcb.getSelectedItem().toString()+"
+grade+
"
+attendDay+"
+salary.floatValue());
jtf1.setText("
jtf2.setText("
jtf3.setText("
jtf4.setText("
remove(jp3);
jp2.add(jp3,BorderLayout.NORTH);
jp2.add(jp4,BorderLayout.SOUTH);
validate();
}
});
jbn.addActionListener(newActionListener(){
Stringtext=jtf1.getText();
BooleanhaveValue=true;
String[]grade=null;
if(text.isEmpty()){
JOptionPane.showMessageDialog(null,"
请输入工号!
haveValue=false;
}
elseif(text.charAt(0)=='
x'
)
{
jlb2.setText("
正常工作时间:
jlb3.setText("
加班时间:
grade=newString[]{"
助教"
讲师"
付教授"
教授"
};
j'
){
正常教学时间:
科研时间:
科员"
主任科员"
主任行政"
if(haveValue==true){
jcb.removeAllItems();
for(inti=0;
i<
grade.length;
i++){
jcb.addItem(grade[i]);
}
jp3.add(jcb);
jp2.add(jp3,BorderLayout.NORTH);
jp2.add(jp4,BorderLayout.SOUTH);
validate();
jcb=newJComboBox<
(newString[]{});
jp4.add(jbn2);
jta=newJTextArea("
结果:
jta.append("
+str);
setLayout(newBorderLayout());
jp1.add(jlb1);
jp1.add(jtf1);
jp1.add(jbn);
jp2.add(jta,BorderLayout.CENTER);
add(jp1,BorderLayout.NORTH);
add(jp2,BorderLayout.CENTER);
setVisible(true);
setSize(600,600);
setTitle("
考勤核算奖金"
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
练习3:
packageex.mao2;
publicclassCard{
privateStringsuit;
//花色;
privateStringface;
//牌面;
publicStringgetSuit(){
returnsuit;
publicvoidsetSuit(Stringsuit){
this.suit=suit;
publicStringgetFace(){
returnface;
publicvoidsetFace(Stringface){
this.fa
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 实验