120 likes | 248 Views
97-2-JAVA3 第三階段考古題說明. class Car { double gas=55; double eff; float move(int distance) { if (gas >= distance/eff ) { gas -= distance/eff; return distance; } else { distance = (int)(gas * eff); gas = 0;
E N D
class Car { double gas=55; double eff; float move(int distance) { if (gas >= distance/eff ) { gas -= distance/eff; return distance; } else { distance = (int)(gas * eff); gas = 0; return distance; } } } 1 oldcar行駛了 100.0 公里 油量剩 45.0 公升 newcar行駛了 500.0 公里 油量剩 35.0 公升 public class EX31 { public static void main(String[] argv) { Car oldcar = new Car(); Car newcar = new Car(); oldcar.eff = 10; newcar.eff = 25; System.out.println("oldcar行駛了 " + oldcar.move(100) + " 公里"); System.out.println("油量剩 " + oldcar.gas + " 公升"); System.out.println("newcar行駛了 " + newcar.move(500) + " 公里"); System.out.println("油量剩 " + newcar.gas + " 公升"); } }
2 class Test { int x = 10; int y = 10; void show(int x){ int y = 20; System.out.println("x = " + x); System.out.println("this.y = " + this.y); } } public class EX32 { public static void main(String[] argv){ Test a = new Test(); Test b = new Test(); b.x=15; b.y=25; a.show(20); b.show(30); } } x = 20 this.y = 10 x = 30 this.y = 25
class TestA { int x = 10; void show() { System.out.println("x = " + x); } } class TestB { void changeTestA(TestA t,int newX) { t.x = newX; } } public class EX33{ public static void main(String[] argv){ TestA a = new TestA(); TestB b = new TestB(); TestA c; c = a; a.show(); b.changeTestA(a,20); a.show(); c.x = 100; a.show(); } } 3 x = 10 x = 20 x = 100
4 public class EX34 { public static void main(String[] argv){ int x = 10; { int y = 20; { int z = 300; System.out.println("x = " + x); System.out.println("y = " + y); System.out.println("z = " + z); System.out.println(""); } } int y = x; int z = 30; System.out.println("y = " + y); System.out.println("z = " + z); } } x = 10 y = 20 z = 300 y = 10 z = 30
5 class Math{ double ex(int n) { double x; if (n == 2) return x=1.0/(2*1); else return ( 1.0/(n*(n-1)) + ex(n-1)); } } public class EX35 { public static void main(String[] argv){ Math m = new Math(); System.out.println("Result=" + m.ex(10)); } } Result=0.9
class Test { private int x,y; public Test(int x,int y) { this.x = x; this.y = y; } public int getX() {return x;} public void setX(int x) {this.x = x;} public int getY() {return y;} public void setY(int y) {this.y = y;} } 6 public class EX36 { public static void main(String[] argv){ Test a = new Test(30,40); Test b; b = a; b.setX(80); a.setY(50); System.out.println("成員x:" + a.getX()); System.out.println("成員y:" + b.getY()); } } 成員x:80 成員y:50
7 class Test { double x = 10,y = 20; Test(int x,int y) { this(x); this.y = y; } Test(int x) { this(50.0,60.0); this.x = x; } Test(double x, double y) { this.x=x; this.y=y; } } 成員x:30.0 成員y:50.0 public class EX37 { public static void main(String[] argv){ Test a = new Test(30,50); System.out.println("成員x:" + a.x); System.out.println("成員y:" + a.y); } }
class Point { public int x,y; public Point(int x,int y) { this.x = x; this.y = y; } } 8 class Rectangle { Point upperleft; Point lowerright; public Rectangle(Point upperleft,Point lowerright) { this.upperleft = upperleft; this.lowerright = lowerright; } public Rectangle() { this(new Point(0,0),new Point(5,-5)); } public Rectangle(int x1,int y1,int x2,int y2) { this(new Point(x1,y1),new Point(x2,y2)); } public Rectangle(Point upperleft,int length) { this(upperleft,new Point(upperleft.x + length, upperleft.y - length)); } public int area() { return (lowerright.x - upperleft.x) * (upperleft.y - lowerright.y); } }
8(conti.) public class EX38 { public static void main(String[] argv){ Rectangle a = new Rectangle(2,1,5,-5); Rectangle b = new Rectangle(new Point(3,3),4); Rectangle c = new Rectangle(); System.out.println("a的面積:" + a.area()); System.out.println("b的面積:" + b.area()); System.out.println("c的面積:" + c.area()); } } a的面積:18 b的面積:16 c的面積:25
class Test { public int x; public static int y; public Test(int x,int y) { this.x = x; this.y = y; } public String toString() { return "(x,y):(" + x + "," + y + ")"; } } 9 public class EX39 { public static void main(String[] argv){ Test a = new Test(100,40); Test b = new Test(200,50); Test c = new Test(300,60); System.out.println("Test.y=" + (Test.y=150)); System.out.println("物件a" + a); System.out.println("物件b" + b); System.out.println("物件c" + c); } } Test.y=150 物件a(x,y):(100,150) 物件b(x,y):(200,150) 物件c(x,y):(300,150)
class Point { private double x,y; public void setx(double x) { this.x = x; } public void sety(double y) { this.y = y; } public String toString() { return "(" + x + "," + y + ")"; } public Point(double x,double y) { //this.x = x; //this.y = y; } public Point() { x =10; y =20; } public Point(Point p) { x = p.x; y = p.y; } } class Circle { private Point p; private double r; public Point getp() { return new Point(p); } Circle(double x,double y,double r) { p = new Point(x,y); this.r = r; } Circle() { this(10,0,1); } public String toString() { return "圓心:" + p.toString() + " 半徑:" + r; } } 10 public class EX3A { public static void main(String[] argv) { Circle c = new Circle(3,4,5); Point p = c.getp(); p.setx(6); System.out.println(c.toString()); } } 圓心座標:(0.0,0.0) 半徑:5.0