/**5-1
* Define interface printable, which includes a method Printitmyway (),
* This method has no formal parameters and the return value is null
**/
Interface Printable{void Printitmyway ();}
/**5-2
* Rewrite the rectangle class in experiment 3 to implement the printable interface,
* Use the Printitmyway () method to relate information about the rectangle (length, width, perimeter, area)
* Print on the screen;
* Rewrite the square class in experiment 4, overloading the Printitmyway () method
* Print information about the square (edge length, perimeter, area) on the screen
**/
public class firstprogram{public static void main (String[] args) {Rectangle rectangle = new rectangle (12, 21); Square square = new square (4); Rectangle.printitmyway (); Square.printitmyway ();}} Interface printable{void printitmyway ();} class rectangle implements printable{protected int length;protected int width; rectangle () {}rectangle (int l, int w) {this.length = l;this.width = w;} Void show () {System.out.println (" length: " + length + " width: " + width);} int perimeter () {return (Length + width) * 2;} int area () {return length * width;} public void printitmyway () {this.show (); System.out.println (" permiter: " + this.perimeter () + " area: " &nbsP;+ this.area ());}} class Square extends Rectangle{protected int side; square (int side) {super (); this.side = side;} Void show () {System.out.println (" side: " + side);} int perimeter () {return 4 * side;} int area () {return side * side;}}
Those years, learn together Java 5-1 5-2