0
Whats wrong in thus code?
import java.util.*; public class PTD { public static void main(String[] args) { SS obj1=SS(); Obj1.setHiddenX(20); Obj1.exposed = 200; System.out.println("hidden is"+" "+obj1.getHiddenX()+" "+"exposed="+" "+obj1.exposed(); } } class SS{ private int hiddenX; // Encapsulated public int exposed; // Not encapsulated public void setHiddenX(int newValueX) { hiddenX = newValueX + 5; } public int getHiddenX() { hiddenX = hiddenX + 5; return hiddenX; } }
4 Réponses
+ 2
- start a new object with the new keyword
SS obj1 = new SS();
- Obj1 and obj1 are different objects, java is case sensitive so you always need to write the variable name exactly the same
- the print statement is a bit messy and hard to read
You can try storing the values to make shorter, easier to read statements
int hid = obj1.getHiddenX();
int exVar = obj1.exposed;
int exMet = obj1.exposed();
System.out.print("hidden is " + hid +".");
System.out.print("exposed = " + exMet + ".");
Mistakes that are easy enough to debug.
Just read the error messages and it shows which line is wrong and what is wrong.
I cleaned it up so the code runs but I had to implement the exposed() method otherwise it wouldn't run.
https://code.sololearn.com/c3ETwOygbOWw/?ref=app
+ 1
1. obj1 is not the same as Obj1
2. SS() must be new SS()
3. exposed is not a method is a property
4. you must close the parenthesis for the println
+ 1
Thnkuu man HNNX 🐿
0
Thanks Gabriel Ilie