gitweb on Svarog
projekti pod git sistemom za održavanje verzija -- projects under the git version control system
summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 20f1ff1)
raw | patch | inline | side by side (parent: 20f1ff1)
author | Doni Pracner <quinnuendo@gmail.com> | |
Tue, 7 May 2019 22:47:24 +0000 (00:47 +0200) | ||
committer | Doni Pracner <quinnuendo@gmail.com> | |
Tue, 7 May 2019 22:47:24 +0000 (00:47 +0200) |
kodovi/skup/OpstiTipoviPrimeri.java | patch | blob | history |
index 26e96d3a2566d40ea34955ac3b426d6296ace140..4a812ae8985b7d6bff585b7399a1d0eef6ccab60 100644 (file)
-class Cuvar<T> {
- T info;
-
- public String toString(){
- return "Cuvam:" + info;
+public class OpstiTipoviPrimeri {
+
+ public static void main(String[] args) {
+ Par<String> pstr = new Par<String>("a", "b");
+ System.out.println(pstr);
+ Par<String> kop = pstr.kopija();
+ kop.zameni();
+ System.out.println(kop);
+
+ Par<Double> dd = new Par<>(2.0, 2.0);
+ System.out.println(
+ "u ovom paru su isti brojevi:" + dd.jednakiPrviIDrugi());
+
+ Par<Integer> pint = new Par<Integer>(2, 8);
+ System.out.println(pint);
+
+ Par<Integer> intovi = new Par<Integer>();
+ intovi.prviel = 2;
+ intovi.drugiel = 8;
+
+ System.out.println("jednaki su:" + pint.jednakSaParom(intovi));
}
-
- public Cuvar(){
- info = null;
+}
+
+class Par<T> {
+ // tip za polja je zadat parametrom uz ime klase
+ T prviel, drugiel;
+
+ public Par(T podatak1, T podatak2) {
+ this.prviel = podatak1;
+ this.drugiel = podatak2;
}
-
- public Cuvar(T podatak){
- this.info = podatak;
+
+ public Par() {
+ this.prviel = null;
+ this.drugiel = null;
}
-}
+ public String toString() {
+ return "Par (" + prviel + ", " + drugiel + ")";
+ }
-public class OpstiTipoviPrimeri {
+ public boolean jednakiPrviIDrugi() {
+ if (prviel == null)
+ return drugiel == null;
+ else
+ return prviel.equals(drugiel);
+ }
- public static void main(String[] args) {
- Cuvar<String> st = new Cuvar<String>();
- st.info = "neki string";
- System.out.println(st);
-
- Cuvar<Integer> broj = new Cuvar<Integer>();
- broj.info = 5;
- System.out.println(broj);
-
- Cuvar<Double> realan = new Cuvar<Double>(5.0);
- System.out.println(realan);
+ public void zameni() {
+ T pom = prviel;
+ prviel = drugiel;
+ drugiel = prviel;
+ }
+
+ public Par<T> kopija() {
+ return new Par<T>(prviel, drugiel);
+ }
+
+ public boolean jednakSaParom(Par<T> par2) {
+ if (par2 == null)
+ return false;
+ // moramo proveravati da li su null elementi, pa ima vise mogucnosti
+ if ((prviel == null) != (par2.prviel == null))
+ return false;
+ if (prviel != null && !prviel.equals(par2.prviel))
+ return false;
+ if ((drugiel == null) != (par2.drugiel == null))
+ return false;
+ if (drugiel != null && !drugiel.equals(par2.drugiel))
+ return false;
+
+ // nismo nasli razliku
+ return true;
}
}