第九周上机作业

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

public class Firm {
    int x;
    int y;

    public Firm() {   //无参
            System.out.println("");
            
        }
    public Firm(int i,int j) {   //有参
            System.out.println("");
            System.out.println("横坐标移动了"+i+"\n"+"纵坐标移动了"+j);
        }

    public void movePoint(int dx,int dy) {
            this.x=dx;
            this.y=dy;
            System.out.println("横坐标移动了"+x+"纵坐标移动了"+y);
    }
    public static void main(String[] args) {
        Firm a=new Firm(3,9);
    }
}
2、定义一个矩形类Rectangle:(知识点:对象的创建和使用)
• 2.1 定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长;
• 2.2 有2个属性:长length、宽width;
• 2.3 通过构造方法Rectangle(int width, int length),分别给两个属性赋值;
• 2.4 创建一个Rectangle对象,并输出相关信息。

public class Rectangle {
    // 类名Rectangle
    String name; // 属性 名字
    int length; // 属性 长度
    int width; // 属性 宽度
    
    public Rectangle(){
        System.out.println("");
    }
    public Rectangle(String n,int l,int w){
        name=n;
        length=l;
        width=w;
    }

    public void showAll() {
        System.out.println(name + "的长为" + length);
        System.out.println(name + "的宽为" + width);
    } // 成员方法-长度、宽度

    public void getArea() {
        System.out.println(name + "的面积为" + (length * width));
    } // 成员方法-面积

    public void getPer() {
        System.out.println(name + "的周长为" + ((length + width) * 2));
    } // 成员方法-周长

    public static void main(String[] args) {
//        Rectangle p = new Rectangle(); // 创建对象
        Rectangle p = new Rectangle("矩形",9,3);
        
//        p.name = "矩形"; // 为成员变量name赋值
//        p.length = 9; // 为成员变量length赋值
//        p.width = 3; // 为成员width变量赋值
        
        p.showAll(); //使用成员方法showAll
        p.getArea(); //使用成员方法getArea
        p.getPer();  //使用成员方法getPer

    }
}
3、定义一个笔记本类,该类有颜色(char)和cpu型号(int)两个属性。
• 3.1 无参和有参的两个构造方法;有参构造方法可以在创建对象的同时为每个属性赋值;
• 3.2 输出笔记本信息的方法;
• 3.3 然后编写一个测试类,测试笔记本类的各个方法。
public class Notebook {
    //类名 Notebook
    String name;
    String 颜色;   //属性
    int cup型号;   //属性
    
    public Notebook(){
        System.out.println("");
    }
    public Notebook(String n,String y, int c){
        name=n;
        颜色=y;
        cup型号=c;
    }
    
    public void color(){
        System.out.println(name+"的颜色是:"+颜色);
    }   //成员方法--颜色
    public void size(){
        System.out.println(name+"的型号是:"+cup型号);
    }   //成员方法--型号
    
    public static void main(String[] args) {
//        Notebook a = new Notebook(); // 创建对象
        Notebook a = new Notebook("杯子","蓝色",3);
        
//        a.name="笔记本";   //为成员变量赋值
//        a.颜色= "蓝色";
//        a.cup型号=3;
        
        a.color();   //使用成员方法
        a.size();
    }
}
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.74;
• 6.2.2分别调用对象的sayHello()方法。
public class Person {
    //类名Person
    
    String name;   //成员属性
    double height;
    int age;
    public Person(){
        System.out.println("");
    }
    
    public Person(String n, int a, double h) {
        name=n;
        age=a;
        height=h;
    }

    public void sayHello(){
        System.out.println("Hello,my name is"+name+"今年"+age+"\n我的身高是"+height);
    }   //成员方法    
}
public class PersonCreate {
    // 类名PersonCreate
    String name; // 成员属性
    double height;
    int age;
    
    public PersonCreate(String string, int i, double d){
        System.out.println("");
    }
    public PersonCreate(String n,double h,int d ){
        name=n;
        height=h;
        age=d;
    }

    public void sayHello() {
        System.out.println("Hello,my name is " + name +"今年"+age+"\n我的身高是"+height+ "Thank you!");
    } // 成员方法
    
    public static void main(String[] args) {
//        PersonCreate b = new PersonCreate(); // 创建对象
//        Person a = new Person(); // 创建对象
        Person a = new Person("张三",33,1.75);
        PersonCreate b = new PersonCreate("李四",55,1.76);

//        a.name = "张三"; // 为成员变量赋值
//        a.age = 33;
//        a.height = 1.75;

        a.sayHello(); // 使用成员方法

//        b.name = "李四"; // 为成员变量赋值
//        b.age = 55;
//        b.height = 1.76;

        b.sayHello(); // 使用成员方法
    }

}

 

posted on 2020-04-30 11:38  豁牙小乞丐的打狗棒  阅读(170)  评论(0编辑  收藏  举报