+ 2

Unable to print red

public class Vehicle { int maxSpeed; int wheels; String color; double fuelCapacity; void horn() { System.out.println("Beep!"); } } class MyClass { public static void main(String[ ] args) { Vehicle v1 = new Vehicle(); Vehicle v2 = new Vehicle(); v1.color = "red"; v2.horn(); } }

10th Sep 2019, 2:20 PM
Mena John
Mena John - avatar
10 Answers
+ 2
You forgot to write a constructor...😉 and i would suggest you put the variables as a private and the write set & get functions.
10th Sep 2019, 2:25 PM
KfirWe
KfirWe - avatar
+ 3
And also you didn't put v.color in syso, so of course it didn't print you "red"
10th Sep 2019, 2:36 PM
KfirWe
KfirWe - avatar
+ 1
I didn't type this code its example from solo learn Kfirwe
10th Sep 2019, 3:56 PM
Mena John
Mena John - avatar
10th Sep 2019, 4:00 PM
Mena John
Mena John - avatar
+ 1
System.out.println(v1.color);
10th Sep 2019, 8:23 PM
zemiak
+ 1
Not work zemiak
10th Sep 2019, 8:35 PM
Mena John
Mena John - avatar
+ 1
Would you pls type the correct code here so i copy it in code playground
10th Sep 2019, 8:41 PM
Mena John
Mena John - avatar
+ 1
copy that line after v1.color="red";
10th Sep 2019, 8:43 PM
zemiak
0
it will works in main where you has v1 object v1.color="red"; System.out.println(v1.color);
10th Sep 2019, 8:39 PM
zemiak
0
Unable to print red public class Vehicle { int maxSpeed; int wheels; String color; double fuelCapacity; void horn() { System.out.println("Beep!"); } } class MyClass { public static void main(String[ ] args) { Vehicle v1 = new Vehicle(); Vehicle v2 = new Vehicle(); v1.color = "red"; System.out.println(v1.color); v2.horn(); } }
11th Sep 2019, 1:32 AM
Anubhav Ajmera
Anubhav Ajmera - avatar