第九周上机练习

 1 public class Point {
 2     int x;
 3     int y;
 4     void Ponit() {
 5         System.out.println(x);
 6         System.out.println(y);
 7     }
 8     void Ponit(int x1, int y1) {
 9         x = x1;
10         y = y1;
11     }
12  
13     void movePonit(int dx, int dy) {
14         x += dx;
15         y += dy;
16         System.out.println("坐标是" + "(" + x + "," + y + ")");
17     }
18     public static void main(String[] args) {
19         Point p1 = new Point();
20         p1.x=1;
21         p1.y=2;
22         p1.movePonit(2, 6);
23         Point p2 = new Point();
24         p2.x=11;
25         p2.y=23 ;
26         p2.movePonit(4,8);
27     }
28 }

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

 1 public class Rectangle {
 2     double length;
 3     double width;
 4      
 5     public void Rectangle(int width, int length) {
 6         this.length= length;
 7         this.width = width;
 8     }
 9      
10     public void getArea() {
11          
12         System.out.println("面积为:"+(width*length));
13     }
14      
15     public void getPer() {
16         System.out.println("周长为:"+(width+length)*2);
17     }
18     public void showAll() {
19         System.out.println("长为:"+length+" 宽为:"+width);
20         getArea();
21         getPer();
22     }
23      
24     public static void main(String[] args) {
25         Rectangle p = new Rectangle();
26         p.length = 9;
27         p.width =  3;
28         p.showAll();
29     }
30 }

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

 1 public class notbook {
 2     static int cpu;
 3     static char color;
 4     public static void all() {
 5         System.out.println("颜色是" + color);
 6         System.out.println("cpu是" + cpu);
 7     }
 8  
 9     static void book(int cpu1, char color1) {
10         cpu = cpu1;
11         color = color1;
12         all();
13     }
14  
15     public static void main(String[] args) {
16         Book r1 = new Book();
17         r1.color = '红黑色';
18         r1.cpu = 9700;
19         r1.all();
20         book(9000, '黑白色');
21  
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()方法。

 1 public class Person {
 2     String name;
 3     double sg;
 4     int age;
 5     public void Person() {};
 6      
 7     public void Person(String name, double sg, int eag) {
 8         this.name = name;
 9         this.sg = sg;
10         this.age = age;
11     }
12     public void sayHello() {
13         System.out.println("hello,my name is "+name);
14     }
15 }
 1 public class Constructor {
 2     public static void main(String[] args) {
 3         Person p1 = new Person();
 4         Person p2 = new Person();
 5         p1.Person("zhangsan", 1.73, 33);
 6         p2.Person("lishi", 1.74, 44);
 7         p1.sayHello();
 8         p2.sayHello();
 9  
10     }
11 }
原文地址:https://www.cnblogs.com/baigei/p/12807919.html