Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question 3: In this question, you must write part of a \"main\" method to solve

ID: 3860203 • Letter: Q

Question

Question 3:

In this question, you must write part of a "main" method to solve a problem using two classes that you can assume are provided already.

Here are the classes that are provided. The contents of the methods are omitted, because you don't need to know how they work.

class Person {

public int howManyChildren() { ... }

// returns an integer that is the number of children the Person has

   public String getName() { ... }

       // returns the name of the Person

}

class PersonReader {

   public PersonReader() { ... }

// a constructor - creates its own Reader object, and whatever else it needs

   public Person readPerson() { ... }

   // reads enough input data to make an object of the class Person, and returns that object. (This is like

   // Format’s readLine( ) method, which reads and returns a String.

}

Complete the program below. It is intended to read the data about 20 people, storing each in an object of type Person. Then it prints the name of the Person(s) with the largest number of children. Your code should handle case where two or more parents have the same number of children (that could be highest).

public class WhoHasTheMostChildren {

   public static void main (String[] args) {

       .....

  

   }

}

Question 4;

1)Write an example to demonstrate overriding of static methods?

2)How is dynamic binding useful in enabling a small executable size in Java? Explain.

Explanation / Answer

Question3:

//Person.java
public class Person
{
  
   //private members
   private String name;
   private int childrens;
  
   //constructor
   public Person(String name, int childrens)
   {
       this.name=name;
       this.childrens=childrens;
   }
  
   //Returns number of childrens
   public int howManyChildren()
   {
       return childrens;
   }
  
   //Returns name
   public String getName()
   {
       return name;
   }
}

//PersonReader.java
import java.util.Scanner;
public class PersonReader
{
  
   private Person person;
  
   //constructor
   public PersonReader()
   {
       person=null;
   }
  
   /*Method that prompts user to enter name and number of children
      and return person object */
   public Person readPerson()
   {
       Scanner scanner=new Scanner(System.in);
      
       System.out.println("Enter name : ");
       String name=scanner.nextLine();
      
       System.out.println("Enter number of childrens");
       int child=scanner.nextInt();
      
       person=new Person(name, child);
      
       return person;
   }
  
}


/**
* The program WhoHasTheMostChildren that creates an instance of
* PersonReader and callds readPerson three times and finds
* the name of the person who have most number of childrens.
* */
//WhoHasTheMostChildren.java
public class WhoHasTheMostChildren
{
   public static void main(String[] args)
   {
      
       //instance of PersonReader
       PersonReader pr=new PersonReader();
      
       //call readPerson
       Person p1=pr.readPerson();
       //call readPerson
       Person p2=pr.readPerson();
       //call readPerson
       Person p3=pr.readPerson();
      
       System.out.println("Person name with most childrens");
      
       //check if p1 has more children
       if((p1.howManyChildren()>p2.howManyChildren())
               && (p1.howManyChildren()>p3.howManyChildren()))
       {
           System.out.println("Name : "+p1.getName());
           System.out.println("# of Childrens : "+p1.howManyChildren());
       }
       //check if p2 has more children
       else if((p2.howManyChildren()>p3.howManyChildren())
               && (p2.howManyChildren()>p1.howManyChildren()))
       {
           System.out.println("Name : "+p2.getName());
           System.out.println("# of Childrens : "+p2.howManyChildren());
       }
       //check if p3 has more children
       else if((p3.howManyChildren()>p1.howManyChildren())
               && (p3.howManyChildren()>p1.howManyChildren()))
       {
           System.out.println("Name : "+p3.getName());
           System.out.println("# of Childrens : "+p3.howManyChildren());
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote