1.给Point类添加一下几个求两点间距离的多态方法:
public double distance(Point p)
public double distance(int x,int y)
public static double distance(Point x,Point y)
package ex6;
import java.util.*;
public class ex6_1 {
public static class Point {
double x,y;
Point(double x,double y){
this.x=x;
this.y=y;
}
Scanner in=new Scanner(System.in);
public double distance(Point p){
System.out.println("请输入坐标x:");
double x=in.nextDouble();
System.out.println("请输入坐标y:");
double y=in.nextDouble();
return Math.sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
}
public double distance(int x,int y){
System.out.println("请输入坐标x:");
double a=in.nextDouble();
System.out.println("请输入坐标y:");
double b=in.nextDouble();
return Math.sqrt((a-x)*(a-x)+(b-y)*(b-y));
}
public double distance(Point x,Point y){
return Math.sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));
}
public static void main(String[] args) {
Point a=new Point(2.5,2.3);
Point b=new Point(3
,3);
System.out.println("该点与p点之间的距离为:"+ a.distance(a));
System.out.println("该点与(x,y)之间的距离为:"+ b.distance(3,3));
System.out.println("x,y两点之间的距离为:"+ b.distance(a,b));
}
}}
2.定义一个Person类,含姓名,性别,年龄等字段;集成Person;类设计Teacher类,增加职称,部门等字段;集成Person类设计Student类,增加学号,入学时间,专业等字段。定义各类的构造方法和toString()方法。
package ex6;
public class ex6_2 {
public static class Person{
String name;
String sex;
int age;
Person(String name,String sex,int age){
this.name=name;
this.sex=sex;
this.age=age;
}
public String toString(){
String s="姓名: "+name+" 性别: "+sex+" 年龄: "+age;
return s;
}
}
public static class Teacher extends Person{
String pos;
String work;
public Teacher(String name,String sex,int age,String pos,String work)
{
super(name,sex,age);
this.pos=pos;
this.work=work;
}
public String toString(){
return super.toString()+" 职称: "+pos+" 部门: "+work;
}
}
public static class Student extends Teacher{
int num;
String time;
String pro;
public Student(String name,String sex,int age,String pos,String work,int num,String time,String pro)
{
super(name,sex,age,pos,work);
this.num=num;
this.time=time;
this.pro=pro;
}
public String toString(){
return super.toString()+" 学号: "+num+" 入学时间: "+time+" 专业: "+pro;
}
}
public static void main(String[] args) {
{
Person ex1=new Person("张三","男",30);
System.out.println(ex1);
Teacher ex2=new Teacher("李四","男",28,"高级教师","数学科");
System.out.println(ex2);
Student ex3=new Student("王美丽","女",22,"学习委员","学生部",10001,"2018.4.1","计算机科学与技术");
System.out.println(ex3);
}
}}
3.改进例5-6的Circle类,提供若干求面积的方法balabalabala....(此处不想打例子)
package ex6;
import java.util.*;
public class ex6_3 {
public static class Circle{
double r;
Circle(double r){
this.r=r;
}
static Scanner in=new Scanner(System.in);
public double area() {
return 3.14*r*r;
}
public static double area(double r) {
System.out.println("请输入圆的半径");
double rim=in.nextDouble();
return 3.14*rim*rim;
}
public static double area(Circle c) {
return 3.14*c.r*c.r;
}
}
public static void main(String[] args) {
Circle ex1=new Circle(5);
double x=3.0;
System.out.println("当前圆的面积为:"+ex1.area());
System.out.println("该圆面积为:"+Circle.area(x));
System.out.println("该圆面积为:"+Circle.area(ex1));
}
}
4.emmm,继续修改例子,反正就是求复数相乘相加,还要计算模长。
package ex6;
public class ex6_4 {
public static class Complex{
private double x,y;
public Complex(double r,double i){
x=r;
y=i;
}
public String toString() {
return "("+x+","+y+"i"+")";
}
public Complex add(Complex a) {//第一种加法
return new Complex(x+a.x,y+a.y);
}
public Complex add(double a,double b) {//第二种加法
return new Complex(x+a,y+b);
}
public static Complex add(Complex a, Complex b) {//第三种加法
return new Complex(a.x+b.x,a.y+b.y);
}
public Complex mul(Complex a) {//第一种乘法
return new Complex(x*a.x,y*a.y);
}
public Complex mul(double a,double b) {//第二种乘法
return new Complex(x*a,y*b);
}
public static Complex mul(Complex a,Complex b) {//第三种乘法
return new Complex(b.x*a.x,a.y*b.y);
}
public double mo(Complex c) {//取模运算
return Math.sqrt(c.x*c.x+c.y*c.y);
}
}
public static void main(String[] args) {
Complex x,y,z,k;
x=new Complex(4,5);
y=new Complex(3.4,2.8);
z=Complex.add(x,y);
k=Complex.mul(x,y);
System.out.println("x的模长为"+x.mo(x)+" y的模长为:"+y.mo(y));
System.out.println("add result1="+z);
System.out.println("multiply result1="+k);
z=x.add(y);
k=x.mul(y);
System.out.println("add result2="+z);
System.out.println("multiply result2="+k);
z=x.add(6,8);
k=x.mul(6,8);
System.out.println("add result3="+z);
System.out.println("multiply result3="+k);
}
}