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

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...
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"); } }...
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 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.
Make a class called CashRegister that has the method public static double getTotalCostOfOfferings(ArrayList<Offering> o) Make this...
Make a class called CashRegister that has the method public static double getTotalCostOfOfferings(ArrayList<Offering> o) Make this method calculate the total cost of all Offering objects in the ArrayList. Submit Product, Service, and CashRegister. Given Files: import java.util.ArrayList; public class Demo3 { public static void crunch(ArrayList<Offering> o) { System.out.println("Adding up the following offerings:"); for (Offering current : o) { System.out.println(current); } System.out.printf("Total for all: $%,.2f\n", CashRegister.getTotalCostOfOfferings(o)); System.out.println("---------------------------------\n"); } public static void main(String[] args) { ArrayList<Offering> offeringList = new ArrayList<>(); offeringList.add(new Product("iPhone",...
You will generate a People class of object and load an ArrayList with person objects, then...
You will generate a People class of object and load an ArrayList with person objects, then report the contents of that ArrayList. To do so, you must perform the following: (30 pts.) A) (15/30 pts. (line break, 11 pt) ) - Create a class file “People.java” (which will generate People.class upon compile). People.java will have eight(8) methods to 1) read a .txt file ‘people.txt’ 2) generate: ▪ List of all students AND teachers ▪ List of all students OR teachers...
Given a class Stack with the interface public void push(char n) // pushes n onto stack...
Given a class Stack with the interface public void push(char n) // pushes n onto stack public char pop() // return the top of the stack, removing element from stack public boolean isEmpty() // return true if stack is empty Write a method public int removeX(Stack<Character> stack) which takes a stack of Characters, removes the occurrences of ‘X’ and returns the count of the number of Xs removed. It must restore the stack to its original order (less the Xs)....
Step 4: Create a class called BabyNamesDatabase This class maintains an ArrayList of BabyName objects. Instance...
Step 4: Create a class called BabyNamesDatabase This class maintains an ArrayList of BabyName objects. Instance Variables Declare an ArrayList of BabyName objects. Constructor public BabyNamesDatabase () - instantiate the ArrayList of BabyName objects. You will not insert the items yet. This method will be one line of code. Mutator Methods public void readBabyNameData(String filename) - open the provided filename given as input parameter, use a loop to read all the data in the file, create a BabyName object for...
Write a class called CheckUserName. CheckUserName must have a main method. Your program must ask for...
Write a class called CheckUserName. CheckUserName must have a main method. Your program must ask for a user name. If the name is on the list below (Liam, for example) greet the user saying: welcome back: Liam If the user is an admin (like Benkamin, for example) print: welcome back: Benjamin you have admin privileges Your program must accept upper case or lower case: emacs% java CheckUserName enter a user name: Liam welcome back: Liam emacs% java CheckUserName enter a...
Write a class called WhereIsMyNumber. WhereIsMyNumber must have a main method. Your program must ask for...
Write a class called WhereIsMyNumber. WhereIsMyNumber must have a main method. Your program must ask for a number (it must work if user enters numbers with decimal point of not). Then your program must print number is negative -- if humber is negative number in [0,10) -- if it is between 0 and 10, not including the 10 number in [10,100) -- if it is between 10 and 100, not including the 100 number in [100,1000) -- if it is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT