0

Class java int and string print.

hey im doing a class on my vehicle and i want to put 1.8 and ltr inside the same output but its saying incompatible types. rough example... public class Mycar{ String cap; double engine; main... Mycar x = new Mycar(); System.out.println(x.engine = 1.8 + x.cap = " ltr"); i basicly want it to say 1.8 ltr, thanks

14th Jul 2017, 7:17 AM
D_Stark
D_Stark - avatar
4 Respuestas
+ 10
Sorry, I made a mistake. I confused it with how C# works. Java's default accessibility is similar to public. Private would make it so properties can only be used inside the class they're in. Objects wouldn't have access. Ex: If engine were private, engine would only be usable in MyCar's methods. You can see more about what private is used for in the Java lesson.
14th Jul 2017, 7:55 AM
Tamra
Tamra - avatar
+ 8
You need to assign x.engine and x.cap, and then you can output them. Also, engine and cap need to be public to be accessed (IIRC, they're private by default). Why you're getting "incompatible types" is because it thinks you're adding 1.8 and x.cap. //class MyCar public String cap; public double engine; //main x.engine = 1.8; x.cap = "ltr"; System.out.println(x.engine + " " + x.cap); //Output 1.8 ltr
14th Jul 2017, 7:24 AM
Tamra
Tamra - avatar
+ 1
thanks
14th Jul 2017, 9:01 PM
D_Stark
D_Stark - avatar
0
ooooh?? so what does it mean if there private? becuase i can still call them from main...
14th Jul 2017, 7:37 AM
D_Stark
D_Stark - avatar