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 /...

  • 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,...

  • 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...

  • I need to create a code for this prompt: In this project we will build a...

    I need to create a code for this prompt: In this project we will build a generic UserInput class for getting keyboard input from the user. Implementation: The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard. Write FOUR simple methods, one...

  • Step 4: Add code that discards any extra entries at the propmt that asks if you...

    Step 4: Add code that discards any extra entries at the propmt that asks if you want to enter another score. Notes from professor: For Step 4, add a loop that will validate the response for the prompt question:       "Enter another test score? (y/n): " This loop must only accept the single letters of ‘y’ or ‘n’ (upper case is okay). I suggest that you put this loop inside the loop that already determines if the program should collect...

  • Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895...

    Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895    {        public static void header()        {            System.out.println("\tWelcome to St. Joseph's College");        }        public static void main(String[] args) {            Scanner input = new Scanner(System.in);            int d;            header();            System.out.println("Enter number of items to process");            d = input.nextInt();      ...

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
ADVERTISEMENT