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 boke5月21日; 2 3 public class Point { 4 int x; 5 int y; 6 7 public Point() { 8 } 9 10 public Point(int x0, int y0) { 11 super(); 12 this.x = x0; 13 this.y = y0; 14 } 15 16 public void movePoint(int dx, int dy) { 17 System.out.printf("移动点前的坐标为(x:%d,y:%d)\n", this.x, this.y); 18 this.x += dx; 19 this.y += dy; 20 System.out.printf("移动点后的坐标为(x:%d,y:%d)\n", this.x, this.y); 21 22 } 23 24 public static void main(String[] args) { 25 Point p1 = new Point(0, 0); 26 Point p2 = new Point(1, 1); 27 p1.movePoint(3, 3); 28 p2.movePoint(5, 5); 29 30 } 31 }

2、定义一个矩形类Rectangle:(知识点:对象的创建和使用)[必做题]
2.1 定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长。
2.2 有2个属性:长length、宽width
2.3 通过构造方法Rectangle(int width, int length),分别给两个属性赋值
2.4 创建一个Rectangle对象,并输出相关信息
1 package boke5月21日; 2 3 public class Rectangle { 4 int length; 5 int width; 6 7 public Rectangle() { 8 9 } 10 11 public Rectangle(int length, int width) { 12 super(); 13 this.length = length; 14 this.width = width; 15 } 16 17 public int getArea(int length, int width) { 18 return this.length * this.width; 19 } 20 21 public int getPer(int length, int width) { 22 return 2 * (this.length + this.width); 23 } 24 25 public void showAll() { 26 System.out.println("长为" + length + ",宽为" + width); 27 System.out.println("面积为" + getArea(this.length, this.width)); 28 System.out.println("周长为" + getPer(this.length,this. width)); 29 30 } 31 32 public static void main(String[] args) { 33 Rectangle r = new Rectangle(3, 4); 34 r.showAll(); 35 } 36 }

3、定义一个笔记本类,该类有颜色(char)和cpu型号(int)两个属性。 [必做题]
3.1 无参和有参的两个构造方法;有参构造方法可以在创建对象的同时为每个属性赋值;
3.2 输出笔记本信息的方法
3.3 然后编写一个测试类,测试笔记本类的各个方法
1 package boke5月21日; 2 3 public class Computer { 4 char color; 5 int cpu; 6 7 public Computer() { 8 9 } 10 11 public Computer(char color, int cpu) { 12 super(); 13 this.color = color; 14 this.cpu = cpu; 15 } 16 17 public char getColor() { 18 return color; 19 } 20 21 public int getCpu() { 22 return cpu; 23 } 24 25 public void setColor(char color) { 26 this.color = color; 27 } 28 29 public void setCpu(int cpu) { 30 this.cpu = cpu; 31 } 32 33 public void showComputer() { 34 System.out.println("笔记本颜色"+color+" "+"型号"+cpu); 35 } 36 }
1 package boke5月21日; 2 3 public class ComputerTest { 4 public static void main(String[] args) { 5 Computer c1 = new Computer(); 6 c1.showComputer(); 7 Computer c2 = new Computer('蓝', 110); 8 c2.showComputer(); 9 } 10 }


浙公网安备 33010602011771号