[关闭]
@XQF 2016-10-27T22:14:19.000000Z 字数 4887 阅读 1528

第二次java上机参考

java作业



万物皆有始终,本例程可以经过小许改变解燃眉之急,但是切不可依赖。要知耻而后勇。---------------曾经的我也尝过很急但是不会的滋味!!!共勉!!!题很简单,可是你不会呀!一定要端正态度!

2

创建一个NewRetangl类,该类有两个不同的构造方法,分别实现拿到面积和周长的方法

  1. package javaAssignment.second;
  2. public class NewRectangle {
  3. private double width;
  4. private double height;
  5. public NewRectangle() {
  6. width = 0;
  7. height = 0;
  8. }
  9. public NewRectangle(double w, double h) {
  10. width = w;
  11. height = h;
  12. }
  13. private double getArea() {
  14. return height * width;
  15. }
  16. private double getPerimeter() {
  17. return 2 * (height + width);
  18. }
  19. public static void main(String[] args) {
  20. NewRectangle nr = new NewRectangle(10.2, 2.5);
  21. NewRectangle nr1 = new NewRectangle();
  22. System.out.println(nr.getArea());
  23. System.out.println(nr.getPerimeter());
  24. System.out.println(nr1.getArea());
  25. System.out.println(nr1.getPerimeter());
  26. }
  27. }

运行结果:

25.5
25.4
0.0
0.0

3


(1)

新建point类

  1. class Point {
  2. private double x;
  3. private double y;
  4. public Point() {
  5. x = 0.0;
  6. y = 0.0;
  7. }
  8. public Point(double x, double y) {
  9. this.x = x;
  10. this.y = y;
  11. }
  12. private double distance(Point p) {
  13. double dx = x - p.x;
  14. double dy = y - p.y;
  15. return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
  16. }
  17. }

(2)(3)(4)

关于点是否在矩形中,利用左下角的点进行平移

  1. package javaAssignment.second;
  2. class Point {
  3. private double x;
  4. private double y;
  5. public double getX() {
  6. return x;
  7. }
  8. public void setX(double x) {
  9. this.x = x;
  10. }
  11. public double getY() {
  12. return y;
  13. }
  14. public void setY(double y) {
  15. this.y = y;
  16. }
  17. public Point() {
  18. x = 0.0;
  19. y = 0.0;
  20. }
  21. public Point(double x, double y) {
  22. this.x = x;
  23. this.y = y;
  24. }
  25. private double distance(Point p) {
  26. double dx = x - p.x;
  27. double dy = y - p.y;
  28. return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
  29. }
  30. }
  31. public class NewRectangle {
  32. private double width;
  33. private double height;
  34. private Point p;
  35. public NewRectangle() {
  36. width = 0;
  37. height = 0;
  38. }
  39. public NewRectangle(double w, double h, double x, double y) {
  40. p = new Point();
  41. width = w;
  42. height = h;
  43. p.setX(x);
  44. p.setY(y);
  45. }
  46. private double getArea() {
  47. return height * width;
  48. }
  49. private double getPerimeter() {
  50. return 2 * (height + width);
  51. }
  52. private boolean bPointIn(Point p1) {
  53. double Dx = p1.getX() - p.getX();
  54. double Dy = p1.getY() - p.getY();
  55. if ((Dx <= 0 && Dx >= -width) && (Dy >= 0 && Dy <= height)) {
  56. return true;
  57. }
  58. return false;
  59. }
  60. public static void main(String[] args) {
  61. NewRectangle nr = new NewRectangle(10, 10, 0, 1);
  62. NewRectangle nr1 = new NewRectangle();
  63. System.out.println(nr.getArea());
  64. System.out.println(nr.getPerimeter());
  65. Point p1 = new Point(-1, 0);
  66. System.out.println(nr.bPointIn(p1));
  67. }
  68. }

测试数据输出:

100.0
40.0
false

9


(1)

  1. package javaAssignment.second;
  2. class Cycle {
  3. public void ride(Cycle c) {
  4. System.out.println(c);
  5. }
  6. }
  7. class Unicycle extends Cycle {
  8. @Override
  9. public String toString() {
  10. // TODO Auto-generated method stub
  11. return "Unicycle";
  12. }
  13. }
  14. class Bicycle extends Cycle {
  15. @Override
  16. public String toString() {
  17. // TODO Auto-generated method stub
  18. return "Bicycle";
  19. }
  20. }
  21. class Tricycle extends Cycle {
  22. @Override
  23. public String toString() {
  24. // TODO Auto-generated method stub
  25. return "Tricycle";
  26. }
  27. }
  28. public class NO_9 {
  29. public static void main(String[] args) {
  30. Cycle c = new Cycle();
  31. Unicycle uc = new Unicycle();
  32. Bicycle bc = new Bicycle();
  33. Tricycle tc = new Tricycle();
  34. c.ride(uc);
  35. c.ride(bc);
  36. c.ride(tc);
  37. }
  38. }

测试结果:

Unicycle
Bicycle
Tricycle

(2)

  1. package javaAssignment.second;
  2. class Cycle {
  3. public void ride(Cycle c) {
  4. System.out.println(c + " " + c.wheel());
  5. }
  6. public int wheel() {
  7. return 0;
  8. }
  9. }
  10. class Unicycle extends Cycle {
  11. private int wheels = 1;
  12. @Override
  13. public String toString() {
  14. // TODO Auto-generated method stub
  15. return "Unicycle";
  16. }
  17. @Override
  18. public int wheel() {
  19. // TODO Auto-generated method stub
  20. return wheels;
  21. }
  22. }
  23. class Bicycle extends Cycle {
  24. private int wheels = 2;
  25. @Override
  26. public String toString() {
  27. // TODO Auto-generated method stub
  28. return "Bicycle";
  29. }
  30. @Override
  31. public int wheel() {
  32. // TODO Auto-generated method stub
  33. return wheels;
  34. }
  35. }
  36. class Tricycle extends Cycle {
  37. private int wheels = 3;
  38. @Override
  39. public String toString() {
  40. // TODO Auto-generated method stub
  41. return "Tricycle";
  42. }
  43. @Override
  44. public int wheel() {
  45. // TODO Auto-generated method stub
  46. return wheels;
  47. }
  48. }
  49. public class NO_9 {
  50. public static void main(String[] args) {
  51. Cycle c = new Cycle();
  52. Unicycle uc = new Unicycle();
  53. Bicycle bc = new Bicycle();
  54. Tricycle tc = new Tricycle();
  55. c.ride(uc);
  56. c.ride(bc);
  57. c.ride(tc);
  58. }
  59. }

(3)

在ride()方法中使用一个instanceof

  1. package javaAssignment.second;
  2. class Cycle {
  3. public void ride(Cycle c) {
  4. if (c instanceof Unicycle) {
  5. ((Unicycle) c).balance();
  6. } else if (c instanceof Bicycle) {
  7. ((Bicycle) c).balance();
  8. } else {
  9. System.out.println("This is a instance of Tricycle");
  10. }
  11. }
  12. public int wheel() {
  13. return 0;
  14. }
  15. }
  16. class Unicycle extends Cycle {
  17. private int wheels = 1;
  18. @Override
  19. public String toString() {
  20. // TODO Auto-generated method stub
  21. return "Unicycle";
  22. }
  23. @Override
  24. public int wheel() {
  25. // TODO Auto-generated method stub
  26. return wheels;
  27. }
  28. public void balance() {
  29. System.out.println("Unicycle balance");
  30. }
  31. }
  32. class Bicycle extends Cycle {
  33. private int wheels = 2;
  34. @Override
  35. public String toString() {
  36. // TODO Auto-generated method stub
  37. return "Bicycle";
  38. }
  39. @Override
  40. public int wheel() {
  41. // TODO Auto-generated method stub
  42. return wheels;
  43. }
  44. public void balance() {
  45. System.out.println("Bicycle balance");
  46. }
  47. }
  48. class Tricycle extends Cycle {
  49. private int wheels = 3;
  50. @Override
  51. public String toString() {
  52. // TODO Auto-generated method stub
  53. return "Tricycle";
  54. }
  55. @Override
  56. public int wheel() {
  57. // TODO Auto-generated method stub
  58. return wheels;
  59. }
  60. }
  61. public class NO_9 {
  62. public static void main(String[] args) {
  63. Cycle c = new Cycle();
  64. Cycle uniCycle = new Unicycle();
  65. c.ride(uniCycle);
  66. }
  67. }

结果:

Unicycle balance

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注