Java编程练习Day02

该博客介绍了如何使用Java实现面向对象编程的基础知识,包括定义一个Point类来表示二维坐标点,具备构造器和移动方法;以及创建Rectangle类,实现计算面积、周长及展示所有信息的方法。示例中展示了如何创建并使用这两个类的对象。
摘要由CSDN通过智能技术生成

定义一个点类Point,包含2个成员变量x、y分别表示x和y坐标,2个构造器Point()和Point(intx0,y0),以及一个movePoint(int dx,int dy)方法实现点的位置移动,创建两个Point对象p1、p2,分别调用movePoint方法后,打印p1和p2的坐标
 

public class Point {
    public static void main(String args[]){
        Point p1 = new Point(1,1);
        Point p2 = new Point(2,2);
        p1.movePoint(p2.x,p2.y);
        System.out.println(p1.x+" "+p1.y);
    }
    int x;
    int y;
    Point(){

    }
    Point(int x,int y){
        this.x = x;
        this.y = y;
    }
    public void movePoint(int dx,int dy){
        this.x += dx;
        this.y += dy;
    }
}

定义一个矩形类Rectangle:(知识点:对象的创建和使用)[必做题]
• 2.1 定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长。
• 2.2 有2个属性:长length、宽width
• 2.3 通过构造方法Rectangle(int width, int length),分别给两个属性赋值
• 2.4 创建一个Rectangle对象,并输出相关信息
 

public class Rectangle {
    public static void main(String args[]){
        Rectangle rectangle = new Rectangle(2,3);
        rectangle.shouAll();
    }
    public double length;
    public double width;
    Rectangle(double length,double width){
        this.width = width;
        this.length = length;
    }
    public double getArea(){
        return length*width;
    }
    public double getPer(){
        return 2*(length+width);
    }
    public void shouAll(){
        System.out.println(this.length+" "+this.width+" "+this.getArea()+" "
                        +this.getPer());
    }
}

转载于JavaSE核心技术——面向对象编程基础练习题_DYS_房东的猫的博客-CSDN博客_java面向对象编程题目

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值