1 oldcar 100 0 45 0 newcar 500

  • Slides: 12
Download presentation

1 oldcar行駛了 100. 0 公里 油量剩 45. 0 公升 newcar行駛了 500. 0 公里 油量剩

1 oldcar行駛了 100. 0 公里 油量剩 45. 0 公升 newcar行駛了 500. 0 公里 油量剩 35. 0 公升 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; } } } public class EX 31 { 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

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 EX 32 { 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

3 x = 10 x = 20 x = 100 class Test. A {

3 x = 10 x = 20 x = 100 class Test. A { int x = 10; void show() { System. out. println("x = " + x); } } class Test. B { void change. Test. A(Test. A t, int new. X) { t. x = new. X; } } public class EX 33{ public static void main(String[] argv){ Test. A a = new Test. A(); Test. B b = new Test. B(); Test. A c; c = a; a. show(); b. change. Test. A(a, 20); a. show(); c. x = 100; a. show(); } }

4 x = 10 y = 20 z = 300 y = 10 z

4 x = 10 y = 20 z = 300 y = 10 z = 30 public class EX 34 { 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); } }

5 class Math{ double ex(int n) { double x; if (n == 2) return

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)); } Result=0. 9 } public class EX 35 { public static void main(String[] argv){ Math m = new Math(); System. out. println("Result=" + m. ex(10)); } }

6 public class EX 36 { public static void main(String[] argv){ Test a =

6 public class EX 36 { public static void main(String[] argv){ Test a = new Test(30, 40); Test b; b = a; b. set. X(80); a. set. Y(50); System. out. println("成員x:" + a. get. X()); System. out. println("成員y:" + b. get. Y()); } } class Test { private int x, y; public Test(int x, int y) { this. x = x; this. y = y; } public int get. X() {return x; } public void set. X(int x) {this. x = x; } public int get. Y() {return y; } public void set. Y(int y) {this. y = y; } } 成員x: 80 成員y: 50

7 class Test { double x = 10, y = 20; Test(int x, int

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 EX 37 { public static void main(String[] argv){ Test a = new Test(30, 50); System. out. println("成員x:" + a. x); System. out. println("成員y:" + a. y); } }

8 class Rectangle { Point upperleft; Point lowerright; class Point { public int x,

8 class Rectangle { Point upperleft; Point lowerright; class Point { public int x, y; public Point(int x, int y) { this. x = x; this. y = y; } } 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 x 1, int y 1, int x 2, int y 2) { this(new Point(x 1, y 1), new Point(x 2, y 2)); } 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 EX 38 { public static void main(String[] argv){ Rectangle a

8(conti. ) public class EX 38 { 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; 9 public Test(int x,

class Test { public int x; public static int y; 9 public Test(int x, int y) { this. x = x; this. y = y; } public String to. String() { return "(x, y): (" + x + ", " + y + ")"; } } Test. y=150 物件a(x, y): (100, 150) 物件b(x, y): (200, 150) 物件c(x, y): (300, 150) public class EX 39 { 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); } }

class Point { class Circle { private double x, y; private Point p; public

class Point { class Circle { private double x, y; private Point p; public void setx(double x) { private double r; this. x = x; public Point getp() { } return new Point(p); public void sety(double y) { } this. y = y; Circle(double x, double y, double r) { } p = new Point(x, y); public String to. String() { this. r = r; return "(" + x + ", " + y + ")"; } } Circle() { public Point(double x, double y) { this(10, 0, 1); //this. x = x; } //this. y = y; public String to. String() { return "圓心:" + p. to. String() + " 半徑:" + r; } public Point() { } public class EX 3 A { x =10; } public static void main(String[] argv) { y =20; Circle c = new Circle(3, 4, 5); } Point p = c. getp(); public Point(Point p) { x = p. x; p. setx(6); y = p. y; System. out. println(c. to. String()); } } } 圓心座標:(0. 0, 0. 0) 半徑: 5. 0 } 10