Those years, learn together Java 5-3

Source: Internet
Author: User

/**5-3

* Add a new Printitmyway (char) method to the printable interface,

* This method has a character-type parameter, and the return value is null.

* Its function is to use the given character to print, for example, if the given character is *, a length of 4

* The screen with a width of 3 rectangle is printed as:

* ****

* ****

* ****

**/

public class firstprogram{public static void main  (String[] args) {Rectangle  rectangle = new rectangle (5, 3); Square square = new square (4); Rectangle.printitmyway (' * '); Square.printitmyway (' * ');}} Interface printable{void printitmyway (); Void printitmyway (char n);}  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);} Void print (char n) {int i, j;for  (i = 0; i < width; i + +) {for  (j = 0; j < length; j++) System.out.print (" "  + n  +  " "); System.out.println ();} System.out.println ();} int perimeter  () {return  (Length + width)  * 2;} int area  () {return length * width;}     public void printitmyway () {this.show (); System.out.println (" permiter: "  + this.perimeter ()  +  " area: "  +  this.area ());}     public void printitmyway (char n)     {     this.print (n);     }} class square  extends rectangle{ protected int side; square  (int side) {super (); this.side = side;} Void show () {System.out.println (" side: "  + side);} Void print (char n) {int i, j;for  (i = 0; i < side; i+ +) {for  (j = 0; j < side; j++) System.out.print (" "  + n  +  " "); System.out.println ();}} Int perimeter  () {return 4 * side;} int area  () {return side * side;}}


This article is from the "hacker" blog, make sure to keep this source http://anglecode.blog.51cto.com/5628271/1619857

Those years, learn together Java 5-3

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.