Question: Can someone correct this code //import statemnts for Random, Scanner and IO import java.util.Random; import java.util.Scanner; import java.io.*; public class Hobbits { public static void

Can someone correct this code

//import statemnts for Random, Scanner and IO import java.util.Random; import java.util.Scanner; import java.io.*;

public class Hobbits { public static void main(String[] args) throws IOException { final int NUM_HOBBITS = 5; final int NUM_COLUMNS = 2; String fileName - "hobbits.csv"; //call populateHobbits( ) to create the two dimensional array double[][] hobbits = populateHobbits(NUM_HOBBITS, NUM_COLUMNS); //display the number of hobbits System.out.println(hobbits.length + " hobbits accepted Gandalf's invitation to lunch "); //calculate the means of the columns double[] hobbitMeans = getColMeans(hobbits); //write hobbits array to file writeHobbits(hobbits, fileName); //read and display the file that has been read readHobbitses(fileName); //call displayColMeans to display hobbit means displayColMeans(hobbitMeans); }

//method to populate hobbits array with random double values public static double[][] populteHobbits(in numHobbits, int numCols) { final double HT_MULTIPLIER = 10.0; //multiplier for the hobbit height final double WT_MULTIPLIER = 250.0; //multiplier for the hobbit weight //instantiate Random object Random rand - new Random (); //declare two dim array with numHobbits rows numCols columns double[][] hobbitArray = new double[numHobbits][numCols]; //assign random double values to all elemsnts for (int i - 0; i 0) //for second line and beyond, format doubles { System.out,printf("%.2f\t\t%.2f ", Double.parseDouble(oneLine[0]), Double.parseDouble(oneLine[1])); } else //if this first line, display the column heading System.outprintln(oneLine[0] + "\t" + oneLine[1]); if (inFile.hadNext()) counter++; //keep track of how many lines } //edn while loop read file inFile.close(); //close the file System.out.println ("The file has now been successfully read. "); } //end of method //methof to determine average values of columns two dim array public static double[] getColMeans(double[][] ar) { //local array to store means //array is sized according to length second dimension of parameter //first element is mean of first cplumn //second element is mean of second column, etc double[] meanAr = new double[ar[0].length]; /ested for loop to iterate through elements in parameter for (int i = 0; i

andalf is conducting a study on the hobbits in the Shire. To encourage them to participate in the study, he invites them to have lunch with him (they nearly always accept an invitation to lunch). While there, he measures their height and weight and records the information in a two dimensional array. Your task is to create the two dimensional array, populate this with random double values in the range of [2.0, 4.0] for the height and [50.0, 100.0] for the weight. To keep a permanent record, write the array to file After successfully writing the file, read the file so that it can be viewed by the user Finally, calculate the mean height and mean weight of the hobbits who participated in the study.

Method to return a random double in a specified range Name of the method: getRandDouble Return-type: double Parameter: An object of class Random Pseudocode: Declare a constant for the min value of the random double (i.e. 0.2) Declare a constant for the max value of the random double (i.e. 0.4) Declare boolean flag variable to control the loop Declare double variable to store the random double Iterate a do while loop until the random double is within the correct range Return the random double valueCan someone correct this code //import statemnts for Random, Scanner and IO

Lab16 Sample output

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!