第五章 继承与接口 习题

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

1.写出程序的运行结果
//扩展Point类
public class Point{
protected int x,y;
public Point(int x, int y){
this.x=x;
this.y=y;
}
public boolean equals(Point p){
return (x==p.x && y=p.y);
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public string toString(){
return new
}
}
public class NamedPoint extends Point{
final private String name;
public NamedPoint(int x, int y, String name){
super(x,y);
=name;
}
public String getName(){
return name;
}
public String toString(){
return new String(name+"("+x+", "+y+")");
}
}
public class TestNamedPoint{
public static void main(String[] a){
NamedPoint p=new NamedPoint(2,-3,"p");
System.out.println("p:"+p);
System.out.println("p.getName():"+p.getName());
System.out.println("p.getX():"+p.getX());
NamedPoint q=new NamedPoint(2,-3,"Q");
System.out.println("q: "+q);
System.out.println("q.equals(p):"+q.equals(p));
}
}
2.//写出程序运行结果
class ClassX{
protected int m;
protected int n;
void f(){
System.out.println("Now in ClassX.f().");
m=22;
}
void g(){
System.out.println("Now in ClassX.g().");
n=44;
}
public String toString(){
return new String("{ m="+m+", n="+n+"}");
}
}
class ClassY extends ClassX{
private double n;
void g(){
System.out.println("Now in ClassY.g().");
n=3.14159265;
}
public String toString(){
return new String("{ m="+m+", n="+n+"}");
}
}
class TestClassY{
public static void main(String[] a){
ClassX x=new ClassX();
x.f();
x.g();
System.out.println("x="+x);
ClassY y=new ClassY();
y.f();
y.g();
System.out.println("y= "+y);
}
}
3.有接口Speaker包含一个方法speak(),实现以下分层结构:
4. 有接口Geometric ,有3个方法getArea(), getCircumference(),
getX()和getY()。

该接口描述了
作用与几何图形的访问方法。

getX()和getY()方法返回几何图形左上角的点的x 和y 坐标,该点确定了该几何图形在坐标平面中的位置。

有用于可移动图形的接口
Movable ,有两个方法setX(double x)和setY(double y)。

该接口描述了编译方法,允许客户把图形移动到坐标平面是新位置上。

通过以下层次图实现这些接口:
在以上层次图上添加一个Scalable 接口,表示图形的伸缩比例,其方法为scaleBy(double factor),并实现它。

Object
public interface Geometric{
public double getArea();
public double getCircumference();
public double getX();
public double getY();
}
public interface Movable{
public void setX(double x);
public void setY(double y);
}
public abstract class Shape impliments Movable , Geometric { private double x, y;
public abstract double getArea();
public abstract double getCircumference();
public double getX() { return x;}
public double getY(){return y;}
public void setX(double x){this.x=x;}
public void setY(double y){this.y=y; }
}
public class Circle extends Shape {
double x, y, radius;
public Circle (double x, double y, double radius){
this.x=x; this.y=y; this.radius=radius;
}
public double getArea (){
return Math.PI*radius*radius;
}
public double[] getCenter(){
return new double[] {x, y} ;
}
public double getCircumference(){
return x*Math.PI*radius;
}
public double getRadius(){
return radius;
}
}
public class Rectangle extends Shape {
private double height, width;
public Rectangle(double height, double width){
this.length=length;
this.width=width;
}
public double getArea(){
return length*width;
}
public double getCircumference(){
return 2*(height+width);
}
public double getHeight(){ return height; } public double getWidth(){ return width; }
}
新增加接口后
public interface Geometric{
public double getArea();
public double getCircumference();
public double getX();
public double getY();
}
public interface Movable{
public void setX(double x);
public void setY(double y);
}
public interface Scalable {
public void scaleBy(double factor);
}
public abstract class Shape impliments Movable , Geometric, Scalabe { private double x, y;
public abstract double getArea();
public abstract double getCircumference();
public abstract void scaleBy(double factor);
public double getX() { return x;}
public double getY(){return y;}
public void setX(double x){this.x=x;}
public void setY(double y){this.y=y; }
}
public class Circle extends Shape {
double x, y, radius;
public Circle (double x, double y, double radius){
this.x=x; this.y=y; this.radius=radius;
}
public double getArea (){ return Math.PI*radius*radius; } public double[] getCenter(){ return new double[] {x, y} ; }
public double getCircumference(){ return x*Math.PI*radius; } public double getRadius(){ return radius; }
public void scaleBy(double factor){radius*=factor; }
}
public class Rectangle extends Shape {
private double height, width;
public Rectangle(double height, double width){
this.length=length;
this.width=width;
}
public double getArea(){ return length*width; }
public double getCircumference(){ return 2*(height+width); } public double getHeight(){ return height; }
public double getWidth(){ return width; }
public void scaleBy(double fanctor){
height*=factor;
width*=width;
}
}
5.有以下两个接口
public interface Swimmer{
int getMaxDepth();
int getMaxSpeed();
}
public interface Driver{
String getDriversLicence(); //汽车驾驶证
String[] getVINs();//车辆识别代码}
然后编写和测试下面类,实现这两个接口:
public class Human implements Swimmer, Driver {
}
测试主类为下图所示:。

相关文档
最新文档