第九周上机练习

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

public class Person {
    int x;
    int y;
    int z;

    public Person() {
        System.out.println(x);
        System.out.println(y);

    }

    public Person(int x0, int y0) {
        this.x = x0;
        this.y = y0;
        System.out.println(x);
        System.out.println(y);
    }

    public void movePoint(int x1, int y1) {
        x1 = x + 3;
        y1 = y + 4;
System.out.println("x1的坐标为"+x1+"\n"+"y1的坐标为"+y1);
    }

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Person p1 = new Person();
        Person p2 = new Person();
        p1.x = 20;
        p1.y = 30;
        p1.movePoint(p1.x, p1.y);
        p2.x=50;
        p2.y=30;
        p2.movePoint(p2.x, p2.y);

    }

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

public class Rectangle {
    double length;
    double width;
    public Rectangle() {
        
    }
    public Rectangle(double length,double width) {
        length=20;
        width=10;
    }
    public void getArea() {
        double area=0;
        area=length*width;
        System.out.println("面积为"+area);
    }
public void getPer() {
    double Per=0;
    Per=2*length+2*width;
    System.out.println("周长为"+Per);

}
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Rectangle r=new Rectangle();
        r.length=20;
        r.width=10;
        r.getArea();
        r.getPer();
    }

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

public class Person1 {
    String colour;
    int cpu;

    public void bijiben1(String n, int m) {
        colour = n;
        cpu = m;
        System.out.println("笔记本颜色为  " + n + "型号为  " + m);
    }

    public void bijiben() {
        colour = "红色";
        cpu = 126448;
        System.out.println("笔记本电脑的颜色为  " + colour + "cpu型号为  " + cpu);
    }

}
package java1;

public class Person {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Person1 P = new Person1();
        P.colour = "绿色";
        P.cpu = 2345;
        System.out.println("笔记本电脑颜色为  " + P.colour + "电脑型号为  " + P.cpu);
        P.bijiben();
        P.bijiben1("白色", 123456);

    }

}
//6、定义两个类,描述如下: [必做题]
• 6.1定义一个人类Person:
• 6.1.1定义一个方法sayHello(),可以向对方发出
问候语“hello,my name is XXX”
• 6.1.2有三个属性:名字、身高、年龄
• 6.1.3通过构造方法,分别给三个属性赋值
• 6.2定义一个Constructor类:
• 6.2.1创建两个对象,分别是zhangsan,33岁,
1.73;lishi,44,1.746.2.2分别调用对象的sayHello()方法。
package java1;

public class Person2 {
    String name;
    double height;
    int weight;
    int age;

    public void sayHello() {
        System.out.println("hello my name is   " + name + "  height是" + height + "  weight是" + weight);
    }
    public void Person1(String name,int age) {
        System.out.println("姓名是"+name+"年龄是"+age);
    }
}
package java1;

public class Person {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Person2 P = new Person2();
        Person2 p1 = new Person2();
        P.name = "wangwu";
        P.height = 1.73;
        P.weight = 66;
        P.sayHello();
        P.Person1("zhansan", 23);
        p1.name = "lishi";
        p1.height = 1.74;
        p1.age = 44;
        p1.weight=55;
        p1.sayHello();
    }

}

 

posted @ 2020-04-30 11:55  龚孝益  阅读(343)  评论(0)    收藏  举报