1. 试编写Java代码实现一个计数器类Computer,其中包括
a) 属性counterValue  用来保存计数器的当前值;
b) 方法increment() 计数器加一;
c) 方法decrement() 计数器减一;
d) 方法reset()计数器清零
e) 方法getCounterValue()返回当前计数器的值2. 编程实现矩形类,其中应包括计算举行周长和面积的方法
a) 至少应该实现两个构造方法
b) 属性值需要进行隐藏,不能直接进行修改3. 为上个实验中定义的矩形类派生一个子类:正方形类。若正方形类的操作同样是求周长和面积,则这个子类除了从父类哪里继承来的方法之外,还需要定义哪些方法。列出正方形类的所有属性与方法。编程检查、运行所编写的正方形类4. 定义接口Printable,其中包括一个方法print (),这个方法没有形参,返回值为空5. 改写矩形类,使之实现Printable接口,用print ()方法将矩形的相关信息(长、宽、周长、面积)打印在屏幕上6. 改写正方形类,重载print()方法,将正方形的边长、周长、面积打印在屏幕上7. 将矩形和正方形组织成一个包mybag

解决方案 »

  1.   

    我刚到JAVA门口,但是我帮你顶。无偿的。顺便鄙视一下
    GlandJacky(Jacky狂奔在Java大地) ( ) 信誉:105 
    呵呵
      

  2.   

    反正手里空着,给你写一段。楼上说得正确。帮你写作业其实是在害你。public class Computer()
    {
      int counterValue;  public void increment()
      {
        counterValue++;
      }  public void decrement()
      {
        counterValue--;
      }  public void reset()
      {
        counterValue=0;
      }  public int getCounterValue()
      {
        return counterValue;
      }
    }
      

  3.   

    忘写constructor了:P
    public Computer()
    {
      counterValue=0;
    }
      

  4.   

    第二题不知道这样对么?
    public class JuXing
    {
    private int length ;
    private int width ;public JuXing()
    {}public JuXing(int length, int width)
    {
    setLength(length);
    setWidth(width);
    }public void setLenth(int length)
    {
    this.length=length;
    }public void setWidth(int width)
    {
    this.width = width ;
    }public int size(){
    return length*width;
    }public int ZhouChang(){
    return 2*(length+width);
    }public static void mian(int args[]){
    JuXing jx=new JuXing();
    jx.setLengh(5);
    jx.setWidth(6);
    System.out.println("矩形的面积是:"jx.size());
    System.out.println("矩形的周长是:"jx.ZhouChang());
    JuXing jx2 = new JuXing(5, 6);
    System.out.println("矩形的面积是:"jx.size());
    System.out.println("矩形的周长是:"jx.ZhouChang());
    }
      

  5.   

    你这作业也忒简单了吧? 如果你是在校学习JAVA, 自己研究吧! 没时间帮你做作业
    如果你是在自学的话还可以帮一下, 不过我看不像!
    自学怎么会有这样的问题.
      

  6.   

    光批评了,也帮一个吧:
    4.定义接口Printable,其中包括一个方法print (),这个方法没有形参,返回值为空
    public interface Printable
    {
        public void print();
    }5.改写矩形类,使之实现Printable接口,用print ()方法将矩形的相关信息(长、宽、周长、面积)打印在屏幕上
    public class JuXing implements Printable
    {
       ............
       // 上面程序全要,就多加一个方法
       public void print()
       {
           System.out.println("Length    : " + this.length);
           System.out.println("Width     : " + this.width);
           System.out.println("Area      : " + size());
           System.out.println("Perimeter : " + ZhouChang());
       }
    }
      

  7.   

    package mybag;/**
     *
     * @author Trumplet
     */
    public class Computer {
        private int counterValue;
        /** Creates a new instance of Computer */
        public Computer() {
            counterValue=0;
        }
        public void increment(){
            if (counterValue<Integer.MAX_VALUE) counterValue++;
        }
        public void decrement(){
            if (counterValue>1) counterValue--;
        }
        public void reset(){
            counterValue=0;
        }
        public int getCounterValue(){
            return counterValue;
        }
    }
    -----------------------------------------
    package mybag;/**
     *
     * @author Trumplet
     */
    public interface Printable {
        public void print();
    }
    ------------------------------------
    package mybag;/**
     *
     * @author Trumplet
     */
    public class Rectangle implements Printable {
        private int width;
        private int height;
        /** Creates a new instance of Rect */
        public Rectangle() {
            this(100,200);
        }
        public Rectangle(int width, int height){
            setWidth(width); setHeight(height);
        }
        
        public int getWidth() {
            return width;
        }
        
        public void setWidth(int width) {
            this.width = width;
        }
        
        public int getHeight() {
            return height;
        }
        
        public void setHeight(int height) {
            this.height = height;
        }
        
        public int getPerimeter(){
            return 2*(width+height);
        }
        public int getArea(){
            return width*height;
        }    public void print() {
            System.out.println("The width of this Rectangle is: " + getWidth());
            System.out.println("The Height of this Rectangle is: " + getHeight());
            System.out.println("The Perimeter of this Rectangle is: " + getPerimeter());
            System.out.println("The Area of this Rectangle is: " + getArea());
        }
    }
    ---------------------------------
    package mybag;/**
     *
     * @author Trumplet
     */
    public class Square extends Rectangle {
        
        /** Creates a new instance of Square */
        public Square() {
            this(100);
        }
        public Square(int width){
            super(width,width);
        }
        
        public void print(){
            System.out.println("The width of this Square is: " + getWidth());
            System.out.println("The Perimeter of this Rectangle is: " + getPerimeter());
            System.out.println("The Area of this Rectangle is: " + getArea());
        }
    }
      

  8.   

    改正:正方形类:
    package mybag;/**
     *
     * @author Trumplet
     */
    public class Square extends Rectangle {
        
        /** Creates a new instance of Square */
        public Square() {
            this(100);
        }
        public Square(int width){
            super(width,width);
        }
        public void setWidth(int width) {
            super.setWidth(width);super.setHeight(width);
        }
        
        public void setHeight(int height) {
            super.setWidth(height);super.setHeight(height);
        }
        public void print(){
            System.out.println("The width of this Square is: " + getWidth());
            System.out.println("The Perimeter of this Square is: " + getPerimeter());
            System.out.println("The Area of this Square is: " + getArea());
        }
        public static void main(String[] args) {
            Square s1 = new Square(20);
            s1.print();
        }
    }