+ 2

Getter Setter problem ( I don't understand what is wrong)

public class Vehicle { private String color; // Getter public String getColor() { return color; } // Setter public void setColor(String cukor) { this.color = cukor; System.out.println("1" + color + "1"); System.out.println("2" + cukor + "2"); System.out.println("3" + this.color + "3"); } } class Program { public static void main(String[ ] args) { Vehicle v1 = ne

9th Mar 2018, 10:06 PM
Erik Horvath
Erik Horvath - avatar
8 odpowiedzi
+ 8
Each object has its own set of properties (in this case, each Vehicle has a Color), and they can be set independently. If you want both objects to have the same color, they have to both be set with the same value separately. If you want more colors for each Vehicle, you will need more class variables, and getters/setters for each. It's in my last post - try it out!
10th Mar 2018, 3:00 PM
Tamra
Tamra - avatar
+ 7
You seem to be trying to store 3 different colors. You'll need separate getters and setters for each color. this.color and color are the same in setColor(). "this" is used to specify that that you're using the variable for the current class (in this case, you're using the "color" class variable in Vehicle). "cukor" is the value you're sending into the setter, and is temporary (it only lasts while setColor() runs). In the setter, you assign the value in "cukor" to "color". When you output them, you use "this.color" and "color" (which are the same value), and "cukor" (which also has the same value). So you are using the setter correctly in order to change the value, but if you want more values simultaneously, you need a variable for each. Ex: get/setColor1(), get/setColor2(), get/setColor3() Or, you could use an array of colors, which would look like this: private string[] Colors = new String[2]; public void setColor(string color, int index) { Colors[index] = color; } public string getColor(int index) { return Color[index]; }
9th Mar 2018, 11:22 PM
Tamra
Tamra - avatar
+ 5
link to a verson of the code in the description https://code.sololearn.com/cjYpLPS5i7Qj/?ref=app
10th Mar 2018, 1:44 AM
Manual
Manual - avatar
+ 3
If you don't need more than 1 color for a single object, there's no need for a color2.
10th Mar 2018, 8:46 PM
Tamra
Tamra - avatar
+ 2
Hello! https://code.sololearn.com/cEgvve08feAv/#java if i have two object: v1 and v2 and color is deafult red but i changed it with v2 to green why not change also v1 to green.? if i want use more color i should use more getter/setter? (but example doesn't do this)? thank you
10th Mar 2018, 12:26 PM
Erik Horvath
Erik Horvath - avatar
+ 2
Sorry my stupid question. But how can i see your last post? What do you mean? this ? get/setColor1(), get/setColor2(), get/setColor3() Or, you could use an array of colors, which would look like this: private string[] Colors = new String[2]; public void setColor(string color, int index) { Colors[index] = color; } public string getColor(int index) { return Color[index]; }
10th Mar 2018, 3:12 PM
Erik Horvath
Erik Horvath - avatar
+ 2
Hello! I made my code but i don't understand why i need color2? I use only the color and give that to object what i want? https://code.sololearn.com/czsRQhb6kHx0/#java Thank you
10th Mar 2018, 8:26 PM
Erik Horvath
Erik Horvath - avatar
+ 1
Thanks for your answer. These were very usufull and understandable.
10th Mar 2018, 10:50 AM
Erik Horvath
Erik Horvath - avatar