Java第十一次作业
1、定义一个点类Point, 包含2个成员变量x、y分别表示x和y坐标,2个构造器Point()和Point( intx0,y0),以及一个movePoint (int dx,intdy)方法实现点的位置移动,创建两个Point对象p1、p2, 分别调用movePoint方法后,打印pl和p2的坐标。[必作题]
1 package sj; 2 public class sjzy { 3 4 public static void main(String[] args) { 5 // TODO 自动生成的方法存根 6 7 Point p1 = new Point(2.0, 4.5); 8 Point p2 = new Point(); 9 p1.movePoint(); 10 p2.movePoint(); 11 } 12 13 } 14 15 class Point { 16 double x; 17 double y; 18 19 public Point() { 20 super(); 21 } 22 23 public Point(double x, double y) { 24 this.x = x; 25 this.y = y; 26 } 27 28 public void movePoint() { 29 System.out.println("点的坐标为"); 30 System.out.println(+x + "," + y); 31 } 32 33 }
2.、定义一个矩形类Rectangle: (知识点: 对象的创建和使用)[必做题]
2.1 定义三个方法: getArea(求面积、getPer0求周长,showAll0分 别在控制台输出长、宽、面积周长。
2.2 有2个属性:长length、 宽width
2.3 通过构造方法Rectangle(int width, int length),分别给两个属性赋值
2.4 创建-个Rectangle对象, 并输出相关信息
1 package sj; 2 public class sjzy { 3 4 public static void main(String[] args) { 5 // TODO 自动生成的方法存根 6 7 Rectangle a = new Rectangle(3, 5); 8 a.getArea(); 9 a.getPer(); 10 a.showAll(); 11 } 12 13 } 14 15 class Rectangle { 16 int length; 17 int width; 18 19 public Rectangle(int dwidth, int dlength) { 20 super(); 21 width = dwidth; 22 length = dlength; 23 } 24 25 public void getArea() { 26 int Area = width * length; 27 System.out.println("面积为" + Area); 28 } 29 30 public void getPer() { 31 int Per = (width + length) * 2; 32 System.out.println("周长为" + Per); 33 } 34 35 public void showAll() { 36 int Area = width * length; 37 int Per = (width + length) * 2; 38 System.out.println("长为" + length + "宽为" + width + "面积为" + Area + "周长为" + Per); 39 } 40 }
3、定义一-个笔记本类,该类有颜色(char) 和cpu型号(int) 两个属性。[必做题]
3.1无参和有参的两个构造方法;有参构造方法可以在创建对象的同时为每个属性赋值;
3.2 输出笔记本信息的方法
3.3 然后编写一-个测试类,测试笔记本类的各个方法。
1 package sj; 2 3 public class sjzy { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 notebook a = new notebook('蓝', 10000); 8 a.zhan(); 9 } 10 } 11 12 class notebook { 13 char colour; 14 int cpu; 15 16 public notebook(char colour, int cpu) { 17 this.colour = colour; 18 this.cpu = cpu; 19 } 20 21 public notebook() { 22 super(); 23 } 24 25 public void zhan() { 26 System.out.println("笔记本颜色为" + colour + "型号为" + cpu + ""); 27 } 28 }