第十三次作业

1、定义一个点类Point,包含2个成员变量x、y分别表示x和y坐标,2个构造器Point()和Point(intx0,y0),以及一个movePoint(int dx,int dy)方法,

实现点的位置移动,创建两个Point对象p1、p2,分别调用movePoint方法后,打印p1和p2的坐标。

package date430;

public class Point {

    int x;
    int y;

    public Point() {

    }

    public Point(int x0, int y0) {
        this.x = x0;
        this.y = y0;
    }

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

    public static void main(String[] args) {
        Point p1 = new Point(6, 6);
        p1.movePoint(8, 8);
        System.out.println("p1的X坐标为:" + p1.x + ",p1的Y坐标为:" + p1.y);
        Point p2 = new Point();
        p2.movePoint(6, 7);
        System.out.println("p2的X坐标为:" + p2.x + ",p2的Y坐标为:" + p2.y);
    }
}

2、定义一个矩形类Rectangle:

·定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积周长。

·有2个属性:长length、宽width

·通过构造方法Rectangle(int width, int length),分别给两个属性赋值。

·创建一个Rectangle对象,并输出相关信息

package date430;

public class Rectangle {
    int length;
    int width;
    int Area;
    int Per;

    public int getArea() {
        int Area;
        Area = length * width;
        return Area;
    }

    public int getPer() {
        int Per;
        Per = (length + width) * 2;
        return Per;
    }

    public Rectangle(int length, int width) {
        this.length = length;
        this.width = width;
        this.Area = Area;
        this.Per = Per;
    }

    public void showAll() {
        System.out.println("该矩形的长为:" + length + "\n" + "该矩形的宽为:" + width + "\n" + "该矩形的面积为:" + getArea() + "\n"
                + "该矩形的周长为:" + getPer());
    }

    public static void main(String[] args) {
        Rectangle i = new Rectangle(6, 8);
        i.showAll();
    }

}

3.定义一个笔记本类,该类有颜色(char)和cpu型号(int)两个属性。

 

·无参和有参的两个构造方法;有参构造方法可以在创建对象的同时为每个属性赋值;

·输出笔记本信息的方法

·然后编写一个测试类,测试笔记本类的各个方法。

1 package date430;
 2 
 3 public class notebook {
 4 
 5     char color;
 6     int cpu;
 7 
 8     public void getDate() {
 9     }
10 
11     public void getDate(char color, int cpu) {
12         this.color = color;
13         this.cpu = cpu;
14     }
15 
16     void showAll() {
17         System.out.println("该笔记本颜色为:" + color + "\n" + "该笔记本cpu型号为:" + cpu);
18     }
19 
20     public static void main(String[] args) {
21         notebook u = new notebook();
22         u.getDate('', 5580);
23         u.showAll();
24     }
25 }

4.定义两个类,描述如下:

·定义一个人类Person:

定义一个方法sayHello(),可以向对方发出问候语“hello,my name is XXX”。

有三个属性:名字、身高、年龄

通过构造方法,分别给三个属性赋值

·定义一个Constructor类:

创建两个对象,分别是zhangsan,33岁,1.73;lishi,44,1.74。

分别调用对象的sayHello()方法。

1 package date430;
 2 
 3 public class Person {
 4     String name;
 5     int age;
 6     double high;
 7 
 8     public void sayHello() {
 9         System.out.println("hello,my name is " + name + ".");
10     }
11 
12 }
1 package date430;
 2 
 3 public class Person0Constructor {
 4 
 5     public static void main(String[] args) {
 6         Person zhangsan = new Person();
 7         Person lisi = new Person();
 8         zhangsan.name = "zhangsan";
 9         zhangsan.age = 33;
10         zhangsan.high = 1.73;
11         lisi.name = "lisi";
12         lisi.age = 44;
13         lisi.high = 1.74;
14         zhangsan.sayHello();
15         lisi.sayHello();
16     }
17 }

 

posted @ 2020-04-30 12:32  王立晓  阅读(192)  评论(0)    收藏  举报