一、 实验目的:掌握类的创建,掌握对象的创建和使用
二、实验内容:
1、定义一个student类,包括属性{学号,姓名,年龄,性别,专业}; 方法{给他两个点,能够求距离,给他三个点能判断是否够构成三角形,给他若干点能够求出距离最短的两个点}
2、 编写一个测试类,测试上述内容
工程图:
second包
//Point类
package second;
public class Point {
int x;
int y;
public Point() {}
public Point(int x, int y) {
super();
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "Point [x=" + x + ", y=" + y + "]";
};
}
//Student类
package second;
public class Student {
private String number;
private String name;
private String sex;
private int age;
private String major;
//构造方法
//源码->使用字段生成构造函数
public Student() {};
public Student(String number, String name, String sex, int age, String major) {
super();
this.number = number;
this.name = name;
this.sex = sex;
this.age = age;
this.major = major;
}
//源码->生成toString()
@Override
public String toString() {
return "Student [number=" + number + ", name=" + name + ", sex=" + sex + ", age=" + age + ", major=" + major
+ "]";
}
//源码->生成Getter和Setter方法
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
//两点之间的距离
public double twoPointDistance(Point p1,Point p2) {
return Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
//三个点构成三角形
public boolean judgeTriangle(Point p1,Point p2,Point p3) {
boolean flag = false;
double c1 = twoPointDistance(p1,p2);
double c2 = twoPointDistance(p1,p3);
double c3 = twoPointDistance(p2,p3);
if(c1+c2 > c3 || c1+c3 > c2 || c2+c3 > c1) flag = true;
return flag;
}
//求平面内最近的两个点之间的距离
//暴力法(分治法)
public double minDistance(Point [] p) {
double MinDistance = Double.MAX_VALUE;
for (int i=0; i< p.length-1; i++) {
for (int j=i+1; j<=p.length-1; j++) {
double PointDistance = twoPointDistance(p[i],p[j]);
if (PointDistance < MinDistance)
MinDistance = PointDistance;
}
}
return MinDistance;
}
}
//Test类
package second;
public class Test {
public static void main(String[] args) {
Student stu1 = new Student("10001","张三","男",20,"计算机");
Student stu2 = new Student();
System.out.println(stu1);
stu2.setNumber("10002");
stu2.setName("李四");
stu2.setSex("男");
stu2.setAge(20);
stu2.setMajor("计算机");
System.out.println(stu2);
Point p1 = new Point(2,2);
Point p2 = new Point(2,3);
Point p3 = new Point(2,4);
Point p4 = new Point(2,5);
System.out.println(stu1.twoPointDistance(p1,p2));
System.out.println( stu1.judgeTriangle(p1,p2,p3));
Point [] p = {p1,p2,p3,p4};
for(int i = 0;i < p.length;i++) {
System.out.println(p[i]);
}
System.out.println(stu1.minDistance(p));
/*
stu2.number = "10002";
stu2.name = "李四";
stu2.sex = "男";
stu2.age = 20;
stu2.major = "计算机";
*/
}
}
//运行结果
Student [number=10001, name=张三, sex=男, age=20, major=计算机]
Student [number=10002, name=李四, sex=男, age=20, major=计算机]
1.0
true
Point [x=2, y=2]
Point [x=2, y=3]
Point [x=2, y=4]
Point [x=2, y=5]
1.0