Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1782431
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Java

2017-03-13 11:10:15

代码出自Java An introduction to Problem solving and Programming 一书.

代码如下: 

点击(此处)折叠或打开

  1. package ObjectDesign;

  2. interface ShapeI{
  3.     //set the offset for the shape
  4.     public void setOffset(int newOffset);
  5.     
  6.     //return the offset for the shape
  7.     public int getOffset();
  8.     
  9.     //Draws the shape at lineNumber lines
  10.     //down from the current line
  11.     public void drawAt(int lineNumber);
  12.     
  13.     //Draws the shape at the current line
  14.     public void drawHere();
  15. }

  16. // interface for a rectangle to be drawn on the screen
  17. interface IRectangle extends ShapeI{
  18.     //set the rectangle's dimension
  19.     public void set(int Height,int width);
  20.     
  21. }

  22. interface ITriangle extends ShapeI{
  23.     //set the triangle's base
  24.     public void set(int newBase);
  25. }

  26. //abstract drawHere with abstract class
  27. // public void drawHere with not abstract class
  28. abstract class ShapeBasis implements ShapeI{
  29.     private int Offset;
  30.     
  31.     public ShapeBasis(){
  32.         Offset=0;
  33.     }
  34.     
  35.     public ShapeBasis(int theOffset){
  36.         this.Offset=theOffset;
  37.     }
  38.     
  39.     public void setOffset(int newOffset){
  40.         this.Offset=newOffset;
  41.     }
  42.     
  43.     public int getOffset(){
  44.         return Offset;
  45.     }
  46.     
  47.     public void drawAt(int lineNumber){
  48.         for(int count=0;count<lineNumber;count++){
  49.             System.out.println();
  50.         }
  51.         drawHere();
  52.     }
  53.     
  54.     public abstract void drawHere();
  55.     /*
  56.     public void drawHere(){
  57.         for(int count=0;count
  58.             System.out.print(" ");
  59.         }
  60.         System.out.println("*");
  61.     }
  62.     */
  63.     
  64. }

  65. class DrawRectangle extends ShapeBasis implements IRectangle{
  66.     private int height;
  67.     private int width;
  68.     
  69.     public DrawRectangle(){
  70.         super();
  71.         height=0;
  72.         width=0;
  73.     }
  74.     
  75.     public DrawRectangle(int Offset,int Height,int width){
  76.         super(Offset);
  77.         this.height=Height;
  78.         this.width = width;
  79.     }
  80.     
  81.     public void set(int Height, int width){
  82.         this.height=Height;
  83.         this.width=width;
  84.     }
  85.     
  86.     public void drawHere(){
  87.         drawHorizontalLine();
  88.         drawSides();
  89.         drawHorizontalLine();
  90.     }
  91.     
  92.     private void drawHorizontalLine(){
  93.         skipSpaces(getOffset());
  94.         for(int count=0;count<width;count++){
  95.             System.out.print("-");
  96.         }System.out.println();
  97.     }
  98.     
  99.     private static void skipSpaces(int number){
  100.         for(int count=0;count<number;count++){
  101.             System.out.print(" ");
  102.         }
  103.     }
  104.     
  105.     private void drawSides(){
  106.         for(int count=0;count<(height-2);count++){
  107.             drawOneLineOfSides();
  108.         }
  109.     }
  110.     
  111.     private void drawOneLineOfSides(){
  112.         skipSpaces(getOffset());
  113.         System.out.print("|");
  114.         skipSpaces(width-2);
  115.         System.out.println("|");
  116.     }
  117. }

  118. class DrawTriangle extends ShapeBasis implements ITriangle{
  119.     private int base;

  120.     public DrawTriangle(){
  121.         super();
  122.         base=0;
  123.     }
  124.     
  125.     public DrawTriangle(int Offset,int base){
  126.         super(Offset);
  127.         this.base=base;
  128.     }
  129.     
  130.     //precondition: base is odd
  131.     public void set(int base){
  132.         this.base = base;
  133.     }
  134.     
  135.     public void drawHere(){
  136.         drawTop();
  137.         drawBase();
  138.     }
  139.     
  140.     public void drawBase(){
  141.         skipSpaces(getOffset());
  142.         for(int count=0;count<base;count++){
  143.             System.out.print('*');
  144.         }
  145.         System.out.println();
  146.     }
  147.     
  148.     public void drawTop(){
  149.         int startOfLine = getOffset() + base/2;
  150.         skipSpaces(startOfLine);
  151.         System.out.println('*');
  152.         int lineCount=base/2-1;
  153.         
  154.         int insideWidth=1;
  155.         for(int count=0;count<lineCount;count++){
  156.             startOfLine--;
  157.             skipSpaces(startOfLine);
  158.             System.out.print('*');
  159.             skipSpaces(insideWidth);
  160.             System.out.println('*');
  161.             
  162.             //Down one line, so the inside is 2 spaces wider
  163.             insideWidth = insideWidth + 2;
  164.         }
  165.     }
  166.     
  167.     private static void skipSpaces(int number){
  168.         for(int i=0;i<number;i++){
  169.             System.out.print(" ");
  170.         }
  171.     }
  172. }

  173. public class DrawShapes {
  174.     public static final int INDENT=5;
  175.     public static final int TREE_TOP_WIDTH = 21;
  176.     public static final int TREE_BOTTOM_WIDTH=4;
  177.     public static final int TREE_BOTTOM_HEIGHT=6;
  178.     
  179.     public static void main(String[] args){
  180.         /*
  181.          *     DrawRectangle dr= new DrawRectangle(5,6,8);
  182.          *    dr.drawHere();
  183.          *    DrawTriangle dT = new DrawTriangle(5,13);
  184.          *    dT.drawHere();
  185.          */
  186.         drawTree(TREE_TOP_WIDTH,TREE_BOTTOM_WIDTH,TREE_BOTTOM_HEIGHT);
  187.     }
  188.     
  189.     public static void drawTree(int topWidth,int bottomWidth, int bottomHeight){
  190.         System.out.printf("%30s","Plant a Tree on Arbor day");
  191.         DrawTriangle treeTop = new DrawTriangle(INDENT,topWidth);
  192.         drawTop(treeTop);
  193.         DrawRectangle treeTrunk = new DrawRectangle(INDENT+(topWidth/2)-(bottomWidth/2),
  194.             bottomHeight,bottomWidth);
  195.         drawTrunk(treeTrunk);
  196.     }
  197.     
  198.     public static void drawTop(DrawTriangle treeTop){
  199.         treeTop.drawAt(1);
  200.     }
  201.     
  202.     public static void drawTrunk(DrawRectangle treeTrunk){
  203.         treeTrunk.drawAt(1);
  204.     }
  205.     
  206.     
  207. }

2. 贴个最近看的Eclipse 教程的地址


阅读(1266) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~