Question

In: Computer Science

We have created an ArrayList of Person class. write a method called push that pushes all...

We have created an ArrayList of Person class. write a method called push that pushes all the people with the even length last name to the end of the ArrayList

Content of the ArrayList before push

[alex Bus, Mary Phillips, Nik Lambard, Rose Rodd, Esa khan, Jose Martinex, Nik Patte]

content of the ArrayList after the push method
[alex Bus, Nik Lambard, Nik Patte, Mary Phillips, Rose Rodd, Esa khan, Jose Martinex]

import java.util.*;
class Person
{
   private String name;
   private String last;
   public Person(String name, String last)
   {
     this.name = name;
     this.last = last;
   }
   public String getLast()
   {
     return last;
   }
   public String getFirst()
   {
     return name;
   }
   public String toString()
   {
     return name + " " + last;
   }
}

public class ArrayList
{
  public static void main(String[] args)
  {
    ArrayList<Person> list = new ArrayList<Person>();
     list.add(new Person ("alex","Bus"));
     list.add(new Person("Mary", "Phillips"));
     list.add(new Person("Nik", "Lambard") );
     list.add(new Person("Rose","Rodd"));
     list.add(new Person("Esa","khan"));
     list.add(new Person("Jose","Martinex"));
     list.add(new Person("Nik","Patte"));
     System.out.println(list);
     push(list);
     System.out.println(list);

  
  }
//this method pushes all the people with the even length last name to the end of the list
 public static void push(ArrayList<Person> list) {
         
 }
     
   
}

Solutions

Expert Solution

import java.util.ArrayList;
import java.util.List;

class Person {
        private String name;
        private String last;

        public Person(String name, String last) {
                this.name = name;
                this.last = last;
        }

        public String getLast() {
                return last;
        }

        public String getFirst() {
                return name;
        }

        public String toString() {
                return name + " " + last;
        }
}

public class ArrayListDemo {
        public static void main(String[] args) {
                ArrayList<Person> list = new ArrayList<Person>();
                list.add(new Person("alex", "Bus"));
                list.add(new Person("Mary", "Phillips"));
                list.add(new Person("Nik", "Lambard"));
                list.add(new Person("Rose", "Rodd"));
                list.add(new Person("Esa", "khan"));
                list.add(new Person("Jose", "Martinex"));
                list.add(new Person("Nik", "Patte"));
                System.out.println(list);
                push(list);
                System.out.println(list);

        }

//this method pushes all the people with the even length last name to the end of the list
        public static void push(ArrayList<Person> list) {
                int size=list.size();
                for (int i=0;i<size;i++) {
                        if (list.get(i).getLast().length() % 2 == 0) {
                                Person p=list.remove(i);
                                list.add(p);
                        }
                }
        }

}

NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.

I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME


Related Solutions

Write a Java class called Person. The class should have the following fields: A field for...
Write a Java class called Person. The class should have the following fields: A field for the person’s name. A field for the person’s SSN. A field for the person’s taxable income. A (Boolean) field for the person’s marital status. The Person class should have a getter and setter for each field. The Person class should have a constructor that takes no parameters and sets the fields to the following values: The name field should be set to “unknown”. The...
***Given a class called Student and a class called Course that contains an ArrayList of Student....
***Given a class called Student and a class called Course that contains an ArrayList of Student. Write a method called dropStudent() as described below. Refer to Student.java below to learn what methods are available.*** Course.java import java.util.*; import java.io.*; /****************************************************** * A list of students in a course *****************************************************/ public class Course{ /** collection of Students */ private ArrayList<Student> roster; /***************************************************** Constructor for objects of class Course *****************************************************/ public Course(){ roster = new ArrayList<Student>(); } /***************************************************** Remove student with the...
1- Write a class called MedicalStaff that is a Person (the class that you wrote in...
1- Write a class called MedicalStaff that is a Person (the class that you wrote in last lab). A MedicalStaff has specialty (i.e. Orthopedic, cardiology, etc.). 2- Then write two classes: Doctor class has office visit fee. Nurse class has title (i.e. RN, NP, etc.) Both classes inherit from MedicalStaff. Be sure these are all complete classes, including toString method. 3- Write a tester to test these classes and their methods, by creating an array or ArrayList of Person and...
Suppose we have a Java class called Person.java public class Person { private String name; private...
Suppose we have a Java class called Person.java public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName(){return name;} public int getAge(){return age;} } (a) In the following main method which lines contain reflection calls? import java.lang.reflect.Field; public class TestPerson { public static void main(String args[]) throws Exception { Person person = new Person("Peter", 20); Field field = person.getClass().getDeclaredField("name"); field.setAccessible(true); field.set(person, "Paul"); } }...
Develop the getSuggestions(ArrayList wordCountList) method as the base method of the class. Develop the countWords(ArrayList wordList)...
Develop the getSuggestions(ArrayList wordCountList) method as the base method of the class. Develop the countWords(ArrayList wordList) method to find the frequencies of all words in the text file. Develop the getWordList(File[] fileArray) method to get all words in the text file. Ignore the words that have 3 or less characters. Your customer wants you to develop a method that will find the sentences that contain a specific word. This is basically a word search, but your customer needs the list...
Write a for-each loop that prints all of the Student objects in an ArrayList<student> object called...
Write a for-each loop that prints all of the Student objects in an ArrayList<student> object called roster. Write this piece in Java. Write the correct code to execute.
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called...
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called without instantiating the class and returns a random Date between Jan 1, 2000 and Dec 31, 2010.
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of Poem objects, read in the information from PoemInfo.txt and create Poem objects to populate the ArrayList. After all data from the file is read in and the Poem objects added to the ArrayList- print the contents of the ArrayList. Paste your PoemDriver.java text (CtrlC to copy, CtrlV to paste) into the open space before. You should not change Poem.java or PoemInfo.txt. Watch your time...
Write a class file called lastname_digits.java. In it, write a method to compute and return the...
Write a class file called lastname_digits.java. In it, write a method to compute and return the total instances where the sum of digits of the array equals to the target. Write a demo file called lastname_digits_demo.java. Create an array of random 10 integers between 301 and 999. Also create an integer variable called target that is equal to a value between 5 and 10. Print the answer within this file. Example: If five of the 20 integers are equal to...
In Java in previous question #1, you created a method called isDNAvalid() in the DNA class....
In Java in previous question #1, you created a method called isDNAvalid() in the DNA class. You probably looped through the entire sequence one character at a time (using charAt() method of the String class), and check if the character is 'A', 'T', 'G', or 'C'. If it is not one of these four characters, the sequence contains an invalid base. Please re-create the isDNAvalid() method using a character pattern and the regular expression to validate the DNA sequence. previous...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT