当前位置:首页 > Java编程题
public class Point { private double x,y; private String s;
public Point(double x,double y){ this.x=x; this.y=y; }
public static double distance(Point p1,Point p2){
return Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y))); }
public String toString(){ this.s=”[\return s; }
public static void main(String[]args){ Point p1=new Point(0,0); Point p2=new Point(3,4);
System.out.println(“点A:”+p1.toString()); System.out.println(“点B:”+p2.toString());
System.out.print(\两点的距离是\distance(p1,p2)); } }
12. 编写一个类Prime,打印300到500之间的所有素数。 public class Sushu {
public static void main(String args[]) {int sum=0,i,j,n=0; for(i=1;i<=10;i++) {if(i%2==0) continue; sum=sum+i;}
System.out.println(\ for(j=200;j<=500;j++) {for(i=2;i<=j/2;i++) {if(j%i==0) break;} if(i>j/2)
{System.out.print(\ n++;
if(n%5==0)
{System.out.println();} } }
}
}
13.定义一个Student类 它的成员变量有xm(姓名),xh(学号),xb(性别),三门课的成绩yw(语文),xs(数学),yy(英语),还有三门课的zf(总分)和pjf(平均分),定义构造方法Student(String xm,String xh,String xb,int yw,int sx,int yy)还定义求总分方法public int qzf()和求平均分方法public double qpjf(),并且重写toString()方法即打印学生对象所有信息。在main()方法中构造三个学生对象s1,s2,s3然后打印这三个学生对象的所有信息。 package reView4;
public class Student { private String s; private String xh; private String xm; private String xb; private int yw; private int yy; private int sx; private int zf; private double pjf;
public Student(String xh,String xm,String xb) {
this.xh=xh; this.xm=xm; this.xb=xb;
this.yw=(int) Math.floor(50*Math.random()) + 51; this.yy=(int) Math.floor(50*Math.random()) + 51; this.sx=(int) Math.floor(50*Math.random()) + 51; sum();qpjf(); }
public int sum() {
this.zf=yw+yy+sx; return(zf); }
public double qpjf() {
this.pjf=zf/3;
return(pjf); }
public static void main(String[] args) {
System.out.println(\学号 姓名 性别 语文 英语 数学 总分 平均分\
Student s1=new Student(\张三\男\ System.out.println(s1);
Student s2=new Student(\李四\女\ System.out.println(s2);
Student s3=new Student(\王五\男\ System.out.println(s3); }
public String toString() {
this.s=this.xh+\ +\ return s; } }
(6)阻塞状态根据产生的原因可分为 _______ 、等待阻塞和其他阻塞。 答:对象锁阻塞
设类名为ThreadTest,写出创建线程的两种形式: 1.__________________________________ 2.__________________________________ 答1.通过继承Thread类创建线程
public class ThreadTest extends Thread{?} 答2.通过继承接口Runnable来创建线程
public class ThreadTest implements Runnable{…}
共分享92篇相关文档