java第十二次作业

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

 

package work;

import work.Point;
public class test {
    public static void main(String[] args) {
        Point x1 = new Point(3.0, 5.5);
        Point x2 = new Point();
        x1.movePoint();
        x2.movePoint();
    }

}
class Point {
    double a;
    double b;

    public Point() {
        super();
    }

    public Point(double a, double b) {
        this.a = a;
        this.b = b;
    }

    public void movePoint() {
        System.out.println("点的坐标为");
        System.out.println(+a + "," + b);
    }
}

 

 

 

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

package work;

import work.Rectangle;
public class test1 {
    public static void main(String[] args) {
        Rectangle a = new Rectangle(3, 4);
        a.getArea();
        a.getPer();
        a.showAll();
    }

}

 class Rectangle {
    int l;
    int w;

    public Rectangle(int dw, int dl) {
        super();
        w = dw;
        l = dl;
    }

    public void getArea() {
        int Area = w * l;
        System.out.println("面积是" + Area);
    }

    public void getPer() {
        int Per = (w + l) * 2;
        System.out.println("周长是" + Per);
    }

    public void showAll() {
        int Area = w * l;
        int Per = (w + l) * 2;
        System.out.println("长是" + l + "宽是" + w + "面积是" + Area + "周长是" + Per);
    }
}

 

 

3.定义一-个笔记本类,该类有颜色(char) cpu型号(int) 两个属性。[必做题]
3.1无参和有参的两个构造方法;有参构造方法可以在创建对象的同时为每个属性赋值;
3.2 输出笔记本信息的方法
3.3 然后编写一-个测试类,测试笔记本类的各个方法

 

package work;

import work.notebook;
public class test3 {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        notebook a = new notebook('红', 9888);
        a.zhan();
    }
}
class notebook {
    char colour;
    int cpu;

    public notebook(char colour, int cpu) {
        this.colour = colour;
        this.cpu = cpu;
    }

    public notebook() {
        super();
    }

    public void zhan() {
        System.out.println("笔记本颜色是" + colour + "型号是 " + cpu + "");
    }
}

 

 

 

 

 

posted @ 2021-05-25 17:45  杨英斌  阅读(34)  评论(0编辑  收藏  举报