Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. package punct;
  2.  
  3. public class Punct2D extends Object implements Cloneable { //catre clasa Clone pentru a nu mai arunca exceptie
  4. private double x;
  5. private double y;
  6. private String eticheta;
  7.  
  8. static final double X_ORIG = 0; //final adica const
  9. static final double Y_ORIG = 0;
  10. static final String ETICHETA = "Origine";
  11.  
  12. public Punct2D(double x, double y, String eticheta) {
  13. this.x = x; //este o eticheta a parametrului x, nu un pointer ca in C++
  14. this.y = y;
  15. this.eticheta = eticheta;
  16. }
  17.  
  18.  
  19. public Punct2D(){
  20. this.x=X_ORIG;
  21. this.y=Y_ORIG;
  22. }
  23.  
  24. public double getX() {
  25. return x;
  26. }
  27.  
  28. public double getY() {
  29. return y;
  30. }
  31.  
  32. @Override
  33. public String toString() {
  34. // return "Punct2D{" +
  35. //// "x=" + x +
  36. //// ", y=" + y +
  37. //// ", eticheta='" + eticheta + '\'' +
  38. //// '}';
  39.  
  40. return this.x + "," + this.y + "," + this.eticheta; // pentru fisiere, citire, scriere
  41. }
  42.  
  43. public void setX(double x) {
  44. this.x = x;
  45. }
  46.  
  47. public void setY(double y) {
  48. this.y = y;
  49. }
  50.  
  51. public void setEticheta(String eticheta) {
  52. this.eticheta = eticheta;
  53. }
  54.  
  55. @Override
  56. protected Object clone() throws CloneNotSupportedException { //in spate aloca memorie si daca nu mai este... arunca exceptie
  57. return super.clone();
  58. }
  59.  
  60.  
  61. }
  62.  
  63. package punct;
  64.  
  65. public class UtilizarePunct2D {
  66. public static void main(String[] args){
  67. Punct2D p1 = new Punct2D();
  68. System.out.println(p1.toString());
  69.  
  70. // Punct2D p2 = p1; //pointeaza catre adresa lui p1;
  71. //
  72. // System.out.println(p2.toString());
  73. // p2.setEticheta("Punct 2");
  74. // System.out.println(p2.toString());
  75. // System.out.println(p1.toString());
  76.  
  77. try {
  78. Punct2D p3= (Punct2D) p1.clone();
  79. System.out.println(p3.toString());
  80. p3.setEticheta("Punct 3");
  81. System.out.println(p3.toString());
  82. System.out.println(p1.toString());
  83. } catch (CloneNotSupportedException e) {
  84. e.printStackTrace();
  85. }
  86.  
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement