Those years, learn together Java 5-1 5-2

Source: Internet
Author: User

/**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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.