实验六 Java期中考试
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
期中考试:
二、
package Exextra_2;
import java.util.*;
publicclass Test {
publicstaticvoid main<String[] args> {
int[] a=newint[10];
Scanner scan=new Scanner<System.in>; for<int i=0;i<10;i++>{
a[i]=scan.nextInt<>;
}
int max=a[0];
int min=a[0];
for<int i=0;i<10;i++>{
if<a[i]>max>
max=a[i];
}
for<int i=0;i<10;i++>{
if<a[i]<min>
min=a[i];
}
System.out.println<"max="+max+" min="+min>; }
}
三、
package Exextra_3;
publicclass Point {
privateint x,y;
privatedouble s;
public Point<>{
}
public Point<int x,int y>{
this.x=x;
this.y=y;
}
publicint getX<>{
return x;
}
publicvoid setX<int x>{
this.x=x;
}
publicint getY<>{
return y;
}
publicvoid setY<int y>{
this.y=y;
}
public String toString<>{
return"<"+x+","+y+">";
}
publicdouble distance<Point p1,Point p2>{
int x1=p1.getX<>;int y1=p1.getY<>;
int x2=p2.getX<>;int y2=p2.getY<>;
s=Math.sqrt<<x1-x2>*<x1-x2>+<y1-y2>*<y1-y2>>; return s;
}
}
package Exextra_3;
publicclass Test {
publicstaticvoid main<String[] args> {
Point p1=new Point<4,5>;
Point p2=new Point<2,1>;
double s=p2.distance<p1,p2>;
System.out.println<p1.toString<>>;
System.out.println<p2.toString<>>;
System.out.println<s>;
}
}
四、
package Exextra_4;
publicinterface Shape{
publicabstractdouble area<>;
}
package Exextra_4;
publicclass MyCircle implements Shape{ privatedouble r;
public MyCircle<double r>{
this.r=r;
}
publicdouble area<>{
return Math.PI*r*r;
}
public String toString<>{
return"circle: r="+r+" area="+this.area<>;
}
}
package Exextra_4;
publicclass MyRectangle implements Shape {
privatedouble w,h;
public MyRectangle<double w,double h>{
this.w=w;
this.h=h;
}
publicdouble area<>{
return w*h;
}
public String toString<>{
return"Rectangle: width="+w+" "+"height="+h+" area="+this.area<>;
}
}
package Exextra_4;
publicclass Test {
publicstaticvoid main<String[] args> {
Shape t1=new MyRectangle<10,5>;
System.out.println<t1.toString<>>;
Shape t2=new MyCircle<3>;
System.out.println<t2.toString<>>;
}
}
五、
package Exextra_5;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
publicclass MyFrm extends JFrame implements ActionListener{
int m=0;
JButton btn1=new JButton<"点我">;
JButton btn2=new JButton<"清零">;
JLabel lb=new JLabel<"共点击了0次">;
public MyFrm<>{
JPanel jp=<JPanel>this.getContentPane<>;
jp.setLayout<new FlowLayout<>>;
jp.add<btn1>;jp.add<btn2>;jp.add<lb>;
btn1.addActionListener<this>;
btn2.addActionListener<this>;
this.setTitle<"ButtonDemol">;
}
publicvoid actionPerformed<ActionEvent e>{
if<e.getSource<>==btn1>{
m=m+1;
lb.setText<"共点击了"+m+"次">;
}