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

Here is what my teacher responded when I asked for help, I have no idea how to g

ID: 3710694 • Letter: H

Question

Here is what my teacher responded when I asked for help, I have no idea how to get min and max values, here is my code

what my teacher told me to do "Make sure to use printf for the field widths, line up column headings, line up numbers right aligned, etc. Make sure the calculations are done on the random field and not the original number field. For the highest and lowest, they don't have to be calculated fifty times in the loop, just once each at the end. We can use get() to get them since we know their index numbers because they are sorted."

My Code

* Kyle Donohue
* This program will have the user input the number of objects into a LinkedList
* April 17th, 2018
*/
import java.util.Scanner;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Random;
import java.util.Collections;

public class hw2 {
  
public static void main(String[] args) {
LinkedList<integer> list=new LinkedList<integer>();
Scanner in= new Scanner(System.in);
System.out.print("Enter the number of objects: ");
int n= in.nextInt();
Random r= new Random();
int random;

for(int i=0;i<n;i++){
random = r.nextInt(150);
list.add(new integer(random,i));
}

Collections.sort(list);

ListIterator<integer> iterator= list.listIterator();
integer b;
int count=0;
System.out.println("Index Order Random");
do{

b=iterator.next();

System.out.printf(" " + " " + iterator.nextIndex() + " " +b.getoriginal() + " " +
+ b.getrandom());
count++;   
}while(iterator.hasNext());
int min=150;
int max=0;
int sum=0;
float mean;
random=r.nextInt(list.size());
integer randy=list.get(random);
iterator= list.listIterator();
do{
b=iterator.next();
sum=sum+b.getrandom();

  
if(max<b.getrandom())
max=b.getrandom();
if(min>b.getrandom())
min=b.getrandom();
}while(iterator.hasNext());
System.out.println(" Sum = "+sum);
mean=sum/list.size();
System.out.println("Mean = "+mean);

System.out.println("Range = "+(max-min));
int med;
if(list.size()%2==0){
med=list.size()/2 -1;
}else{
med=list.size()/2;
}
  
System.out.println("Median= "+list.get(med).getrandom());
  
  
}
}

Explanation / Answer

import java.util.Scanner;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Random;
import java.util.Collections;

public class hw2 {

   public static void main(String[] args) {
       LinkedList<Integer> list = new LinkedList<Integer>();
       Scanner in = new Scanner(System.in);
       System.out.print("Enter the number of objects: ");
       int n = in.nextInt();
       Random r = new Random();
       int random;
       // Adding 150 Random Integers to Linked List
       for (int i = 0; i < n; i++) {
           random = r.nextInt(150);
           list.add(new Integer(random));
       }

       // Sort the Linked list in Natural Order
       Collections.sort(list);

       ListIterator<Integer> iterator = list.listIterator();
       Integer b;
       int count = 0;
       System.out.println("Index Order Random");
       do {

           b = iterator.next();
           count++;
           System.out.printf(" " + " " + iterator.nextIndex() + "    " + count + "    " + b);
       } while (iterator.hasNext());

       int sum = 0;
       float mean;
       random = r.nextInt(list.size());
       iterator = list.listIterator();
       do {
           b = iterator.next();
           sum = sum + b;

       } while (iterator.hasNext());

       // As the list is Sorted , So last element will be Maximum
       System.out.println(" Maximum Value : " + list.getLast());

       // As the list is Sorted , So first element will be Minimum
       System.out.println(" Minimum Value : " + list.getFirst());

       System.out.println(" Sum = " + sum);
       mean = sum / n;
       System.out.println("Mean = " + mean);

       System.out.println("Range = " + (list.getLast() - list.getFirst()));

       // Median Will be Calculated based on the formula
       // if number of elements is odd.
       // Then Median will be (n+1)/2 th Element

       // Else Median will be mean of (n/2 th Element and (n/2)+1 th element)

       double med;
       if (list.size() % 2 == 0) {
           med = (list.get(n / 2) + list.get((n / 2) + 1)) / 2;
       } else {
           med = list.get((n + 1) / 2);
       }
       System.out.println("Median= " + med);

   }
}

Output:

Enter the number of objects: 40
Index Order Random

1    1    2
2    2    5
3    3    13
4    4    14
5    5    19
6    6    23
7    7    31
8    8    34
9    9    38
10    10    44
11    11    46
12    12    54
13    13    55
14    14    55
15    15    57
16    16    59
17    17    66
18    18    68
19    19    73
20    20    77
21    21    87
22    22    90
23    23    91
24    24    100
25    25    104
26    26    107
27    27    114
28    28    115
29    29    115
30    30    119
31    31    119
32    32    121
33    33    122
34    34    126
35    35    127
36    36    128
37    37    130
38    38    137
39    39    142
40    40    145

Maximum Value : 145


Minimum Value : 2


Sum = 3172
Mean = 79.0
Range = 143
Median= 88.0