JAVA第十二周作业0521
package com.homework12; public class Point { int x; int y; public Point() { } public Point(int x, int y) { this.x = x; this.y = y; } public void movePoint(int dx, int dy){ x += dx; y += dy; } public void show(){ System.out.print("X = " + x); System.out.println("\tY = " + y); } }
package com.homework12; public class P1 { public static void main(String[] args) { Point p1 = new Point(); Point p2 = new Point(2, 3); p1.x = 1; p1.y = 2; p1.show(); p2.show(); System.out.println("-----移动后-----"); p1.movePoint(4, 5); p2.movePoint(0, 2); p1.show(); p2.show(); } }
package com.homework12; public class Rectangle { int length; int width; public double getArea(){ //面积 return length * width; } public double getPer(){ //周长 return 2*(length + width); } public void showAll(){ //全输出 System.out.println("length = " + length); System.out.println("width = " + width); System.out.println("area = " + getArea()); System.out.println("per = " + getPer()); } public Rectangle(int length, int width) { this.length = length; this.width = width; } }
package com.homework12; public class p2 { public static void main(String[] args) { Rectangle p = new Rectangle(5, 3); p.showAll(); } }
package com.homework12; public class laptop { public laptop() { } public laptop(char clour, int cpu) { this.clour = clour; this.cpu = cpu; } char clour; int cpu; public void show(){ System.out.println("clour = " + clour); System.out.println("cpu = " + cpu); } }
package com.homework12; public class Test { public static void main(String[] args) { laptop p1 = new laptop(); p1.clour = '白'; p1.cpu = 10002; laptop p2 = new laptop('黑', 10003); p1.show(); p2.show(); } }