JAVA第六章:介面與實現

努力變得優秀發表於2020-11-17

1.按以下要求編寫程式。

(1)定義一個Shape介面,該介面中有一個常量PI,兩個方法:getPerimeter()和getArea(),方法均無引數,返回值型別為double型;

package cn.hygzs.demo6;
/**
 * 
 * @author 湖雲工作室
 *
 */
public interface Shape {
	
	final double PI = 3.14159267;
	
	double getPerimeter();
	double getArea();
}

(2)定義一個圓類Circle,滿足以下條件:

  1. Circle類實現Shape介面;
  2. 定義Circle類的成員變數r,表示圓的半徑,資料型別為double;
  3. 定義Circle類的無參構造方法,輸出“—介面測試程式—”;
  4. 定義Circle類的構造方法,引數名為r,用該引數初始化圓的半徑,呼叫無參構造方法;
  5. 實現getPerimeter()方法,計算圓的周長;
  6. 實現getArea()方法,計算圓的面積。
package cn.hygzs.demo6;
/**
 * 
 * @author 湖雲工作室
 *
 */
public class Circle implements Shape {
	
	double r ;

	public double getR() {
		return r;
	}
	
	public Circle(double r) {
		this();
		this.r = r;
		
	}

	public Circle() {
		super();
		System.out.println("--------介面測試程式------");
	}

	@Override
	public double getPerimeter() {
		double p = 2*r*PI;
		return p;
	}

	@Override
	public double getArea() {
		double s = r*r*PI;
		return s;
	}

}

(3)編寫一個測試類,類名為TestCircle,讓使用者輸入圓的半徑,利用Circle類計算該圓的周長和麵積,

package cn.hygzs.demo6;

import java.util.Scanner;
/**
 * 
 * @author 湖雲工作室
 *
 */
public class TestCircle {
   
	public static void main(String[] args) {
		System.out.println("請輸入圓的半徑:");
		Scanner input = new Scanner(System.in);
		double r = input.nextDouble();
		Circle c = new Circle(r);
		System.out.println("半徑為"+r+"的圓的周長為"+c.getPerimeter());
		System.out.println("半徑為"+r+"的圓的面積為"+c.getArea());

	}

}

2.定義介面Shape,其中包括兩個方法size()、getInfo();,分別計算形狀的大小和顯示諸如:“半徑為XXX的圓的面積為:”等提示資訊;設計“直線”、“圓”、“三角形”和“矩形”類分別實現Shape介面。在測試類中通過語句:Shape s[] = new Shape [] ;建立一個包括“直線”、“圓”、 三角形”和“矩形”四個物件的物件陣列,將各類圖形的大小輸出。

Shapes.java

package cn.hygzs.demo6;
/**
 * 
 * @author 湖雲工作室
 *
 */
public interface Shapes {
	void getInfo();
	void getInfo(Line l);
	double size();
	double size(Line l);

}

Line.java

package cn.hygzs.demo6;
/**
 * 
 * @author 湖雲工作室
 * 直線
 */
public class Line implements Shapes{
	
	double x;
	double y;

	public double getX() {
		return x;
	}

	public double getY() {
		return y;
	}
	public Line(double x, double y) {
		super();
		this.x = x;
		this.y = y;
	}
	
	@Override
	public double size() {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public double size(Line l) {		
		double sum =Math.sqrt((x-l.x) * (x-l.x) + (y-l.y) * (y-l.y) );
		return sum;
	}
	
	@Override
	public void getInfo() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void getInfo(Line l) {
		System.out.println("點("+x+","+y+ ")到點("+l.getX()+","+l.getY()+")長度為:"+size(l));	
		
	}
	
}

Circles.java

package cn.hygzs.demo6;
/**
 * 
 * @author 湖雲工作室
 * 圓形
 */
public class Circles implements Shapes {
	
	private double r;
	

	public double getR() {
		return r;
	}

	public void setR(double r) {
		this.r = r;
	}

	public Circles(double r) {
		super();
		this.r = r;
	}

	@Override
	public double size() {
		return 3.14*getR()*getR();
	}

	@Override
	public void getInfo() {
		System.out.println("半徑為:"+getR()+"的圓面積為:"+size());	
		
	}

	@Override
	public double size(Line l) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void getInfo(Line l) {
		// TODO Auto-generated method stub
		
	}

}

Rectangles.java

package cn.hygzs.demo6;
/**
 * 
 * @author 湖雲工作室
 * 三角形
 */
public class Rectangles implements Shapes {
	
	 double sideA;
	 double sideB;
	 double sideC;
	 
	public Rectangles(double sideA, double sideB, double sideC) {
		super();
		this.sideA = sideA;
		this.sideB = sideB;
		this.sideC = sideC;
	}

	@Override
	public double size() {
		 double p=(sideA+sideB+sideC)/2.0;
		 double area=Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC)) ;
		 return area;
	}

	@Override
	public void getInfo() {
		System.out.println("邊長為:"+sideA+","+sideB+","+sideC+"的三角形面積為:"+size());	
	}

	@Override
	public double size(Line l) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void getInfo(Line l) {
		// TODO Auto-generated method stub
		
	}


}

Triangles.java

package cn.hygzs.demo6;
/**
 * 
 * @author 湖雲工作室
 * 矩形
 */
public class Triangles implements Shapes {
    
	 private double a;
	 private double b;
	 
	public double getA() {
		return a;
	}

	public void setA(double a) {
		this.a = a;
	}

	public void setB(double b) {
		this.b = b;
	}

	public double getB() {
		return b;
	}

	public Triangles(double a, double b) {
		super();
		this.a = a;
		this.b = b;
	}

	@Override
	public double size() {
		return getA()*getB();
	}

	@Override
	public void getInfo() {
		System.out.println("長為:"+getA()+"高為:"+getB()+"的矩形面積為:"+size());	
	}

	@Override
	public double size(Line l) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void getInfo(Line l) {
		// TODO Auto-generated method stub
		
	}

}

Test.java

package cn.hygzs.demo6;
/**
 * 
 * @author 湖雲工作室
 *
 */
public class Test {

	public static void main(String[] args) {
		  //定義物件陣列 
		  Shapes s[] = new Shapes[4];  
		  s[0] = new Circles(47.0);	  
		  s[1] = new Triangles(3,4);
		  s[2] = new Rectangles(3,4,5);
		   s[3] = new Line(22.0,56.0);
		  Line l =  new Line(41.0,57.0);
		  //引用方法
		  s[3].getInfo(l);
		  s[0].getInfo();
		  s[2].getInfo();
		  s[1].getInfo();
		  

	}

}

相關文章