Hakunin

OOP V2

Mar 25th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.41 KB | None | 0 0
  1. package aplikacija;
  2.  
  3. /**
  4.  * Created by gost on 25.3.2017..
  5.  */
  6. public class Tacka {
  7.     private int x;                  // pored promenljivih se mora pisati vidljivost
  8.     private int y;
  9.     public Tacka(int x, int y){     //izgleda da se ovde sve trpa u isti fajl!?!?
  10.         this.x=x;                   //-> postaje tacka...
  11.         this.y=y;
  12.     };
  13.  
  14.     public void setX(int x) {       // ovaj tip f-ja se zove setter,u kojima se dodatno moze-
  15.         this.x = x;                 // -kontrolisati unos
  16.     }
  17.  
  18.     public void setY(int y) {
  19.         this.y = y;
  20.     }
  21.    
  22.     public int getX() {             // ovi tipovi f-ja se zovu "geteri"...
  23.         return x;
  24.     }
  25.  
  26.     public int getY() {
  27.         return y;
  28.     }
  29.  
  30.     public void transliraj(int a) {     //f-ja koja povecava vrednost koordinata za 5
  31.         this.x+=a;
  32.         this.y+=a;
  33.     }
  34.  
  35.     public boolean uporedi(Tacka t){
  36.         return x==t.x && y==t.y;        //ne mora this.x !??!!?    
  37.     }
  38.  
  39.     public void ispisi(){
  40.         System.out.println("T (" + x + " , " + y + ")");        //ispisivanje tacke
  41.     }
  42.  
  43.    public double rastojanje(Tacka t){           // rastojanje izmedju tacaka
  44.         return Math.sqrt(Math.pow(x - t.x,2)+ Math.pow(y-t.y,2));  //Math biblioteka...
  45.     }
  46.  
  47. }
  48.  
  49.  
  50.  
  51.  
  52.  
  53. //method chain, (sve vraca objekte) - bad practise
  54.  
  55. ////////////////////////////////////////////////////////////////////////////
  56.  
  57. public class Main {
  58.     public static void main(String[] args) {        //skracenica za main je "psvm" -jetbrains
  59.         Tacka t1= new Tacka(3,5);                   //zvanje konstruktora
  60.         Tacka t2= new Tacka(0,0);
  61.         Tacka t3= new Tacka(-1,-5);
  62.  
  63.         t1.ispisi();
  64.         t2.ispisi();
  65.         t3.ispisi();
  66.  
  67.         double rastojanje = t1.rastojanje(t3);
  68.         System.out.println(rastojanje);
  69.  
  70.         Tacka[] niz = new Tacka[3];     // dodela prostora za niz tacaka...
  71.         niz[0] = new Tacka(2,3);
  72.         niz[1] = new Tacka(-1,1);
  73.         niz[2] = new Tacka(0,-2);
  74.  
  75.         System.out.println("For petlja");
  76.         for(int i = 0; i < niz.length; i++){    //vraca duzinu niza
  77.             niz[i].ispisi();
  78.         }
  79.  
  80.         System.out.println("For Each petlja");
  81.         for(Tacka t : niz){     //temp tacka t : niz kroz koji se prolazi
  82.             t.ispisi();
  83.         }
  84.     }
  85. }
  86.  
  87.  
  88. t1.getX(t2.getX());
  89.  
  90.  
  91. //////////////////////////////////////////////////////////////////////////////////////
  92.  
  93. package aplikacija;
  94.  
  95. /**
  96.  * Created by gost on 25.3.2017..
  97.  */
  98. public class Imaginarni {
  99.     private double re;
  100.     private double im;
  101.     public Imaginarni(double re, double im){
  102.  
  103.     }
  104.     public double moduo(Imaginarni t){
  105.         return Math.sqrt(Math.pow(t.re,2)+Math.pow(t.im,2));
  106.     }
  107.  
  108.     public double getRE(Imaginarni i){
  109.         return i.re;
  110.     };
  111.     public double getIM(Imaginarni i){
  112.         return i.im;
  113.     };
  114.  
  115.  
  116.     public Imaginarni saberi(Imaginarni i){
  117.         Imaginarni a;
  118.         a= new Imaginarni( this.re+=getRE(i),this.im+=getIM(i)); //mora se dinamicki dodeliti
  119.         return  a;
  120.     }
  121.  
  122.     public void ispisiim(Imaginarni i){
  123.         if(i.re > 0 & i.im > 0){
  124.             System.out.println("BROJ:" + i.im + " + " +i.re);
  125.         }
  126.         else if(i.re == 0 & i.im >0){
  127.             System.out.println("BROJ:" + i.re);
  128.         }
  129.         else if(i.re > 0 & i.im < 0){
  130.             System.out.println("BROJ:" + i.re " - " + i.im);
  131.         }
  132.         else
  133.         {
  134.             System.out.println("BROJ:" + i.im + "*i");
  135.         }
  136.     }
  137. }
Add Comment
Please, Sign In to add comment