A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© a554305211 中级黑马   /  2015-8-29 20:55  /  353 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. class TestRectangle {
  2.         public static void main(String[] args) {
  3.                 DemoRectangle rt = new DemoRectangle(3,5);        //创建对象rt
  4.                 System.out.println("周长为" + rt.getLength());        //打印周长
  5.                 System.out.println("面积为" + rt.getArea());        //打印面积
  6.         }
  7. }
  8. class DemoRectangle {
  9.         private int width;                                                        //宽
  10.         private int high;                                                        //高
  11.         public DemoRectangle(){}                                        //无参构造函数
  12.         public DemoRectangle(int width,int high) {        //有参构造函数(参数为宽和高)
  13.                 this.width = width;
  14.                 this.high = high;
  15.         }
  16.         public void setWidth(int width) {                        //设置宽
  17.                 this.width = width;
  18.         }
  19.         public int getWidth() {                                                //获取宽
  20.                 return width;
  21.         }
  22.         public void setHigh(int high) {                                //设置高
  23.                 this.high = high;
  24.         }       
  25.         public int getHigh() {                                                //获取高
  26.                 return high;
  27.         }
  28.         public int getLength() {                                        //求周长
  29.          return (width + high) * 2;
  30.         }
  31.         public int getArea() {                                                //求面积
  32.                 return width * high;
  33.         }
  34. }
复制代码

3 个回复

倒序浏览
0819哪位?求认识
回复 使用道具 举报
这个是我自己写的程序,楼主可以借鉴一下

/*
        要求:创建一个矩形类,长度与宽度自行输入,计算出周长与面积并打印
        思路:新建矩形对象,根据输入的值导入矩形类中并调用
*/

import java.util.Scanner;
class rectangle                                                                //定义一个矩形类
{
        private double length;                                        //定义长度
        private double width;                                        //定义宽度
        public void setting(double x,double y)                //判定输入长度和宽度的值是否为正值
        {
                if (x<=0 || y<=0)
                {
                        System.out.println("输入值不符合要求");
                }
                else
                {
                        length=x;
                        width=y;
                }
        }
        public void option()                                                   //定义矩形的属性
        {
                System.out.println("长为:"+length+",宽为:"+width);
        }
        public void perimeter()                                        //定义一个方法打印周长
        {
                double i=(length+width)*2;
                System.out.println("周长为:"+i);
        }
        public void area()                                                //定义一个方法打印面积
        {
                double j=length*width;
                System.out.println("面积为:"+j);
        }
}
public class rectangleDemo                                        //定义测试类
{
        public static void main(String[] args)
        {
                rectangle tg=new rectangle();                           //新建一个矩形对象
                Scanner sc=new Scanner(System.in);       
                System.out.println("请输入长度:");
                double length=sc.nextInt();
                System.out.println("请输入宽度:");
                double width=sc.nextInt();
                tg.setting(length,width);                                //定义矩形类中的长\宽
                tg.option();                                                //调用矩形属性打印方法
                tg.perimeter();                                                //调用矩形周长打印方法
                tg.area();                                                        //调用矩形面积打印方法
        }
}
回复 使用道具 举报
0819吗?童鞋,球心理阴影面积
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马