#MOOC PART 5
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
@burnt star
@haughty kestrel by the if condition not evaluating to true.
Look at the condition
Don't mention random helpers please
i was doing the course with hhim
so i pinged him
out of alliance
:p
ah
so yeah
dosent that mean
that if it is not simple date go away
then it says to convert it into simple date
Yes?
well if it goes away how does it reach the next part?
isnt it supposed to return false
By being a SimpleDate.
see if it return false the whole method is done right
isnt it supposed to return true/false
if it isn't a SimpleDate, why would you want it to reach the next part ?
it only proceeds through the first if statement if the compared object is not an instance of the SimpleDate class
but if it is simpledate why convert it into simpel date again
which is the check, as everyone else mentioned
No, it's not because it's a SimpleDate that it's the same date. Hence the further checks.
Because you need to, well kinda
because you retrieve that data as an Object type
and you cannot access to the data inside directly without converting it to what it was made as
second loop we use it to check for simple date if it is simpeldate why we converting it again to simple date
such as, if you have a "String name" variable inside this compared object, without changing the type back to what it was you cannot reach the name variable
so you convert it and retrieve it agian 
Because while you, you know that it is a SimpleDate, java doesn't
yes, because you want to compare the other data inside this compared object in order to check if this object is exactly identical to the one that you are comparing it
such as, we can both be Students
but have different names
it first checks if you are even a student
so you need to convert it into its own type again to get the data in it?
yes
if you are indeed a student, the first if block doesn't activate, but if you are a teacher the first if block stops the program
fix java alathreon
It's logic
no logic
It's already fixed
java dumb add ai to it
if you are a student it continues to check if you have the same name and such, but in order to check your name it needs to change you from Object, back to Student
because the name variable itself is in class Student
thats roughly the idea
hmm
The if just tells java if it should go in its branch or not
The variable type won't magically change
just wanted to knwo why we switch it to its own type after knwing its the type we want
Because we can ?
I think the reason why it comes as Object and not Student is because the classes could be typecasted to something else that they inherit one way or another
animal cat we know but we again define it under animal
we can nuke the world?
you misunderstand
such as, you could cast a Student, Teacher, President, Janitor to Human if you implemented it
so its better to just take it as Object
because Object is implicitly extended by all other, well, objects
You want to go to the toilet
you have an if(!isToiletFree()) return;
but if it doesn't return, it means that you know that you can go to the toilet, but you still need to gotToToilet();
The if just gives you info that you can do it
then you use this info
my only doubt is why the second comparison after have checked it is the type we want
to do what you want
like we know its a cat at the start by using the if
wdym, which comparaison ?
We are making it a cat once
your name is mirza, mine is hirayui, how can the program know that we are not the same user without comparing something that we have different, if we are both users?
@haughty kestrel
The if just gives you information, it doesn't change it into a cat
see this
isnt it alr a cat or does it change its type when it gets inputted as an object
does it get changed to object?
It was always a cat
so when we give it as an parameter it turns into object type?
and we need to make it simple date again?
but you first need to check if it is actually a cat
and if it is, you tell java, "well that's a cat"
because the parameter's type is Object
yes
bring the cat as an dinasour check if it is a cat orginally if yes then convert into a cat?
damn
i get it now
compare the object type date into
simpledate type date
damn i annoyed yall
dw
but...
if its imported as object type then how we seeing the compared type
whatever
that has to do with inheritance I think
oo
did you learn anything about it?
instanceof
i remember it kinda
bro writes instanceof as if iam an expert in java and i can understand what he means
- let's stay polite
- It's documented in the course, and the keyword does kind of say it.
instance of. To put it alternatively: what do you thinkmirza instanceof Humanresults in?
so as an example off the top of my head, if you have a class Teacher that implements Employee, you can both do Teacher mirza = new Teacher(); and Employee mirza = new Teacher();
but the object saved in the memory with an id is still a new Teacher();
even though you can also put it in a variable with the type Employee
so if I am not mistaken, instanceOf just checks what is saved in the memory for that variable, therefore it can see and compare what it actually was
that iam a human
if that makes sense to you
sure
So you would be passed in as an Object in the above example. The same logic applies.
but this is my own interpretation
thx
simon do you think my example is bad, mistaken?
I think I'd have started by explaining what instanceof does, since there the confusion seemingly started.
@haughty kestrel variables have a name and type
the type is like a label, which hints at what is inside the variable
even if you check the actual object inside the variable, the type won't magically change, you need to explicitly create a new variable with the correct type
@haughty kestrel here
public class Person {
private String name;
private int age;
private int weight;
private int height;
// constructors and methods
public boolean equals(Object compared) {
// if the variables are located in the same position, they are equal
if (this == compared) {
return true;
}
// if the compared object is not of type Person, the objects are not equal
if (!(compared instanceof Person)) {
return false;
}
// convert the object into a Person object
Person comparedPerson = (Person) compared;
// if the values of the object variables are equal, the objects are equal
if (this.name.equals(comparedPerson.name) &&
this.age == comparedPerson.age &&
this.weight == comparedPerson.weight &&
this.height == comparedPerson.height) {
return true;
}
// otherwise the objects are not equal
return false;
}
Also don't omit @Override
@Override
public boolean equals(Object compared) {
Also
any
if(foo) {
return true;
} else {
return false
}
can be simplified into
return foo;
So instead of this
if (this.name.equals(comparedPerson.name) &&
this.age == comparedPerson.age &&
this.weight == comparedPerson.weight &&
this.height == comparedPerson.height) {
return true;
}
// otherwise the objects are not equal
return false;
You can write this
return this.name.equals(comparedPerson.name) &&
this.age == comparedPerson.age &&
this.weight == comparedPerson.weight &&
this.height == comparedPerson.height;
public boolean equals(Object compared) {
// if the variables are located in the same position, they are equal
if (this == compared) {
return true;
}
// if the compared object is not of type Person, the objects are not equal
if (!(compared instanceof Person comparedPerson)) {
return false;
}
// if the values of the object variables are equal, the objects are equal
if (this.name.equals(comparedPerson.name) &&
this.age == comparedPerson.age &&
this.weight == comparedPerson.weight &&
this.height == comparedPerson.height) {
return true;
}
// otherwise the objects are not equal
return false;
}
Also you can do this
You can instanceof and cast at the same time