面向对象练习题

1、定义一个点Point类,用来表示二维平面上的一个点。
       a、可以生成具有特定坐标的点对象
       b、提供可以设置具体坐标的方法。
       c、提供可以计算该点到原点(0,0)的距离的方法

       编写测试程序实现上述三个方法,并画出内存的分配

public class Point {
private double x;
private double y;
public Point() {
super();

// TODO Auto-generated constructor stub
}
public void setPoint(double x, double y) {

this.x = x;
this.y = y;
}
public Point(double x,double y) {
setPoint(x,y);
}
public double getdistance(){
return Math.sqrt(x*x+y*y);
}
public static void main(String[] args){
System.out.println("请输入点的二维坐标,输入两个数字,空格隔开:");
Scanner sc=new Scanner(System.in);
double x=sc.nextDouble();
double y=sc.nextDouble();
Point point1=new Point(x,y);
System.out.println(point1.x+","+point1.y);
System.out.println("距离远点巨鹿是"+point1.getdistance());
System.out.println("设置坐标为4,3");
point1.setPoint(4, 3);
System.out.println(point1.x+","+point1.y);
}
}

2、完成员工类 Employee,需要包含员工的基本属性
            (编号,姓名,工资)和上司以及下属,提供如下方法:
           属性的存/取方法
            构造方法
            业务方法包含:
            添加下属的方法
            输出自己所有下属的方法
   获取自己下属的人数个数

package day4;


public class Employee {
private String id;
private String name;
private double salary;
private int index;
private Employee manager;
private Employee[] xiashu;


public Employee() {
super();
// TODO Auto-generated constructor stub
}


public String getId() {
return id;
}


public void setId(String id) {
this.id = id;
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public double getSalary() {
return salary;
}


public void setSalary(double salary) {
this.salary = salary;
}


public Employee getManager() {
return manager;
}


public void setManager(Employee manager) {
this.manager = manager;
}


public Employee[] getXiashu() {


return xiashu;
}


public void setXiashu(Employee xiashu) {
// this.xiashu = xiashu;
if (this.xiashu == null) {
this.xiashu = new Employee[5];
}
if (index < this.xiashu.length) {
this.xiashu[index] = xiashu;
index++;


} else {
Employee newxiashu[] = new Employee[this.xiashu.length * 2];
for (int i = 0; i < index; i++) {
newxiashu[i] = this.xiashu[i];


}
newxiashu[index] = xiashu;
this.xiashu = newxiashu;
index++;
}


}


public int getIndex() {
return index;
}


public void setIndex(int index) {
this.index = index;
}


public Employee(String id, String name, double salary) {
super();
this.id = id;
this.name = name;
this.salary = salary;
}


public static void main(String[] args) {


Employee manager = new Employee("14010050", "竹之林", 33333);


Employee e = new Employee("14010051", "张志强", 3333);
e.setManager(e);
Employee e2 = new Employee("14010052", "张志强2", 3333);
e2.setManager(e);
Employee e3 = new Employee("14010051", "张志强3", 3333);
e3.setManager(e);
Employee e4 = new Employee("14010051", "张志强4", 3333);
Employee e5 = new Employee("14010051", "张志强5", 3333);
Employee e6 = new Employee("14010051", "张志强6", 3333);
manager.setXiashu(e);
manager.setXiashu(e2);
manager.setXiashu(e3);
manager.setXiashu(e4);
manager.setXiashu(e5);
manager.setXiashu(e6);
e4.setManager(e);
Employee x[] = manager.getXiashu();
for (int i = 0; i < manager.index; i++) {
System.out.println(x[i].name);
}
System.out.println("下属人数" + manager.getIndex());


}


}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值