SlideShare a Scribd company logo
//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 lunchn");
//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 < numHobbits; i++) //outer loop is for rows
{
for (int j = 0; j < numCols; j++) //inner loop is for columns
{
//get a random double value in range [0.2, 0.4]
double randDouble = getRandDouble(rand);
//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] *= HI_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 columen heading of the array of stats
outFile.println("HEIGHT,WEIGHT");
for (int i = 0; i < ar.length; i++)
{
for (int j = 0; j < ar[i].length; j++)
{
outFile.print(ar[i][j]);
//if at end of a row, add newline char
if (j == ar[i].length - 1)
outFile.print("n");
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 writtenn");
} //end of method
//method to read 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("%.2ftt%.2fn", 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.n");
} //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 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 i = 0; i < ar.length; i++) {
for (int j = 0; j < ar[i].length; j++) {
//sum the elements in the current column
meanAr[j] += ar[i][j];
}
} //end of outer loop
//replace the sum with the average; i.e. divide by number of rows
for (int i = 0; i < meanAr.length; i++)
meanAr[i] /= ar.length;
Lab16 Sample output

More Related Content

PDF
--12 Points- DEVORESTATS 2-5-074- Suppose thut the proportions of bloo.pdf
PDF
--kindly help with the correct answer --must show diagram Explain what.pdf
PDF
--- If you were start a business- what are the advantages in using equ.pdf
PDF
--- Interpret the figure below given what you know about wood frogs---.pdf
PDF
--------- Lala--- the noll (fnrmad plement) indicated by letter A is a.pdf
PDF
--- Which of the following influences the instantaneous rate of change.pdf
PDF
-- USING UNITY TRYING TO CREATE A CLICK TO PATH- THAT YOU CLICK ON AND.pdf
PDF
-- Write the compiler used- Visual studio or gcc -- Reminder that your.pdf
--12 Points- DEVORESTATS 2-5-074- Suppose thut the proportions of bloo.pdf
--kindly help with the correct answer --must show diagram Explain what.pdf
--- If you were start a business- what are the advantages in using equ.pdf
--- Interpret the figure below given what you know about wood frogs---.pdf
--------- Lala--- the noll (fnrmad plement) indicated by letter A is a.pdf
--- Which of the following influences the instantaneous rate of change.pdf
-- USING UNITY TRYING TO CREATE A CLICK TO PATH- THAT YOU CLICK ON AND.pdf
-- Write the compiler used- Visual studio or gcc -- Reminder that your.pdf

More from ganisyedtrd (20)

PDF
- There are 50 fish in a pond in January- The fish grow at a rate of 6.pdf
PDF
- TIF B cells and T cells are able to alter their genome- What kind of.pdf
PDF
- The array data of integers is sorted in an increaning order- - The i.pdf
PDF
- Quantitative (record completenessdocuments are present- forms authen.pdf
PDF
- Lab T Reriow Hemneork + Oun on March 10- 2023 2 anantwred Table 72 P.pdf
PDF
- In 150-250 words- respond to all questions- This is your initial dis.pdf
PDF
- Insert the following number into this tree 20 - Show all the steps o.pdf
PDF
- Bag breaks open- included as delay in the allowance factor ^ Conveyo.pdf
PDF
- If your class investigated two different types of tissues (plants)-.pdf
PDF
- Cytosolic pathogens- - Where are they degraded- - What do their pept.pdf
PDF
- Drag the labels of Group 1 to their respective targets to identify p.pdf
PDF
- Each student must post ane (1) substantial intid post as a response.pdf
PDF
- Flowers are the part of the plant- - The anther of a stamen contains.pdf
PDF
- Bridge City Developers is coordinating the construction of an office.pdf
PDF
- Class Exercise 1- Draw a decision matrix that illustrates the decisi.pdf
PDF
- Be able to briefly describe and - or diagram the flow of water (in o.pdf
PDF
- Assume Job 27 is sold for $350-000-.pdf
PDF
- According to E- Perkins- the colonial era is appealing in regard to.pdf
PDF
-#1) In genomic studies- it is common to score thousands of SNPs in nu.pdf
PDF
-- Exercise 1- Manipulating linked lists in C++ #include -iostream-.pdf
- There are 50 fish in a pond in January- The fish grow at a rate of 6.pdf
- TIF B cells and T cells are able to alter their genome- What kind of.pdf
- The array data of integers is sorted in an increaning order- - The i.pdf
- Quantitative (record completenessdocuments are present- forms authen.pdf
- Lab T Reriow Hemneork + Oun on March 10- 2023 2 anantwred Table 72 P.pdf
- In 150-250 words- respond to all questions- This is your initial dis.pdf
- Insert the following number into this tree 20 - Show all the steps o.pdf
- Bag breaks open- included as delay in the allowance factor ^ Conveyo.pdf
- If your class investigated two different types of tissues (plants)-.pdf
- Cytosolic pathogens- - Where are they degraded- - What do their pept.pdf
- Drag the labels of Group 1 to their respective targets to identify p.pdf
- Each student must post ane (1) substantial intid post as a response.pdf
- Flowers are the part of the plant- - The anther of a stamen contains.pdf
- Bridge City Developers is coordinating the construction of an office.pdf
- Class Exercise 1- Draw a decision matrix that illustrates the decisi.pdf
- Be able to briefly describe and - or diagram the flow of water (in o.pdf
- Assume Job 27 is sold for $350-000-.pdf
- According to E- Perkins- the colonial era is appealing in regard to.pdf
-#1) In genomic studies- it is common to score thousands of SNPs in nu.pdf
-- Exercise 1- Manipulating linked lists in C++ #include -iostream-.pdf
Ad

Recently uploaded (20)

PDF
Computing-Curriculum for Schools in Ghana
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
master seminar digital applications in india
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Institutional Correction lecture only . . .
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Lesson notes of climatology university.
PPTX
GDM (1) (1).pptx small presentation for students
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Cell Types and Its function , kingdom of life
Computing-Curriculum for Schools in Ghana
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
master seminar digital applications in india
Final Presentation General Medicine 03-08-2024.pptx
Institutional Correction lecture only . . .
Supply Chain Operations Speaking Notes -ICLT Program
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Complications of Minimal Access Surgery at WLH
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Lesson notes of climatology university.
GDM (1) (1).pptx small presentation for students
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Chinmaya Tiranga quiz Grand Finale.pdf
Cell Types and Its function , kingdom of life
Ad

--import statemnts for Random- Scanner and IO import java-util-Random-.pdf

  • 1. //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 lunchn"); //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];
  • 2. //assign random double values to all elements for (int i = 0; i < numHobbits; i++) //outer loop is for rows { for (int j = 0; j < numCols; j++) //inner loop is for columns { //get a random double value in range [0.2, 0.4] double randDouble = getRandDouble(rand); //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] *= HI_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 columen heading of the array of stats outFile.println("HEIGHT,WEIGHT"); for (int i = 0; i < ar.length; i++) { for (int j = 0; j < ar[i].length; j++) { outFile.print(ar[i][j]); //if at end of a row, add newline char if (j == ar[i].length - 1) outFile.print("n"); else //add the "," delimiter outFile.print(","); } //end of inner loop } //end of outer loop outFile.close(); //close outfile
  • 3. System.out.println("The file was successfully writtenn"); } //end of method //method to read 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("%.2ftt%.2fn", 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.n"); } //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 column //second element is mean of second column, etc double[] meanAr = new double[ar[0].length];
  • 4. //nested for loop to iterate through elements in parameter for (int i = 0; i < ar.length; i++) { for (int j = 0; j < ar[i].length; j++) { //sum the elements in the current column meanAr[j] += ar[i][j]; } } //end of outer loop //replace the sum with the average; i.e. divide by number of rows for (int i = 0; i < meanAr.length; i++) meanAr[i] /= ar.length; Lab16 Sample output