-2

Well I have an array of people. They contain instance variables such as: NAME, AGE, LOCATION. These are private instance variables.

Now there are various of people in my array with their own instance variables. Ill use myself to make it easier.

Oscar 22 USA

However after successfully creating my array, I need to create a boolean method that will allow me to search throughout my array, find my name and change USA to Japan (or whatever). So if Oscar is in Japan, fine, but if Oscar is in USA change it to Japan.

This project is supposedly a fairly simple one and it is not really homework since it was a challenge but it has been a week since the professor assigned it and I can not understand it. I know how to do other methods but Boolean trips me up.


public class Frogs {

    private String breed;
    private String nativeTo;


    public Frogs(String breed, String nativeTo)
    {
        this.breed=breed;
        this.nativeTo=nativeTo;

    }

    //accessor(GETTER)

    public String getRegion() 
    {
        return nativeTo;
    }

    public String getBreedName()
    {
        return breed;
    }

    public String toString() ////(RETURNS A STRING (not the memory) ////////////
    {
        return ("Frog Breed -  " +breed+ "From: " +nativeTo);
    }}
public class WildAnimalsDemo {

public static void main(String[]args)
        {

            Frogs [] stuff = new Frogs[6];  //array of objects of type frogs

            stuff[0] = new Frogs ("Tree Frog " , "NJ");
            stuff[1] = new Frogs ("Lizard Frog " , "PA");
            stuff[2] = new Frogs ("Kermit Frog " , "Muppets");
            stuff[3] = new Frogs ("Bear Frog " , "Philipines");
            stuff[4] = new Frogs ("Bull Frog " , "New York");
            stuff[5] = new Frogs ("Lizard " , "PA");           // DOES NOT HAVE "FROG" IN IT

            System.out.println(stuff[0]);
            System.out.println(stuff[1]);
            System.out.println(stuff[2]);
            System.out.println(stuff[3]);
            System.out.println(stuff[4]);
            System.out.println(stuff[5]);
            System.out.println();
            //////////////////////////////////////////////////////////////////////////////////////////////////1stPRINT
            int count=findFrogFromPlace(stuff,"New York");

            System.out.println("Found: " + count);
            //////////////////////////////////////////////////////////////////////
            int count1=findFrogFromPlace(stuff,"PA");

            System.out.println("Found: " + count1);
            /////////////////////////////////////////////////////////////////////////////////////////////////2ndPrint


        /*  boolean possible = findAndSetBreed(stuff, "Frog");
            System.out.println(possible + " Species " + stuff[5] );
            boolean possible1 = findAndSetBreed(stuff, "Frog");
            System.out.println(possible1 + " Species " + stuff[4] );*/

        }

    public static int findFrogFromPlace(Frogs[] F , String theRegion)
    {
        int count=0;             //////THIS COUNT IS NOT THE SAME AS COUNT FROM THE MAIN

        for(int x=0; x<F.length; x++)
        {

               //Returns String/
            if (F[x].getRegion().equalsIgnoreCase(theRegion) )
                count++;
        }

        return count;
    }

    /*public static boolean findAndSetBreed(Frogs[] frog, String theBreed)
    {


        for(int x=0; x<frog.length; x++)
        {


        if (frog[x].getBreedName().equalsIgnoreCase(theBreed) )
                System.out.println(x);

        }
        return frogg;
    }   */
}
Chai T. Rex
  • 2,972
  • 1
  • 15
  • 33
EspadaAzul
  • 23
  • 4
  • What language are you using? What have you tried so far? – OliverRadini Sep 26 '18 at 08:44
  • I am using Java and I am programming on Eclipse. – EspadaAzul Sep 26 '18 at 08:52
  • When the function should return TRUE? when the location had changed? or just report finish status? – dWinder Sep 26 '18 at 09:10
  • If you can provide some examples of what you've tried so far, then people may be able to help – OliverRadini Sep 26 '18 at 09:13
  • Well Oscar is no longer from the USA so if it says USA it should be false and it should then change it to a current location – EspadaAzul Sep 26 '18 at 09:13
  • @OliverRadini Okay well I added a code in class as an example of what we did that night, but he wanted to change the String Lizard to a Type of Frog using a Boolean method ///// The main purpose was to change the string instance variable from one thing to another from the array – EspadaAzul Sep 26 '18 at 09:26
  • So use reference when sending the array to the function and after finding the element set if needed and return boolean accordingly – dWinder Sep 26 '18 at 09:49

1 Answers1

0

The example you posted is a bit different from what you asked. Anyway, you have to implement getter and setter methods for each field of the class. Then you can write a method

public boolean checkName(String name) {
   return (name.equals(this.name));
}

In the main you can set the right location by checking every object of your array, and calling setLocation() if checkName return true

Person[] array={.......}
for(Person p: array) {
   if(p.checkName("Oscar"))
       p.setLocation("Japan");
}
robertobatts
  • 965
  • 11
  • 22