+ 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(); } }
10 Antworten
+ 2
You forgot to write a constructor...😉
and i would suggest you put the variables as a private and the write set & get functions.
+ 3
And also you didn't put v.color in syso, so of course it didn't print you "red"
+ 1
I didn't type this code its example from solo learn
Kfirwe
+ 1
System.out.println(v1.color);
+ 1
Not work zemiak
+ 1
Would you pls type the correct code here so i copy it in code playground
+ 1
copy that line after v1.color="red";
0
it will works in main where you has v1 object
v1.color="red";
System.out.println(v1.color);
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();
}
}