0% found this document useful (0 votes)
12 views3 pages

M251 Meeting 7 Activity Solution

The document outlines a Java program consisting of two classes: DemoFiles and Activity_Meeting7. DemoFiles manages a list of student names and grades, allowing for filling the list, writing to and reading from a file, and printing the list with calculated averages. Activity_Meeting7 serves as the main class to execute the methods of DemoFiles in sequence.

Uploaded by

tonisaliba2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

M251 Meeting 7 Activity Solution

The document outlines a Java program consisting of two classes: DemoFiles and Activity_Meeting7. DemoFiles manages a list of student names and grades, allowing for filling the list, writing to and reading from a file, and printing the list with calculated averages. Activity_Meeting7 serves as the main class to execute the methods of DemoFiles in sequence.

Uploaded by

tonisaliba2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Solution of Activities of M251-Meeting 7

)Input/Output Files(

A. Write a Java class DemoFiles, the class has the following methods:

1. A private attribute list of type ArrayList of String.


2. Zero argument constructor that initialize list to an empty list.
3. A method Fill() that allow user to fill the list with the following lines,
where each line represent student name, followed by three grades.
Ali 90 95 88
Sami 85 84 90
Rana 78 85 88
Huda 66 75 68
4. A method writeToFile(): which writes the list on a file called
“c:/students.txt”, each element on a line. For example: Ali 90 95 88 on a
line.
5. A method readFrom(): which reads the contents of “c:/students.txt” file,
calculates the average of each student, then replace the list elements with
new ones that has the string in addition to average as in the below format.
Name: Ali 90 95 88, avg = 91.0
6. A method printList(): which print the list content each on a line.

B. Write a Java class TestDemoFiles, that has a main method to do


the followings:

 Create an object of DemoFiles class call it dl.

 Use dl object to call fill() method.

 Use dl object to call writeTofFile() method.

 Use dl object to call readFrom() method.

 Use dl object to call printList() method.

Solution: A.
import java.io.*;
import java.util.*;

public class DemoFiles {


//1
private ArrayList<String> list;

//2
public DemoFiles(){
list = new ArrayList<String>();
}

//3
public void fill(){
list.add("Ali 90 95 88");
list.add("Sami 85 84 90");
list.add("Rana 78 85 88");
list.add("Huda 66 75 68");
}

//4
public void writeToFile(){
try{
File f = new File("C:/students.txt");
PrintWriter pr = new PrintWriter(f);
for(String x : list)
pr.println(x);
pr.close();
}
catch(IOException e){
System.out.println("There is an error "+ e);
}
}

//5
public void readFrom(){
try{
File f = new File("C:/students.txt");
Scanner sc = new Scanner(f);
String s, name; int g1,g2, g3; double avg;
int c = -1;
while(sc.hasNext()){
name = sc.next();
g1 = sc.nextInt();
g2 = sc.nextInt();
g3 = sc.nextInt();
avg = (g1+g2+g3)/3.0;
c++;
s = "Name is: "+list.get(c)+", avg ="+ avg;
list.set(c, s);
}
sc.close();
}
catch(FileNotFoundException e){
System.out.println("The File is not found");
}
}

//6
public void printList(){
for(String x : list)
System.out.println(x);
}
}

Solution: B.
public class Activity_Meeting7 {

public static void main(String[] args) {

DemoFiles df = new DemoFiles();

df.fill();

df.writeToFile();

df.readFrom();

df.printList(); }

You might also like