第8周-矩形类

问题及代码:

/*    
*Copyright (c)2015,烟台大学计算机与控制工程学院    
*All rights reserved.    
*文件名称:Rectangle.java    
*作    者:单昕昕    
*完成日期:2015年10月21日    
*版 本 号:v1.0    
*问题描述:设计一个矩形类。 
*程序输入:无。 
*程序输出:矩形的边长、面积、周长。 
*/
import java.util.*;
import java.util.Scanner;
public class Test
{
    public static void main(String[] args)
    {
        Rectangle rectangle1 = new Rectangle();
        System.out.println("The width is:"+rectangle1.width+",the width is:"+rectangle1.height+",the area is:"+rectangle1.getArea()+",the perimeter is:"+rectangle1.getPerimeter());
        Rectangle rectangle2 = new Rectangle(4,40);
        System.out.println("The width is:"+rectangle2.width+",the width is:"+rectangle2.height+",the area is:"+rectangle2.getArea()+",the perimeter is:"+rectangle2.getPerimeter());
        Rectangle rectangle3 = new Rectangle(3.5,35.9);
        System.out.println("The width is:"+rectangle3.width+",the width is:"+rectangle3.height+",the area is:"+rectangle3.getArea()+",the perimeter is:"+rectangle3.getPerimeter());
    }
}

class Rectangle
{
    double width,height;
    Rectangle()
    {
        width=height=1.0;
    }
    Rectangle(double newwidth,double newheight)
    {
        width= newwidth;
        height= newheight;
    }
    double getArea()//面积
    {
        return width*height;
    }
    double getPerimeter()//周长
    {
        return 2*(width+height);
    }
}

运行结果:



知识点总结:

类和对象。


学习心得:

感觉和C++里学的差不多。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值