#Help with Inheritance, Interface, class, objects

88 messages · Page 1 of 1 (latest)

unique geode
#

Please help

My code:

    
    private int age;
    private String name;
    
    public Person(String name,int age) {

        if(age<=0 || name.isEmpty() || name==null) {
            throw new IllegalArgumentException();}
        this.age = age;
        this.name = name;}
    
    @Override
    public int compareTo(Human h) {
       
        if(h.getAge()>this.age)
            return -1;
        
        if(h.getAge()<this.age)
            return 1;
        
        return 0;
    }
   
    public boolean equlas(Object o) {
        Human h=(Human)o;
        return (h.getAge()==this.age)&&(h.getName().equalsIgnoreCase(this.name));
    }
   
    @Override
    public int getAge() {
        
        return 0;
    }
    @Override
    public String getName() {
        
        return null;
    }
    
    @Override
    public String toString() {
        return "Person:[" + name + ", " + age + "]";
    }
}```
unique geode
#

anyoneeeeeeee

full linden
#

Your code doesn't even compile, you know

unique geode
#

yes which is why i need help

#

private int age;
private String name;

public Person(String name,int age) {

        if(age<=0 || name.isEmpty() || name==null) {
        throw new IllegalArgumentException();
        }

        this.age = age;
        this.name = name;

        }

@Override
public int compareTo(Human h) {

        if(h.getAge()>this.age)
        return -1;

        else if(h.getAge()<this.age)
        return 1;

        else
        return 0;
        }

public boolean equlas(Object o) {
        Human h=(Human)o;
        return (h.getAge()==this.age)&&(h.getName().equalsIgnoreCase(this.name));
        }

        /*public boolean equals(Object o) {
        Human h=(Human)o;
        if((h.getAge()==this.age)&&(h.getName()==this.name))
        return true;

        else
                return false;
        }*/

@Override
public int getAge() {

        return 0;
        }
@Override
public String getName() {

        return null;
        }

@Override
public String toString() {
        return "Person:[" + name + ", " + age + "]";
        }
        }```
#

new code

full linden
#

First reflex when things aren't like you want them to be: you format and indent correctly

unique geode
#

okay

#

is this not good

#

im really confused man

full linden
#

? Just show your code again, correctly indented and, if needs be, correctly formatted

unique geode
#
private int age;
private String name;

public Person(String name,int age) {

        if(age<=0 || name.isEmpty() || name==null) {
                
        throw new IllegalArgumentException();
        
    }

        this.age = age;
        this.name = name;

}

@Override
public int compareTo(Human h) {

        if(h.getAge()>this.age)
        return -1;

        else if(h.getAge()<this.age)
        return 1;

        else
        return 0;
}

public boolean equals(Object o) {
        
        Human h=(Human)o;
        return (h.getAge()==this.age)&&(h.getName().equalsIgnoreCase(this.name));
        
}

        /*public boolean equals(Object o) {
        Human h=(Human)o;
        if((h.getAge()==this.age)&&(h.getName()==this.name))
        return true;

        else
                return false;
        }*/

@Override
public int getAge() {

        return 0;
    }
@Override
public String getName() {

        return null;
        }

@Override
public String toString() {
        
        return "Person:[" + name + ", " + age + "]";
    }
}```
#

is this not properly indented?

full linden
#

Does it look properly indented?

unique geode
#

to me it does idk looool

full linden
#

Okay, let's use your teacher's standards rather than yours

unique geode
#

okkay

#

wait what does that mean

#

i mean ngl i really need the question done yk like the indenting not toooo important

full linden
#

Beginners don't understand that, but code that looks bad complicates doing stuff greatly

#

As you're a beginner you don't need complications, you need to make things easier for yourself

#

Also indenting correctly costs nothing, so you could just do it whenever you feel like at no cost, rather than discussing against it

#

So, in the end, you should indent your code correctly because otherwise you won't get anywhere.

unique geode
#

you are right i know that

#

im just under time pressure

full linden
#

Then don't waste it and correct indentation at all times it could be possible to. Like I already reminded, that costs nothing and notably no time, so really no reason not to save time by doing it

unique geode
#

okay

#

so how do i correctly indent??

full linden
#

By making it so it's not badly indented, come on, you already saw code.

unique geode
#

what bro im so confused

full linden
#

Also your IDE can do it for you

unique geode
#

i think its indente d correcgtly

#

ur saying its noit

full linden
#

Then show it

unique geode
#

so can u tell me how to do it proper;y

#

this is from my ide lmao

full linden
#

Yes, you shouldn't have modified what your IDE does

unique geode
#

this isnt modiefiedddddddd

full linden
#

So make it format your code and stop discussing

unique geode
#

its straight ide

#

??????

#

what do u mean???

#

i didnt modify it i took it straight from the ide

full linden
#

Yes obviously you didn't take it from your dishwasher, what I mean is that when you type normally, your IDE indents correctly

unique geode
#

yes

#

tjhat is what i did

full linden
#

It can also re-do it entirely for you

unique geode
#

and this si the resulting format

#

??

full linden
#

What IDE are you using?

unique geode
#

intellij

full linden
#

Then no, it doesn't do that

dawn bough
#

CTRL + ALT + L

full linden
#

When the person literally denies that they undid what the IDE did in the first place, there is no much you can do until they come back by themselves after losing their will to play games

unique geode
#
        private int age;
        private String name;
        
        public Person(String name, int age) {
                if(age<=0 || name.isEmpty() || name==null) {
                        throw new IllegalArgumentException();
                        
                }
                this.age = age;
                this.name = name;
        }
        
        @Override
        public int compareTo(Human h) {
                if(h.getAge()>this.age)
                        return -1;
                
                else if(h.getAge()<this.age)
                        return 1;
                
                else
                        return 0;
        }
        
        public boolean equals(Object o) {
                Human h = (Human)o;
                return (h.getAge()==this.age) && (h.getName().equalsIgnoreCase(this.name));
        }
        
        /*public boolean equals(Object o) {
        Human h=(Human)o;
        if((h.getAge()==this.age)&&(h.getName()==this.name))
        return true;

        else
                return false;
        }*/
        
        @Override 
        public int getAge() {
                return 0;
        }
        
        @Override 
        public String getName() {
                return 0;
        }
        
        @Override
        public String toString() {
                return "Person:[" + name + ", " + age + "]";
        }
}```
#

wait no

full linden
#

Unfortunately, this is a case where the class declaration is better mentioned

unique geode
#

        private int age;
        private String name;

        public Person(String name, int age) {
                if(age<=0 || name.isEmpty() || name==null) {
                        throw new IllegalArgumentException();

                }
                this.age = age;
                this.name = name;
        }

        @Override
        public int compareTo(Human h) {
                if(h.getAge()>this.age)
                        return -1;

                else if(h.getAge()<this.age)
                        return 1;

                else
                        return 0;
        }

        public boolean equals(Object o) {
                Human h = (Human)o;
                return (h.getAge()==this.age) && (h.getName().equalsIgnoreCase(this.name));
        }

        /*public boolean equals(Object o) {
        Human h=(Human)o;
        if((h.getAge()==this.age)&&(h.getName()==this.name))
        return true;

        else
                return false;
        }*/

        @Override
        public int getAge() {
                return 0;
        }

        @Override
        public String getName() {
                return 0;
        }

        @Override
        public String toString() {
                return "Person:[" + name + ", " + age + "]";
        }
}```
full linden
#
public class Person implements Human {

        private int age;
        private String name;

        public Person(String name, int age) {
                if(age<=0 || name.isEmpty() || name==null) {
                        throw new IllegalArgumentException();

                }
                this.age = age;
                this.name = name;
        }

        @Override
        public int compareTo(Human h) {
                if(h.getAge()>this.age)
                        return -1;

                else if(h.getAge()<this.age)
                        return 1;

                else
                        return 0;
        }

        public boolean equals(Object o) {
                Human h = (Human)o;
                return (h.getAge()==this.age) && (h.getName().equalsIgnoreCase(this.name));
        }

        /*public boolean equals(Object o) {
        Human h=(Human)o;
        if((h.getAge()==this.age)&&(h.getName()==this.name))
        return true;

        else
                return false;
        }*/

        @Override
        public int getAge() {
                return 0;
        }

        @Override
        public String getName() {
                return 0;
        }

        @Override
        public String toString() {
                return "Person:[" + name + ", " + age + "]";
        }
}
#

Good. Would be nice if getting the prerequisite steps wouldn't take an hour, but we got there

#

So, let's look at that code

unique geode
#

okay

full linden
#

I imagine the error you got changed?

unique geode
#

well i haven't tested it in moodle

#

but one of the tests

#

the second one wher eit should come up as true

#

it comes up as false instead

full linden
#

Hmm. On my computer that still doesn't compile

unique geode
#

well i also have another class

#

called

#

human

#

as in the quesstion

#

and my main class

#

where i test the things

#

    int getAge();

    String getName();

}```
#

the human class

full linden
#

getName() should return a String, zero is not good

unique geode
#

okay

unique geode
#

how do i do this

#

return null;

#

return null;

unique geode
#

can anyone help pls

full linden
#

You didn't ask a question

#

Also you left it all hanging at some point before

#

So nobody can have a clue what you need help with

full linden