JAVA 接口练习题

Python022

JAVA 接口练习题,第1张

public interface Ishape {

    /**

     * 求周长

     * @return

     */

    int getPerimeter()

    /**

     * 求面积

     * @return

     */

    int getArea()

} public class Triangle implements Ishape {

    private int length

    private int width

    public Triangle(int length, int width) {

        this.length = length

        this.width = width

    }

    @Override

    public int getPerimeter() {

        return (length+width)*2

    }

    @Override

    public int getArea() {

        return length*width

    }

}

public class Rectangle implements Ishape {

    private int line1

    private int line2

    private int line3

    private int height1

    private int height2

    private int height3

    public Rectangle(int line1, int line2, int line3, int height1, int height2, int height3) {

        this.line1 = line1

        this.line2 = line2

        this.line3 = line3

        this.height1 = height1

        this.height2 = height2

        this.height3 = height3

    }

    @Override

    public int getPerimeter() {

        return line1+line2+line3

    }

    @Override

    public int getArea() {

        return line1*height1/2

    }

} /**

 * Created by tony on 2015/11/6.

 * 平行四边形

 */

public class Parallelogram implements Ishape {

    private int line1

    private int line2

    private int height1

    private int height2

    public Parallelogram(int line1, int line2, int height1, int height2) {

        this.line1 = line1

        this.line2 = line2

        this.height1 = height1

        this.height2 = height2

    }

    @Override

    public int getPerimeter() {

        return (line1+line2)*2

    }

    @Override

    public int getArea() {

        return line1*height1

    }

}

/**

 * Created by tony on 2015/11/6.

 * 梯形

 */

public class Echelon implements Ishape {

    private int topLine

    private int bottomLine

    private int line1

    private int line2

    private int height

    public Echelon(int topLine, int bottomLine, int line1, int line2, int height) {

        this.topLine = topLine

        this.bottomLine = bottomLine

        this.line1 = line1

        this.line2 = line2

        this.height = height

    }

    @Override

    public int getPerimeter() {

        return topLine+bottomLine+line1+line2

    }

    @Override

    public int getArea() {

        return (topLine+bottomLine)*height/2

    }

}

public class TestShape {

    public static void main(String[] args) {

        // 矩形

        Triangle triangle = new Triangle(4,5)

        // 三角形

        Rectangle rectangle = new Rectangle(3,4,5,4,3,6)

        // 平行四边形

        Parallelogram parallelogram = new Parallelogram(6,7,4,5)

        // 梯形

        Echelon echelon = new Echelon(3,4,6,7,8)

        System.out.println("矩形周长:"+triangle.getPerimeter())

        System.out.println("矩形面积:"+triangle.getArea())

        System.out.println("三角形周长:"+rectangle.getPerimeter())

        System.out.println("三角形面积:"+rectangle.getArea())

        System.out.println("平行四边形周长:"+parallelogram.getPerimeter())

        System.out.println("平行四边形面积:"+parallelogram.getArea())

        System.out.println("梯形周长:"+echelon.getPerimeter())

        System.out.println("梯形面积:"+echelon.getArea())

    }

}

关键了解接口的作用,你把下面的Cp改成你要的TestSort就OK乐,Cp是我的类文件名 懒得改了interface Sortble{

public int Compare(Sortble s)

}

class Student implements Sortble{

private int score

Student(int s){

score=s

}

public int Compare(Sortble s) {

// TODO Auto-generated method stub

Student ss=null

if(s instanceof Student){

ss=(Student)s

}else{

System.out.println("程序出错,意外退出")

System.exit(0)

}

if(this.getScore()>ss.getScore()){

return 1

}else if(this.getScore()<ss.getScore()){

return -1

}else{

return 0

}

}

public String toString(){

return ""+getScore()

}

public void setScore(int score) {

this.score = score

}

public int getScore() {

return score

}

}

class Rectangle implements Sortble{

private int length,width

Rectangle(int length,int width){

this.length=length

this.width=width

}

public int area(){

return length*width

}

public int getLength() {

return length

}

public void setLength(int length) {

this.length = length

}

public int getWidth() {

return width

}

public void setWidth(int width) {

this.width = width

}

public int Compare(Sortble s) {

Rectangle ss=null

// TODO Auto-generated method stub

if(s instanceof Rectangle){

ss=(Rectangle)s

}else{

System.out.println("程序出错,意外退出")

System.exit(0)

}

if(this.area()>ss.area()){

return 1

}else if(this.area()<ss.area()){

return -1

}else{

return 0

}

}

public String toString(){

return ""+area()

}

}

class Sort{

public static void SelectSort(Sortble[] a){

Sortble m=null

for(int i=0i<a.length-1i++){//升序

for(int j=i+1j<a.lengthj++){

if(a[j].Compare(a[i])<0){

m=a[i]

a[i]=a[j]

a[j]=m

}

}

}

}

}

public class Cp{

Cp(){

Student[] s=new Student[5]

for(int i=0i<s.lengthi++){

s[i]=new Student((int)(Math.random()*100))

}

Sort.SelectSort(s)

System.out.println("下面是按升序输出学生成绩")

for(int i=0i<s.lengthi++){

System.out.println(s[i])

}

Rectangle[] ss=new Rectangle[5]

for(int i=0i<ss.lengthi++){

ss[i]=new Rectangle((int)(Math.random()*100),(int)(Math.random()*100))

}

Sort.SelectSort(ss)

System.out.println("下面是按升序输出矩形面积")

for(int i=0i<ss.lengthi++){

System.out.println(ss[i])

}

}

public static void main(String[] arg){

new Cp()

}

}

第一题有问题:1、创建Person接口(即“人”),它有setData()和getData()方法对“人”属性name、sex和birthday赋值和获得这些属性组成的字符串信息。

问题是:你说要创建一个人(接口),然后里面有方法对人的属性进行赋值?这怎么可能呢,接口是没有成员变量(属性)的,怎么能赋值?接口里只能有常量。

第二题可以答一下:

package pillar

public class Pillar { private Geometry buttom

private double height

public Pillar() {

// TODO Auto-generated constructor stub

}

public Pillar(Geometry button,double height){

this.buttom = button

this.height = height

}

public double getVolume(){

return this.buttom.getArea()*height

}

public Geometry getButtom() {

return buttom

}

public void setButtom(Geometry buttom) {

this.buttom = buttom

}

public double getHeight() {

return height

}

public void setHeight(double height) {

this.height = height

}

}

------------------------------------------------类分割线---------------------------------------------------------

package pillar

public interface Geometry { double getArea()

}

------------------------------------------------类分割线---------------------------------------------------------

package pillar

public class Circle implements Geometry { private double r

public Circle() {

// TODO Auto-generated constructor stub

}

public Circle(double r) {

this.r = r

}

public double getArea() { return Math.PI*r*r

}

public double getR() {

return r

}

public void setR(double r) {

this.r = r

}

}

------------------------------------------------类分割线---------------------------------------------------------

package pillar

public class Rectangle implements Geometry { private double width

private double height

public Rectangle() {

// TODO Auto-generated constructor stub

}

public Rectangle(double width, double height) {

this.width = width

this.height = height

}

public double getArea() { return this.width*this.height

}

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

}

}

------------------------------------------------类分割线---------------------------------------------------------

package pillar

public class TestPillar {

/** * @param args

*/

public static void main(String[] args) {

Circle c = new Circle(5)

Rectangle r = new Rectangle(3,4)

Pillar p1 = new Pillar(c,6)

Pillar p2 = new Pillar(r,6)

System.out.println("圆的体积:"+p1.getVolume()+"\t矩形的体积:"+p2.getVolume())

}

}