#MOOC PART 5

1 messages · Page 1 of 1 (latest)

haughty kestrel
#

This is an example problem under Comparing the equality of objects (equals) and I have an doubt.

If it returns false already how does it go to the next part

Help?

dapper bayBOT
#

<@&987246399047479336> please have a look, thanks.

haughty kestrel
#

@burnt star

hexed drum
#

@haughty kestrel by the if condition not evaluating to true.

surreal blade
haughty kestrel
#

so i pinged him

#

out of alliance

#

:p

surreal blade
#

ah

haughty kestrel
#

dosent that mean

#

that if it is not simple date go away

#

then it says to convert it into simple date

hexed drum
#

Yes?

haughty kestrel
#

isnt it supposed to return false

hexed drum
#

By being a SimpleDate.

burnt star
#

dont worry

haughty kestrel
haughty kestrel
#

isnt it supposed to return true/false

surreal blade
burnt star
#

it only proceeds through the first if statement if the compared object is not an instance of the SimpleDate class

haughty kestrel
burnt star
#

which is the check, as everyone else mentioned

hexed drum
#

No, it's not because it's a SimpleDate that it's the same date. Hence the further checks.

surreal blade
burnt star
#

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

haughty kestrel
#

second loop we use it to check for simple date if it is simpeldate why we converting it again to simple date

burnt star
#

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

haughty kestrel
surreal blade
burnt star
#

such as, we can both be Students

#

but have different names

#

it first checks if you are even a student

haughty kestrel
#

so you need to convert it into its own type again to get the data in it?

haughty kestrel
#

werid

#

wonder why

burnt star
#

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

haughty kestrel
#

fix java alathreon

surreal blade
haughty kestrel
#

no logic

surreal blade
haughty kestrel
#

java dumb add ai to it

burnt star
#

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

haughty kestrel
#

hmm

surreal blade
haughty kestrel
#

just wanted to knwo why we switch it to its own type after knwing its the type we want

burnt star
#

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

haughty kestrel
#

animal cat we know but we again define it under animal

haughty kestrel
surreal blade
burnt star
#

such as, you could cast a Student, Teacher, President, Janitor to Human if you implemented it

haughty kestrel
#

i think its deeper into java smth

#

hmm

burnt star
#

so its better to just take it as Object

#

because Object is implicitly extended by all other, well, objects

haughty kestrel
#

aight

#

stop right here

#

can we start from the start?

burnt star
#

I didn't word that well

#

XD

surreal blade
# haughty kestrel we can nuke the world?

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

haughty kestrel
#

my only doubt is why the second comparison after have checked it is the type we want

surreal blade
#

to do what you want

haughty kestrel
#

like we know its a cat at the start by using the if

haughty kestrel
#

but why we making it a cat again

#

gimme a sec

surreal blade
burnt star
haughty kestrel
surreal blade
haughty kestrel
#

isnt it alr a cat or does it change its type when it gets inputted as an object

#

does it get changed to object?

surreal blade
#

It was always a cat

haughty kestrel
#

so when we give it as an parameter it turns into object type?

#

and we need to make it simple date again?

surreal blade
#

but you first need to check if it is actually a cat
and if it is, you tell java, "well that's a cat"

burnt star
#

yes

haughty kestrel
#

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

burnt star
#

dw

haughty kestrel
#

if its imported as object type then how we seeing the compared type

#

whatever

burnt star
#

that has to do with inheritance I think

haughty kestrel
#

oo

burnt star
#

did you learn anything about it?

haughty kestrel
#

not yet

#

but in uniyes

#

inherit the things from diff clases

haughty kestrel
#

i remember it kinda

haughty kestrel
hexed drum
burnt star
#

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

burnt star
#

if that makes sense to you

haughty kestrel
#

sure

hexed drum
burnt star
#

but this is my own interpretation

haughty kestrel
#

thx

burnt star
hexed drum
surreal blade
#

@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

surreal blade
#

@haughty kestrel here

haughty kestrel
#
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;
    }
surreal blade
#

    @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