第九次java作业

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

 4      int x;
 5         int y;
 6         point() {
 7                     
 8         }
 9         point(int dx,int dy) {
10             x+=dx;
11             y+=dy;
12                     
13         }
14         void movepoint(int p1,int p2) {           
15             p1=x*2;
16             p2=y*2;
17             System.out.println("p1的坐标为"+p1+"\n"+"p2的坐标为"+p2);             
18         }
19 }  
20       
21 package text6;
22
23 public class person { 24 public static void main(String[] args) { 25 point p=new point(); 26 p.x=6; 27 p.y=2; 28 p.movepoint(p.x,p.y); 29 } 30 }

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

 1 package text6;
 2 
 3 public class Rectangle {
 4     int Width;
 5     int Length;
 6 
 7     public Rectangle() {
 8     }
 9 
10     public Rectangle(int Width, int Length) {
11         this.Width = width;
12         this.Length = length;
13     }
14 
15     public int getPer() {
16         return (Width + Length) * 2;
17     }
18 
19     public int getArea() {
20         return Width * Length;
21     }
22 
23     public void showAll() {
24         System.out.println("长和宽为:" + Width + "," + Length + "周长为:" + getPer() + "面积为:" + getArea());
25 
26     }
27 
28     public static void main(String[] args) {
29         Rectangle n = new Rectangle(2, 2);
30         n.showAll();
31     }
32 }
3.定义一个笔记本类,该类有颜色(char)和cpu型号(int)两个属性。
 
·无参和有参的两个构造方法;有参构造方法可以在创建对象的同时为每个属性赋值;
·输出笔记本信息的方法。
·然后编写一个测试类,测试笔记本类的各个方法。
 1 package text6;
 2  
 3 public class point {
 4          
 5         char color;
 6         int cpu;
 7  
 8         point() {
 9        }
10          point(char color1, int cpu1) {
11             this.color = color;
12             this.cpu = cpu;
13         }
14  
15         void point() {
16             System.out.println("颜色是:"+color);
17             System.out.println( "型号为:"+cpu);
18         }
19     public static void main(String[] args) {
20         point p= new point('p',4689);
21         p.point();
22        }
23     }
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()方法。

package text6;

public class Person {
        String name;
        int age;
        double height;
        public void sayHello(){
            System.out.println("hello,my name is "+this.name);
        }
        public void getValue(String name,int age,double height){
            this.name = name;
            this.age = age;
            this.height = height;
        }
    }



 package text6;

public class TEXT {

    public static void main(String[] args) {
        Person p1 = new Person();
        p1.getValue("zs",25,1.85);
        p1.sayHello();
        Person p2 = new Person();
        p2.getValue("ls",35,1.75);
        p2.sayHello();
        }
    }

 

 


posted @ 2020-04-30 16:00  就这水平?  阅读(123)  评论(0编辑  收藏  举报