Question

Write a static method named findMin that accepts an ArrayList of Person objects named list. Each...

Write a static method named findMin that accepts an ArrayList of Person objects named list. Each Person object has private myAge and myName properties and corresponding public getAge and getName accessors. The method must return the name of the youngest person.
public static String findMin(ArrayList<Person>list)
{
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The method you asked for:

public static Person findMin(ArrayList<Person> list) {

              // TODO Auto-generated method stub

              Person min = list.get(0);

              for(Person p:list) {

                     if(min.getMyAge()>p.getMyAge()) {

                           min = p;

                     }

              }

              return min;

       }

Whole code:

//Person.Java

public class Person{

       private int myAge=0;

       private String myName;

       public Person(int myAge, String myName) {

              // TODO Auto-generated constructor stub

              this.myAge = myAge;

              this.myName = myName;     

       }

       public Person() {

              // TODO Auto-generated constructor stub

       }

       public int getMyAge() {

              return myAge;

       }

       public void setMyAge(int myAge) {

              this.myAge = myAge;

       }

       public String getMyName() {

              return myName;

       }

       public void setMyName(String myName) {

              this.myName = myName;

       }

      

}//End of Person.java

//Tester.java

import java.util.ArrayList;

public class Tester {

      

       public static void main(String[] args) {

              Person p1 = new Person(16, "P1");

              Person p2 = new Person(18, "P2");

              Person p3 = new Person(19, "P3");

              Person p4 = new Person(15, "P4");

             

              ArrayList<Person> list = new ArrayList<Person>();

              list.add(p1);

              list.add(p2);

              list.add(p3);

              list.add(p4);

             

              Person min = findMin(list);

              System.out.println("Minimum Age Employee: "+min.getMyName()+" "+min.getMyAge());

       }

       public static Person findMin(ArrayList<Person> list) {

              // TODO Auto-generated method stub

              Person min = list.get(0);

              for(Person p:list) {

                     if(min.getMyAge()>p.getMyAge()) {

                           min = p;

                     }

              }

              return min;

       }

}

//End of Tester.java

Output:

Add a comment
Know the answer?
Add Answer to:
Write a static method named findMin that accepts an ArrayList of Person objects named list. Each...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For the code below write a public static main() method in class Student that: - creates...

    For the code below write a public static main() method in class Student that: - creates an ArrayList<Student> object called students - adds 4 new Student objects to the students list, with some made up names and dates - sort the students list by name and display the sorted collection to System.out. use function getCompByName() - sort the students list by enrollment date and display the sorted collection to System.out. use function getCompByDate() import java.util.Comparator;    import java.util.Date;    public...

  • (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList list)...

    (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList list) Write a program to test the method with three different arrays: Integers: 2, 4, and 3; Doubles: 3.4, 1.2, and -12.3; Strings: "Bob", "Alice", "Ted", and "Carol" This program is similar to the Case Study in the book: Sorting an Array of Objects The language must be written, compiled, and run on TEXTPAD. The Language is in Java.

  • java Write a generic method that takes an ArrayList of objects (of a valid concrete object...

    java Write a generic method that takes an ArrayList of objects (of a valid concrete object type) and returns a new ArrayList containing no duplicate elements from the original ArrayList. The method signature is as follows: public static ArrayList UniqueObjects(ArrayList list) Test the method with at least 2 array lists of different concrete object types. Label your outputs properly and show the array lists content before and after calling method UniqueObjects

  • Write a static, void method named replaceAll that accepts an array of int's named numbers as...

    Write a static, void method named replaceAll that accepts an array of int's named numbers as well as an int named num. The method must replace all occurrences of num . that are found in even-numbered index portions with the value of 3. That is, if the value num is stored in numbers [4] then it must be replaced with the value of 3 since 4 is an even number. But if num is found in numbers[5], it must not...

  • Java help: 1.1) Create a class TA  that extends class Student. 1.2) Add a variable to the...

    Java help: 1.1) Create a class TA  that extends class Student. 1.2) Add a variable to the TA class to represent the course the student is TA'ing. 1.3) Provide a constructor for the class so the code in the PeopleFactory will work as provided. 1.4) Provide a toString() method for the TA  class so that TA's will output as shown in the sample code. Provide a class Staff so that you can un-comment the lines of code in the PeopleFactory class that...

  • Write a public static method named getMaxOf2Ints that takes in 2 int arguments and returns the...

    Write a public static method named getMaxOf2Ints that takes in 2 int arguments and returns the Maximum of the 2 values Write a public static method named getMinOf2Ints that takes in 2 int arguments and returns the Minimum of the 2 values Write apublic static method named getMaxOf3Ints that takes in 3 int arguments and returns the Maximum of the 3 values Write a public static method named getMedianOf3Ints that takes in 3 int arguments and returns the Median Value...

  • java code level: beginner write a method sumList() public static Integer sumList(ArrayList<Integer> list) This method calculates...

    java code level: beginner write a method sumList() public static Integer sumList(ArrayList<Integer> list) This method calculates the sum of all Integer values in a given ArrayList. For example, if ArrayList<Integer> list contains {1, 2, 3}, a call to sumList(list) should return 1+2+3, which is 6 as an Integer. Return null if the list is empty or null. Think about the base case. When should the method terminate/end? Under what condition should your method stop making recursive calls and return your...

  • Add another method public static void displayObject(ArrayList list, int n) that will display then information on...

    Add another method public static void displayObject(ArrayList list, int n) that will display then information on the nth object of list. If n is not a valid index, the method should generate and throw and exception that the main can then process. You get to decide what exception (one built into Java or a custom exception) and how you would like to “handle” the exception (terminate the program, prompt for more input, etc). Here is the program so far: import...

  • Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating...

    Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating elements from both array lists. If one array list is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer array list. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 then merge returns the array list 1 9 4 7 9 4 16 9 11...

  • Write you code in a class named HighScores, in file named HighScores.java. Write a program that...

    Write you code in a class named HighScores, in file named HighScores.java. Write a program that records high-score data for a fictitious game. The program will ask the user to enter five names, and five scores. It will store the data in memory, and print it back out sorted by score. The output from your program should look approximately like this: Enter the name for score #1: Suzy Enter the score for score #1: 600 Enter the name for score...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT