Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//author: Jazmine Tapia //date: 20 February 2023 //file: Hobbits.java /* Lab 16 is going to generate a random double hight and random weight for hobbit

//author: Jazmine Tapia //date: 20 February 2023 //file: Hobbits.java

/* Lab 16 is going to generate a random double hight and random weight for hobbit */

//import statements for Random, Scanner and IO import java.util.Random; import java.util.Scanner; import java.io.*; public class Hobbits { //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

// variable to contain 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:");

// read file, one line at a time while (inFile.hasNextLine()) { String dataLine = inFile.nextLine(); oneLine = dataLine.split(","); if (counter == 0) { // if this is first line, display the column headings System.out.println(oneLine[0] + "\t" + oneLine[1]); } else { // for second line and beyond System.out.printf("%.2f\t\t%.2f ", Double.parseDouble(oneLine[0]), Double.parseDouble(oneLine[1])); } counter++; // increment counter for each line } // end while loop to read file

inFile.close(); // close the file System.out.println("The file has been successfully read. "); }

// method to determine average values of column 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 column // second element is mean of second column, etc double[] meanAr = new double[ar[0].length];

// nested for loop to iterate through elements in parameter for (int j = 0; j

// sum the elements in the current column meanAr[j] += ar[i][j]; } // replace the sum with the average; i.e divide meanAr[j] /= ar.length; } // end of outer loop

return meanAr; } // end of method } //method to diplay column means public static void displayColMeans(double[] ar) { System.out.println("CALCULATED COLUMN MEANS:"); System.out.println("HEIGHT\tWEIGHT"); for (int i = 0; i

//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); } }

image text in transcribed

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

Refactoring Databases Evolutionary Database Design

Authors: Scott Ambler, Pramod Sadalage

1st Edition

0321774515, 978-0321774514

More Books

Students also viewed these Databases questions

Question

=+Have you changed your name?

Answered: 1 week ago

Question

What is the Definition for Third Normal Form?

Answered: 1 week ago

Question

Provide two examples of a One-To-Many relationship.

Answered: 1 week ago