Question
java
Challenge Avocado Prices Programming challenge description: Consider a dataset containing the distribution of avocado prices
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the solution if you have any doubts then you can write in the comment section.

Please give feedback.

Solution:

import java.io.File;  
import java.io.FileNotFoundException;  
import java.util.*; 

public class Main 
{
        public static void main(String[] args) 
        {
                //try catch block to handle exceptions 
                try 
                {
                        //open a new file named input.csv
                        File myObj = new File("input.csv");
                        //create Scanner Object
                        Scanner myReader = new Scanner(myObj);
                        //read 1st line, we want to skip this because it contain field name 
                        myReader.nextLine();
                        //varibles
                        double value,sum=0,count=0,min=999999999,max=0;
                        //ArrayList to store values for finding std
                        ArrayList <Double> al=new ArrayList <Double>();
                        //read from file until line is present
                        while (myReader.hasNextLine()) 
                        {
                                // read line and store in data
                                String data = myReader.nextLine();
                                //split string by ,
                                String arr[]=data.split(",");
                                //value is 2nd element of array according to input given in question
                                value=Double.parseDouble(arr[1]);
                                //add value to ArrayList
                                al.add(value);
                                //add value to sum
                                sum+=value;
                                //increment count
                                count++;
                                //calculate max
                                if(value>max)
                                {
                                        max=value;
                                }
                                //calculate min
                                else if(value<min)
                                {
                                        min=value;
                                }
                        }
                        //calculate mean
                        double mean=sum/count;
                        double std=0.0;
                        //loop through all values
                        for(double values:al)
                        {
                                //calculate std
                                std+=Math.pow((values-mean),2);  
                        }
                        //calculate sample std, please be sure that n>1
                        std/=(count-1);
                        //print values
                        System.out.println("Min: "+min);
                        System.out.println("Max: "+max);
                        System.out.println("Mean: "+mean);
                        System.out.println("Std: "+std);
                        //close file
                        myReader.close();
                } 
                catch (FileNotFoundException e) 
                {
                        System.out.println("An error occurred.");
                        e.printStackTrace();
                }
                catch (ArithmeticException Ae)
                {
                        System.out.println("Count should be greater than 1 for std");
                }
        }
}

Input/Output:

input.csv

date,price,region
2015-07-19,1.08,Louisville
2015-08-16,1.05,RichmondNorfolk
2015-04-05,1.1,Orlando
2015-07-26,1.12,GrandRapids
2015-05-31,1.1,Atlanta

A B c D E F 1 2 3 date price 2015-07-19 2015-08-16 2015-04-05 2015-07-26 2015-05-31 region 1.08 Louisville 1.05 RichmondNorfo

output:

input Min: 1.05 Max: 1.12 Mean: 1.0899999999999999 Std: 7.000000000000012E-4 ... Program finished with exit code 0 Press ENTE

Thank You!

Add a comment
Know the answer?
Add Answer to:
java Challenge Avocado Prices Programming challenge description: Consider a dataset containing the distribution of avocado prices...
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
  • Netflix stock prices have climbed since it was first introduced in 2002. A stock broker is...

    Netflix stock prices have climbed since it was first introduced in 2002. A stock broker is interested in estimating the mean opening price for one share of Netflix stock. To do this, a random sample of 51 days from 2014 was taken, and the opening price of one share of the stock was recorded. The sample data is summarized in the StatCrunch output: Summary statistics: Column Mean Std. dev. Median Range Min Max Q1 Q3 Opening Price 51 409.6449 49.881232...

  • Hypothesis Testing Challenge #2-Discussion Board Group 3 Is there a significant difference between the mean age o...

    Hypothesis Testing Challenge #2-Discussion Board Group 3 Is there a significant difference between the mean age of people surveyed at the Home Show and the mean age of people surveyed at Pybus Market? From My on-campus statistics classes have been conducting surveys for Pybus Market in Wenatchee. The Fall 2014 surveyed customers at Pybus Market to learn more about customer demographics and inform marketing and funding strategies. The Winter 2015 class surveyed people at the Home Show to learn how...

  • Please help with the following, I have already done most of them. Please verify Question1 Select...

    Please help with the following, I have already done most of them. Please verify Question1 Select one answer The histogram below displays the distribution of so ages at death due to trauma (unnatural accidents and homicides) that were observed in a certain hospital during a week. opoints 18 16 14 12 10 а» 10 20 30 40 50 60 70 80 90 Age Which of the following are the appropriate numerical measures to describe the center and spread of the...

  • Lab Exercise #15 Assignment Overview This lab exercise provides practice with Pandas data analysis library. Data...

    Lab Exercise #15 Assignment Overview This lab exercise provides practice with Pandas data analysis library. Data Files We provide three comma-separated-value file, scores.csv , college_scorecard.csv, and mpg.csv. The first file is list of a few students and their exam grades. The second file includes data from 1996 through 2016 for all undergraduate degree-granting institutions of higher education. The data about the institution will help the students to make decision about the institution for their higher education such as student completion,...

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