1、(1)編寫一個接口ShapePara,要求: 接口中的方法: int getArea():獲得圖形的面積。int getCircumference():獲得圖形的周長
(2)編寫一個圓類Circle,要求:圓類Circle實現接口ShapePara。
該類包含有成員變量:
radius:public 修飾的double類型radius,表示圓的半徑。
x:private修飾的double型變量x,表示圓心的橫坐標。
y:protected修飾的double型變量y,表示圓心的縱坐標。
包含的方法有:
Circle(double radius) 有參構造方法。以形參表中的參數初始化半徑,圓心為坐標原點。 double getRadius():獲取半徑為方法的返回值。void setCenter(double x, double y):利用形參表中的參數設置類Circle的圓心坐標。void setRadius(double radius):利用形參表中的參數設置類Circle的radius域。
public interface ShapePara { int getArea(); int getCircumference(); }
public class Circle implements ShapePara { public double radius; private double x; protected double y; Circle(double radius){ this.radius=radius; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } @Override public int getArea() { // TODO 自動生成的方法存根 double area=Math.PI*Math.pow(radius, 2); return (int)area; } @Override public int getCircumference() { // TODO 自動生成的方法存根 double ference=Math.PI*2*radius; return (int)ference; } }
public class TestCircle { public static void main(String[] args) { // TODO 自動生成的方法存根 Circle c=new Circle(3); System.out.println("半徑為3的圓的面積是:"+c.getArea()); System.out.println("半徑為3的圓的周長是:"+c.getCircumference()); } }
2、定義圖形類Shape,該類中有獲得面積的方法getArea();定義長方形類Rect,該類是Shape的子類,類中有矩形長和寬的變量double a,double b,設置長和寬的方法setWidth()、setHeight(),使用getArea()求矩形面積
public class Shape { public void getArea(){ double area=0; System.out.println("面積是:"+area); } }
public class Rect extends Shape { private double width; private double height; Rect(double a, double b){ this.width=a; this.height=b; } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public void getArea(){ System.out.println("面積是:"+(width*height)); } }
3、編寫Java應用程序,定義Animal類,此類中有動物的屬性:名稱 name,腿的數量legs,統計動物的數量 count;方法:設置動物腿數量的方法 void setLegs(),獲得腿數量的方法 getLegs(),設置動物名稱的方法 setKind(),獲得動物名稱的方法 getKind(),獲得動物數量的方法 getCount()。定義Fish類,是Animal類的子類,統計魚的數量 count,獲得魚數量的方法 getCount()。定義Tiger類,是Animal類的子類,統計老虎的數量 count,獲得老虎數量的方法 getCount()。定義SouthEastTiger類,是Tiger類的子類,統計老虎的數量 count,獲得老虎數量的方法 getCount()。
public class Animal { private String name; private int legs; private int count; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getLegs() { return legs; } public void setLegs(int legs) { this.legs = legs; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } }
public class Fish extends Animal{ public int getFishCount() { setCount(10); return getCount(); } }
public class Tiger extends Animal{ public int getTigerCount() { setCount(20); return getCount(); } }
public class SouthEastTiger extends Tiger { public int getTigerCount() { setCount(10); return getCount(); } }
public class Test { public static void main(String[] args) { // TODO 自動生成的方法存根 Fish f=new Fish(); Tiger t=new Tiger(); SouthEastTiger s=new SouthEastTiger(); System.out.println("魚有"+f.getFishCount()+"條"); System.out.println("老虎有"+t.getTigerCount()+"只"); System.out.println("東北虎有"+s.getTigerCount()+"只"); } }