Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java: passing string of an object to another object

My post has been closed because of duplication of another question that doesn’t really resolve my question so i’m posting it again. because its not about comparing strings its about passing a string of an object to another objects.

I was trying to practise inheritence on Java by writing a code that consist of Parent class that is called Person that has two child classes of SuperPerson and Civil, and the SuperPerson class that has two child classes called Hero and Villian.
I was trying to implement a method called Protect which is used only by the Hero class that can only works on the Civil class as a target.
So, the main issue here when i run the code, it doesn’t show the protector name in the case of a different hero trying to protect a civil who’s already under another hero’s protection and in the case of a villian trying to attack a civil who’s under hero’s protection.

The main method:

Hero h1 = new Hero("Spiderman", 25,"Male", "Web");
Hero h2 = new Hero("Batman", 35, "Male","Wealth");
SuperPerson v1 = new Villian("Goblin", 47,"Male", "Goblin Power");
Person c3 = new Civil("Mary", 24,"Female");
h1.protect(c3); //First Hero object protecting the Civil object
h2.protect(c3);//Different Hero object trying to protect the same Civil object
v1.attack(c3);//Villian attacking civil under protection

The output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Mary is under Spiderman's proction.
Mary is already under null's protection //Bug here
The target is under null's protection //Bug here

The (Person) parent class:

public class Person {
    boolean protection;
}

The (SuperPerson) parent class:

public class SuperPerson extends Person {
    String protector;
}

The first child (Hero) class that has the protect method:

public class Hero extends SuperPerson {
    void protect(Person target) {
        if(target.type.equals("Super Villian")) {
            System.out.println("You can not protect a villian!");
        }
        else {
            if(target.health != 0) {
                if(target.protection == false) {//to protect
                    target.protection = true;
                    this.protector = this.name;
                    System.out.println(target.name + " is under " + this.protector + "'s proction.");
                }
                else {
                    if(this.protector != this.name) {//under someone else protection so can not unprotect
                        System.out.println(target.name + " is already under " + protector + "'s protection");//Bug here
                    }
                    else {//to unprotect
                        target.protection = false;
                        System.out.println(target.name + " is no longer under " + this.name + "'s protection");
                    }
                }
            }
            else {
                System.out.println("Target is already dead.");
            }
        }
    }
}

the other child (Villian) class:

public class Villian extends SuperPerson{
    int attack(Person target) {
        if(target.type.equals(this.type)) {
            System.out.println("Invalid target!");
            return 0 ;
        }
        else {
            if(target.protection == true) {
                System.out.println("The target is under " + protector + "'s protection"); //Bug here
                return 0;
            }
            else {
                return super.attack(target);
            }
        }
        
    }
}

>Solution :

When you call

h1.protect(c3);

you are storing in c3 that it is now protected.

However, you are storing the name of the protector not in c3 but in h1:

                this.protector = this.name;

That means that when you later call

h2.protect(c3);

the h2 can read that c3 is protected, but it cannot read who is protecting c3.

To fix this you need to make the field String protector; an instance field of the Person class and store the name of the protector there:

public class Person {
    boolean protection;
    String protector;
}

and

public class Hero extends SuperPerson {
    void protect(Person target) {
        if(target.type.equals("Super Villian")) {
            System.out.println("You can not protect a villian!");
        } else {
            if (target.health != 0) {
                if (!target.protection) {//to protect
                    target.protection = true;
                    target.protector = this.name;
                    System.out.println(target.name + " is under " + this.protector + "'s proction.");
                } else {
                    if (!target.protector.equals(this.name)) {//under someone else protection so can not unprotect
                        System.out.println(target.name + " is already under " + target.protector + "'s protection");
                    } else {//to unprotect
                        target.protection = false;
                        System.out.println(target.name + " is no longer under " + this.name + "'s protection");
                        target.protector = null;
                    }
                }
            } else {
                System.out.println("Target is already dead.");
            }
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading