Java学校项目4-继承与接口

项目4 继承与接口

实验目的:掌握类继承的实现;掌握方法的继承、重载与覆盖;理解子类构造方法与父类构造方法之间的关系;理解抽象类和抽象方法;理解接口的定义;掌握接口的实现。
实验内容:
(1)分析调试教材的第4章中的实例
(2)根据下面的要求实现圆类Circle
成员变量:radius
方法:
 构造方法: Circle(),将半径置为0;
Circle(int r),将半径置初始化为r;
 修改器/访问器:对半径进行操作;
 double getPerimeter();获得圆的周长;
 double getArea();获得圆的面积;
 void show();将圆的半径、周长、面积输出

package 实验四;
public class Yuan {
	public static void main(String[] args) {
		Circle c=new Circle();
		c.show();
		c.setRedius(2);
		c.show();
	}
}
class Circle
{
	private double redius;
	Circle()//将半径置为0;
	{
		this.redius=0;
	}
	Circle(int r)//将半径置初始化为r
	{
		this.redius=r;
	}
	/*修改器/访问器:对半径进行操作*/
	public double getRedius() {
		return redius;
	}
	public void setRedius(double redius) {
		this.redius = redius;
	}	
	double getPerimeter()//获得圆的周长
	{	
		return redius*2*3.14;
	}
	double getArea()//获得圆的面积
	{
		return 3.14*redius*redius;
	}
	void show()//将圆的半径、周长、面积输出
	{
		System.out.println("圆的半径:"+this.redius+"、周长:"+getPerimeter()+"、面积:"+getArea());
	}
}

(3)编写一个程序,用于创建一个名为Employee的父类和两个名为Manager和Director的子类。要求创建Manager和Director的对象,并显示其详细信息。
Employee类:属性(name,sex,address)
方法show()显示这些属性
Manager类:属性(department),重写show方法
Director类:属性(lengthOfService),重写show方法

package 实验四;
public class Thrid {
	public static void main(String[] args) {
		Manager m=new Manager("LFC",'男',"广东省","计科");
		Dricetor d=new Dricetor("LFC",'男',"广东省",4);
		m.show();
		d.show();
	}
}
class Employee
{
	protected String name;
	protected char sex;
	protected String address;
	Employee(String name,char sex,String address)
	{
		this.name=name;
		this.sex=sex;
		this.address=address;
	}
	void show()
	{
		System.out.print("姓名:"+this.name+"、性别:"+this.sex+"、地址:"+this.address);
	}
}
class Manager extends Employee
{
	String department;//部门
	Manager(String name, char sex, String address,String department) {
		super(name, sex, address);
		this.department=department;
	}
	void show()
	{
		super.show();
		System.out.println("、部门:"+department);
	}
}
class Dricetor extends Employee
{
	int lengthofService;//服务年限
	Dricetor(String name, char sex, String address,int lengthofService) {
		super(name, sex, address);
		this.lengthofService=lengthofService;
	}
	void show()
	{
		super.show();
		System.out.println("、服务年限:"+lengthofService);
	}
}

(4)定义如下的类
○1 Point类:用于描述坐标系中的一个点
属性:x、y,int类型,分别描述x坐标值和y坐标值;
方法:int distanceTo(Point p):计算两点之间的距离;
Point centerPoint(Point p):求两点之间的中心点;
○2 Graphic抽象类:描述图形信息
属性:color(String类型,图形颜色)
方法:
 构造方法:2个
无参的(颜色为空)
有参的(根据参数设置颜色信息)
 void printInfo():显示图形信息
 抽象方法:double getArea() 功能:计算图形面积
 抽象方法:double getCircum() 功能:计算图形周长
○3 Rect类:继承了Graphic类,用于描述矩形图形
添加属性: lefttop和rightbottom,均为Point类型,分别描述矩形的左上角和右下角点的坐标信息;
方法:
 构造方法3个;
无参格式:利用基类无参构造方法设置基类部分信息,将两个点均设置为坐标原点;
Rect(String color, Point lefttop, Point rightbottom)
Rect(String color, int x_left, int y_top, int x_right, int y_bottom)
 重写方法:getArea(),getCircum(),printInfo() 根据矩形信息进行相关处理;
 添加方法:int getHeight(),求出矩形的长;
int getWidth(),求出矩形的宽;
boolean isRect():基于两点信息判断是否构成矩形;
○4 Circle类继承了Graphic类,用于描述圆形图形
添加属性:center,为Point类型,描述圆形的圆心;
r,int类型,描述圆形半径
方法:
 构造方法:根据实际需要,自行设定;
 重写方法:getArea(),getCircum(), printinfo()根据圆形信息进行相关处理;
 添加方法:
void setinfo (string color, point center, int r):根据参数设置圆形的属性
void changeSize(int):根据参数缩放圆形大小(提示:对半径进行修改)
○5 定义主程序类,分别创建矩形对象和圆形对象,并对其操作验证类定义是否正确;同时练习向上转型和向下转型,加深对面向对象的多态性的理解。

package 实验四;
public class Four {
	public static void main(String[] args) {
		Rect r=new Rect("蓝",0,0,1,3);
		if(!r.isRect())
		{
			System.out.println("矩形不合理");
		}
		else
		{
			System.out.println("矩形合理");
			r.printInfo();
		}
		System.out.println("-------------------------");
		Circle c=new Circle();
		c.printInfo();
		System.out.println("设置圆参数后、");
		c.setinfo("红", new Point(1,3),5);
		c.printInfo();
		System.out.println("变大为原来的2倍后、");
		c.changeSize(2);
		c.printInfo();
		
	}
}
class Point//点类
{
	int x;//x坐标
	int y;//y坐标
	Point(int x,int y)
	{
		this.x=x;
		this.y=y;
	}
	Point()
	{
		this.x=0;this.y=0;
	}
	int distanceTo(Point p)//返回两点之间距离
	{
	return (int) Math.sqrt(Math.pow(this.x-p.x,2)+Math.pow(this.y-p.y, 2));
	}
	Point centerPoint(Point p)//返回两点之间的中心点
	{
		return new Point(Math.abs(this.x-p.x)/2,Math.abs(this.y-p.y)/2);
	}
}
abstract class Graphic
{
	String color;
	Graphic()
	{
		this.color="空";
	}
	Graphic(String color)
	{
		this.color=color;
	}
	void printInfo()
	{
		System.out.print("图形颜色:"+this.color+"、");
	}
	abstract double getArea();
	abstract double getCircum();
}
class Rect extends Graphic//矩形
{
	Point lefttop=null;//矩形左上角的坐标
	Point rightbottom=null;//矩形右下角的坐标
	Rect()
	{
		lefttop=new Point();
		rightbottom=new Point();
	}
	Rect(String color, Point lefttop, Point rightbottom)
	{
		this.color=color;
		this.lefttop=lefttop;
		this.rightbottom=rightbottom;
	}
	Rect(String color, int x_left, int y_top, int x_right, int y_bottom)
	{
		this.color=color;
		lefttop=new Point(x_left,y_top);
		rightbottom=new Point(x_right,y_bottom);
	}
	double getArea()//面积
	{	
	return getHeight()*getWidth();
	}
	double getCircum()//周长
	{
	return 2*getHeight()+2*getWidth();
	}
	void printInfo()//输出长方形信息
	{
		super.printInfo();
		System.out.println("矩形长:"+getHeight()
				+"、矩形宽:"+getWidth()+"、矩形的面积:"+getArea()
				+"、矩形的周长:"+getCircum()+"、矩形其中一坐标:("
				+lefttop.x+","+lefttop.y+")、另一坐标:("
				+rightbottom.x+","+rightbottom.y+")");
	}
	int getHeight()//求出矩形的长
	{
		return Math.abs(rightbottom.x-lefttop.x);
	}
	int getWidth()//求出矩形的宽
	{
		return Math.abs(lefttop.y-rightbottom.y);
	}
	boolean isRect()//基于两点信息判断是否构成矩形
	{
		if(getHeight()<=0||getWidth()<=0)
			return false;
		else 
			return true;
	}
}
class  Circle extends Graphic
{
	Point center=null;//圆形的圆心
	int r;//半径
	Circle()
	{
		this.center=new Point();
		this.r=0;
	}
	Circle(String color,int r,Point center)
	{
		this.color=color;
		this.r=r;
		this.center=center;
	}
	Circle(String color,int r,int x,int y)
	{
		this.color=color;
		this.r=r;
		this.center=new Point(x,y);
	}
	double getArea()//圆面积
	{
	return 3.14*r*r;
	}
	double getCircum()//圆周长
	{
	return r*2.0*3.14*100/100;//防止半径为5时,出现精度问题
	}
	void printInfo()//输出圆信息
	{
		super.printInfo();
		System.out.println("圆的半径:"+this.r+"、圆心坐标:("
				+center.x+","+center.y+")"
				+"圆的面积:"+getArea()
				+"圆的周长:"+getCircum());
	}
	void setinfo(String color, Point center, int r)//根据参数设置圆形的属性
	{
		this.color=color;
		this.r=r;
		this.center=center;
	}
	void changeSize(int multiple)//根据参数缩放圆形大小,倍数
	{
		this.r=this.r*multiple;
	}
}

(5)对(4)题进一步补充:
定义接口:GraphicOperate,用于完成图形对象的操作
成员有:int compareTo(Graphic):比较两个图形的关系(重合、相交、不相交)
boolean pointIsIn(Point):判断参数点坐标是否在图形区域中
要求Rect和Circle两个类实现该接口。
编写程序,验证正确性。

package 实验四;
interface GraphicOperate
{
   int compareTo(Graphic g);
   boolean pointIsIn(Point a);
}
public class Four {
   public static void main(String[] args) {
   	Rect r=new Rect("蓝",0,0,1,3);
   	if(!r.isRect())
   	{
   		System.out.println("矩形不合理");
   	}
   	else
   	{
   		System.out.println("矩形合理");
   		r.printInfo();
   	}
   	System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
   	Circle c=new Circle();
   	c.printInfo();
   	System.out.println("设置圆参数后、");
   	c.setinfo("红", new Point(1,3),5);
   	c.printInfo();
   	System.out.println("变大为原来的2倍后、");
   	c.changeSize(2);
   	c.printInfo();
   	System.out.println("------------------------第五题----------------------------------------------");
   	Rect r2=new Rect("蓝",0,0,3,4);
   	r2.printInfo();
   	if(r.compareTo(r2)==0)
   		System.out.println("与第一个矩形重合");
   	if(r.compareTo(r2)==1)
   		System.out.println("与第一个矩形相交");
   	if(r.compareTo(r2)==2)
   		System.out.println("与第一个矩形不相交");
   	Point p=new Point();
   	System.out.print("点("+p.x+","+p.y+")");
   	if(r.pointIsIn(p))
   			System.out.println("在矩形区域中");
   	else
   		System.out.println("不在矩形区域中");
   	System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
   	Circle c2=new Circle("蓝",2,0,1);
   	c2.printInfo();
   	if(c.compareTo(c2)==0)
   		System.out.println("与第一个圆形重合");
   	if(c.compareTo(c2)==1)
   		System.out.println("与第一个圆形相交");
   	if(c.compareTo(c2)==2)
   		System.out.println("与第一个圆形不相交");
   	System.out.print("点("+p.x+","+p.y+")");
   	if(c.pointIsIn(p))
   			System.out.println("在圆形区域中");
   	else
   		System.out.println("不在圆形区域中");
   }
}
class Point//点类
{
   int x;//x坐标
   int y;//y坐标
   Point(int x,int y)
   {
   	this.x=x;
   	this.y=y;
   }
   Point()
   {
   	this.x=0;this.y=0;
   }
   int distanceTo(Point p)//返回两点之间距离
   {
   return (int) Math.sqrt(Math.pow(this.x-p.x,2)+Math.pow(this.y-p.y, 2));
   }
   Point centerPoint(Point p)//返回两点之间的中心点
   {
   	return new Point(Math.abs(this.x-p.x)/2,Math.abs(this.y-p.y)/2);
   }
}
abstract class Graphic//抽象类图形
{
   String color;
   Graphic()
   {
   	this.color="空";
   }
   Graphic(String color)
   {
   	this.color=color;
   }
   void printInfo()
   {
   	System.out.print("图形颜色:"+this.color+"、");
   }
   abstract double getArea();
   abstract double getCircum();
}
class Rect extends Graphic implements GraphicOperate//矩形
{
   Point lefttop=null;//矩形左上角的坐标
   Point rightbottom=null;//矩形右下角的坐标
   Rect()
   {
   	lefttop=new Point();
   	rightbottom=new Point();
   }
   Rect(String color, Point lefttop, Point rightbottom)
   {
   	this.color=color;
   	this.lefttop=lefttop;
   	this.rightbottom=rightbottom;
   }
   Rect(String color, int x_left, int y_top, int x_right, int y_bottom)
   {
   	this.color=color;
   	lefttop=new Point(x_left,y_top);
   	rightbottom=new Point(x_right,y_bottom);
   }
   double getArea()//面积
   {	
   return getHeight()*getWidth();
   }
   double getCircum()//周长
   {
   return 2*getHeight()+2*getWidth();
   }
   void printInfo()//输出长方形信息
   {
   	super.printInfo();
   	System.out.println("矩形长:"+getHeight()
   			+"、矩形宽:"+getWidth()+"、矩形的面积:"+getArea()
   			+"、矩形的周长:"+getCircum()+"、矩形其中一坐标:("
   			+lefttop.x+","+lefttop.y+")、另一坐标:("
   			+rightbottom.x+","+rightbottom.y+")");
   }
   int getHeight()//求出矩形的长
   {
   	return Math.abs(rightbottom.x-lefttop.x);
   }
   int getWidth()//求出矩形的宽
   {
   	return Math.abs(lefttop.y-rightbottom.y);
   }
   boolean isRect()//基于两点信息判断是否构成矩形
   {
   	if(getHeight()<=0||getWidth()<=0)
   		return false;
   	else 
   		return true;
   }
   @Override
   public int compareTo(Graphic g)//比较两个图形的关系
   {
   	Rect a=(Rect)g;
   	if(lefttop.x==a.lefttop.x &&
   		lefttop.y==a.lefttop.y &&
   		rightbottom.x==a.rightbottom.x &&
   		rightbottom.y==a.rightbottom.y)
   		return 0;//重合
   	else if((a.lefttop.x<=Math.max(lefttop.x, rightbottom.x)&&Math.min(lefttop.x, rightbottom.x)<=a.lefttop.x)
   			||(a.rightbottom.x<=Math.max(lefttop.x, rightbottom.x)&&Math.min(lefttop.x, rightbottom.x)<=a.rightbottom.x))
   		 {
   			if((a.lefttop.y>=Math.min(lefttop.y, rightbottom.y)&&Math.max(lefttop.y, rightbottom.y)>=a.rightbottom.y)
   				||(a.lefttop.y>=Math.min(lefttop.y, rightbottom.y)&&Math.max(lefttop.y, rightbottom.y)>=a.rightbottom.x))
   			 		return 1;//相交
   			else 
   			 		return 2;//不相交	
   		 }
   	else
   			return 2;//不相交		
   }
   @Override
   public boolean pointIsIn(Point a) //判断参数点坐标是否在图形区域中
   {
   	if(Math.min(lefttop.x, rightbottom.x)<=a.x
   			&& a.x<=Math.max(lefttop.x, rightbottom.x)
   			&& Math.min(lefttop.y, rightbottom.y)<=a.y 
   			&& a.y<=Math.max(lefttop.y, rightbottom.y))
   			 return true;
   	return false;
   }
}
class  Circle extends Graphic implements GraphicOperate
{
   Point center=null;//圆形的圆心
   int r;//半径
   Circle()
   {
   	this.center=new Point();
   	this.r=0;
   }
   Circle(String color,int r,Point center)
   {
   	this.color=color;
   	this.r=r;
   	this.center=center;
   }
   Circle(String color,int r,int x,int y)
   {
   	this.color=color;
   	this.r=r;
   	this.center=new Point(x,y);
   }
   double getArea()//圆面积
   {
   return 3.14*r*r;
   }
   double getCircum()//圆周长
   {
   return r*2.0*3.14*100/100;//防止半径为5时,出现精度问题
   }
   void printInfo()//输出圆信息
   {
   	super.printInfo();
   	System.out.println("圆的半径:"+this.r+"、圆心坐标:("
   			+center.x+","+center.y+")"
   			+"圆的面积:"+getArea()
   			+"圆的周长:"+getCircum());
   }
   void setinfo(String color, Point center, int r)//根据参数设置圆形的属性
   {
   	this.color=color;
   	this.r=r;
   	this.center=center;
   }
   void changeSize(int multiple)//根据参数缩放圆形大小,倍数
   {
   	this.r=this.r*multiple;
   }
   public int compareTo(Graphic g)//比较两个图形的关系
   {
   	Circle c=(Circle)g;
   	if(this==c)
   	return 0;//重合
   	if((Math.sqrt(Math.pow(center.x-c.center.x,2)+Math.pow(center.y-c.center.y, 2)))<(this.r+c.r))
   	return 1;//相交
   	return 2;//不相交
   }
   public boolean pointIsIn(Point a)判断参数点坐标是否在图形区域中
   {
   	if((Math.sqrt(Math.pow(center.x-a.x,2)+Math.pow(center.y-a.y, 2)))<this.r)
   	return true;
   	return false;
   }
}
  • 😃联系交流QQ:2283093518
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值