Java第八次作业
一、定义一个点类Point,包含2个成员变量x、y分别表示x和y坐标,2个构造器Point()和Point(intx0,y0),以及一个movePoint(intdx,intdy)方法实现点的位置移动,创建两个Point对象p1、p2,分别调用movePoint方法后,打印p1和p2的坐标。[必作题]
1 package wen; 2 3 4 5 public class Point { 6 7 8 9 int x; 10 11 int y; 12 13 14 15 public Point(int x0, int y0) { 16 17 super(); 18 19 this.x = x0; 20 21 this.y = y0; 22 23 } 24 25 26 27 public Point() { 28 29 super(); 30 31 } 32 33 34 35 public String movePoint(int dx, int dy) { 36 37 x = dx + x; 38 39 y = dy + y; 40 41 return ("x为" + x + "y为" + y); 42 43 } 44 45 46 47 public static void main(String[] args) { 48 49 Point p1 = new Point(2, 5); 50 51 System.out.println(p1.movePoint(5, 5)); 52 53 Point p2 = new Point(4, 7); 54 55 System.out.println(p2.movePoint(3, 7)); 56 57 58 59 } 60 61 62 63 }
二、定义一个矩形类Rectangle:(知识点:对象的创建和使用)[必做题]
1.定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长。
2.有2个属性:长length、宽width
3.通过构造方法Rectangle(intwidth,intlength),分别给两个属性赋值
4.创建一个Rectangle对象,并输出相关信息
1 package wen; 2 3 4 5 public class Rectangle { 6 7 int length,width; 8 9 public int getArea(int length,int width){ 10 11 return length*width; 12 13 } 14 15 public int getPer(int length,int width){ 16 17 return (length+width)*2; 18 19 } 20 21 public void showAll(){ 22 23 System.out.println("长方形的长是"+length+"宽是"+width); 24 25 System.out.println("周长是"+ (length+width)*2); 26 27 System.out.println("面积是"+length*width); 28 29 } 30 31 32 33 public Rectangle(int length, int width) { 34 35 this.length = length; 36 37 this.width = width; 38 39 40 41 } 42 43 44 45 public static void main(String[] args) { 46 47 // TODO Auto-generated method stub 48 49 Rectangle r=new Rectangle(10, 10); 50 51 r.showAll(); 52 53 54 55 } 56 57 58 59 }
三、定义一个笔记本类,该类有颜色(char)和cpu型号(int)两个属性。[必做题]
1.无参和有参的两个构造方法;有参构造方法可以在创建对象的同时为每个属性赋值;
2.输出笔记本信息的方法
3.然后编写一个测试类,测试笔记本类的各个方法。
package wen; public class computer { char colour; int cpu; public void show() { System.out.println("我的颜色是:" + colour+"色" + ",型号是:" + cpu); } public computer(char colour, int cpu) { super(); this.colour = colour; this.cpu = cpu; } public computer() { super(); } } package wen; public class test { public static void main(String[] args) { // TODO Auto-generated method stub computer s1=new computer(); s1.colour='黑'; s1.cpu=121; s1.show(); computer s2=new computer('蓝',111); s2.show(); } }
四、定义两个类,描述如下:[必做题]
1.定义一个人类Person:
(1)定义一个方法sayHello(),可以向对方发出问候语“hello,mynameisXXX”
(2)有三个属性:名字、身高、体重
2.定义一个PersonCreate类:
(1)创建两个对象,分别是zhangsan,33岁,1.73;lishi,44,1.74
(2)分别调用对象的sayHello()方法。
package wen; public class Person { String name; double height; int weight; int age; public void sayhello() { System.out.println("hello,my name is " + name); System.out.println("my height is " + height + "米"); System.out.println("my weight is " + weight + "斤"); System.out.println("my age is " + age + "岁"); } } package wen; public class PersonCreate { public static void main(String[] args) { // TODO Auto-generated method stub Person a = new Person(); Person b = new Person(); a.name ="zhangsan"; a.age = 33; a.height = 1.73; a.weight = 120; b.name = "lishi"; b.age = 44; b.height = 1.74; b.weight = 140; a.sayhello(); b.sayhello(); } }
五、定义两个类,描述如下:[必做题]
1.定义一个人类Person:
(1)定义一个方法sayHello(),可以向对方发出问候语“hello,mynameisXXX”
(2)有三个属性:名字、身高、体重
(3)通过构造方法,分别给三个属性赋值
2.定义一个Constructor类:
(1)创建两个对象,分别是zhangsan,33岁,1.73;lishi,44,1.74
(2)分别调用对象的sayHello()方法。
1 package wen; 2 3 4 5 public class Constructor { 6 7 8 9 String name; 10 11 double height; 12 13 int weight; 14 15 int age; 16 17 public Constructor (String name, double height, int weight,int age) { 18 19 super(); 20 21 this.name = name; 22 23 this.height = height; 24 25 this.weight = weight; 26 27 this.age=age; 28 29 } 30 31 32 33 public void sayhello() { 34 35 System.out.println("hello,my name is " + name); 36 37 System.out.println("my height is " + height + "米"); 38 39 System.out.println("my weight is " + weight + "千克"); 40 41 System.out.println("my age is " + age + "岁"); 42 43 } 44 45 46 47 } 48 49 50 51 package wen; 52 53 54 55 public class sayHello { 56 57 58 59 public static void main(String[] args) { 60 61 // TODO Auto-generated method stub 62 63 Constructor a=new Constructor ("zhangsan",1.73,55,33); 64 65 Constructor b=new Constructor ("lishi",1.74,54,44); 66 67 a.sayhello(); 68 69 b.sayhello(); 70 71 72 73 } 74 75 76 77 }
六、定义一个汽车类Vehicle,要求如下:[选做题]
1.属性包括:汽车品牌brand(String类型)、颜色color(String类型)和速度speed(double类型),并且所有属性为私有。
2.至少提供一个有参的构造方法(要求品牌和颜色可以初始化为任意值,但速度的初始值必须为0)。
3.为私有属性提供访问器方法。注意:汽车品牌一旦初始化之后不能修改。
4.定义一个一般方法run(),用打印语句描述汽车奔跑
的功能
5.定义测试类VehicleTest,在其main方法中创建一个品牌为“benz”、颜色为“black”的汽车。
1 package wen; 2 3 4 5 public class Vehicle { 6 7 8 9 private String brand; 10 11 12 13 private String color; 14 15 16 17 private double speed; 18 19 20 21 public Vehicle(String brand,String color){ 22 23 24 25 this.brand=brand; 26 27 28 29 this.color=color; 30 31 32 33 34 35 36 37 } 38 39 40 41 Vehicle(String brand, String color, double speed) { 42 43 44 45 super(); 46 47 48 49 this.brand = brand; 50 51 52 53 this.color = color; 54 55 56 57 this.speed = speed; 58 59 60 61 } 62 63 64 65 public void run(){ 66 67 68 69 System.out.println("这个汽车的品牌为"+this.brand+"这个汽车的颜色为"+this.color+"这个汽车的速度为"+this.speed); 70 71 72 73 } 74 75 76 77 } 78 79 80 81 package wen; 82 83 84 85 public class VehicleTest { 86 87 88 89 public static void main(String[] args) { 90 91 92 93 Vehicle v=new Vehicle("benz","black"); 94 95 96 97 v.run(); 98 99 100 101 Vehicle v1=new Vehicle("benz","black",13); 102 103 104 105 v1.run(); 106 107 } 108 109 110 111 }