Advertisement
Guest User

BBBBBB

a guest
Jan 21st, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. /*
  2. Welcome back CS02 students
  3.  
  4. please log in to your VM
  5. https://vm.ktbyte.com
  6. username: 16fa + ktbyte_username
  7. password: ktbyte
  8.  
  9. Today's lesson: inheritance review, interfaces
  10.  
  11. */
  12. import java.util.*;
  13.  
  14. // public class is a class that is visible to other files
  15. public class Lesson4 {
  16. // a public function/method is something that can be
  17. // accessed outside of the class
  18. public static void main(String[] args) {
  19. Point p = new Point(2, 5);
  20. System.out.println(p.getX());
  21. System.out.println(Point.getCount()); // static function
  22.  
  23. Point p2 = new LabeledPoint(-5, 10, "KTByte office");
  24. System.out.println(p2.getX()); // -5
  25. System.out.println(Point.getCount()); // 2
  26.  
  27. // Object class has a toString() that you can override
  28. // to change what this displays
  29. System.out.println(p); //
  30. System.out.println(p2); // (-5, 10)
  31.  
  32. // also look up: boolean equals(Object o) inside Object
  33.  
  34. Point[] points = {p, p2, new Point(3, -4)};
  35. System.out.println(Arrays.toString(points));
  36. Arrays.sort(points); // sort based on the compareTo method
  37. System.out.println(Arrays.toString(points));
  38. }
  39. }
  40.  
  41. // we cannot access private functions/variable inside Point
  42. class LabeledPoint extends Point {
  43. private String label;
  44.  
  45. public LabeledPoint(int x, int y, String label) {
  46. super(x, y); // calls the super classes' constructor
  47. this.label = label;
  48. }
  49.  
  50. public String toString() {
  51. return label + ": " + super.toString();
  52. }
  53. }
  54.  
  55. // represents a point on a 2D plane
  56. // once a Point is created, its x y values cannot be modified
  57. class Point implements Comparable<Point> {
  58. // private means classes outside of Point
  59. // cannot access it
  60. private int x;
  61. private int y;
  62.  
  63. // counts the number of Point objects created
  64. // static means the variable is shared across all Point object
  65. private static int count = 0;
  66.  
  67. // static function can be called without a Point object
  68. public static int getCount() {
  69. return count;
  70. }
  71.  
  72. // force it so that when you create a Poitn object
  73. // you have to set x and y
  74. public Point(int x, int y) {
  75. // "this" means the current object
  76. this.x = x;
  77. this.y = y;
  78. count++;
  79. }
  80.  
  81. public int getX() {
  82. return x;
  83. }
  84.  
  85. public int getY() {
  86. return y;
  87. }
  88.  
  89. public String toString() {
  90. return "(" + x + ", " + y + ")";
  91. }
  92.  
  93. // look up: Comparable
  94. // point with a lower x value comes first
  95. public int compareTo(Point p) {
  96. return x - p.x;
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement