Question

In: Computer Science

For this class exercise, each group will be assigned the file with the exercise number that...

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()));

    }

}

Solutions

Expert Solution

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:-


Related Solutions

9.50. Each person in a group of twenty persons is assigned a random number from 1...
9.50. Each person in a group of twenty persons is assigned a random number from 1 to 100 (both included). It is possible for multiple persons to be assigned the same number, and all assignments are equally likely. What is the probability that at least two persons in the group share the same number?
c++ Write the implementation (.cpp file) of the Counter class of the previous exercise. The full...
c++ Write the implementation (.cpp file) of the Counter class of the previous exercise. The full specification of the class is: A data member counter of type int. An data member named counterID of type int. A static int data member named nCounters which is initialized to 0. A constructor that takes an int argument and assigns its value to counter. It also adds one to the static variable nCounters and assigns the (new) value of nCounters to counterID. A...
University of Southern Ontario has been assigned a Class B address with a network number of...
University of Southern Ontario has been assigned a Class B address with a network number of 132.27.0.0. You are asked to come up with a subnetting scheme that allows to create 650 subnets on that network. Show your work. 1)         How many bits must be borrowed for the subnet ID? 2)         What is the subnet mask required? 3)         How many hosts there can be on each subnet? 4)         How many subnets can there be? 5)         Explain in 4 lines MAX,...
Create a Java class file for a Car class. In the File menu select New File......
Create a Java class file for a Car class. In the File menu select New File... Under Categories: make sure that Java is selected. Under File Types: make sure that Java Class is selected. Click Next. For Class Name: type Car. For Package: select csci2011.lab7. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab7; /** * * @author Your Name */ public class Car { } Implement the Car...
Create a Java class file for an Account class. In the File menu select New File......
Create a Java class file for an Account class. In the File menu select New File... Under Categories: make sure that Java is selected. Under File Types: make sure that Java Class is selected. Click Next. For Class Name: type Account. For Package: select csci1011.lab8. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab8; /** * * @author Your Name */ public class Account { } Implement the Account...
// FILE: table1.h // // ABSTRACT BASE CLASS: Table //    1. The number of records...
// FILE: table1.h // // ABSTRACT BASE CLASS: Table //    1. The number of records in the Table is stored in total_records // 2. The hashcode function returns a location in the table for the // input key. It calls hash function in functional library. // 3. insert and print are two virtual functions to be overridden // insert: Add a new record into the Table; //           If key is already in the table, do nothing //...
Health Policy Group Project Work with your assigned group and assigned question. Identify the historical perspective,...
Health Policy Group Project Work with your assigned group and assigned question. Identify the historical perspective, current issues/ problems (include regulations), and what is currently being done to change or improve.  Students will be graded as a group. Please demonstrate engagement within group work. Review the literature and address one of the following: FNP/AGNP:Your group has been asked to present a poster on Practice Authority. Develop a power point poster template ofallstatesauthority to practice for APRNS.Three categories must be address,...
File Account.java (see 4.1. A Flexible Account Class exercise) contains a definition for a simple bank...
File Account.java (see 4.1. A Flexible Account Class exercise) contains a definition for a simple bank account class withmethods to withdraw, deposit, get the balance and account number, and return a String representation. Note that theconstructor for this class creates a random account number. Save this class to your directory and study it to see how it works.Now modify it to keep track of the total number of deposits and withdrawals (separately) for each day, and the total amountdeposited and...
Risk management and regulation after the 2008 Financial Crisis Each study group is assigned to a...
Risk management and regulation after the 2008 Financial Crisis Each study group is assigned to a bank as follows and reponsible for summarizing their risk management policies. Your group number can be found in the attached list. Group Bank 1 Goldman Sachs 2 UBS 3 JP Morgan Chase 4 Citigroup 5 Barclays Capital 6 Morgan Stanley 7 Deutsche Bank 8 Bank of America 9 BNP Paribas 10 Credit Suisse Download their 2009 and most recent annual reports (10-K for US...
Research Questions for Student Groups Each student group will be assigned one of the topics below...
Research Questions for Student Groups Each student group will be assigned one of the topics below to research in some detail and prepare a short, -minute PowerPoint presentation on it. Th ey will then use their PowerPoint presentation to explain the topic to their fellow students at the next class period. . Provide a brief overview of recombinant DNA technology. What are restriction enzymes? What are plasmids? . Who are Stanley Cohen and Herbert Boyer and what was their role...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT