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
Ad

More Related Content

Similar to --import statemnts for Random- Scanner and IO import java-util-Random-.pdf (20)

Oop lecture9 11
Oop lecture9 11Oop lecture9 11
Oop lecture9 11
Shahriar Robbani
 
Java 7 - short intro to NIO.2
Java 7 - short intro to NIO.2Java 7 - short intro to NIO.2
Java 7 - short intro to NIO.2
Martijn Verburg
 
please navigate to cs112 webpage and go to assignments -- Trees. Th.pdf
please navigate to cs112 webpage and go to assignments -- Trees. Th.pdfplease navigate to cs112 webpage and go to assignments -- Trees. Th.pdf
please navigate to cs112 webpage and go to assignments -- Trees. Th.pdf
aioils
 
Inheritance
InheritanceInheritance
Inheritance
آصف الصيفي
 
DOC-20241121-WA0004bwushshusjssjuwh..pdf
DOC-20241121-WA0004bwushshusjssjuwh..pdfDOC-20241121-WA0004bwushshusjssjuwh..pdf
DOC-20241121-WA0004bwushshusjssjuwh..pdf
TanishaWaichal
 
C Programming Project
C Programming ProjectC Programming Project
C Programming Project
Vijayananda Mohire
 
File & Exception Handling in C++.pptx
File & Exception Handling in C++.pptxFile & Exception Handling in C++.pptx
File & Exception Handling in C++.pptx
RutujaTandalwade
 
Exmaples of file handling
Exmaples of file handlingExmaples of file handling
Exmaples of file handling
sparkishpearl
 
DisplayBook.java import java.io.File; import java.io.FileInput.pdf
DisplayBook.java import java.io.File; import java.io.FileInput.pdfDisplayBook.java import java.io.File; import java.io.FileInput.pdf
DisplayBook.java import java.io.File; import java.io.FileInput.pdf
sudheerforce
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
Kavitha713564
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
Rex Joe
 
Files in c++
Files in c++Files in c++
Files in c++
Selvin Josy Bai Somu
 
csc1201_lecture13.ppt
csc1201_lecture13.pptcsc1201_lecture13.ppt
csc1201_lecture13.ppt
HEMAHEMS5
 
Explaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to ComeExplaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to Come
Cory Forsyth
 
Files and streams In Java
Files and streams In JavaFiles and streams In Java
Files and streams In Java
Rajan Shah
 
files.pptx
files.pptxfiles.pptx
files.pptx
KeerthanaM738437
 
import org-jsoup-Jsoup- import org-jsoup-nodes-Document- import java-.docx
import org-jsoup-Jsoup- import org-jsoup-nodes-Document-  import java-.docximport org-jsoup-Jsoup- import org-jsoup-nodes-Document-  import java-.docx
import org-jsoup-Jsoup- import org-jsoup-nodes-Document- import java-.docx
RyanEAcTuckern
 
Session 23 - JDBC
Session 23 - JDBCSession 23 - JDBC
Session 23 - JDBC
PawanMM
 
11_Str11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.eams.pdf
11_Str11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.eams.pdf11_Str11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.eams.pdf
11_Str11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.eams.pdf
hungvidien123
 
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
Dmitry Soshnikov
 
Java 7 - short intro to NIO.2
Java 7 - short intro to NIO.2Java 7 - short intro to NIO.2
Java 7 - short intro to NIO.2
Martijn Verburg
 
please navigate to cs112 webpage and go to assignments -- Trees. Th.pdf
please navigate to cs112 webpage and go to assignments -- Trees. Th.pdfplease navigate to cs112 webpage and go to assignments -- Trees. Th.pdf
please navigate to cs112 webpage and go to assignments -- Trees. Th.pdf
aioils
 
DOC-20241121-WA0004bwushshusjssjuwh..pdf
DOC-20241121-WA0004bwushshusjssjuwh..pdfDOC-20241121-WA0004bwushshusjssjuwh..pdf
DOC-20241121-WA0004bwushshusjssjuwh..pdf
TanishaWaichal
 
File & Exception Handling in C++.pptx
File & Exception Handling in C++.pptxFile & Exception Handling in C++.pptx
File & Exception Handling in C++.pptx
RutujaTandalwade
 
Exmaples of file handling
Exmaples of file handlingExmaples of file handling
Exmaples of file handling
sparkishpearl
 
DisplayBook.java import java.io.File; import java.io.FileInput.pdf
DisplayBook.java import java.io.File; import java.io.FileInput.pdfDisplayBook.java import java.io.File; import java.io.FileInput.pdf
DisplayBook.java import java.io.File; import java.io.FileInput.pdf
sudheerforce
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
Kavitha713564
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
Rex Joe
 
csc1201_lecture13.ppt
csc1201_lecture13.pptcsc1201_lecture13.ppt
csc1201_lecture13.ppt
HEMAHEMS5
 
Explaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to ComeExplaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to Come
Cory Forsyth
 
Files and streams In Java
Files and streams In JavaFiles and streams In Java
Files and streams In Java
Rajan Shah
 
import org-jsoup-Jsoup- import org-jsoup-nodes-Document- import java-.docx
import org-jsoup-Jsoup- import org-jsoup-nodes-Document-  import java-.docximport org-jsoup-Jsoup- import org-jsoup-nodes-Document-  import java-.docx
import org-jsoup-Jsoup- import org-jsoup-nodes-Document- import java-.docx
RyanEAcTuckern
 
Session 23 - JDBC
Session 23 - JDBCSession 23 - JDBC
Session 23 - JDBC
PawanMM
 
11_Str11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.eams.pdf
11_Str11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.eams.pdf11_Str11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.eams.pdf
11_Str11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.eams.pdf
hungvidien123
 
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
Dmitry Soshnikov
 

More from ganisyedtrd (20)

--12 Points- DEVORESTATS 2-5-074- Suppose thut the proportions of bloo.pdf
--12 Points- DEVORESTATS 2-5-074- Suppose thut the proportions of bloo.pdf--12 Points- DEVORESTATS 2-5-074- Suppose thut the proportions of bloo.pdf
--12 Points- DEVORESTATS 2-5-074- Suppose thut the proportions of bloo.pdf
ganisyedtrd
 
--kindly help with the correct answer --must show diagram Explain what.pdf
--kindly help with the correct answer --must show diagram Explain what.pdf--kindly help with the correct answer --must show diagram Explain what.pdf
--kindly help with the correct answer --must show diagram Explain what.pdf
ganisyedtrd
 
--- If you were start a business- what are the advantages in using equ.pdf
--- If you were start a business- what are the advantages in using equ.pdf--- If you were start a business- what are the advantages in using equ.pdf
--- If you were start a business- what are the advantages in using equ.pdf
ganisyedtrd
 
--- Interpret the figure below given what you know about wood frogs---.pdf
--- Interpret the figure below given what you know about wood frogs---.pdf--- Interpret the figure below given what you know about wood frogs---.pdf
--- Interpret the figure below given what you know about wood frogs---.pdf
ganisyedtrd
 
--------- Lala--- the noll (fnrmad plement) indicated by letter A is a.pdf
--------- Lala--- the noll (fnrmad plement) indicated by letter A is a.pdf--------- Lala--- the noll (fnrmad plement) indicated by letter A is a.pdf
--------- Lala--- the noll (fnrmad plement) indicated by letter A is a.pdf
ganisyedtrd
 
--- Which of the following influences the instantaneous rate of change.pdf
--- Which of the following influences the instantaneous rate of change.pdf--- Which of the following influences the instantaneous rate of change.pdf
--- Which of the following influences the instantaneous rate of change.pdf
ganisyedtrd
 
-- USING UNITY TRYING TO CREATE A CLICK TO PATH- THAT YOU CLICK ON AND.pdf
-- USING UNITY TRYING TO CREATE A CLICK TO PATH- THAT YOU CLICK ON AND.pdf-- USING UNITY TRYING TO CREATE A CLICK TO PATH- THAT YOU CLICK ON AND.pdf
-- USING UNITY TRYING TO CREATE A CLICK TO PATH- THAT YOU CLICK ON AND.pdf
ganisyedtrd
 
-- Write the compiler used- Visual studio or gcc -- Reminder that your.pdf
-- Write the compiler used- Visual studio or gcc -- Reminder that your.pdf-- Write the compiler used- Visual studio or gcc -- Reminder that your.pdf
-- Write the compiler used- Visual studio or gcc -- Reminder that your.pdf
ganisyedtrd
 
- There are 50 fish in a pond in January- The fish grow at a rate of 6.pdf
- There are 50 fish in a pond in January- The fish grow at a rate of 6.pdf- There are 50 fish in a pond in January- The fish grow at a rate of 6.pdf
- There are 50 fish in a pond in January- The fish grow at a rate of 6.pdf
ganisyedtrd
 
- TIF B cells and T cells are able to alter their genome- What kind of.pdf
- TIF B cells and T cells are able to alter their genome- What kind of.pdf- TIF B cells and T cells are able to alter their genome- What kind of.pdf
- TIF B cells and T cells are able to alter their genome- What kind of.pdf
ganisyedtrd
 
- The array data of integers is sorted in an increaning order- - The i.pdf
- The array data of integers is sorted in an increaning order- - The i.pdf- The array data of integers is sorted in an increaning order- - The i.pdf
- The array data of integers is sorted in an increaning order- - The i.pdf
ganisyedtrd
 
- Quantitative (record completenessdocuments are present- forms authen.pdf
- Quantitative (record completenessdocuments are present- forms authen.pdf- Quantitative (record completenessdocuments are present- forms authen.pdf
- Quantitative (record completenessdocuments are present- forms authen.pdf
ganisyedtrd
 
- Lab T Reriow Hemneork + Oun on March 10- 2023 2 anantwred Table 72 P.pdf
- Lab T Reriow Hemneork + Oun on March 10- 2023 2 anantwred Table 72 P.pdf- Lab T Reriow Hemneork + Oun on March 10- 2023 2 anantwred Table 72 P.pdf
- Lab T Reriow Hemneork + Oun on March 10- 2023 2 anantwred Table 72 P.pdf
ganisyedtrd
 
- In 150-250 words- respond to all questions- This is your initial dis.pdf
- In 150-250 words- respond to all questions- This is your initial dis.pdf- In 150-250 words- respond to all questions- This is your initial dis.pdf
- In 150-250 words- respond to all questions- This is your initial dis.pdf
ganisyedtrd
 
- Insert the following number into this tree 20 - Show all the steps o.pdf
- Insert the following number into this tree 20 - Show all the steps o.pdf- Insert the following number into this tree 20 - Show all the steps o.pdf
- Insert the following number into this tree 20 - Show all the steps o.pdf
ganisyedtrd
 
- Bag breaks open- included as delay in the allowance factor ^ Conveyo.pdf
- Bag breaks open- included as delay in the allowance factor ^ Conveyo.pdf- Bag breaks open- included as delay in the allowance factor ^ Conveyo.pdf
- Bag breaks open- included as delay in the allowance factor ^ Conveyo.pdf
ganisyedtrd
 
- If your class investigated two different types of tissues (plants)-.pdf
- If your class investigated two different types of tissues (plants)-.pdf- If your class investigated two different types of tissues (plants)-.pdf
- If your class investigated two different types of tissues (plants)-.pdf
ganisyedtrd
 
- Cytosolic pathogens- - Where are they degraded- - What do their pept.pdf
- Cytosolic pathogens- - Where are they degraded- - What do their pept.pdf- Cytosolic pathogens- - Where are they degraded- - What do their pept.pdf
- Cytosolic pathogens- - Where are they degraded- - What do their pept.pdf
ganisyedtrd
 
- Drag the labels of Group 1 to their respective targets to identify p.pdf
- Drag the labels of Group 1 to their respective targets to identify p.pdf- Drag the labels of Group 1 to their respective targets to identify p.pdf
- Drag the labels of Group 1 to their respective targets to identify p.pdf
ganisyedtrd
 
- Each student must post ane (1) substantial intid post as a response.pdf
- Each student must post ane (1) substantial intid post as a response.pdf- Each student must post ane (1) substantial intid post as a response.pdf
- Each student must post ane (1) substantial intid post as a response.pdf
ganisyedtrd
 
--12 Points- DEVORESTATS 2-5-074- Suppose thut the proportions of bloo.pdf
--12 Points- DEVORESTATS 2-5-074- Suppose thut the proportions of bloo.pdf--12 Points- DEVORESTATS 2-5-074- Suppose thut the proportions of bloo.pdf
--12 Points- DEVORESTATS 2-5-074- Suppose thut the proportions of bloo.pdf
ganisyedtrd
 
--kindly help with the correct answer --must show diagram Explain what.pdf
--kindly help with the correct answer --must show diagram Explain what.pdf--kindly help with the correct answer --must show diagram Explain what.pdf
--kindly help with the correct answer --must show diagram Explain what.pdf
ganisyedtrd
 
--- If you were start a business- what are the advantages in using equ.pdf
--- If you were start a business- what are the advantages in using equ.pdf--- If you were start a business- what are the advantages in using equ.pdf
--- If you were start a business- what are the advantages in using equ.pdf
ganisyedtrd
 
--- Interpret the figure below given what you know about wood frogs---.pdf
--- Interpret the figure below given what you know about wood frogs---.pdf--- Interpret the figure below given what you know about wood frogs---.pdf
--- Interpret the figure below given what you know about wood frogs---.pdf
ganisyedtrd
 
--------- Lala--- the noll (fnrmad plement) indicated by letter A is a.pdf
--------- Lala--- the noll (fnrmad plement) indicated by letter A is a.pdf--------- Lala--- the noll (fnrmad plement) indicated by letter A is a.pdf
--------- Lala--- the noll (fnrmad plement) indicated by letter A is a.pdf
ganisyedtrd
 
--- Which of the following influences the instantaneous rate of change.pdf
--- Which of the following influences the instantaneous rate of change.pdf--- Which of the following influences the instantaneous rate of change.pdf
--- Which of the following influences the instantaneous rate of change.pdf
ganisyedtrd
 
-- USING UNITY TRYING TO CREATE A CLICK TO PATH- THAT YOU CLICK ON AND.pdf
-- USING UNITY TRYING TO CREATE A CLICK TO PATH- THAT YOU CLICK ON AND.pdf-- USING UNITY TRYING TO CREATE A CLICK TO PATH- THAT YOU CLICK ON AND.pdf
-- USING UNITY TRYING TO CREATE A CLICK TO PATH- THAT YOU CLICK ON AND.pdf
ganisyedtrd
 
-- Write the compiler used- Visual studio or gcc -- Reminder that your.pdf
-- Write the compiler used- Visual studio or gcc -- Reminder that your.pdf-- Write the compiler used- Visual studio or gcc -- Reminder that your.pdf
-- Write the compiler used- Visual studio or gcc -- Reminder that your.pdf
ganisyedtrd
 
- There are 50 fish in a pond in January- The fish grow at a rate of 6.pdf
- There are 50 fish in a pond in January- The fish grow at a rate of 6.pdf- There are 50 fish in a pond in January- The fish grow at a rate of 6.pdf
- There are 50 fish in a pond in January- The fish grow at a rate of 6.pdf
ganisyedtrd
 
- TIF B cells and T cells are able to alter their genome- What kind of.pdf
- TIF B cells and T cells are able to alter their genome- What kind of.pdf- TIF B cells and T cells are able to alter their genome- What kind of.pdf
- TIF B cells and T cells are able to alter their genome- What kind of.pdf
ganisyedtrd
 
- The array data of integers is sorted in an increaning order- - The i.pdf
- The array data of integers is sorted in an increaning order- - The i.pdf- The array data of integers is sorted in an increaning order- - The i.pdf
- The array data of integers is sorted in an increaning order- - The i.pdf
ganisyedtrd
 
- Quantitative (record completenessdocuments are present- forms authen.pdf
- Quantitative (record completenessdocuments are present- forms authen.pdf- Quantitative (record completenessdocuments are present- forms authen.pdf
- Quantitative (record completenessdocuments are present- forms authen.pdf
ganisyedtrd
 
- Lab T Reriow Hemneork + Oun on March 10- 2023 2 anantwred Table 72 P.pdf
- Lab T Reriow Hemneork + Oun on March 10- 2023 2 anantwred Table 72 P.pdf- Lab T Reriow Hemneork + Oun on March 10- 2023 2 anantwred Table 72 P.pdf
- Lab T Reriow Hemneork + Oun on March 10- 2023 2 anantwred Table 72 P.pdf
ganisyedtrd
 
- In 150-250 words- respond to all questions- This is your initial dis.pdf
- In 150-250 words- respond to all questions- This is your initial dis.pdf- In 150-250 words- respond to all questions- This is your initial dis.pdf
- In 150-250 words- respond to all questions- This is your initial dis.pdf
ganisyedtrd
 
- Insert the following number into this tree 20 - Show all the steps o.pdf
- Insert the following number into this tree 20 - Show all the steps o.pdf- Insert the following number into this tree 20 - Show all the steps o.pdf
- Insert the following number into this tree 20 - Show all the steps o.pdf
ganisyedtrd
 
- Bag breaks open- included as delay in the allowance factor ^ Conveyo.pdf
- Bag breaks open- included as delay in the allowance factor ^ Conveyo.pdf- Bag breaks open- included as delay in the allowance factor ^ Conveyo.pdf
- Bag breaks open- included as delay in the allowance factor ^ Conveyo.pdf
ganisyedtrd
 
- If your class investigated two different types of tissues (plants)-.pdf
- If your class investigated two different types of tissues (plants)-.pdf- If your class investigated two different types of tissues (plants)-.pdf
- If your class investigated two different types of tissues (plants)-.pdf
ganisyedtrd
 
- Cytosolic pathogens- - Where are they degraded- - What do their pept.pdf
- Cytosolic pathogens- - Where are they degraded- - What do their pept.pdf- Cytosolic pathogens- - Where are they degraded- - What do their pept.pdf
- Cytosolic pathogens- - Where are they degraded- - What do their pept.pdf
ganisyedtrd
 
- Drag the labels of Group 1 to their respective targets to identify p.pdf
- Drag the labels of Group 1 to their respective targets to identify p.pdf- Drag the labels of Group 1 to their respective targets to identify p.pdf
- Drag the labels of Group 1 to their respective targets to identify p.pdf
ganisyedtrd
 
- Each student must post ane (1) substantial intid post as a response.pdf
- Each student must post ane (1) substantial intid post as a response.pdf- Each student must post ane (1) substantial intid post as a response.pdf
- Each student must post ane (1) substantial intid post as a response.pdf
ganisyedtrd
 
Ad

Recently uploaded (20)

Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
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
  翻译: