In: Computer Science
For this class exercise, each group will be assigned the file with the exercise number that corresponds to their group. The group will analyze the code to determine what the code does, how the data is structured, and why it is the appropriate data structure. Note that these are various examples, some are partial code with just classes and methods and do not include the "main" or "test" code needed to execute. Each team will add a header describing the purpose of the program (each class and method), comments to the code, and walkthrough the code with the class, answering questions as the instructor. IF the code is executable, then your group will demonstrate the code. Submit your file with the commented code. Make sure to include the names of everyone in your group on the file.
.......................................................................................................................................................................................................................................................................................
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Random;
public class ListTest3 {
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<Integer>();
int newNumber;
Random randomNumber = new Random();
for (int k = 0; k <25; k++) {
newNumber = randomNumber.nextInt(101);
list.add(newNumber);
}
Collections.sort(list);
System.out.println(list);
int count = 0;
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()) {
count += iterator.next();
}
System.out.printf("Sum is: %d%nAverage is: %.2f", count,
((double) count / list.size()));
}
}
Purpose:-
This code is generating 25 random numbers between 0 to 101 (101 is
exclusive)
and add them into the Collection Linkedlist
After adding them into the linked list it iterates the linked list
and adds all the numbers and displays that as sum
And using that sum it displays the average of all the random
generator numbers
DataStructure
In this code we are using LinkedList collection and it is
appropriate as it contains the elements of the specified
collection
in the insertion order .
also it can contain duplicate elements.
it is non-synchronized and manipulation is fast because no shifting
needs to occur, this gives it advantage over ArrayList
it can also be used as a list, stack or queue.
comments and code walkthrough: i have attached them as comments in code .comments are in bold
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Random;
/*
*
* @author give the names here
*
*/
/* Purpose:-
* This code is generating 25 random numbers between 0 to 101 (101
is exclusive)
* and add them into the Collection Linkedlist
* After adding them into the linked list it iterates the linked
list and adds all the numbers and displays that
* as sum
* And using that sum it displays the average of all the random
generator numbers
*/
/*
*
In this code we are using LinkedList collection and it is
appropriate as it contains the elements of the specified
collection
in the insertion order .
also it can contain duplicate elements.
it is non-synchronized and manipulation is fast because no shifting
needs to occur, this gives it advantage over ArrayList
it can also be used as a list, stack or queue.
*/
public class ListTest3 {
public static void main(String[] args) {
LinkedList<Integer> list = new
LinkedList<Integer>();
/*
* Creating a List of Type LinkedList name is list
*/
int newNumber;
Random randomNumber = new Random();
/*
* Random class is used to generate pseudo-random numbers in
java
* Here we are creating an object of Random Class which is a random
number generator
*/
for (int k = 0; k <25; k++) {
newNumber = randomNumber.nextInt(101);
/*
* here we use the nextInt() method with the Random class object to
generate random number between
* 0 inclusive and 101 exclusive
* that means it will only generate numbers from 0 to 100
*/
list.add(newNumber);
/*
* Here we use the add method which adds the genearted number in the
list
*/
}
Collections.sort(list);
/*
* Here we are calling the sort method of collections class which
sorts the elements of List in ascending order
*/
System.out.println(list);
/*
* we print the whole list
*/
int count = 0;// initialising the count variable to 0
Iterator<Integer> iterator = list.iterator();
/*
* creating the Integer type object of Iterator interface and using
it to traverse the whole list to access each number in list
seperately
*/
while (iterator.hasNext()) {
/*
* iterator.hasNext() used to check if there are more
elements.
* it returns true if there are more elements and loop
continues till we reach the last of list
*/
count += iterator.next();
/*
* using the loop we access each element and add it to count to get
the sum of all the numbers in list
*/
}
System.out.printf("Sum is: %d%nAverage is: %.2f", count,
((double) count / list.size()));
/*
* here we print the sum and find out the average by dividing
* it with size of list i.e is 25 and print it to only next two
digits after decimal using %.2f
*/
}
}
output of code:-
In the output we can see that list only contains numbers between 0 to 100 as we used randomNumber.nextInt(101) so it will generate numbers between 0 to 100
in 2nd line we sum of all the numbers in list
in 3rd line we see the average of numbers in list and its printed till only 2 digits after decimal
code snippets for better understanding:-