Create a method countAges() inside of class LinkedListnside of class LinkedList.
ID: 3559530 • Letter: C
Question
Create a method countAges() inside of class LinkedListnside of class LinkedList. This method will create an Array of type int. The array size should be 100. All of the Person objects have an age value between 0 and 99(inclusive). As you traverse the list, keep track of how many of each age there is in the Array. Then after traversing the entire Array, output which age appears the most times.
Creat a method Move(Node n, Node place) inside of class LinkedList. The method will remove Node n from its location in the list and add it in the list AFTER Node place. (HINT: this method is performing the tasks of a delete method and an insert method in one method, BUT you CANNOT call method insert or delete.)
Explanation / Answer
// DO THE SECOND PART OF QUESTION NEED TO BE DONE IN SAME PROGRAM ? IT DOES NOT SEEM //RELEVANT
//ACCORDING TO QUESTION THERE IS A NEED TO MAKE ARRAYS SO I AM DOING IT
//IF U NEED THE SECOND PART SEPERATELY PLZ COMMENT AND I WILL MAKE IT SEPAERATELY FOR U
//AS PER QUESTION I AM CREATING AN ARRAY OF 100 SIZE
// THE AGES ARE AUTOMATICALLY SET BY A RANDOM GENERATOR BETWEEN 1 AND 100;
import java.util.Arrays;
import java.util.Random;
class LinkedList
{
public static void main(String[] args)
{
LinkedList ob=new LinkedList();
ob.countAges();
}
int countAges()
{
Random rand=new Random(System.currentTimeMillis());
int []person=new int[100];
int i,hold;
int[]temp=new int[100] ;
int[] counter=new int[1000]; //array to hold data of 100 person
for( i=0;i<100;i++)
{
person[i]=rand.nextInt(99)+1;
System.out.println(person[i]);
} //SETTING UP RANDOM GENERATED VALUES
for( i=0;i<100;i++)
{
hold=person[i];
temp[hold]++;
counter[hold]++;
}
Arrays.sort(temp);
i=0;
while(counter[i]!=temp[99])
{
i++;
}
// System.out.println(temp[99]);
System.out.println(" "+i+"age first appears for maximum of"+temp[99]+"times");
return 0;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.