Boby Gandhi Boby Gandhi - 14 days ago 6
Java Question

Whats wrong with my getter and setter code?

im a begginer and other getter and setter questions here have not helped my identify what im missing so im asking a new question. I havent done anything to the code yet(i.e print it out or something) i've only tried to use getters and setters. But it wont compile. What am i missing?`

class gallery{
public static void main(String[] args) {

painting a = new painting();
a = setPaintingLength1(a, 81.3);
a = setPaintingWidth1(a, 59.7);
a = setPaintingYear1(a, 1879);
a = setPaintingDirection1(a, "ahead of you");
a = setPaintingArtist1(a, "Mary Cassatt");
a = setPaintingName1(a, "Women with a Pearl Necklace in a Loge");

System.exit(0);
}//end main method




public static painting setPaintingLength1(painting a, double l1) {
a.length1 = l1;
return a;
}

public static double getPaintingLength1(painting a){
return a.length1;
}

public static painting setPaintingWidth1(painting a, double w1) {
a.width1 = w1;
return a;
}

public static double getPaintingWidth1(painting a){
return a.width1;
}

public static painting setPaintingYear1(painting a, int y1) {
a.year1 = y1;
return a;
}

public static int getPaintingYear1(painting a){
return a.year1;
}

public static painting setPaintingDirection1(painting a, String d1) {
a.direction1 = d1;
return a;
}

public static String getPaintingDirection1(painting a){
return a.direction1;
}

public static painting setPaintingArtist1(painting a, String art1) {
a.artist1 = art1;
return a;
}

public static String getPaintingArtist1(painting a){
return a.artist1;
}

public static painting setPaintingName1(painting a, String n1) {
a.name1 = n1;
return a;
}

public static String getPaintingName1(painting a){
return a.name1;
}






}//end class gallery

class painting{

int length;
int width;
int year;
String direction;
String artist;
String name;

}//end class painting

Answer

No need for System.exit in main method.

class gallery {
    public static void main(String[] args) {
        painting a = new painting();

        a.setPaintingLength1(81.3);
        a.setPaintingWidth1(59.7);
        a.setPaintingYear1(1879);
        a.setPaintingDirection1("ahead of you");
        a.setPaintingArtist1("Mary Cassatt");
        a.setPaintingName1("Women with a Pearl Necklace in a Loge");
    }
}
  • painting class should have all relevant methods for it.
  • a getter always takes 0 argument and returns 1 thing.
  • a setter always takes 1 argument and returns 0 thing.
  • variables used inside getters and setters are usually private (that's why you use getter and setter for them)

So it could be written as...

class painting {
    private int length;
    private int width;
    private int year;
    private String direction;
    private String artist;
    private String name;

    public void setPaintingLength1(double l1) {
        length1 = l1;
    }

    public double getPaintingLength1() {
        return length1;
    }

    public void setPaintingWidth1(double w1) {
        width1 = w1;
    }

    public double getPaintingWidth1() {
        return width1;
    }

    public void setPaintingYear1(int y1) {
        year1 = y1;
    }

    public int getPaintingYear1() {
        return year1;
    }

    public void setPaintingDirection1(String d1) {
        direction1 = d1;
    }

    public String getPaintingDirection1() {
        return direction1;
    }

    public void setPaintingArtist1(String art1) {
        artist1 = art1;
    }

    public String getPaintingArtist1() {
        return artist1;
    }

    public void setPaintingName1(String n1) {
        name1 = n1;
    }

    public String getPaintingName1() {
        return name1;
    }
}