Simple Exercises on Java Interface Interface

  • 2021-12-12 08:44:21
  • OfStack

Directory 1. Problem description 2. Solution 3. Code listing

This article is transferred from WeChat official account: "Beauty of Algorithm and Programming"

1. Description of the problem

1) Define the interface Printx Which includes 1 method printMyWay() This method has no formal parameters and the return value is null.

2) Write rectangular classes Rectangle The rectangular class requires the implementation of Printx interface, and has the method of seeking area and perimeter. printMyWay The () method should be able to display the side length, area, and circumference of the rectangle.

3) Compile square class as a subclass of rectangle class. Square class inherits the method of rectangle class to find area and perimeter, and adds a new method to find diagonal length. Rewrite printMyWay() Method, which is required to display the side length, area, perimeter and diagonal length of a square.

2. Solutions

For the solution of this problem, It is necessary to understand the interface knowledge of Java first. The official explanation is: Official explanation: Java interface is a declaration of a series of methods, which is a collection of some method characteristics. An interface only has the characteristics of methods but has no implementation of methods, so these methods can be implemented by different classes in different places, and these implementations can have different behaviors (functions). Interfaces can have methods and attributes, can inherit the attributes of classes, and can be linked with classes, which is a special class. However, the methods in interfaces are abstract and cannot be instantiated alone, so they need to be combined with class calls. This topic first defines an interface ' Interface ', (note that when creating a new interface, you should add' I 'before naming) and then use it in the interface 法'printMyWay' Be careful to use void Methods; Then create a new Java class, Rectangle , establish Squera You should pay attention to the inheritance of the parent class when you are in the class of ' printMyWay()0 And then use ' implements' Access interface, define the length, width, area and perimeter according to the proposal, instantiate the defined data, and finally use main Enter the Java program to obtain the area and perimeter.

3. Code list

Interface interface:


package cn.edu.sctu.java20.homework.Printx;

    

public interface IPrintx {

    void printMyWay();

 

}

Classes for Rectangle:


package cn.edu.sctu.java20.homework.Printx;

 

public class Rectangle implements IPrintx{

    double width;

    double length;

    double perimeter;

    double area;

 

    public void SetLength(double width1,double length1){// Reference transmission 

        this.width = width1;

        this.length = length1;

    }

    double getArea(){

        area = length*width;

        return area;

    }

    double getPerimeter(){

        perimeter = length*2+width*2;

        return perimeter;

    }

    @Override

    public void printMyWay(){

        System.out.println(" The length is: "+length+" Width is: "+width+" The perimeter is: "+perimeter+" The area is: "+area);

    };

 

    public static void main(String[] args) {

        Rectangle rectangle = new Rectangle();

        rectangle.SetLength(6,4);

        rectangle.getArea();

        rectangle.getPerimeter();

        rectangle.printMyWay();

    }

}

Square


package cn.edu.sctu.java20.homework.Printx;

 

public class Square extends Rectangle {// Inheritance Rectangle Properties and methods of 

    double diagonal;

    public void setDiagonal(double diagonal1){

        this.diagonal = diagonal1;

    }

    double getDiagonal(){

        diagonal = Math.sqrt(width*width + length*length);

        return diagonal;

    }

 

    @Override

    public void printMyWay(){

        System.out.println(" The length is: "+length+" Width is: "+width+" The perimeter is: "+perimeter+" The area is: "+area+" The diagonal line is: "+diagonal);

    };

    public static void main(String[] args) {

        Square square = new Square();

        square.SetLength(6,6);

        square.getDiagonal();

        square.getArea();

        square.getPerimeter();

        square.printMyWay();

    }

}


Related articles: