Question

Can someone explain me parts of this code I do not fully understand what its doing...

Can someone explain me parts of this code I do not fully understand what its doing or what its purpose is. I have highlighted the parts I'm confused over. Please explain thoroughly.

import java.util.Scanner;

class A1Stats {
public static void main(String args[]) {
double min, max, sum, mean;
int n;


Scanner scanner = new Scanner(System.in);

String input = scanner.nextLine();
String[] numbers = input.split(" ");
double value[] = new double[numbers.length];

if(numbers.length > 0){
for (int i = 0; i < numbers.length; i++) {
   value[i] = Double.parseDouble(numbers[i]);
}
max = value[0];
min = value[0];
sum = value[0];
n = numbers.length;

for (int i = 1; i < value.length; i++) {
   if (max < value[i]) {
    max = value[i];
   }
   if (min > value[i]) {
    min = value[i];
   }
   sum = sum + value[i];
}

mean = sum/value.length;

System.out.println("min = " + min);
System.out.println("max = " + max);
System.out.println("sum = " + sum);
System.out.println("n = " + n);
System.out.println("mean = " + mean);

}

else{
System.out.println("There was no input data.");
}
}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is code in comments :

import java.util.Scanner;

class A1Stats {

public static void main(String args[]) {

double min, max, sum, mean;

int n;

Scanner scanner = new Scanner(System.in);

// reads line of numbers splited by spaces

String input = scanner.nextLine();

String[] numbers = input.split(" "); // split the line by space

double value[] = new double[numbers.length]; // creating new array of double array

// check if number are present

if (numbers.length > 0) {

// conveting the number enter to double values

for (int i = 0; i < numbers.length; i++) {

value[i] = Double.parseDouble(numbers[i]);

}

// setting up initial value to max, min, sum

max = value[0];

min = value[0];

sum = value[0];

n = numbers.length;

// loop each value and find min, max, sum

for (int i = 1; i < value.length; i++) {

if (max < value[i]) {

max = value[i];

}

if (min > value[i]) {

min = value[i];

}

sum = sum + value[i];

}

// calculating the average

mean = sum / value.length;

// displaying the output

System.out.println("min = " + min);

System.out.println("max = " + max);

System.out.println("sum = " + sum);

System.out.println("n = " + n);

System.out.println("mean = " + mean);

}

else {

System.out.println("There was no input data.");

}

}

}

Output:

Add a comment
Know the answer?
Add Answer to:
Can someone explain me parts of this code I do not fully understand what its doing...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • import java.util.Scanner; // TASK #1 Add the file I/O import statement here /** This class reads...

    import java.util.Scanner; // TASK #1 Add the file I/O import statement here /** This class reads numbers from a file, calculates the mean and standard deviation, and writes the results to a file. */ public class StatsDemo { // TASK #1 Add the throws clause public static void main(String[] args) { double sum = 0; // The sum of the numbers int count = 0; // The number of numbers added double mean = 0; // The average of the...

  • /** this is my code, and I want to invoke the method, doubleArraySize() in main method....

    /** this is my code, and I want to invoke the method, doubleArraySize() in main method. and I need to display some result in cmd or terminal. also, I have classes such as Average and RandomN needed for piping(pipe). please help me and thank you. **/ import java.util.Scanner; public class StdDev { static double[] arr; static int N; public static void main(String[] args) { if(args.length==0){ arr= new double[N]; Scanner input = new Scanner(System.in); int i=0; while(input.hasNextDouble()){ arr[i] = i++; }...

  • Computer Science - Java Explain the code step by step (in detailed order). Also explain what...

    Computer Science - Java Explain the code step by step (in detailed order). Also explain what the program required. Thanks 7 import java.io.File; 8 import java.util.Scanner; 9 import java.util.Arrays; 10 import java.io.FileNotFoundException; 12 public class insertionSort 13 14 public static void insertionSort (double array]) 15 int n -array.length; for (int j-1; j < n; j++) 17 18 19 20 21 double key - array[j]; int i-_1 while ( (i > -1) && ( array [i] > key array [i+1] -...

  • In the code shown above are two parts of two different exercises. HOW WE CAN HAVE...

    In the code shown above are two parts of two different exercises. HOW WE CAN HAVE BOTH OF THEM ON THE SAME CLASS BUT SO WE CAN RECALL them threw different methods. Thank you. 1-First exercise import java.util.Scanner; public class first { public static int average(int[] array){    int total = 0;    for(int x: array)        total += x;    return total / array.length;    } public static double average(double[] array){    double total = 0;    for(double x: array)        total += x;    return total /...

  • The mean of a list of numbers is its arithmetic average. The median of a list...

    The mean of a list of numbers is its arithmetic average. The median of a list is its middle value when the values are placed in order. For example, if an ordered list contains 1, 2, 3, 4, 5, 6, 10, 11, and 12, then the mean is 6, and their median is 5. Write an application that allows you to enter nine integers and displays the values, their mean, and their median. Correct program import java.util.Scanner; import java.util.*; class...

  • Please fix my code so I can get this output: Enter the first 12-digit of an...

    Please fix my code so I can get this output: Enter the first 12-digit of an ISBN number as a string: 978013213080 The ISBN number is 9780132130806 This was my output: import java.util.Scanner; public class Isbn { private static int getChecksum(String s) { // Calculate checksum int sum = 0; for (int i = 0; i < s.length(); i++) if (i % 2 == 0) sum += (s.charAt(i) - '0') * 3; else sum += s.charAt(i) - '0'; return 10...

  • Can you help me rearrange my code to make it look cleaner but still give the...

    Can you help me rearrange my code to make it look cleaner but still give the same output? Also kindly add an in-line comment on what changes and rearrangement you've done so I can understand what's going on. import java.util.ArrayList; import java.util.Scanner; public class Bank { /** * Add and read bank information to the user. * @param arg A string, double and int array containing * the command line arguments. * @exception Any exception * @return an arraylsit of...

  • How would I make it so these are dialog boxes instead of just like a regular...

    How would I make it so these are dialog boxes instead of just like a regular question? Here are the instructions. "Ask the user to input a number.  You must use an input dialog box for this input. Be sure to convert the String from the dialog box into an integer (int). The program needs to keep track of the smallest number the user entered as well as the largest number entered. Use a Confirm dialog box to ask the user...

  • Can you help me rearrange my code by incorporating switch I'm not really sure how to...

    Can you help me rearrange my code by incorporating switch I'm not really sure how to do it and it makes the code look cleaner. Can you help me do it but still give the same output? Also kindly add an in-line comment on what changes and rearrangement you've done so I can understand what's going on. import java.util.ArrayList; import java.util.Scanner; public class Bank { /** * Add and read bank information to the user. * @param arg A string,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT