Click here to Skip to main content
15,895,142 members
Home / Discussions / Java
   

Java

 
AnswerRe: Multilevelqueue Pin
Richard MacCutchan5-Oct-11 21:49
mveRichard MacCutchan5-Oct-11 21:49 
AnswerRe: Multilevelqueue Pin
Nagy Vilmos5-Oct-11 22:35
professionalNagy Vilmos5-Oct-11 22:35 
QuestionTroubble with "new" Pin
Tor Danielsen4-Oct-11 11:00
Tor Danielsen4-Oct-11 11:00 
AnswerRe: Troubble with "new" Pin
DaveAuld4-Oct-11 12:03
professionalDaveAuld4-Oct-11 12:03 
GeneralRe: Troubble with "new" Pin
Tor Danielsen4-Oct-11 12:28
Tor Danielsen4-Oct-11 12:28 
GeneralRe: Troubble with "new" Pin
DaveAuld4-Oct-11 13:37
professionalDaveAuld4-Oct-11 13:37 
AnswerRe: Troubble with "new" Pin
Luc Pattyn4-Oct-11 13:40
sitebuilderLuc Pattyn4-Oct-11 13:40 
AnswerRe: Troubble with "new" Pin
Nagy Vilmos4-Oct-11 21:47
professionalNagy Vilmos4-Oct-11 21:47 
A slightly modified version of your program:
Java
public class Bil1{
	private final String regNr;
	private final String merke;
	private final int årsmodell;
	private final int hastighet;
    private final boolean motorenIGang;
 
    public Bil1() {
        this (null, null, 0, 0 false);
    }

    public Bil1(String regNr, String merke, int årsmodell, int hastighet, boolean motorenIGang){
        this.regNr = regNr;
        this.merke = merke;
        this.årsmodell = årsmodell;
        this.hastighet = hastighet;
        this.motorenIGang = motorenIGang;
    }
 
    public String getRegNr() {
        if (this.regNr == null) {
            return "Ukjent";
        }
        return this.regNr;
    }
 
    public String getMerke() {
        if (this.merke == null) {
            return "Ukjent";
        }
        return this.merke;
    }
 
    public int getÅrsmodell() {
        return this.årsmodell;
    }
 
    public int getHastighet() {
        return this.hastighet;
    }
 
    public boolean getMotorenIGang() {
        return this.motorenIGang;
    }
    
    public String toString() {
        return "Regnr " + this.getRegNr()
        + ", merke " + this.getMerke()
        + ", årsmodell " + this.getÅrsmodell()
        + ", hastighet " + this.getHastighet()
        + ", motorenIGang " + this.getMotorenIGang()
    }

    public int hashCode() {
        return 7 * ((this.getRegNr().hashCode()  +
                9 * this.getMerke()) +
                3 * this.getÅrsmodell()) +
                this.getHastighet() +
                (this.getMotorenIGang()    ? 1 : 0);
    }
        
    public boolean equals(Object obj) {
        if (obj == null || obj.getClass() != this.getClass()) {
            return null();
        }
        Bil1 that = (Bil1)obj;
        
        return this.getRegNr().equals(that.getRegNr()) &&
                this.getMerke().equals(that.getMerke()) &&
                this.getÅrsmodell() == that.getÅrsmodell() &&
                this.getHastighet() == that.getHastighet() &&
                this.getMotorenIGang() == that.getMotorenIGang();
    }
}
 

public class Bil1Test {
  public static void main(String[] args) {
 
    /* 1. programsetning: Lager et Bil1-objekt (med referanse) minBil
     vha. standardkonstruktøren: */
    Bil1 minBil = new Bil1("VD-12345", "Volvo", 2002, 55, true);
 
    /* 2. programsetning: Skriver ut innholdet i minBils objektvariabler: */
    System.out.println(minBill);
  }
}


Now, the lessons are:
- Member variables should be private. Just don't go any other way without very good reason, you don't have one.
- Try to have all constructors lead to a single one with all - or most - of the logic. If you wish to change the way it is initialising the class, then only one place needs top be changed.
- Internally use methods rather than variables. It is possible that you need to change the internal representation of a member variable but not the method. This ensures that the methods always get what is expected - design by contract - as you'll see from the way I've changed the methods for make and model.
- If you need to describe an object, let the object do it. Every class should override toString, equals, and hashCode.


Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

AnswerRe: Troubble with "new" Pin
Richard MacCutchan4-Oct-11 21:50
mveRichard MacCutchan4-Oct-11 21:50 
AnswerRe: Troubble with "new" Pin
TorstenH.4-Oct-11 23:57
TorstenH.4-Oct-11 23:57 
GeneralRe: Troubble with "new" Pin
Nagy Vilmos5-Oct-11 0:12
professionalNagy Vilmos5-Oct-11 0:12 
GeneralRe: Troubble with "new" Pin
Richard MacCutchan5-Oct-11 0:20
mveRichard MacCutchan5-Oct-11 0:20 
GeneralRe: Troubble with "new" Pin
TorstenH.5-Oct-11 4:08
TorstenH.5-Oct-11 4:08 
GeneralRe: Troubble with "new" Pin
Richard MacCutchan5-Oct-11 4:37
mveRichard MacCutchan5-Oct-11 4:37 
GeneralRe: Troubble with "new" Pin
David Skelly5-Oct-11 3:41
David Skelly5-Oct-11 3:41 
GeneralRe: Troubble with "new" Pin
TorstenH.5-Oct-11 4:06
TorstenH.5-Oct-11 4:06 
GeneralRe: Troubble with "new" Pin
David Skelly5-Oct-11 22:17
David Skelly5-Oct-11 22:17 
GeneralRe: Troubble with "new" Pin
Nagy Vilmos5-Oct-11 22:29
professionalNagy Vilmos5-Oct-11 22:29 
AnswerRe: Troubble with "new" Pin
chalspaul6-Oct-11 4:41
chalspaul6-Oct-11 4:41 
QuestionJava Based Search Service in Oracle CC&B Pin
BalajiGuna3-Oct-11 21:56
BalajiGuna3-Oct-11 21:56 
AnswerRe: Java Based Search Service in Oracle CC&B Pin
Nagy Vilmos3-Oct-11 22:11
professionalNagy Vilmos3-Oct-11 22:11 
Questionpoker application Pin
moises242-Oct-11 20:36
moises242-Oct-11 20:36 
AnswerRe: poker application Pin
Richard MacCutchan2-Oct-11 21:44
mveRichard MacCutchan2-Oct-11 21:44 
AnswerRe: poker application Pin
CodingLover2-Oct-11 23:59
CodingLover2-Oct-11 23:59 
Questionerror: cannot in coding Pin
Iswandi Abdul Rahman30-Sep-11 12:56
Iswandi Abdul Rahman30-Sep-11 12:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.