Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

can someone correct the code so that it works? //import statements for Random, Scanner and IO import java.util.Random; import java.util.Scanner; import java.io.*; public class Hobbit

can someone correct the code so that it works?

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

public class Hobbit { 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 readHobbits(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 and 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 { // Open the file to write PrintWriter outFile = new PrintWriter(fileName);

// Print column headings 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 the outfile

System.out.println("The file was successfully written "); } // End of method

// Method to read hobbits array from file and display it public static void readHobbits(String fileName) throws IOExcption { //open the file to read File dataFile = new File(fileName); Scanner inFile = new Scanner(dataFile); //variable to conatin the substrings of one line of file String[] oneLine = new String[2]; int counter = 0; //keep track of line number System.out.println("Data read from the " + fileName + " file:"); //method to get random double in range [0.2, 0.4] //parameter is an object of class Random public static double getRandDouble(Random rand) { final double MIN_RAND_DOUBLE = 0.2; final double MAX_RAND_DOUBLE = 0.4; boolean validDouble = false; double randDouble; do { randDouble = rand.nextDouble(); //if num in correct range, flip boolean to true if ( randDouble >= MIN_RAND_DOUBLE && randDouble

//method to determine average values of column dim arrary 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 column //second element is mean of second column, etc double[] meanAr = new double[ar[0].length]; Arrays.fill(meanAr, 0.0); /ested for loop to iterate through elements in parameter for (int i = 0; i

}

image text in transcribed

image text in transcribedimage text in transcribedimage text in transcribed

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Lab16 Sample output 4 //import statements for Random, Scanner and I0 5 import java.util. Random; 6 import java.util. Scanner; 7 import java.io.*

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

From Zero To Data Hero With Chatgpt

Authors: Andrew Wu

1st Edition

B0CQRJPXD9, 979-8989523009

More Books

Students also viewed these Databases questions

Question

2 What people-related issues do you foresee?

Answered: 1 week ago

Question

=+11. Should changes be introduced to improve the service/product?

Answered: 1 week ago