Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need hlep I have posted this proble 4 times have gotten no help can come re write my code so that It works //import

I need hlep I have posted this proble 4 times have gotten no help can come re write my code so that It works

//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[][] populateHobbits(int 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 elements for (int i = 0; i

//assign this double to the current array element hobbitArray[i][j] = randDouble;

//determine which multiplier to use if (j == 0) //this is column for height hobbitArray[i][j] *= HT_MULTIPLIER; else //this is column for weight hobbitArray[i][j] *= WT_MULTIPLIER; } } return hobbitArray; //return the two-dimensional array }

//method to write hobbits array to file public static void writeHobbits(double[][] ar, String fileName) throws IOException { PrintWriter outFile = new PrintWriter(new FileWriter(fileName));

//print column heading of the array of stats outFile.println("HEIGHT,WEIGHT");

for (int i = 0; i

//if at end of a row, add newline char if (j == ar[i].length - 1) outFile.print(" "); else //add the "," delimiter outFile.print(","); } //end of inner loop } //end of outer loop

outFile.close(); //close outfile

System.out.println("The file was successfully written ");

} //end of method

//method to reads the hobbits file public static void readHobbitses(String fileName) throws IOException { //open the file to read File dataFile = new File(fileName); Scanner inFile = new Scanner(dataFile);

//variable to contain the substrings of one line of file String[] oneLine = new String[2];

int counter = 0; //keep track of line numbers

System.out.println("Data read from the " + fileName + " file:"); //read file, one line at a time while (inFile.hasNext()) { String dataLine = inFile.nextLine();

oneLine = dataLine.split(","); //split line with delimiter

if (counter > 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.out.println(oneLine[0] + "\t" + oneLine[1]); if (inFile.hasNext()) counter++; //keep track of how many lines } //end while loop read file

inFile.close(); //close the file

System.out.println("The file has now been successfully read. "); } //end of method

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

//replace the sum with the average; i.e. divide by number of rows for (int i = 0; i

return meanAr;

return meansAr; } //end of method

To solve the Lab16 problem we must get a random double for height and a random double for weight of a hobbit Assume that a hobbits height is in the range of [2.0, 4.0] Assume that a hobbits weight is in the range of [50.0, 100.0] NOTE: If you wish, you may set different ranges for your hobbits, but this must be clearly stated in your code The nextDouble( ) method of the Random class returns a double in the range [0.0, 1.0] How can we get a random value for height and weight in the correct range? One, but not the only, possible approach: Call the nextDouble( ) method inside a while loop Iterate the loop until the random value is in the range [0.2, 0.4] To get a random height, then use a multiplier of 10 To get a random weight, then use a multiplier of 250image text in transcribedplease and thank you I need help.

Lab16 Sample output

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Put Your Data To Work 52 Tips And Techniques For Effectively Managing Your Database

Authors: Wes Trochlil

1st Edition

0880343079, 978-0880343077

More Books

Students also viewed these Databases questions

Question

Explain the service recovery paradox.

Answered: 1 week ago