Forum » Programiranje » problemi z javo
problemi z javo
kretze ::
Dober dan pišem vam ker imam probleme z java programiranjem...naredil sem program ki bi moral delat ampak iz nekega razloga ne dela pa me zanima če mi kdo ve povedat zakaj...naredil sem class stock...v glavnem programu sem naredil 2 stocka(s1,s2) jo enačil(z equals) vendar mi vedno napiše da sta "diferent" moralo pa bi pisat da sta "same"
class stock
glavni "main" program
class stock
package javap35.stockcompare; public class stock { private String symb; private double Price; public stock(String s,double p) { symb=s; Price=p; } public String getSymbol() { return symb; } public double getPrice() { return Price; } @Override public String toString() { String str="trading symbol: "+symb+ "\nShare price "+Price; return str; } }
glavni "main" program
public class JavaP35StockCompare { public static void main(String[] args) { stock s1=new stock("XYZ",9.66); stock s2=new stock("XYZ",9.66); System.out.println(s1); System.out.println(s2); if(s1.equals(s2)) { System.out.print("stock are same"); System.out.print("good"); } else { System.out.print("stock are te diferent\n"); System.out.print("bad"); } } }
Wrop ::
Saj sta različna. To da imata enake vrednosti, še ne pomeni, da sta pri klicanju metode "equals" enaka.
V razredu "stock" dodaj še metodo equals in tam naredi primerjavo spremenljivk "symb" in "price" ter ustrezno vrni true oz. false.
V razredu "stock" dodaj še metodo equals in tam naredi primerjavo spremenljivk "symb" in "price" ter ustrezno vrni true oz. false.
Genetic ::
Kdaj pa sta dva objekta razreda stock enaka? Kako naj program to ve? Kaj dela metoda equals razreda Object? Zakaj si overridal metodo toString, če pa že obstaja v razredu Object?
predi ::
The Java Tutorials: Object as a Superclass (večkrat preberi celo poglavje).
Java practices: Implementing equals.
Pomaga ti lahko tudi poljuben IDE za javo: desni klik na code editor v classu brez equals(), in potem "Insert Code... > equals() and hashCode()" (ali nekaj v tej smeri), ampak s to bližnjico se ne boš ničesar naučil.
Java practices: Implementing equals.
Pomaga ti lahko tudi poljuben IDE za javo: desni klik na code editor v classu brez equals(), in potem "Insert Code... > equals() and hashCode()" (ali nekaj v tej smeri), ampak s to bližnjico se ne boš ničesar naučil.
HotBurek ::
Modificiran sample, če ti bo kaj v pomoč...
public class Main { public static class stock { private String Symbol; private double Price; stock(String s, double p) { this.Symbol = s; this.Price = p; } String printAll() { return "SYM=" + this.Symbol + "|price=" + this.Price; } int compare(stock ss){ int eq = 0; if (this.Symbol.equals(ss.Symbol)) { eq += 1; } if (this.Price == ss.Price) { eq += 2; } return eq; } } public static void main(String[] args) { stock s1 = new stock("XYZa",9.66); stock s2 = new stock("XYZ",9.66); System.out.print("\n"); System.out.print(s1.printAll() + "\n"); System.out.print(s2.printAll() + "\n"); System.out.print("\n"); if(s1.compare(s2) == 0) { System.out.print("Stock symbols AND prices are different\n"); System.out.print(s1.Symbol + " != " + s2.Symbol + "\n"); System.out.print(s1.Price + " != " + s2.Price + "\n"); } else if (s1.compare(s2) == 1) { System.out.print("Stock symbols are the same but prices are different\n"); System.out.print(s1.Symbol + " == " + s2.Symbol + "\n"); System.out.print(s1.Price + " != " + s2.Price + "\n"); } else if (s1.compare(s2) == 2) { System.out.print("Stock symbols are different but prices are the same\n"); System.out.print(s1.Symbol + " != " + s2.Symbol + "\n"); System.out.print(s1.Price + " == " + s2.Price + "\n"); } else if (s1.compare(s2) == 3) { System.out.print("Stock symbols AND prices are the same\n"); System.out.print(s1.Symbol + " == " + s2.Symbol + "\n"); System.out.print(s1.Price + " == " + s2.Price + "\n"); } } }
root@debian:/# iptraf-ng
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window
Zgodovina sprememb…
- spremenilo: HotBurek ()
MrStein ::
Dober dan pišem vam ker imam probleme z java programiranjem...naredil sem program ki bi moral delat ampak iz nekega razloga ne dela pa me zanima če mi kdo ve povedat zakaj...naredil sem class stock...v glavnem programu sem naredil 2 stocka(s1,s2) jo enačil(z equals) vendar mi vedno napiše da sta "diferent" moralo pa bi pisat da sta "same"
To kliče tole metodo (iz Object.java):
public boolean equals(Object obj) { return (this == obj); }
Če še vedno ni razumljivo zakaj ne dela po pričakovanju, vprašaj.
Motiti se je človeško.
Motiti se pogosto je neumno.
Vztrajati pri zmoti je... oh, pozdravljen!
Motiti se pogosto je neumno.
Vztrajati pri zmoti je... oh, pozdravljen!
Vredno ogleda ...
Tema | Ogledi | Zadnje sporočilo | |
---|---|---|---|
Tema | Ogledi | Zadnje sporočilo | |
» | [Java] Prevajanje in šumniki v ubuntuOddelek: Programiranje | 2504 (2121) | mmaestro |
⊘ | Pomoč pri Domači nalogiOddelek: Programiranje | 4570 (3418) | Gandalfar |
» | [Java]Pri obratu stevila mi prva cifra zgineOddelek: Programiranje | 1123 (944) | Robi182 |
» | [java] napakaOddelek: Programiranje | 1605 (1605) | tx-z |
» | Vmesnik v JaviOddelek: Programiranje | 2276 (2059) | Camel |