Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

The data file power1.txt contains a power plant output in megawatts over a period of 8 weeks. Each row of data contains 7 integers that represent 1 week's data. Write a program that will compute and print to a file and to the screen the average power output over this period of time. Also print the number of days with greater-than-average power output.

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

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote