第九周上机练习
1.
package LH; public class Point { int x; int y; Point() { } Point(int x0, int y0) { this.x = x0; this.y= y0; } void movePoint(int dx,int dy) { this.x += dx; this.y += dy; } }
package LH; public class Test { public static void main(String[] args) { Point p1 = new Point(1, 1); p1.movePoint(2, 2); System.out.println("p1当前的X坐标为:" + p1.x + ",p1当前的Y坐标为:" + p1.y); Point p2 = new Point(1,1); p2.movePoint(3, 3); System.out.println("p2当前的X坐标为:" + p2.x + ",p2当前的Y坐标为:" + p2.y); } }
2.
package LH; public class Rectangle { int length; int width; public void getArea() { System.out.println(length * width); } public void getPer() { System.out.println((length + width) * 2); } public void showAll() { System.out.println("长:" + length); System.out.println("宽:" + width); System.out.print("面积:"); getArea(); System.out.print("周长:"); getPer(); } public Rectangle(int width, int length) { this.length = length; this.width = width; } }
package LH; public class Text { public static void main(String[] args) { Rectangle rc = new Rectangle(3, 5); rc.showAll(); } }
3.
package LH; public class Com { char color; String cpu; public bjb() { System.out.println("无参数"); } public bjb(char a,String b) { color=a; cpu=b; System.out.println(color); System.out.println(cpu); System.out.println("有参数"); } public void sj(){ System.out.println("颜色是"+color); System.out.println("cpu"+cpu); } }
package LH; public class Test { public static void main(String[] args) { bjb b1=new bjb(); bjb b2=new bjb('白',"i7"); b2.sj(); } }
6.
package LH; public class Preson { 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 LH; public class Test { 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(); } }