207 301 222 302 22 167 125 367 60 120 111 301 499 434 211 62 441 192 21 293 316
ID: 3555870 • Letter: 2
Question
207 301 222 302 22 167 125
367 60 120 111 301 499 434
211 62 441 192 21 293 316
401 340 161 297 441 117 206
448 111 370 220 264 444 207
21 313 204 22 446 401 337
213 208 444 321 320 335 313
162 137 265 44 370 315 322
Explanation / Answer
package com.Project.com.Project.Chegg;
//List of Libraries to be imported needed to implement
import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class PowerAverage { //Class
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException{ //Exception handling
ArrayList List = new ArrayList(); //To store the power output element
FileReader file = null;
int number = 0;
int sum =0;
int iter = 0;
int Average =0;
try {
file =new FileReader("c:\power1.txt"); // Opening the local file for reading
BufferedReader reader = new BufferedReader(file);// creating a buffer reader
String line = "";
while ((line = reader.readLine()) !=null){
StringTokenizer st = new StringTokenizer(line);//gettingTokens the line separated with the space
while(st.hasMoreElements()){
number = Integer.parseInt((String) st.nextElement());
List.add(number); //Adding the element in Array List
sum = sum+number;
iter +=1;
}
}
Average = sum/iter;
System.out.println("The days with greater than average power output ::"+" ");//Printing the elements
for(int i=0;i<List.size();i++){
int item = (int) List.get(i);
if(item>Average){ // printing the days having greater than average power output
System.out.print(item + " ");
}
}
System.out.println(" ");
System.out.println("The average power consumption is "+sum/iter);
}
catch(Exception e){
throw new RuntimeException(e);
}
finally{
if(file !=null){
try {
file.close();
}
catch(IOException e){
}
}
}
String S = "The average power consumption is "+sum/iter;
File file1 = new File("C:\Hello-1.txt");// Creating file handle for the file to provide output
// creates the file
file1.createNewFile();
// creates a FileWriter Object
FileWriter writer = new FileWriter(file1);
// Writes the content to the file
writer.write(S);
writer.flush();
writer.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.