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

(1) Write a program to keep log (record) of students who entered in laboratory a

ID: 3543792 • Letter: #

Question

(1) Write a program to keep log (record) of students who entered in laboratory and to calculate the average number of hours each student spends in class. Program should ask students to enter Name and number of hours she/ he spent inside laboratory. After 7 entries (one week) program should give us average number of hours a student spent in Lab. The output should be saved in a file named login.txt. To calculate the average, your program should use another class AverageCalculator to find the average number of hours each student spends in week in Lab. Your AverageCaluclator class should find average number of hours spent in Lab, by taking input from login.txt and should save output text file named averageStudenthours.txt

Explanation / Answer

/* package whatever; // don't place package name! */


import java.util.*;

import java.lang.*;

import java.io.*;


//File Operations are often comes with Exceptions


class AverageCalculator

{

File f;

AverageCalculator(File f) throws Exception

{

try{

double count = 0.0;

//Reading from "login.txt"

BufferedReader reader = new BufferedReader(new FileReader(f));

String line = null;

//Calculating the avg No: Of Hoyrs

while ((line = reader.readLine()) != null)

{

count = count + new Integer(line);

}

count = count/7;

//Writing results to output file "averageStudenthours.txt"

String path = "E:/averageStudenthours.txt";

File f1 = new File(path);

f1.createNewFile();

FileWriter writer = new FileWriter(f1);

writer.write(count+" ");

writer.flush();

writer.close();

System.out.println(count);}

catch(Exception e){}

}

}



class driver

{

public static void main (String[] args) throws Exception

{

try{

Scanner scan = new Scanner(System.in);

int i = 0;

String name = scan.nextLine();

int arr[] = new int[7];

//Opening File "login.txt"

String path = "E:/login.txt";

File f = new File(path);

f.createNewFile();

//writing the results to" login.txt"

FileWriter writer = new FileWriter(f);


while(i<7)

{

arr[i] = new Integer(scan.nextLine());

writer.write(arr[i]+" ");

i++;

}

writer.flush();

writer.close();

//Calling the AverageCalculator to calculate the avg no: of hours

AverageCalculator ac = new AverageCalculator(f);}

catch(Exception e){}

}

}